mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-14 11:23:07 +02:00
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import * as fs from 'fs';
|
|
import { ILocalUser } from '../../models/entities/user';
|
|
import { IEndpointMeta } from './endpoints';
|
|
import { ApiError } from './error';
|
|
import { App } from '../../models/entities/app';
|
|
import { SchemaType } from '../../misc/schema';
|
|
|
|
type Params<T extends IEndpointMeta> = {
|
|
[P in keyof T['params']]: NonNullable<T['params']>[P]['transform'] extends Function
|
|
? ReturnType<NonNullable<T['params']>[P]['transform']>
|
|
: ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0];
|
|
};
|
|
|
|
export type Response = Record<string, any> | void;
|
|
|
|
type executor<T extends IEndpointMeta> =
|
|
(params: Params<T>, user: ILocalUser, app: App, file?: any, cleanup?: Function) =>
|
|
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;
|
|
|
|
export default function <T extends IEndpointMeta>(meta: T, cb: executor<T>)
|
|
: (params: any, user: ILocalUser, app: App, file?: any) => Promise<any> {
|
|
return (params: any, user: ILocalUser, app: App, file?: any) => {
|
|
function cleanup() {
|
|
fs.unlink(file.path, () => {});
|
|
}
|
|
|
|
if (meta.requireFile && file == null) return Promise.reject(new ApiError({
|
|
message: 'File required.',
|
|
code: 'FILE_REQUIRED',
|
|
id: '4267801e-70d1-416a-b011-4ee502885d8b',
|
|
}));
|
|
|
|
const [ps, pserr] = getParams(meta, params);
|
|
if (pserr) {
|
|
if (file) cleanup();
|
|
return Promise.reject(pserr);
|
|
}
|
|
|
|
return cb(ps, user, app, file, cleanup);
|
|
};
|
|
}
|
|
|
|
function getParams<T extends IEndpointMeta>(defs: T, params: any): [Params<T>, ApiError | null] {
|
|
if (defs.params == null) return [params, null];
|
|
|
|
const x: any = {};
|
|
let err: ApiError | null = null;
|
|
Object.entries(defs.params).some(([k, def]) => {
|
|
const [v, e] = def.validator.get(params[k]);
|
|
if (e) {
|
|
err = new ApiError({
|
|
message: 'Invalid param.',
|
|
code: 'INVALID_PARAM',
|
|
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
|
|
}, {
|
|
param: k,
|
|
reason: e.message
|
|
});
|
|
return true;
|
|
} else {
|
|
if (v === undefined && def.hasOwnProperty('default')) {
|
|
x[k] = def.default;
|
|
} else {
|
|
x[k] = v;
|
|
}
|
|
if (def.transform) x[k] = def.transform(x[k]);
|
|
return false;
|
|
}
|
|
});
|
|
return [x, err];
|
|
}
|