Skip to content

Commit

Permalink
feat(code-gen): include validators when targetting Node.js with ts-axios
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed Nov 21, 2024
1 parent 8ed5778 commit 5c9f891
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions packages/code-gen/src/api-client/ts-axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ export function tsAxiosGetApiClientFile(generateContext, route) {
typeImportCollector.destructure("axios", "AxiosInstance");
}

if (
generateContext.options.generators.apiClient?.target.targetRuntime ===
"node.js"
) {
importCollector.destructure("@compas/stdlib", "AppError");
importCollector.raw(`import FormData from "form-data";`);
}

return file;
}

Expand Down Expand Up @@ -220,7 +228,17 @@ export function tsAxiosGenerateFunction(
}

// Allow overwriting any request config
args.push(`requestConfig?: AxiosRequestConfig`);
if (
generateContext.options.generators.apiClient?.target.targetRuntime ===
"node.js" &&
route.response
) {
args.push(
`requestConfig?: AxiosRequestConfig & { skipResponseValidation?: boolean }`,
);
} else {
args.push(`requestConfig?: AxiosRequestConfig`);
}

fileContextRemoveLinePrefix(file, 3);
fileWrite(file, ` */`);
Expand Down Expand Up @@ -336,7 +354,37 @@ export function tsAxiosGenerateFunction(
fileContextSetIndent(file, -1);
fileWrite(file, `});`);

fileWrite(file, `return response.data;`);
if (
route.response &&
generateContext.options.generators.apiClient?.target.targetRuntime ===
"node.js"
) {
fileBlockStart(file, `if (requestConfig?.skipResponseValidation)`);
fileWrite(file, `return response.data;`);
fileBlockEnd(file);

fileWrite(
file,
`const { value, error } = ${contextNames.responseValidator}(response.data);`,
);
fileBlockStart(file, `if (error)`);

fileWrite(
file,
`throw AppError.validationError("validator.error", {
route: { group: "${route.group}", name: "${route.name}", },
error,
});`,
);

fileBlockEnd(file);

fileBlockStart(file, `else`);
fileWrite(file, `return value;`);
fileBlockEnd(file);
} else {
fileWrite(file, `return response.data;`);
}

fileBlockEnd(file);

Expand Down

0 comments on commit 5c9f891

Please sign in to comment.