Skip to content

Commit

Permalink
Reduce dts size (#15)
Browse files Browse the repository at this point in the history
* refactor: access type members with of instead of t

* chore: update zf.shared usage to zf.of

* refactor: ZodToken type alias for cleaner d.ts

* chore: update package-lock

* chore: bump minor version

* docs: update ref discussion for open-api
  • Loading branch information
rexfordessilfie authored Apr 15, 2023
1 parent 0fd6a30 commit 5d6f04e
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ $ cd playground
$ ts-node "./codegen/codegen-openapi.ts" -d "sources" "openapi-schema:.*"
```

For OpenApi schemas, references are resolved by replacing them with the name of the referenced schema. For example:
For OpenApi schemas, references are resolved by replacing them with the name of the referenced schema (notice the `"favoriteEmoji"` property below).

**Example**
```json
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-factory",
"version": "0.0.9",
"version": "0.0.10",
"description": "Typescript compiler API factory abstractions for creating zod validation schemas.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 8 additions & 11 deletions playground/codegen/codegen-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
zodTokens
} from "../../dist";
import { parseArguments } from "./helpers";
import { ZodToken } from "../../dist/utils";

class MissingRefError extends Error {
isMissingRefError = true;
Expand All @@ -19,10 +20,8 @@ class MissingRefError extends Error {

const sharedTokens = {
default: "catch",
description: "describe",
} satisfies Partial<
Record<keyof OpenAPIV3.SchemaObject, keyof typeof zodTokens>
>;
description: "describe"
} satisfies Partial<Record<keyof OpenAPIV3.SchemaObject, ZodToken>>;

const stringFormats = {
date: "date",
Expand All @@ -34,8 +33,8 @@ const stringFormats = {
uuid: "uuid",
cuid: "cuid",
cuid2: "cuid2",
ulid: "ulid",
} satisfies Partial<Record<string, keyof typeof zodTokens>>;
ulid: "ulid"
} satisfies Partial<Record<string, ZodToken>>;

const visitRef = (ref: string) => {
const name = ref.replace("#/components/schemas/", "");
Expand Down Expand Up @@ -211,7 +210,7 @@ const visitOneOf = (schema: OpenAPIV3.SchemaObject) => {
if (schema.oneOf)
params.push([
zodTokens.union,
schema.oneOf.map((item) => convertOpenApiSchemaObjectToZod(item)),
schema.oneOf.map((item) => convertOpenApiSchemaObjectToZod(item))
]);

extendSharedParams(schema, params);
Expand Down Expand Up @@ -336,9 +335,7 @@ for (const schemaDestination of schemaDestinations) {
}

if (schemaExpression) {
statements.push(
schemaExport(schemaExpressionName, schemaExpression)
);
statements.push(schemaExport(schemaExpressionName, schemaExpression));

schemaExpressionsRegistry.set(schemaExpressionName, schemaExpression);
}
Expand All @@ -358,6 +355,6 @@ Object.entries(statementsPerFile).forEach(([fileName, statements]) => {
printStatementsToFile(statements, {
header: "// generated file. do not edit.",
filename: `${fileName}.generated.ts`,
directory: "./generated",
directory: "./generated"
});
});
8 changes: 3 additions & 5 deletions src/core/shared.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
zodTokens,
zodSharedMembers,
ValueOf,
callExpressionCreator,
callExpressionCreatorWithFactoryType
callExpressionCreatorWithFactoryType,
ZodToken
} from "../utils";

export function buildSharedZodMemberCreators<
T extends ValueOf<typeof zodTokens>
>(type?: T) {
export function buildSharedZodMemberCreators<T extends ZodToken>(type?: T) {
return {
// Type changing methods
promise: callExpressionCreator(zodTokens.promise),
Expand Down
26 changes: 8 additions & 18 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { zodTokens, zodZ, zodZod } from ".";
import { zodZ, zodZod } from ".";

import ts, { factory } from "typescript";
import { ZodToken } from "./types";

export const zodImport = () => {
return factory.createImportDeclaration(
Expand Down Expand Up @@ -132,39 +133,31 @@ export const createPropertyAccessCall = (
);
};

export type ValueOf<T extends Record<string, any>> = T[keyof T];

export const callExpressionCreatorWithTarget =
<T1 extends ValueOf<typeof zodTokens>, E extends ts.Expression>(
target: E,
name: T1
) =>
<T1 extends ZodToken, E extends ts.Expression>(target: E, name: T1) =>
<Args extends any[] = any[]>(...args: Args) => {
const expression = createPropertyAccessCall(target, name, args);
return extendExpressionWithFactoryType(expression, name);
};

export const propertyAccessExpressionCreatorWithTarget =
<T1 extends ValueOf<typeof zodTokens>, E extends ts.Expression>(
target: E,
name: T1
) =>
<T1 extends ZodToken, E extends ts.Expression>(target: E, name: T1) =>
<Args extends any[] = any[]>(...args: Args) => {
const expression = createPropertyAccess(target, name);
return extendExpressionWithFactoryType(expression, name);
};

export const callExpressionCreator =
<T1 extends ValueOf<typeof zodTokens>>(name: T1) =>
<T1 extends ZodToken>(name: T1) =>
<Args extends any[] = any[]>(target: ts.Expression, ...args: Args) => {
const expression = createPropertyAccessCall(target, name, args);
return extendExpressionWithFactoryType(expression, name);
};

export const callExpressionCreatorWithFactoryType =
<
T1 extends ValueOf<typeof zodTokens>,
T2 extends ValueOf<typeof zodTokens>,
T1 extends ZodToken,
T2 extends ZodToken,
E extends ts.Expression & { _zfType: T2 }
>(
name: T1,
Expand All @@ -177,10 +170,7 @@ export const callExpressionCreatorWithFactoryType =
};

export const callExpressionCreatorWithPreviousType =
<
T1 extends ValueOf<typeof zodTokens>,
E extends ts.Expression & { _zfType: ValueOf<typeof zodTokens> }
>(
<T1 extends ZodToken, E extends ts.Expression & { _zfType: ZodToken }>(
name: T1
) =>
<Args extends any[] = any[]>(target: E, ...args: Args) => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./constants";
export * from "./printer";
export * from "./ast";
export * from "./types";
3 changes: 3 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { zodTokens } from "./constants";
export type ValueOf<T extends Record<string, any>> = T[keyof T];
export type ZodToken = ValueOf<typeof zodTokens>;

0 comments on commit 5d6f04e

Please sign in to comment.