Skip to content

Commit

Permalink
Merge pull request #187 from beckn/feat/cache-spec-on-boot
Browse files Browse the repository at this point in the history
Added: Feature For load OpenAPIValidator Cache on app boot
  • Loading branch information
shreyvishal authored Jun 18, 2024
2 parents 5947561 + 7ea9829 commit 8ac868a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/middlewares/schemaValidator.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class OpenApiValidatorMiddleware {
count: 0,
requestHandler: requestHandler
};
await initializeOpenApiValidatorCache(requestHandler);
await initializeOpenApiValidatorCache(requestHandler, file);
}
}
} catch (err) {
Expand Down Expand Up @@ -156,7 +156,10 @@ export class OpenApiValidatorMiddleware {
}
}

const initializeOpenApiValidatorCache = async (stack: any) => {
const initializeOpenApiValidatorCache = async (
stack: any,
specFile: string
) => {
try {
let actions: string[] = [];
if (
Expand All @@ -169,7 +172,8 @@ const initializeOpenApiValidatorCache = async (stack: any) => {
} else {
actions = Object.keys(ResponseActions);
}
actions.forEach((action) => {

actions.slice(0, 10).forEach((action) => {
const mockRequest = (body: any) => {
const req = httpMocks.createRequest({
method: "POST",
Expand Down Expand Up @@ -198,27 +202,35 @@ const initializeOpenApiValidatorCache = async (stack: any) => {
context: { action: `${action}` },
message: {}
});
walkSubstack(stack, reqObj, {}, () => {
return;
});
console.log("This is for specfile: ", specFile);

walkSubstack(
stack,
reqObj,
{},
() => {
return;
},
false
);
});
} catch (error) {
console.log(error);
} catch (error: any) {
console.log(error.message);
}
};

const walkSubstack = function (
stack: any,
req: any,
res: any,
next: NextFunction
next: NextFunction,
showError = true
) {
if (typeof stack === "function") {
stack = [stack];
}
const walkStack = function (i: any, err?: any) {
if (err) {
console.log(err);
if (err && showError) {
return schemaErrorHandler(err, req, res, next);
}
if (i >= stack.length) {
Expand Down

0 comments on commit 8ac868a

Please sign in to comment.