Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use entity references instead of id holding constants #2234

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,44 +182,49 @@ export class Integration {
walkRouting({
routing,
onEndpoint: (endpoint, path, method) => {
const [
inputId,
positiveResponseId,
negativeResponseId,
genericResponseId,
] = ["input", "positive.response", "negative.response", "response"].map(
(name) => makeCleanId(method, path, name),
const entitle = makeCleanId.bind(null, method, path); // clean id with method+path prefix
const input = createTypeAlias(
zodToTs(endpoint.getSchema("input"), ctxIn),
entitle("input"),
);
const input = zodToTs(endpoint.getSchema("input"), ctxIn);
const positiveSchema = endpoint
.getResponses("positive")
.map(({ schema, mimeTypes }) => (mimeTypes ? schema : noContent))
.reduce((agg, schema) => agg.or(schema));
const positiveResponse = zodToTs(positiveSchema, ctxOut);
const positiveResponse = createTypeAlias(
zodToTs(positiveSchema, ctxOut),
entitle("positive.response"),
);
const negativeSchema = endpoint
.getResponses("negative")
.map(({ schema, mimeTypes }) => (mimeTypes ? schema : noContent))
.reduce((agg, schema) => agg.or(schema));
const negativeResponse = zodToTs(negativeSchema, ctxOut);
const genericResponse = f.createUnionTypeNode([
f.createTypeReferenceNode(positiveResponseId),
f.createTypeReferenceNode(negativeResponseId),
]);
const negativeResponse = createTypeAlias(
zodToTs(negativeSchema, ctxOut),
entitle("negative.response"),
);
const genericResponse = createTypeAlias(
f.createUnionTypeNode([
f.createTypeReferenceNode(positiveResponse.name.text),
f.createTypeReferenceNode(negativeResponse.name.text),
]),
entitle("response"),
);
this.program.push(
createTypeAlias(input, inputId),
createTypeAlias(positiveResponse, positiveResponseId),
createTypeAlias(negativeResponse, negativeResponseId),
createTypeAlias(genericResponse, genericResponseId),
input,
positiveResponse,
negativeResponse,
genericResponse,
);
this.paths.push(path);
const isJson = endpoint
.getResponses("positive")
.some(({ mimeTypes }) => mimeTypes?.includes(contentTypes.json));
this.registry.set(quoteProp(method, path), {
input: inputId,
positive: positiveResponseId,
negative: negativeResponseId,
response: genericResponseId,
input: input.name.text,
positive: positiveResponse.name.text,
negative: negativeResponse.name.text,
response: genericResponse.name.text,
tags: endpoint.getTags(),
isJson,
});
Expand Down
1 change: 1 addition & 0 deletions src/zts-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const addJsDocComment = <T extends ts.Node>(node: T, text: string) =>
true,
);

// @todo deduplicate with makePublicType() integration helper
export const createTypeAlias = (
node: ts.TypeNode,
name: string,
Expand Down
Loading