Initial commit 🍀
This commit is contained in:
commit
b3f42e62af
405 changed files with 31017 additions and 0 deletions
75
src/api/endpoints/app/create.js
Normal file
75
src/api/endpoints/app/create.js
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import rndstr from 'rndstr';
|
||||
import App from '../../models/app';
|
||||
import serialize from '../../serializers/app';
|
||||
|
||||
/**
|
||||
* Create an app
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = async (params, user) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'name_id' parameter
|
||||
const nameId = params.name_id;
|
||||
if (nameId == null || nameId == '') {
|
||||
return rej('name_id is required');
|
||||
}
|
||||
|
||||
// Validate name_id
|
||||
if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) {
|
||||
return rej('invalid name_id');
|
||||
}
|
||||
|
||||
// Get 'name' parameter
|
||||
const name = params.name;
|
||||
if (name == null || name == '') {
|
||||
return rej('name is required');
|
||||
}
|
||||
|
||||
// Get 'description' parameter
|
||||
const description = params.description;
|
||||
if (description == null || description == '') {
|
||||
return rej('description is required');
|
||||
}
|
||||
|
||||
// Get 'permission' parameter
|
||||
const permission = params.permission;
|
||||
if (permission == null || permission == '') {
|
||||
return rej('permission is required');
|
||||
}
|
||||
|
||||
// Get 'callback_url' parameter
|
||||
let callback = params.callback_url;
|
||||
if (callback === '') {
|
||||
callback = null;
|
||||
}
|
||||
|
||||
// Generate secret
|
||||
const secret = rndstr('a-zA-Z0-9', 32);
|
||||
|
||||
// Create account
|
||||
const inserted = await App.insert({
|
||||
created_at: new Date(),
|
||||
user_id: user._id,
|
||||
name: name,
|
||||
name_id: nameId,
|
||||
name_id_lower: nameId.toLowerCase(),
|
||||
description: description,
|
||||
permission: permission.split(','),
|
||||
callback_url: callback,
|
||||
secret: secret
|
||||
});
|
||||
|
||||
const app = inserted.ops[0];
|
||||
|
||||
// Response
|
||||
res(await serialize(app));
|
||||
});
|
||||
40
src/api/endpoints/app/name_id/available.js
Normal file
40
src/api/endpoints/app/name_id/available.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import App from '../../../models/app';
|
||||
|
||||
/**
|
||||
* Check available name_id of app
|
||||
*
|
||||
* @param {Object} params
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = async (params) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'name_id' parameter
|
||||
const nameId = params.name_id;
|
||||
if (nameId == null || nameId == '') {
|
||||
return rej('name_id is required');
|
||||
}
|
||||
|
||||
// Validate name_id
|
||||
if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) {
|
||||
return rej('invalid name_id');
|
||||
}
|
||||
|
||||
// Get exist
|
||||
const exist = await App
|
||||
.count({
|
||||
name_id_lower: nameId.toLowerCase()
|
||||
}, {
|
||||
limit: 1
|
||||
});
|
||||
|
||||
// Reply
|
||||
res({
|
||||
available: exist === 0
|
||||
});
|
||||
});
|
||||
51
src/api/endpoints/app/show.js
Normal file
51
src/api/endpoints/app/show.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import App from '../../models/app';
|
||||
import serialize from '../../serializers/app';
|
||||
|
||||
/**
|
||||
* Show an app
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} _
|
||||
* @param {Object} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user, _, isSecure) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'app_id' parameter
|
||||
let appId = params.app_id;
|
||||
if (appId == null || appId == '') {
|
||||
appId = null;
|
||||
}
|
||||
|
||||
// Get 'name_id' parameter
|
||||
let nameId = params.name_id;
|
||||
if (nameId == null || nameId == '') {
|
||||
nameId = null;
|
||||
}
|
||||
|
||||
if (appId === null && nameId === null) {
|
||||
return rej('app_id or name_id is required');
|
||||
}
|
||||
|
||||
// Lookup app
|
||||
const app = appId !== null
|
||||
? await App.findOne({ _id: new mongo.ObjectID(appId) })
|
||||
: await App.findOne({ name_id_lower: nameId.toLowerCase() });
|
||||
|
||||
if (app === null) {
|
||||
return rej('app not found');
|
||||
}
|
||||
|
||||
// Send response
|
||||
res(await serialize(app, user, {
|
||||
includeSecret: isSecure && app.user_id.equals(user._id)
|
||||
}));
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue