리팩토링
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ngGET } from "../defs";
|
||||
import { GET } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
@@ -9,7 +9,7 @@ type QType = ExtractQuery<BaseAPI>;
|
||||
const argValidator = undefined;
|
||||
const procDecorator = [] as const;
|
||||
|
||||
export const ReqNonce = ngGET<RType, EType, QType>(argValidator)(procDecorator)(
|
||||
export const ReqNonce = GET<RType, EType, QType>(argValidator)(procDecorator)(
|
||||
(query, ctx, req, res) => {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ngPOST } from "../defs";
|
||||
import { POST } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
import { StartSession } from "../ProcDecorator/StartSession";
|
||||
@@ -19,7 +19,7 @@ const procDecorator = [
|
||||
StartSession,
|
||||
] as const;
|
||||
|
||||
export const LoginByID = ngPOST<RType, EType, QType>(ArgValidator)(procDecorator)(
|
||||
export const LoginByID = POST<RType, EType, QType>(ArgValidator)(procDecorator)(
|
||||
async (query, ctx, req, res) => {
|
||||
const username = query.username;
|
||||
const password = query.password;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ngPOST } from "../defs";
|
||||
import { POST } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
import { StartSession } from "../ProcDecorator/StartSession";
|
||||
@@ -18,7 +18,7 @@ const procDecorator = [
|
||||
StartSession,
|
||||
] as const;
|
||||
|
||||
export const LoginByToken = ngPOST<RType, EType, QType>(ArgValidator)(procDecorator)(
|
||||
export const LoginByToken = POST<RType, EType, QType>(ArgValidator)(procDecorator)(
|
||||
async (query, ctx) => {
|
||||
query.hashedToken;
|
||||
ctx.clearSession();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type APINamespaceType, ngGET } from "../defs";
|
||||
import { type APINamespaceType, GET as GET } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { DefAPINamespace, ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
import { StartSession } from "../ProcDecorator/StartSession";
|
||||
@@ -12,7 +12,7 @@ const procDecorator = [
|
||||
StartSession,
|
||||
] as const;
|
||||
|
||||
export const ReqNonce = ngGET<RType, EType, QType>(argValidator)(procDecorator)(
|
||||
export const ReqNonce = GET<RType, EType, QType>(argValidator)(procDecorator)(
|
||||
async (query, ctx) => {
|
||||
const nonce = await ctx.getValue<string>("nonce");
|
||||
if(nonce !== undefined){
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ngGET } from "../defs";
|
||||
import { GET } from "../defs";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
@@ -7,7 +7,7 @@ type RType = ExtractResponse<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export const test = ngGET<RType, EType, QType>(undefined)(undefined)(
|
||||
export const test = GET<RType, EType, QType>(undefined)(undefined)(
|
||||
(query, ctx, req, res) => {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
+22
-22
@@ -29,9 +29,9 @@ export type APINamespace = {
|
||||
type ValidatorType<T> = T extends undefined ? undefined : ClassType<T>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type AnyAPIExecuter = ngAPIExecuter<any, any, any, any>;
|
||||
export type AnyAPIExecuter = APIExecuter<any, any, any, any>;
|
||||
|
||||
interface ngAPIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> {
|
||||
interface APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> {
|
||||
(query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
||||
httpMethod: HttpMethod;
|
||||
argValidator?: ValidatorType<Q>;
|
||||
@@ -39,35 +39,35 @@ interface ngAPIExecuter<R extends ValidResponse, E extends InvalidResponse, Q ex
|
||||
postDecorator: (ProcDecorator<any, any> | undefined)[];
|
||||
}
|
||||
|
||||
export interface iAPI_GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'get';
|
||||
}
|
||||
|
||||
export interface iAPI_POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'post';
|
||||
}
|
||||
|
||||
export interface iAPI_PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'put';
|
||||
}
|
||||
|
||||
export interface iAPI_DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'delete';
|
||||
}
|
||||
|
||||
export interface iAPI_PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'patch';
|
||||
}
|
||||
|
||||
export interface iAPI_HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD> {
|
||||
export interface iAPI_HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD> {
|
||||
httpMethod: 'head';
|
||||
}
|
||||
|
||||
function ngGenerateAPI<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain>(httpMethod: HttpMethod,
|
||||
function generateAPI<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain>(httpMethod: HttpMethod,
|
||||
argValidator: ValidatorType<Q> | undefined,
|
||||
procDecoratorChain: PD,
|
||||
callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>,
|
||||
): ngAPIExecuter<R, E, Q, PD> {
|
||||
): APIExecuter<R, E, Q, PD> {
|
||||
const preDecorator: ProcDecorator<any, any>[] = [];
|
||||
const postDecorator: (ProcDecorator<any, any> | undefined)[] = [];
|
||||
if (procDecoratorChain) {
|
||||
@@ -95,10 +95,10 @@ function ngGenerateAPI<R extends ValidResponse, E extends InvalidResponse, Q ext
|
||||
);
|
||||
}
|
||||
|
||||
export function ngGET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'get',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
@@ -108,10 +108,10 @@ export function ngGET<R extends ValidResponse, E extends InvalidResponse, Q exte
|
||||
}
|
||||
}
|
||||
|
||||
export function ngPOST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'post',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
@@ -121,10 +121,10 @@ export function ngPOST<R extends ValidResponse, E extends InvalidResponse, Q ext
|
||||
}
|
||||
}
|
||||
|
||||
export function ngPUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'put',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
@@ -134,10 +134,10 @@ export function ngPUT<R extends ValidResponse, E extends InvalidResponse, Q exte
|
||||
}
|
||||
}
|
||||
|
||||
export function ngDELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'delete',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
@@ -147,10 +147,10 @@ export function ngDELETE<R extends ValidResponse, E extends InvalidResponse, Q e
|
||||
}
|
||||
}
|
||||
|
||||
export function ngPATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'patch',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
@@ -160,10 +160,10 @@ export function ngPATCH<R extends ValidResponse, E extends InvalidResponse, Q ex
|
||||
}
|
||||
}
|
||||
|
||||
export function ngHEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
export function HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>(argValidator?: ValidatorType<Q>) {
|
||||
return <PD extends ProcDecoratorChain>(procDecoratorChain: PD) => {
|
||||
return (callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>) => {
|
||||
return ngGenerateAPI<R, E, Q, PD>(
|
||||
return generateAPI<R, E, Q, PD>(
|
||||
'head',
|
||||
argValidator,
|
||||
procDecoratorChain,
|
||||
|
||||
+48
-46
@@ -64,7 +64,7 @@ async function preCtx<PD extends ProcDecorator<any, any>>(preDecorator: PlainDec
|
||||
return ctx;
|
||||
}
|
||||
|
||||
async function postCtx<PD extends ProcDecoratorChain>(ctx: ResolveChain<PD>, postDecorator: (undefined|PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
||||
async function postCtx<PD extends ProcDecoratorChain>(ctx: ResolveChain<PD>, postDecorator: (undefined | PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
||||
if (!postDecorator) {
|
||||
return;
|
||||
}
|
||||
@@ -137,53 +137,55 @@ async function apiRun(query: object, req: Request, res: Response, api: AnyAPIExe
|
||||
|
||||
export function buildAPISystem<N extends APINamespace, Q extends AnyAPIExecuter>(api: N | Q): Router {
|
||||
const router = Router();
|
||||
|
||||
if(typeof api === 'function') {
|
||||
throw 'root api cannot be function';
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(api)) {
|
||||
if (typeof value === 'function') {
|
||||
const executer = new value() as Q;
|
||||
|
||||
if (!executer.httpMethod) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
|
||||
if (executer.httpMethod === 'head') {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseParam(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
else {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseBody(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
continue;
|
||||
} else {
|
||||
if (typeof value !== 'function') {
|
||||
router.use(key, buildAPISystem(value));
|
||||
continue;
|
||||
}
|
||||
const executer = value;
|
||||
|
||||
if (!executer.httpMethod) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
|
||||
if (executer.httpMethod === 'get') {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseParam(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
else {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseBody(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
}
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user