Sharkey/packages/backend/src/server/api/endpoints.ts

125 lines
2.9 KiB
TypeScript
Raw Normal View History

import { fileURLToPath } from 'url';
import { dirname } from 'path';
2018-11-01 20:32:24 +02:00
import { Context } from 'cafy';
2018-07-15 21:43:36 +03:00
import * as path from 'path';
import * as glob from 'glob';
import { SimpleSchema } from '@/misc/simple-schema';
//const _filename = fileURLToPath(import.meta.url);
const _filename = __filename;
const _dirname = dirname(_filename);
2018-07-15 21:25:35 +03:00
export type Param = {
validator: Context<any>;
transform?: any;
default?: any;
2019-02-24 21:18:09 +02:00
deprecated?: boolean;
ref?: string;
};
2018-07-15 21:43:36 +03:00
export interface IEndpointMeta {
2018-11-01 20:32:24 +02:00
stability?: string; //'deprecated' | 'experimental' | 'stable';
2018-10-21 23:16:27 +03:00
tags?: string[];
2018-11-01 20:32:24 +02:00
params?: {
[key: string]: Param;
};
errors?: {
2018-11-01 20:32:24 +02:00
[key: string]: {
message: string;
code: string;
id: string;
2018-11-01 20:32:24 +02:00
};
};
2018-07-15 21:53:03 +03:00
res?: SimpleSchema;
2018-07-15 21:53:03 +03:00
2018-07-15 21:25:35 +03:00
/**
*
* false
*/
requireCredential?: boolean;
2018-08-13 19:05:58 +03:00
/**
* 使
*/
requireAdmin?: boolean;
2018-11-14 21:15:42 +02:00
/**
* 使
*/
requireModerator?: boolean;
2018-07-15 21:25:35 +03:00
/**
*
*
* withCredential false
*/
limit?: {
/**
*
*/
key?: string;
/**
* (ms)
* max
*/
duration?: number;
/**
* durationで指定した期間内にいくつまでリクエストできるのか
* duration
*/
max?: number;
/**
* (ms)
*/
minInterval?: number;
};
/**
*
* false
*/
2018-07-25 01:18:50 +03:00
requireFile?: boolean;
2018-07-15 21:25:35 +03:00
/**
*
* false
*/
secure?: boolean;
/**
*
*
*/
kind?: string;
}
2018-07-15 21:43:36 +03:00
export interface IEndpoint {
name: string;
exec: any;
meta: IEndpointMeta;
}
const files = glob.sync('**/*.js', {
2021-12-09 16:58:30 +02:00
cwd: path.resolve(_dirname + '/endpoints/'),
2018-07-15 21:43:36 +03:00
});
const endpoints: IEndpoint[] = files.map(f => {
2018-09-01 17:12:51 +03:00
const ep = require(`./endpoints/${f}`);
2018-07-15 21:43:36 +03:00
return {
name: f.replace('.js', ''),
exec: ep.default,
2021-12-09 16:58:30 +02:00
meta: ep.meta || {},
2018-07-15 21:43:36 +03:00
};
});
export default endpoints;