From f11bdf36b9c75a3e10850e46ef045cf9675ab7f7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 19:48:00 +0900 Subject: wip --- src/api/endpoints/app/name_id/available.js | 69 ------------------------------ src/api/endpoints/app/name_id/available.ts | 64 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 69 deletions(-) delete mode 100644 src/api/endpoints/app/name_id/available.js create mode 100644 src/api/endpoints/app/name_id/available.ts (limited to 'src/api/endpoints/app/name_id') diff --git a/src/api/endpoints/app/name_id/available.js b/src/api/endpoints/app/name_id/available.js deleted file mode 100644 index 159d4fff4e..0000000000 --- a/src/api/endpoints/app/name_id/available.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import App from '../../../models/app'; - -/** - * @swagger - * /app/name_id/available: - * post: - * summary: Check available name_id on creation an application - * parameters: - * - - * name: name_id - * description: Application unique name - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * available: - * description: Whether name_id is available - * type: boolean - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Check available name_id of app - * - * @param {any} params - * @return {Promise} - */ -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 - }); -}); diff --git a/src/api/endpoints/app/name_id/available.ts b/src/api/endpoints/app/name_id/available.ts new file mode 100644 index 0000000000..6af18ae83c --- /dev/null +++ b/src/api/endpoints/app/name_id/available.ts @@ -0,0 +1,64 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import App from '../../../models/app'; +import { isValidNameId } from '../../../models/app'; + +/** + * @swagger + * /app/name_id/available: + * post: + * summary: Check available name_id on creation an application + * parameters: + * - + * name: name_id + * description: Application unique name + * in: formData + * required: true + * type: string + * + * responses: + * 200: + * description: Success + * schema: + * type: object + * properties: + * available: + * description: Whether name_id is available + * type: boolean + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Check available name_id of app + * + * @param {any} params + * @return {Promise} + */ +module.exports = async (params) => + new Promise(async (res, rej) => +{ + // Get 'name_id' parameter + const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed(); + if (nameIdErr) return rej('invalid name_id param'); + + // Get exist + const exist = await App + .count({ + name_id_lower: nameId.toLowerCase() + }, { + limit: 1 + }); + + // Reply + res({ + available: exist === 0 + }); +}); -- cgit v1.2.3-freya