강력한 API 타입 체크
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { POST } from "../base";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
type BaseAPI = typeof structure.Login.LoginByID;
|
||||
type RType = ExtractResponse<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export class LoginByID extends POST<RType, EType, QType>{
|
||||
protected LoginByID = Symbol("LoginByID");//TODO: remove this
|
||||
protected override async api(query: QType): Promise<RType | EType | true> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { POST } from "../base";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
type BaseAPI = typeof structure.Login.LoginByToken;
|
||||
type RType = ExtractResponse<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export class LoginByToken extends POST<RType, EType, QType>{
|
||||
protected LoginByToken = Symbol("LoginByToken");//TODO: remove this
|
||||
protected override async api(query: QType): Promise<RType | EType | true> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { POST } from "../base";
|
||||
import type { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs";
|
||||
|
||||
type BaseAPI = typeof structure.Login.ReqNonce;
|
||||
type RType = ExtractResponse<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export class ReqNonce extends POST<RType, EType, QType>{
|
||||
protected ReqNonce = Symbol("ReqNonce");//TODO: remove this
|
||||
protected override async api(query: QType): Promise<RType | EType | true> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { structure } from "../../apiStructure/sammoRootAPI";
|
||||
import { APINamespaceType } from "../base";
|
||||
import { LoginByID } from "./LoginByID";
|
||||
import { LoginByToken } from "./LoginByToken";
|
||||
import { ReqNonce } from "./ReqNonce";
|
||||
|
||||
export const Login = {
|
||||
LoginByID,
|
||||
LoginByToken,
|
||||
ReqNonce,
|
||||
} satisfies APINamespaceType<typeof structure.Login>;
|
||||
+32
-19
@@ -1,22 +1,8 @@
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
|
||||
export interface ValidResponse {
|
||||
result: true;
|
||||
}
|
||||
|
||||
export type recoveryMethod = 'refreshEntirePage' | 'retryAPI' | 'gameLogin' | 'gatewayLogin' | 'gatewayPIN';
|
||||
|
||||
export interface InvalidResponse {
|
||||
result: false;
|
||||
reason: string;
|
||||
recovery?: recoveryMethod;
|
||||
}
|
||||
import type { APICallT, Callable, DefAPINamespace, EmptyAPICallT, HttpMethod, InvalidResponse, RawArgType, ValidResponse, recoveryMethod } from '../apiStructure/defs';
|
||||
|
||||
export const treatedSpecial = Symbol('treatedSpecial');
|
||||
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
|
||||
export type RawArgType = Record<string, unknown> | Record<string, unknown>[] | undefined;
|
||||
|
||||
export type APINamespace = {
|
||||
[key: string]: APINamespace
|
||||
| GET<any, any, any>
|
||||
@@ -29,14 +15,14 @@ export type APINamespace = {
|
||||
}
|
||||
|
||||
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
readonly abstract reqType: HttpMethod | HttpMethod[];
|
||||
readonly abstract reqType: HttpMethod;
|
||||
protected abstract parseQuery(expressReq: Request): Promise<Q>;
|
||||
protected abstract api(query: Q, expressReq: Request, expressRes: Response): Promise<R | E | typeof treatedSpecial>;
|
||||
protected abstract api(query: Q, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
||||
public async run(expressReq: Request, expressRes: Response): Promise<void> {
|
||||
const query = await this.parseQuery(expressReq);
|
||||
//TODO: middleware (인증, 요구사항, 아마도 decorator)
|
||||
const result = await this.api(query, expressReq, expressRes);
|
||||
if (result === treatedSpecial) {
|
||||
if (result === true) {
|
||||
return;
|
||||
}
|
||||
expressRes.json(result);
|
||||
@@ -77,4 +63,31 @@ export abstract class PATCH<R extends ValidResponse, E extends InvalidResponse,
|
||||
|
||||
export abstract class HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'head';
|
||||
}
|
||||
}
|
||||
|
||||
export function raiseError(reason: string, recovery?: recoveryMethod): InvalidResponse {
|
||||
if (recovery) {
|
||||
return {
|
||||
result: false,
|
||||
reason,
|
||||
recovery,
|
||||
};
|
||||
}
|
||||
return {
|
||||
result: false,
|
||||
reason,
|
||||
};
|
||||
}
|
||||
|
||||
export type GetClassGenerator<T> = {
|
||||
prototype: T
|
||||
};
|
||||
export type APIServerType<T extends Callable> =
|
||||
T extends APICallT<infer Q, infer R, infer E> ? GetClassGenerator<APIExecuter<R, E, Q>> :
|
||||
never;
|
||||
export type APINamespaceType<T extends DefAPINamespace> = {
|
||||
[K in keyof T]:
|
||||
T[K] extends Callable ? APIServerType<T[K]> :
|
||||
T[K] extends DefAPINamespace ? APINamespaceType<T[K]> :
|
||||
never;
|
||||
};
|
||||
+5
-12
@@ -6,20 +6,13 @@ export function buildAPISystem<N extends APINamespace, Q extends APIExecuter<any
|
||||
for (const [key, value] of Object.entries(api)) {
|
||||
if (typeof value === 'function') {
|
||||
const executer = new value() as Q;
|
||||
if(!executer.reqType){
|
||||
if (!executer.reqType) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
if(!Array.isArray(executer.reqType)){
|
||||
router[executer.reqType](key, async (req, res) => {
|
||||
await executer.run(req, res);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
for(const type of new Set(executer.reqType)){
|
||||
router[type](key, async (req, res) => {
|
||||
await executer.run(req, res);
|
||||
});
|
||||
}
|
||||
router[executer.reqType](key, async (req, res) => {
|
||||
await executer.run(req, res);
|
||||
});
|
||||
continue;
|
||||
} else {
|
||||
router.use(key, buildAPISystem(value));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user