express.js 준비

This commit is contained in:
2023-08-06 13:53:12 +00:00
parent 691f5e79ce
commit 69de10642e
4 changed files with 20 additions and 36 deletions
+2 -2
View File
@@ -8,8 +8,8 @@ type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
class Q implements QType { class Q implements QType {
username: string; username!: string;
password: string; password!: string;
} }
export class LoginByID extends POST<RType, EType, QType>{ export class LoginByID extends POST<RType, EType, QType>{
readonly argValidator = undefined; readonly argValidator = undefined;
+11 -13
View File
@@ -3,20 +3,18 @@ import type { Request, Response } from 'express';
import type { Callable, DefAPINamespace, DeleteAPICallT, GetAPICallT, HeadAPICallT, HttpMethod, InvalidResponse, PatchAPICallT, PostAPICallT, PutAPICallT, RawArgType, ValidResponse, recoveryMethod } from '../apiStructure/defs'; import type { Callable, DefAPINamespace, DeleteAPICallT, GetAPICallT, HeadAPICallT, HttpMethod, InvalidResponse, PatchAPICallT, PostAPICallT, PutAPICallT, RawArgType, ValidResponse, recoveryMethod } from '../apiStructure/defs';
import { import {
validate, validate,
validateSync, type ValidatorOptions,
validateOrReject, } from "class-validator";
ValidatorOptions, import { type ClassTransformOptions, plainToInstance } from "class-transformer";
} from "class-validator";
import { ClassTransformOptions, plainToInstance } from "class-transformer";
export type APINamespace = { export type APINamespace = {
[key: string]: APINamespace [key: string]: APINamespace
| GET<any, any, any> | ClassType<GET<any, any, any>>
| POST<any, any, any> | ClassType<POST<any, any, any>>
| PUT<any, any, any> | ClassType<PUT<any, any, any>>
| DELETE<any, any, any> | ClassType<DELETE<any, any, any>>
| PATCH<any, any, any> | ClassType<PATCH<any, any, any>>
| HEAD<any, any, any> | ClassType<HEAD<any, any, any>>
; ;
} }
@@ -41,7 +39,7 @@ export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResp
export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{ 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> { protected async parseQuery(expressReq: Request): Promise<Q> {
if(!this.argValidator){ if (!this.argValidator) {
return expressReq.body as Q; return expressReq.body as Q;
} }
@@ -59,7 +57,7 @@ export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends In
export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{ export abstract class GET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType> extends APIExecuter<R, E, Q>{
override readonly reqType = 'get'; override readonly reqType = 'get';
protected async parseQuery(expressReq: Request): Promise<Q> { protected async parseQuery(expressReq: Request): Promise<Q> {
if(!this.argValidator){ if (!this.argValidator) {
return expressReq.query as Q; return expressReq.query as Q;
} }
+1 -1
View File
@@ -3,7 +3,7 @@ import type { APITail, RawArgType } from "../apiStructure/defs";
import { structure } from "../apiStructure/sammoRootAPI"; import { structure } from "../apiStructure/sammoRootAPI";
import { callClientAPI } from "./generator"; import { callClientAPI } from "./generator";
const apiRoot = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/api'; const apiRoot = process.env.API_ROOT_PATH ?? process.env.VITE_API_ROOT_PATH ?? '/rootAPI';
export const SammoRootAPI = APIPathGen(structure, (path: string[], tail: APITail, pathParam) => { export const SammoRootAPI = APIPathGen(structure, (path: string[], tail: APITail, pathParam) => {
const method = tail.reqType; const method = tail.reqType;
+6 -20
View File
@@ -8,29 +8,15 @@ import path from 'node:path';
import { LiteHashDRBG } from './util/LiteHashDRBG.js'; import { LiteHashDRBG } from './util/LiteHashDRBG.js';
import { simpleSerialize } from './util/simpleSerialize.js'; import { simpleSerialize } from './util/simpleSerialize.js';
import { SammoRootAPI } from './clientAPI/sammoRootAPI.js'; import { SammoRootAPI } from './clientAPI/sammoRootAPI.js';
import { buildAPISystem } from './api/generator.js';
export async function test() { import { sammoRootAPI } from './api/root.js';
console.log("Hello World!");
console.log(process.env.GAME_DB_HOST);
const abc= {
a: 1,
b: 2,
c: 3,
}
const t2 = await validate(abc);
console.log(t2);
}
await test();
const ownConfig = { const ownConfig = {
sessionSecret: process.env.SESSION_SECRET, sessionSecret: process.env.SESSION_SECRET,
port: Number(process.env.SERVER_PORT), port: Number(process.env.SERVER_PORT),
} }
/*
AppDataSource.initialize().then(async () => { AppDataSource.initialize().then(async () => {
// create express app // create express app
const app = express() const app = express()
@@ -40,9 +26,9 @@ AppDataSource.initialize().then(async () => {
resave: false, resave: false,
saveUninitialized: false, saveUninitialized: false,
})) }))
app.set('etag', false); //app.set('etag', false);
//app.use('/', rootRouter); app.use('/rootAPI', buildAPISystem(sammoRootAPI));
// start express server // start express server
app.listen(ownConfig.port) app.listen(ownConfig.port)
@@ -50,5 +36,5 @@ AppDataSource.initialize().then(async () => {
console.log(`Express server has started on port ${ownConfig.port}`) console.log(`Express server has started on port ${ownConfig.port}`)
}).catch(error => console.log(error)) }).catch(error => console.log(error))
*/
export default {}; export default {};