작업중
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export function APIPathGen<T extends object, V>(
|
||||
obj: T,
|
||||
callback: (path: string[], tail: V, pathParam?: Record<string, string | number>) => unknown,
|
||||
path: string[] = [],
|
||||
): T {
|
||||
const map = new Map<string, unknown>();
|
||||
return new Proxy(obj, {
|
||||
get(target, key) {
|
||||
if(typeof key === 'symbol'){
|
||||
throw new Error('Symbol is not supported');
|
||||
}
|
||||
|
||||
if(map.has(key)){
|
||||
return map.get(key)!;
|
||||
}
|
||||
|
||||
const nextPath = [...path, key];
|
||||
|
||||
let next: T[keyof T];
|
||||
if (key in target) {
|
||||
next = target[key as keyof typeof target];
|
||||
}
|
||||
else {
|
||||
throw `${nextPath} is not exists`;
|
||||
}
|
||||
|
||||
if (typeof (next) === 'function') {
|
||||
const result = callback(nextPath, next);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
const result = APIPathGen<T[keyof T], V>(next, callback, nextPath);
|
||||
map.set(key, result);
|
||||
return result;
|
||||
}
|
||||
}) as T;
|
||||
};
|
||||
+10
-10
@@ -28,8 +28,8 @@ export type APINamespace = {
|
||||
;
|
||||
}
|
||||
|
||||
export abstract class APIExecuter<Q extends object, R extends ValidResponse, E extends InvalidResponse>{
|
||||
readonly reqType: HttpMethod | HttpMethod[];
|
||||
export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
readonly abstract reqType: HttpMethod | HttpMethod[];
|
||||
protected abstract parseQuery(expressReq: Request): Promise<Q>;
|
||||
protected abstract api(query: Q, expressReq: Request, expressRes: Response): Promise<R | E | typeof treatedSpecial>;
|
||||
public async run(expressReq: Request, expressRes: Response): Promise<void> {
|
||||
@@ -43,15 +43,15 @@ export abstract class APIExecuter<Q extends object, R extends ValidResponse, E e
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class APIBodyParseExecuter<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIExecuter<Q, R, E>{
|
||||
export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{
|
||||
protected async parseQuery(expressReq: Request): Promise<Q> {
|
||||
throw new Error('Not implemented');
|
||||
return expressReq.body;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class GET<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIExecuter<Q, R, E>{
|
||||
readonly reqType = 'get';
|
||||
export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{
|
||||
override readonly reqType = 'get';
|
||||
protected async parseQuery(expressReq: Request): Promise<Q> {
|
||||
expressReq.query;
|
||||
throw new Error('Not implemented');
|
||||
@@ -59,22 +59,22 @@ export abstract class GET<Q extends object, R extends ValidResponse, E extends I
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class POST<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class POST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'post';
|
||||
}
|
||||
|
||||
export abstract class PUT<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class PUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'put';
|
||||
}
|
||||
|
||||
export abstract class DELETE<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class DELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'delete';
|
||||
}
|
||||
|
||||
export abstract class PATCH<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class PATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'patch';
|
||||
}
|
||||
|
||||
export abstract class HEAD<Q extends object, R extends ValidResponse, E extends InvalidResponse> extends APIBodyParseExecuter<Q, R, E>{
|
||||
export abstract class HEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIBodyParseExecuter<R, E, Q>{
|
||||
readonly reqType = 'head';
|
||||
}
|
||||
@@ -1,10 +1,53 @@
|
||||
import ky from 'ky';
|
||||
import type { APIExecuter, APINamespace, HttpMethod, InvalidResponse, RawArgType, ValidResponse } from './base.js';
|
||||
import type { APIExecuter, APINamespace, HttpMethod, InvalidResponse, RawArgType, ValidResponse } from '../api/base.js';
|
||||
import isArray from 'lodash-es/isArray.js';
|
||||
import isEmpty from 'lodash-es/isEmpty.js';
|
||||
|
||||
const apiPath = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api';
|
||||
|
||||
interface BasicAPICallT<
|
||||
ArgType extends RawArgType,
|
||||
ResultType extends ValidResponse,
|
||||
ErrorType extends InvalidResponse
|
||||
> {
|
||||
(args: ArgType): Promise<ResultType>;
|
||||
(args: ArgType, returnError: false): Promise<ResultType>;
|
||||
(args: ArgType, returnError: true): Promise<ResultType | ErrorType>;
|
||||
}
|
||||
|
||||
interface EmptyAPICallT<ResultType extends ValidResponse, ErrorType extends InvalidResponse> {
|
||||
(): Promise<ResultType>;
|
||||
(args: undefined): Promise<ResultType>;
|
||||
(args: undefined, returnError: false): Promise<ResultType>;
|
||||
(args: undefined, returnError: true): Promise<ResultType | ErrorType>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type ArgTypeOf<T> = T extends APICallT<infer A, any, any> ? A : never;
|
||||
|
||||
|
||||
export type APICallT<
|
||||
ArgType extends RawArgType,
|
||||
ResultType extends ValidResponse = ValidResponse,
|
||||
ErrorType extends InvalidResponse = InvalidResponse
|
||||
> = ArgType extends undefined ? EmptyAPICallT<ResultType, ErrorType> : BasicAPICallT<ArgType, ResultType, ErrorType>;
|
||||
|
||||
export type APITail = typeof GET | typeof POST | typeof PUT | typeof PATCH | typeof HEAD | typeof DELETE;
|
||||
|
||||
const httpMethodMap = new Map<APITail, HttpMethod>([
|
||||
[GET, "get"],
|
||||
[POST, "post"],
|
||||
[PUT, "put"],
|
||||
[PATCH, "patch"],
|
||||
[HEAD, "head"],
|
||||
[DELETE, "delete"],
|
||||
]);
|
||||
|
||||
export function extractHttpMethod(tail: APITail): HttpMethod {
|
||||
return httpMethodMap.get(tail) ?? "post";
|
||||
}
|
||||
|
||||
|
||||
export async function GET<ResultType extends ValidResponse, ArgType extends undefined = undefined>(
|
||||
args?: ArgType
|
||||
): Promise<ResultType>;
|
||||
@@ -0,0 +1,56 @@
|
||||
import { APIPathGen } from "../api/APIPathGen";
|
||||
import { RawArgType } from "../api/base";
|
||||
import { APICallT, APITail, GET, POST, callClientAPI, extractHttpMethod } from "./clientAPI";
|
||||
|
||||
export type LoginResponse = {
|
||||
result: true,
|
||||
nextToken: [number, string] | undefined,
|
||||
}
|
||||
|
||||
export type LoginFailed = {
|
||||
result: false,
|
||||
reqOTP: boolean,
|
||||
reason: string,
|
||||
}
|
||||
|
||||
|
||||
export type AutoLoginNonceResponse = {
|
||||
result: true,
|
||||
loginNonce: string,
|
||||
};
|
||||
|
||||
export type AutoLoginResponse = {
|
||||
result: true,
|
||||
nextToken: [number, string] | undefined,
|
||||
}
|
||||
|
||||
|
||||
export type AutoLoginFailed = {
|
||||
result: false,
|
||||
silent: boolean,
|
||||
reason: string,
|
||||
}
|
||||
|
||||
const apiRealPath = {
|
||||
Login: {
|
||||
LoginByID: POST as APICallT<{
|
||||
username: string,
|
||||
password: string,
|
||||
}, LoginResponse, LoginFailed>,
|
||||
LoginByToken: POST as APICallT<{
|
||||
hashedToken: string,
|
||||
token_id: number,
|
||||
}, AutoLoginResponse, AutoLoginFailed>,
|
||||
ReqNonce: GET as APICallT<undefined, AutoLoginNonceResponse, AutoLoginFailed>
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const SammoRootAPI = APIPathGen(apiRealPath, (path: string[], tail: APITail, pathParam) => {
|
||||
const method = extractHttpMethod(tail);
|
||||
return (args?: RawArgType, returnError?: boolean) => {
|
||||
if (returnError) {
|
||||
return callClientAPI(method, path.join('/'), args, pathParam, true);
|
||||
}
|
||||
return callClientAPI(method, path.join('/'), args, pathParam);
|
||||
};
|
||||
});
|
||||
+7
-16
@@ -1,6 +1,6 @@
|
||||
import 'dotenv/config';
|
||||
import 'reflect-metadata';
|
||||
import { BasicQueryDTO } from './api/index.js';
|
||||
import { BasicQueryDTO } from './api/serverImpl.js';
|
||||
import { validate } from 'class-validator';
|
||||
import express, { type Request, type Response } from "express"
|
||||
import { AppDataSource } from "./data_source.js"
|
||||
@@ -21,22 +21,13 @@ export async function test() {
|
||||
console.log(result);
|
||||
//must be integer가 나타나야함
|
||||
|
||||
const rawRng = new LiteHashDRBG(simpleSerialize(
|
||||
'haha',
|
||||
'hoho'
|
||||
));
|
||||
|
||||
const arr: Promise<Uint8Array>[] = [];
|
||||
let j = 0;
|
||||
for(let i = 0; i < 100; i += 1){
|
||||
arr.push(rawRng.nextBits(32).then((v) => {
|
||||
console.log(`${j}: ${i}`);
|
||||
j += 1;
|
||||
return v;
|
||||
}));
|
||||
const abc= {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 3,
|
||||
}
|
||||
|
||||
await Promise.all(arr);
|
||||
const t2 = await validate(abc);
|
||||
console.log(t2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+7
-5
@@ -11,18 +11,20 @@
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"noImplicitOverride": true,
|
||||
"noImplicitReturns": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"importHelpers": true,
|
||||
"isolatedModules": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "node",
|
||||
"strict": true,
|
||||
"importHelpers": true,
|
||||
"noImplicitOverride": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"paths": {
|
||||
"@util/*": ["./server/util/*"],
|
||||
"@server/*": ["./server/*"],
|
||||
|
||||
Reference in New Issue
Block a user