Skip to content

Commit

Permalink
Merge pull request #171 from hemedani/main
Browse files Browse the repository at this point in the history
upgrade superstruct and improve declaration creation
  • Loading branch information
hemedani authored Jul 16, 2024
2 parents 82f3902 + 902379e commit 29f4217
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lesan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export const lesan = () => {
odm: { ...odm(schemasObj) },
runServer: lesanServer(schemasObj, actsObj),
contextFns,
generateSchemTypes: () => generateSchemTypes(schemasObj),
generateSchemTypes: () => generateSchemTypes(schemasObj, actsObj),
};
};
26 changes: 13 additions & 13 deletions src/models/selectStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const selectStructFns = (schemasObj: TSchemas) => {
};
}

const mainRelations = (
const numDepthIterateRelations = (
schema: keyof TSchemas,
depth: number,
pureObj: Record<string, unknown>,
Expand All @@ -45,30 +45,30 @@ export const selectStructFns = (schemasObj: TSchemas) => {
for (const property in foundedSchema.mainRelations) {
returnObj = {
...returnObj,
[property]: selectStruct(
[property]: optional(selectStruct(
foundedSchema.mainRelations[property].schemaName,
depth,
),
)),
};
}
for (const property in foundedSchema.relatedRelations) {
returnObj = {
...returnObj,
[property]: selectStruct(
[property]: optional(selectStruct(
foundedSchema.relatedRelations[property].schemaName,
depth,
),
)),
};
}

return optional(object(returnObj as ObjectSchema));
return object(returnObj as ObjectSchema);
};

const numberDepth = (depth: number, pureObj: Record<string, any>) => {
depth--;
return depth > -1
? mainRelations(schema, depth, pureObj)
: optional(object(pureObj));
? numDepthIterateRelations(schema, depth, pureObj)
: object(pureObj);
};

const objectDepth = (depth: any, pureObj: Record<string, any>) => {
Expand All @@ -79,24 +79,24 @@ export const selectStructFns = (schemasObj: TSchemas) => {
checkRelation(depth, property) &&
(pureObj = {
...pureObj,
[property]: selectStruct(
[property]: optional(selectStruct(
foundedSchema.mainRelations[property].schemaName,
depth[property],
),
)),
});
}
for (const property in foundedSchema.relatedRelations) {
checkRelation(depth, property) &&
(pureObj = {
...pureObj,
[property]: selectStruct(
[property]: optional(selectStruct(
foundedSchema.relatedRelations[property].schemaName,
depth[property],
),
)),
});
}

return optional(object(pureObj));
return object(pureObj);
};

const completeObj = typeof depth === "number"
Expand Down
2 changes: 1 addition & 1 deletion src/npmDeps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "npm:superstruct@1.0.3";
export * from "npm:superstruct@2.0.2";
export * from "npm:[email protected]";
9 changes: 9 additions & 0 deletions src/types/generateTypesFromStruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export const generateTypesFromStruct = (
},
) => {
let returnStr = "";

const schemaStructValidate = schemaStruct.validate(undefined)[0];

if (schemaStructValidate === undefined || schemaStructValidate === null) {
if (keyname && !keyname.endsWith("?")) {
keyname = `${keyname}?`;
}
}

if (schemaStruct.type === "object") {
returnStr = returnStr + (keyname ? `${keyname}: {\n` : `{\n`);
for (const key in schemaStruct.schema as Object) {
Expand Down

0 comments on commit 29f4217

Please sign in to comment.