Skip to content

Commit

Permalink
Merge branch 'master' into make-v22
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Dec 3, 2024
2 parents 3dde002 + d574ba5 commit 599db21
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 55 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const tsFactoryConcerns = [
},
{
selector: "Identifier[name='createTypeAliasDeclaration']",
message: "use makePublicType() or makePublicLiteralType() helpers",
message: "use makeType() or makePublicLiteralType() helpers",
},
{
selector: "Identifier[name='createVariableStatement']",
Expand Down
21 changes: 17 additions & 4 deletions src/integration-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ts from "typescript";
import { Method } from "./method";
import { addJsDocComment } from "./zts-helpers";

export const f = ts.factory;

Expand Down Expand Up @@ -96,20 +97,32 @@ export const makeConst = (
);

export const makePublicLiteralType = (
name: ts.Identifier,
name: ts.Identifier | string,
literals: string[],
) =>
makePublicType(
makeType(
name,
f.createUnionTypeNode(
literals.map((option) =>
f.createLiteralTypeNode(f.createStringLiteral(option)),
),
),
{ isPublic: true },
);

export const makePublicType = (name: ts.Identifier, value: ts.TypeNode) =>
f.createTypeAliasDeclaration(exportModifier, name, undefined, value);
export const makeType = (
name: ts.Identifier | string,
value: ts.TypeNode,
{ isPublic, comment }: { isPublic?: boolean; comment?: string } = {},
) => {
const node = f.createTypeAliasDeclaration(
isPublic ? exportModifier : undefined,
name,
undefined,
value,
);
return comment ? addJsDocComment(node, comment) : node;
};

export const makePublicMethod = (
name: ts.Identifier,
Expand Down
34 changes: 17 additions & 17 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
makePublicInterface,
makePublicLiteralType,
makePublicMethod,
makePublicType,
makeType,
makeTernary,
makeTypeParams,
propOf,
Expand All @@ -37,7 +37,7 @@ import { Routing } from "./routing";
import { walkRouting } from "./routing-walker";
import { HandlingRules } from "./schema-walker";
import { zodToTs } from "./zts";
import { ZTSContext, createTypeAlias, printNode } from "./zts-helpers";
import { ZTSContext, printNode } from "./zts-helpers";
import type Prettier from "prettier";

type IOKind = "input" | "response" | ResponseVariant;
Expand Down Expand Up @@ -145,8 +145,8 @@ export class Integration {
if (!name) {
name = `Type${this.aliases.size + 1}`;
const temp = f.createLiteralTypeNode(f.createNull());
this.aliases.set(schema, createTypeAlias(temp, name));
this.aliases.set(schema, createTypeAlias(produce(), name));
this.aliases.set(schema, makeType(name, temp));
this.aliases.set(schema, makeType(name, produce()));
}
return f.createTypeReferenceNode(name);
}
Expand All @@ -165,32 +165,32 @@ export class Integration {
routing,
onEndpoint: (endpoint, path, method) => {
const entitle = makeCleanId.bind(null, method, path); // clean id with method+path prefix
const input = createTypeAlias(
zodToTs(endpoint.getSchema("input"), ctxIn),
const input = makeType(
entitle("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 = createTypeAlias(
zodToTs(positiveSchema, ctxOut),
const positiveResponse = makeType(
entitle("positive.response"),
zodToTs(positiveSchema, ctxOut),
);
const negativeSchema = endpoint
.getResponses("negative")
.map(({ schema, mimeTypes }) => (mimeTypes ? schema : noContent))
.reduce((agg, schema) => agg.or(schema));
const negativeResponse = createTypeAlias(
zodToTs(negativeSchema, ctxOut),
const negativeResponse = makeType(
entitle("negative.response"),
zodToTs(negativeSchema, ctxOut),
);
const genericResponse = createTypeAlias(
const genericResponse = makeType(
entitle("response"),
f.createUnionTypeNode([
f.createTypeReferenceNode(positiveResponse.name.text),
f.createTypeReferenceNode(negativeResponse.name.text),
]),
entitle("response"),
);
this.program.push(
input,
Expand Down Expand Up @@ -268,10 +268,9 @@ export class Integration {

// export type MethodPath = keyof Input;
this.program.push(
makePublicType(
this.ids.methodPathType,
makeKeyOf(this.ids.inputInterface),
),
makeType(this.ids.methodPathType, makeKeyOf(this.ids.inputInterface), {
isPublic: true,
}),
);

if (variant === "types") return;
Expand All @@ -291,7 +290,7 @@ export class Integration {
);

// export type Implementation = (method: Method, path: string, params: Record<string, any>) => Promise<any>;
const implementationType = makePublicType(
const implementationType = makeType(
this.ids.implementationType,
f.createFunctionTypeNode(
undefined,
Expand All @@ -306,6 +305,7 @@ export class Integration {
}),
makePromise("any"),
),
{ isPublic: true },
);

// `:${key}`
Expand Down
15 changes: 0 additions & 15 deletions src/zts-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ 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,
comment?: string,
) => {
const typeAlias = f.createTypeAliasDeclaration(
undefined,
f.createIdentifier(name),
undefined,
node,
);
return comment ? addJsDocComment(typeAlias, comment) : typeAlias;
};

export const printNode = (
node: ts.Node,
printerOptions?: ts.PrinterOptions,
Expand Down
19 changes: 1 addition & 18 deletions tests/unit/zts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import { ez } from "../../src";
import { f } from "../../src/integration-helpers";
import { zodToTs } from "../../src/zts";
import { ZTSContext, createTypeAlias, printNode } from "../../src/zts-helpers";
import { ZTSContext, printNode } from "../../src/zts-helpers";

describe("zod-to-ts", () => {
const printNodeTest = (node: ts.Node) =>
Expand Down Expand Up @@ -34,23 +34,6 @@ describe("zod-to-ts", () => {
},
);

describe("createTypeAlias()", () => {
const identifier = "User";
const node = zodToTs(z.object({ username: z.string(), age: z.number() }), {
ctx,
});

test("outputs correct typescript", () => {
const typeAlias = createTypeAlias(node, identifier);
expect(printNodeTest(typeAlias)).toMatchSnapshot();
});

test("optionally takes a comment", () => {
const typeAlias = createTypeAlias(node, identifier, "A basic user");
expect(printNodeTest(typeAlias)).toMatchSnapshot();
});
});

describe("enums", () => {
// noinspection JSUnusedGlobalSymbols
enum Color {
Expand Down

0 comments on commit 599db21

Please sign in to comment.