monkeeShark/src/server/api/endpoints/app/create.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
import rndstr from 'rndstr';
2017-03-08 18:50:09 +00:00
import $ from 'cafy';
import App, { pack } from '../../../../models/app';
2018-06-18 00:54:53 +00:00
import { ILocalUser } from '../../../../models/user';
2016-12-28 22:49:51 +00:00
2018-07-16 19:36:44 +00:00
export const meta = {
requireCredential: false
2018-07-16 19:36:44 +00:00
};
2016-12-28 22:49:51 +00:00
/**
* Create an app
*/
2018-07-05 17:58:29 +00:00
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
2016-12-28 22:49:51 +00:00
// Get 'name' parameter
2018-05-02 09:06:16 +00:00
const [name, nameErr] = $.str.get(params.name);
2017-03-03 10:48:00 +00:00
if (nameErr) return rej('invalid name param');
2016-12-28 22:49:51 +00:00
// Get 'description' parameter
2018-05-02 09:06:16 +00:00
const [description, descriptionErr] = $.str.get(params.description);
2017-03-03 10:48:00 +00:00
if (descriptionErr) return rej('invalid description param');
2016-12-28 22:49:51 +00:00
// Get 'permission' parameter
2018-05-02 09:06:16 +00:00
const [permission, permissionErr] = $.arr($.str).unique().get(params.permission);
2017-03-03 10:48:00 +00:00
if (permissionErr) return rej('invalid permission param');
2016-12-28 22:49:51 +00:00
2018-03-29 05:48:47 +00:00
// Get 'callbackUrl' parameter
2017-11-18 16:47:50 +00:00
// TODO: Check it is valid url
2018-07-05 14:36:07 +00:00
const [callbackUrl = null, callbackUrlErr] = $.str.optional.nullable.get(params.callbackUrl);
2018-03-29 05:48:47 +00:00
if (callbackUrlErr) return rej('invalid callbackUrl param');
2016-12-28 22:49:51 +00:00
// Generate secret
const secret = rndstr('a-zA-Z0-9', 32);
// Create account
2017-01-20 08:38:05 +00:00
const app = await App.insert({
2018-03-29 05:48:47 +00:00
createdAt: new Date(),
userId: user && user._id,
2016-12-28 22:49:51 +00:00
name: name,
description: description,
2017-03-03 10:48:00 +00:00
permission: permission,
2018-03-29 05:48:47 +00:00
callbackUrl: callbackUrl,
2016-12-28 22:49:51 +00:00
secret: secret
});
// Response
res(await pack(app, null, {
2018-11-01 00:19:22 +00:00
detail: true,
includeSecret: true
}));
2016-12-28 22:49:51 +00:00
});