Refactoring

This commit is contained in:
syuilo 2019-01-23 19:33:29 +09:00
parent 4ec64b4c57
commit ad66c8478a
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
1 changed files with 14 additions and 15 deletions

View File

@ -4,37 +4,37 @@ import { IUser } from '../../models/user';
import { IApp } from '../../models/app'; import { IApp } from '../../models/app';
import endpoints from './endpoints'; import endpoints from './endpoints';
export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any) => new Promise<any>(async (ok, rej) => { export default async (endpoint: string, user: IUser, app: IApp, data: any, file?: any) => {
const isSecure = user != null && app == null; const isSecure = user != null && app == null;
const ep = endpoints.find(e => e.name === endpoint); const ep = endpoints.find(e => e.name === endpoint);
if (ep == null) { if (ep == null) {
return rej('ENDPOINT_NOT_FOUND'); throw 'ENDPOINT_NOT_FOUND';
} }
if (ep.meta.secure && !isSecure) { if (ep.meta.secure && !isSecure) {
return rej('ACCESS_DENIED'); throw 'ACCESS_DENIED';
} }
if (ep.meta.requireCredential && user == null) { if (ep.meta.requireCredential && user == null) {
return rej('CREDENTIAL_REQUIRED'); throw 'CREDENTIAL_REQUIRED';
} }
if (ep.meta.requireCredential && user.isSuspended) { if (ep.meta.requireCredential && user.isSuspended) {
return rej('YOUR_ACCOUNT_HAS_BEEN_SUSPENDED'); throw 'YOUR_ACCOUNT_HAS_BEEN_SUSPENDED';
} }
if (ep.meta.requireAdmin && !user.isAdmin) { if (ep.meta.requireAdmin && !user.isAdmin) {
return rej('YOU_ARE_NOT_ADMIN'); throw 'YOU_ARE_NOT_ADMIN';
} }
if (ep.meta.requireModerator && !user.isAdmin && !user.isModerator) { if (ep.meta.requireModerator && !user.isAdmin && !user.isModerator) {
return rej('YOU_ARE_NOT_MODERATOR'); throw 'YOU_ARE_NOT_MODERATOR';
} }
if (app && ep.meta.kind && !app.permission.some(p => p === ep.meta.kind)) { if (app && ep.meta.kind && !app.permission.some(p => p === ep.meta.kind)) {
return rej('PERMISSION_DENIED'); throw 'PERMISSION_DENIED';
} }
if (ep.meta.requireCredential && ep.meta.limit) { if (ep.meta.requireCredential && ep.meta.limit) {
@ -42,7 +42,7 @@ export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any)
await limiter(ep, user); // Rate limit await limiter(ep, user); // Rate limit
} catch (e) { } catch (e) {
// drop request if limit exceeded // drop request if limit exceeded
return rej('RATE_LIMIT_EXCEEDED'); throw 'RATE_LIMIT_EXCEEDED';
} }
} }
@ -61,16 +61,15 @@ export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any)
} }
} catch (e) { } catch (e) {
if (e && e.name == 'INVALID_PARAM') { if (e && e.name == 'INVALID_PARAM') {
rej({ throw {
code: e.name, code: e.name,
param: e.param, param: e.param,
reason: e.message reason: e.message
}); };
} else { } else {
rej(e); throw e;
} }
return;
} }
ok(res); return res;
}); };