Skip to content

Commit

Permalink
πŸ”€ Merge pull request #162 from hemedani/main
Browse files Browse the repository at this point in the history
🍱 add request type safty to declarations
  • Loading branch information
hemedani committed May 25, 2024
2 parents 1ee489a + 5a71332 commit db10439
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/server/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
try {
if (request.method === "OPTIONS") {
Expand Down
41 changes: 39 additions & 2 deletions src/types/mod.ts
Original file line number Diff line number Diff line change
@@ -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 = "";
Expand Down Expand Up @@ -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);
};

0 comments on commit db10439

Please sign in to comment.