Skip to content

Commit

Permalink
⭐ add two schema of response to declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
etefaghian committed Jul 18, 2021
1 parent bcf9a5c commit 0129153
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
16 changes: 13 additions & 3 deletions declarations/functions/response/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ export const getResponseDeclarations = async (dirPath?: string) => {
{ overwrite: true }
);

const createdInterface = createdSourceFile.addInterface({
name: "FunQLResponse",
const createdInterfaceWithDetails = createdSourceFile.addInterface({
name: "FunQLResponseWithDetails",
isExported: true,
});

constructResponseSchema(sourceFile, createdInterface, createdSourceFile);
const createdInterfaceWithoutDetails = createdSourceFile.addInterface({
name: "FunQLResponseWithoutDetails",
isExported: true,
});

constructResponseSchema(
sourceFile,
createdInterfaceWithDetails,
createdInterfaceWithoutDetails,
createdSourceFile
);

createdSourceFile.formatText({ indentSize: 1 });
await createdSourceFile.save();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { SourceFile, SyntaxKind, log } from "./../../../../deps.ts";
import { SourceFile, SyntaxKind } from "./../../../../deps.ts";
import { constructResponseModel } from "./constructResponseModel.ts";
import { getImpSourceFile, exObIterator } from "../../request/utils/mod.ts";

export function constructResponseContent(
sourceFile: SourceFile,
createdSourceFile: SourceFile
createdSourceFile: SourceFile,
withDetails: boolean = true
) {
//get object iterator in mod.ts
const objectIterator = sourceFile.getFirstDescendantByKindOrThrow(
Expand All @@ -25,7 +26,8 @@ export function constructResponseContent(
res.models = constructResponseModel(
getImpSourceFile(sourceFile, listOfFn.functionName!),
res.name,
createdSourceFile
createdSourceFile,
withDetails
);

results.push(res);
Expand Down
20 changes: 12 additions & 8 deletions declarations/functions/response/utils/constructResponseDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { throwError } from "../../../utils/throwError.ts";
export const constructResponseDetails = (
sourceFile: SourceFile,
functionName: string,
createdSourceFile: SourceFile
createdSourceFile: SourceFile,
withDetails: boolean = true
): string => {
try {
//finds funql function that by convention ends with Fn and declare with variable declaration
Expand Down Expand Up @@ -48,7 +49,6 @@ export const constructResponseDetails = (
fnTypeDeclaration!.getLastChildByKind(SyntaxKind.TypeReference) ||
fnTypeDeclaration!.getLastChildIfKind(SyntaxKind.AnyKeyword);

// || fnTypeDeclaration.getLastChildByKind(SyntaxKind.CallExpression);
//TODO add more conditions
(returnTypeDeclaration!.getText() === "any" ||
returnTypeDeclaration!.getText() === "Promise<any>" ||
Expand Down Expand Up @@ -76,18 +76,22 @@ export const constructResponseDetails = (
);

//returns all of things that we need to create details in string format
return JSON.stringify({
response: returnTypeDeclarationWithoutPromise?.getText() ?? "any",
}).replaceAll('"', "");
return withDetails
? JSON.stringify({
response: returnTypeDeclarationWithoutPromise?.getText() ?? "any",
}).replaceAll('"', "")
: returnTypeDeclarationWithoutPromise?.getText() ?? "any";
} catch (error) {
log.warning(
`we have some problem in finding return type in file: ${sourceFile.getFilePath()} we assume it any
Error: ${error}
`
);
//returns all of things that we need to create details in string format
return JSON.stringify({
response: "any",
}).replaceAll('"', "");
return withDetails
? JSON.stringify({
response: "any",
}).replaceAll('"', "")
: "any";
}
};
10 changes: 7 additions & 3 deletions declarations/functions/response/utils/constructResponseDoit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { constructResponseDetails } from "./constructResponseDetails.ts";
export function constructResponseDoit(
sourceFile: SourceFile,
modelName: string,
createdSourceFile: SourceFile
createdSourceFile: SourceFile,
withDetails: boolean = true
) {
log.info(
bgRgb24(`in construction of doits for model: ${modelName}`, 0x010217)
Expand All @@ -40,15 +41,18 @@ export function constructResponseDoit(
details: constructResponseDetails(
getImpSourceFile(sourceFile, fn.functionName!),
fn.functionName!,
createdSourceFile
createdSourceFile,
withDetails
),
});
}

//convert array to object
return JSON.stringify(
results.reduce((pre: any, curr) => {
pre[curr.name!] = { details: curr.details };
withDetails
? (pre[curr.name!] = { details: curr.details })
: (pre[curr.name!] = curr.details);
return pre;
}, {})
).replaceAll('"', "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { constructResponseDoit } from "./constructResponseDoit.ts";
export function constructResponseModel(
sourceFile: SourceFile,
contentName: string,
createdSourceFile: SourceFile
createdSourceFile: SourceFile,
withDetails: boolean = true
) {
log.info(
bgRgb24(`in construction of models for content: ${contentName}`, 0x380406)
Expand All @@ -29,7 +30,8 @@ export function constructResponseModel(
(res.doits = constructResponseDoit(
getImpSourceFile(sourceFile, listOfFn.functionName!),
listOfFn.name!,
createdSourceFile
createdSourceFile,
withDetails
)),
results.push(res);
}
Expand Down
26 changes: 21 additions & 5 deletions declarations/functions/response/utils/constructResponseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ import { constructResponseContent } from "./constructResponseContent.ts";

export function constructResponseSchema(
sourceFile: SourceFile,
createdInterface: InterfaceDeclaration,
createdInterfaceWihDetails: InterfaceDeclaration,
createdInterfaceWithoutDetails: InterfaceDeclaration,
createdSourceFile: SourceFile
) {
const property = createdInterface.addProperty({ name: "schema" });
const responseContent = constructResponseContent(
const propertyWithDetails = createdInterfaceWihDetails.addProperty({
name: "schema",
});
const propertyWithoutDetails = createdInterfaceWithoutDetails.addProperty({
name: "schema",
});

const responseContentWithDetails = constructResponseContent(
sourceFile,
createdSourceFile,
true
);
const responseContentWithoutDetails = constructResponseContent(
sourceFile,
createdSourceFile
createdSourceFile,
false
);
propertyWithDetails.setType(`{contents: ${responseContentWithDetails}}`);
propertyWithoutDetails.setType(
`{contents: ${responseContentWithoutDetails}}`
);
property.setType(`{contents: ${responseContent}}`);
}

0 comments on commit 0129153

Please sign in to comment.