diff --git a/server/api/Login/ChangePassword.ts b/server/api/Login/ChangePassword.ts index 50f105c..ef82b79 100644 --- a/server/api/Login/ChangePassword.ts +++ b/server/api/Login/ChangePassword.ts @@ -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; const argValidator = undefined; const procDecorator = [] as const; -export const ReqNonce = ngGET(argValidator)(procDecorator)( +export const ReqNonce = GET(argValidator)(procDecorator)( (query, ctx, req, res) => { throw new Error("Method not implemented."); } diff --git a/server/api/Login/LoginByID.ts b/server/api/Login/LoginByID.ts index 073a229..109e724 100644 --- a/server/api/Login/LoginByID.ts +++ b/server/api/Login/LoginByID.ts @@ -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(ArgValidator)(procDecorator)( +export const LoginByID = POST(ArgValidator)(procDecorator)( async (query, ctx, req, res) => { const username = query.username; const password = query.password; diff --git a/server/api/Login/LoginByToken.ts b/server/api/Login/LoginByToken.ts index 0748b2c..892cb94 100644 --- a/server/api/Login/LoginByToken.ts +++ b/server/api/Login/LoginByToken.ts @@ -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(ArgValidator)(procDecorator)( +export const LoginByToken = POST(ArgValidator)(procDecorator)( async (query, ctx) => { query.hashedToken; ctx.clearSession(); diff --git a/server/api/Login/ReqNonce.ts b/server/api/Login/ReqNonce.ts index 1e59aff..dded3b1 100644 --- a/server/api/Login/ReqNonce.ts +++ b/server/api/Login/ReqNonce.ts @@ -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(argValidator)(procDecorator)( +export const ReqNonce = GET(argValidator)(procDecorator)( async (query, ctx) => { const nonce = await ctx.getValue("nonce"); if(nonce !== undefined){ diff --git a/server/api/Login/test.ts b/server/api/Login/test.ts index c5c5469..32f6981 100644 --- a/server/api/Login/test.ts +++ b/server/api/Login/test.ts @@ -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; type EType = ExtractError; type QType = ExtractQuery; -export const test = ngGET(undefined)(undefined)( +export const test = GET(undefined)(undefined)( (query, ctx, req, res) => { throw new Error("Method not implemented."); } diff --git a/server/api/defs.ts b/server/api/defs.ts index f71c254..18ec97a 100644 --- a/server/api/defs.ts +++ b/server/api/defs.ts @@ -29,9 +29,9 @@ export type APINamespace = { type ValidatorType = T extends undefined ? undefined : ClassType; // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type AnyAPIExecuter = ngAPIExecuter; +export type AnyAPIExecuter = APIExecuter; -interface ngAPIExecuter { +interface APIExecuter { (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response): Promise; httpMethod: HttpMethod; argValidator?: ValidatorType; @@ -39,35 +39,35 @@ interface ngAPIExecuter | undefined)[]; } -export interface iAPI_GET extends ngAPIExecuter { +export interface iAPI_GET extends APIExecuter { httpMethod: 'get'; } -export interface iAPI_POST extends ngAPIExecuter { +export interface iAPI_POST extends APIExecuter { httpMethod: 'post'; } -export interface iAPI_PUT extends ngAPIExecuter { +export interface iAPI_PUT extends APIExecuter { httpMethod: 'put'; } -export interface iAPI_DELETE extends ngAPIExecuter { +export interface iAPI_DELETE extends APIExecuter { httpMethod: 'delete'; } -export interface iAPI_PATCH extends ngAPIExecuter { +export interface iAPI_PATCH extends APIExecuter { httpMethod: 'patch'; } -export interface iAPI_HEAD extends ngAPIExecuter { +export interface iAPI_HEAD extends APIExecuter { httpMethod: 'head'; } -function ngGenerateAPI(httpMethod: HttpMethod, +function generateAPI(httpMethod: HttpMethod, argValidator: ValidatorType | undefined, procDecoratorChain: PD, callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise, -): ngAPIExecuter { +): APIExecuter { const preDecorator: ProcDecorator[] = []; const postDecorator: (ProcDecorator | undefined)[] = []; if (procDecoratorChain) { @@ -95,10 +95,10 @@ function ngGenerateAPI(argValidator?: ValidatorType) { +export function GET(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'get', argValidator, procDecoratorChain, @@ -108,10 +108,10 @@ export function ngGET(argValidator?: ValidatorType) { +export function POST(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'post', argValidator, procDecoratorChain, @@ -121,10 +121,10 @@ export function ngPOST(argValidator?: ValidatorType) { +export function PUT(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'put', argValidator, procDecoratorChain, @@ -134,10 +134,10 @@ export function ngPUT(argValidator?: ValidatorType) { +export function DELETE(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'delete', argValidator, procDecoratorChain, @@ -147,10 +147,10 @@ export function ngDELETE(argValidator?: ValidatorType) { +export function PATCH(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'patch', argValidator, procDecoratorChain, @@ -160,10 +160,10 @@ export function ngPATCH(argValidator?: ValidatorType) { +export function HEAD(argValidator?: ValidatorType) { return (procDecoratorChain: PD) => { return (callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise) => { - return ngGenerateAPI( + return generateAPI( 'head', argValidator, procDecoratorChain, diff --git a/server/api/generator.ts b/server/api/generator.ts index 758a941..acca317 100644 --- a/server/api/generator.ts +++ b/server/api/generator.ts @@ -64,7 +64,7 @@ async function preCtx>(preDecorator: PlainDec return ctx; } -async function postCtx(ctx: ResolveChain, postDecorator: (undefined|PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> { +async function postCtx(ctx: ResolveChain, 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(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; - 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; - 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; + 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; + 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;