iceshrimp-legacy/src/server/api/endpoints/app/name_id/available.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
2017-03-08 19:50:09 +01:00
import $ from 'cafy';
2018-03-29 13:32:18 +02:00
import App from '../../../../../models/app';
import { isValidNameId } from '../../../../../models/app';
2016-12-28 23:49:51 +01:00
2017-01-06 09:45:04 +01:00
/**
* @swagger
2018-03-29 07:48:47 +02:00
* /app/nameId/available:
2018-04-07 19:30:37 +02:00
* note:
2018-03-29 07:48:47 +02:00
* summary: Check available nameId on creation an application
2017-01-06 09:45:04 +01:00
* parameters:
* -
2018-03-29 07:48:47 +02:00
* name: nameId
2017-01-06 09:45:04 +01:00
* description: Application unique name
* in: formData
* required: true
* type: string
2017-03-01 09:37:01 +01:00
*
2017-01-06 09:45:04 +01:00
* responses:
* 200:
* description: Success
* schema:
* type: object
* properties:
* available:
2018-03-29 07:48:47 +02:00
* description: Whether nameId is available
2017-01-06 09:45:04 +01:00
* type: boolean
2017-03-01 09:37:01 +01:00
*
2017-01-06 09:45:04 +01:00
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
2016-12-28 23:49:51 +01:00
/**
2018-03-29 07:48:47 +02:00
* Check available nameId of app
2016-12-28 23:49:51 +01:00
*
2017-03-01 09:37:01 +01:00
* @param {any} params
* @return {Promise<any>}
2016-12-28 23:49:51 +01:00
*/
2018-06-18 02:54:53 +02:00
module.exports = async (params: any) => new Promise(async (res, rej) => {
2018-03-29 07:48:47 +02:00
// Get 'nameId' parameter
2018-05-02 11:06:16 +02:00
const [nameId, nameIdErr] = $.str.pipe(isValidNameId).get(params.nameId);
2018-03-29 07:48:47 +02:00
if (nameIdErr) return rej('invalid nameId param');
2016-12-28 23:49:51 +01:00
// Get exist
const exist = await App
.count({
2018-03-29 07:48:47 +02:00
nameIdLower: nameId.toLowerCase()
2016-12-28 23:49:51 +01:00
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});