api 구조 변경

This commit is contained in:
2023-08-18 13:11:59 +00:00
parent bfe0b99d44
commit de0b851217
8 changed files with 70 additions and 53 deletions
+7 -8
View File
@@ -11,12 +11,11 @@ type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
const argValidator = undefined; const argValidator = undefined;
export const ReqNonce = GET<RType, EType, QType> export const ReqNonce = GET<RType, EType, QType>(argValidator)(declProcDecorators(
(argValidator) StartSession,
(declProcDecorators([ ReqLogin,
StartSession, ))(
ReqLogin, (query, ctx) => {
] as const))
((query, ctx) => {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
}); }
);
+23 -14
View File
@@ -5,25 +5,23 @@ import { StartSession } from "../ProcDecorator/StartSession.js";
import { IsString } from "class-validator"; import { IsString } from "class-validator";
import { delay } from "../../util/delay.js"; import { delay } from "../../util/delay.js";
import { declProcDecorators } from "../ProcDecorator/base.js"; import { declProcDecorators } from "../ProcDecorator/base.js";
import { z } from "zod";
import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
type BaseAPI = typeof structure.Login.LoginByID; type BaseAPI = typeof structure.Login.LoginByID;
type RType = ExtractResponse<BaseAPI>; type RType = ExtractResponse<BaseAPI>;
type EType = ExtractError<BaseAPI>; type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
class ArgValidator implements QType { const LoginByIDReq = z.object({
@IsString() id: z.string(),
username!: string; password: z.string(),
@IsString() }) satisfies z.ZodType<QType>
password!: string;
}
export const LoginByID = POST<RType, EType, QType> export const LoginByID = POST<RType, EType, QType>(LoginByIDReq)(declProcDecorators(
(ArgValidator) StartSession,
(declProcDecorators([ ))(
StartSession, async (query, ctx, req, res) => {
] as const)) const id = query.id;
(async (query, ctx, req, res) => {
const username = query.username;
const password = query.password; const password = query.password;
//TODO: DB에서 뭔가 가져와야 함 //TODO: DB에서 뭔가 가져와야 함
@@ -46,9 +44,20 @@ export const LoginByID = POST<RType, EType, QType>
} }
const userID = 1; const userID = 1;
const userName = "test";
const userLevel = 1;
const nextToken: [number, string] = [1, "1234567890"]; const nextToken: [number, string] = [1, "1234567890"];
const loginCtx: LoginCtx = {
userID,
userName,
userLevel,
allowServerAction: {},
loginDate: new Date(),
}
ctx.session.setItem(loginCtxSessionKey, loginCtx);
await ctx.setValue("userID", userID);
return { return {
result: true, result: true,
nextToken, nextToken,
+21 -13
View File
@@ -5,34 +5,42 @@ import { StartSession } from "../ProcDecorator/StartSession.js";
import { IsNumber, IsString } from "class-validator"; import { IsNumber, IsString } from "class-validator";
import { delay } from "../../util/delay.js"; import { delay } from "../../util/delay.js";
import { declProcDecorators } from "../ProcDecorator/base.js"; import { declProcDecorators } from "../ProcDecorator/base.js";
import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js";
import { z } from "zod";
type BaseAPI = typeof structure.Login.LoginByToken; type BaseAPI = typeof structure.Login.LoginByToken;
type RType = ExtractResponse<BaseAPI>; type RType = ExtractResponse<BaseAPI>;
type EType = ExtractError<BaseAPI>; type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
class ArgValidator implements QType { const LoginByTokenReq = z.object({
@IsNumber() token_id: z.number(),
token_id!: number; hashedToken: z.string(),
@IsString() }) satisfies z.ZodType<QType>
hashedToken!: string;
}
export const LoginByToken = POST<RType, EType, QType> export const LoginByToken = POST<RType, EType, QType>(LoginByTokenReq)(declProcDecorators(
(ArgValidator) StartSession,
(declProcDecorators([ ))
StartSession,
] as const))
(async (query, ctx) => { (async (query, ctx) => {
query.hashedToken; query.hashedToken;
ctx.clearSession(); ctx.session.clear();
await delay(1); await delay(1);
//무언가 로그인 //무언가 로그인
//TODO: DB는 어디서 들고옴? //TODO: DB는 어디서 들고옴?
const userID = 1; const userID = 1;
const userName = "test";
const userLevel = 1;
const nextToken: [number, string] = [1, "1234567890"]; const nextToken: [number, string] = [1, "1234567890"];
await ctx.setValue("userID", userID); const loginCtx: LoginCtx = {
userID,
userName,
userLevel,
allowServerAction: {},
loginDate: new Date(),
}
ctx.session.setItem(loginCtxSessionKey, loginCtx);
//throw new Error("Method not implemented."); //throw new Error("Method not implemented.");
+10 -9
View File
@@ -9,13 +9,13 @@ type RType = ExtractResponse<BaseAPI>;
type EType = ExtractError<BaseAPI>; type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
export const ReqNonce = GET<RType, EType, QType> export const ReqNonceSessionKey = 'loginNonce';
(undefined)
(declProcDecorators([ export const ReqNonce = GET<RType, EType, QType>(undefined)(declProcDecorators(
StartSession, StartSession,
] as const)) ))(
(async (query, ctx) => { async (query, ctx) => {
const nonce = await ctx.getValue<string>("nonce"); const nonce = ctx.session.getItem<string>(ReqNonceSessionKey);
if (nonce !== undefined) { if (nonce !== undefined) {
return { return {
loginNonce: nonce, loginNonce: nonce,
@@ -24,9 +24,10 @@ export const ReqNonce = GET<RType, EType, QType>
} }
const newNonce = "1234567890"; const newNonce = "1234567890";
await ctx.setValue("nonce", newNonce); ctx.session.setItem(ReqNonceSessionKey, newNonce);
return { return {
loginNonce: newNonce, loginNonce: newNonce,
result: true, result: true,
} }
}); }
)
+5 -6
View File
@@ -1,16 +1,15 @@
import { GET } from "../defs.js"; import { GET } from "../defs.js";
import type { structure } from "../../apiStructure/sammoRootAPI.js"; import type { structure } from "../../apiStructure/sammoRootAPI.js";
import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs.js"; import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs.js";
import { declProcDecorators } from "../ProcDecorator/base.js"; import { EmptyProcDecorator } from "../ProcDecorator/base.js";
type BaseAPI = typeof structure.Login.test; type BaseAPI = typeof structure.Login.test;
type RType = ExtractResponse<BaseAPI>; type RType = ExtractResponse<BaseAPI>;
type EType = ExtractError<BaseAPI>; type EType = ExtractError<BaseAPI>;
type QType = ExtractQuery<BaseAPI>; type QType = ExtractQuery<BaseAPI>;
export const test = GET<RType, EType, QType> export const test = GET<RType, EType, QType>(undefined)(EmptyProcDecorator)(
(undefined) () => {
(declProcDecorators([] as const))
(() => {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
}); }
);
+1 -1
View File
@@ -1,6 +1,6 @@
import type { LoginCtx } from "./ReqLogin.js"; import type { LoginCtx } from "./ReqLogin.js";
import type { SessionCtx } from "./StartSession.js"; import type { SessionCtx } from "./StartSession.js";
import type { DecoratorResult, ProcDecoratorGenerator } from "./base.js"; import type { ProcDecoratorGenerator } from "./base.js";
export type GameLoginCtx = { export type GameLoginCtx = {
generalID: number; generalID: number;
+2 -1
View File
@@ -8,10 +8,11 @@ export type LoginCtx = {
allowServerAction: Record<string, number>; allowServerAction: Record<string, number>;
loginDate: Date; loginDate: Date;
} }
export const loginCtxSessionKey = 'loginCtx';
export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q> { export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q> {
return (ctx) => { return (ctx) => {
const loginCtx = ctx.session.getItem<LoginCtx>('loginCtx'); const loginCtx = ctx.session.getItem<LoginCtx>(loginCtxSessionKey);
if(!loginCtx){ if(!loginCtx){
return [{ return [{
result: false, result: false,
+1 -1
View File
@@ -33,7 +33,7 @@ export type AutoLoginFailed = {
export const structure = { export const structure = {
Login: { Login: {
LoginByID: POST<{ LoginByID: POST<{
username: string, id: string,
password: string, password: string,
}, LoginResponse, LoginFailed>(), }, LoginResponse, LoginFailed>(),
LoginByToken: POST<{ LoginByToken: POST<{