From 619442e272f38a690bf007f0f64d9f470cfd8443 Mon Sep 17 00:00:00 2001 From: Syd Amir Date: Sat, 25 May 2024 16:20:25 +0330 Subject: [PATCH] :bento: add request type safty to declarations --- src/server/mod.ts | 2 +- src/types/mod.ts | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/server/mod.ts b/src/server/mod.ts index 80bf5f4f..fb3e15ad 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -40,7 +40,7 @@ export const lesanServer = (schemasObj: TSchemas, actsObj: Services) => { typeGeneration: false, staticPath: [], }) => { - typeGeneration && (await generateSchemTypes(schemasObj)); + typeGeneration && (await generateSchemTypes(schemasObj, actsObj)); const handler = async (request: Request): Promise => { try { if (request.method === "OPTIONS") { diff --git a/src/types/mod.ts b/src/types/mod.ts index d9e58331..267453d4 100644 --- a/src/types/mod.ts +++ b/src/types/mod.ts @@ -1,9 +1,12 @@ -import { ensureDir } from "../mod.ts"; +import { Acts, ensureDir, Services } from "../mod.ts"; import { createStruct } from "../models/mod.ts"; import { schemas as schemaFns, TSchemas } from "../models/mod.ts"; import { generateTypesFromStruct } from "./generateTypesFromStruct.ts"; -export const generateSchemTypes = async (schemasObj: TSchemas) => { +export const generateSchemTypes = async ( + schemasObj: TSchemas, + actsObj: Services, +) => { const schemas = schemaFns(schemasObj).getSchemas(); let str = ""; @@ -37,6 +40,40 @@ export const generateSchemTypes = async (schemasObj: TSchemas) => { `; } + str = str + ` + export type ReqType = {\n + `; + for (const service in actsObj) { + if (typeof actsObj[service] === "object") { + str = str + ` + ${service}: {\n + `; + for (const model in actsObj[service] as Acts) { + str = str + ` + ${model}: {\n + `; + for (const fn in (actsObj[service] as Acts)[model]) { + str = str + ` + ${fn}: ${ + generateTypesFromStruct({ + schemaStruct: (actsObj[service] as Acts)[model][fn].validator, + }) + } + `; + } + str = str + ` + }\n + `; + } + str = str + ` + }\n + `; + } + } + str = str + ` + }\n + `; + await ensureDir("./declarations"); await Deno.writeTextFile("./declarations/selectInp.ts", str); };