misskey/src/server/api/endpoints/app/name_id/available.ts
2018-03-29 14:48:47 +09:00

61 lines
1.3 KiB
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
import App from '../../../models/app';
import { isValidNameId } from '../../../models/app';
/**
* @swagger
* /app/nameId/available:
* post:
* summary: Check available nameId on creation an application
* parameters:
* -
* name: nameId
* description: Application unique name
* in: formData
* required: true
* type: string
*
* responses:
* 200:
* description: Success
* schema:
* type: object
* properties:
* available:
* description: Whether nameId is available
* type: boolean
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Check available nameId of app
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = async (params) => new Promise(async (res, rej) => {
// Get 'nameId' parameter
const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
if (nameIdErr) return rej('invalid nameId param');
// Get exist
const exist = await App
.count({
nameIdLower: nameId.toLowerCase()
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});