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

feat: add typescriptDefinitionRefiner to TemplateContext options #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions lib/src/openApiToTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ TsConversionArgs): ts.Node | TypeDefinitionObject | string => {
throw new Error("Name is required to convert an object schema to a type reference");
}

const base = t.type(inheritedMeta.name, doWrapReadOnly(objectType));
if (!isPartial) return base;
if (!isPartial) return t.type(inheritedMeta.name, doWrapReadOnly(objectType));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adjustment was necessary due to an unforeseen issue where t.type caused modifications to the generated types (even when isPartial is true), leading to bugs when attempting to modify the Tanu object after that.


return t.type(inheritedMeta.name, t.reference("Partial", [doWrapReadOnly(objectType)]));
}
Expand All @@ -305,7 +304,13 @@ TsConversionArgs): ts.Node | TypeDefinitionObject | string => {
throw new Error(`Unsupported schema type: ${schemaType}`);
};

const tsResult = getTs();
let tsResult = getTs();

// If the schema is not a reference object, we can refine the type definition
if (options?.typescriptDefinitionRefiner && !isReferenceObject(schema)) {
tsResult = options.typescriptDefinitionRefiner(getTs(), schema);
}

return canBeWrapped
? wrapTypeIfInline({ isInline, name: inheritedMeta?.name, typeDef: tsResult as TypeDefinition })
: tsResult;
Expand Down
10 changes: 10 additions & 0 deletions lib/src/template-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OpenAPIObject, OperationObject, PathItemObject, SchemaObject } fro
import { sortBy, sortListFromRefArray, sortObjKeysFromArray } from "pastable/server";
import { ts } from "tanu";
import { match } from "ts-pattern";
import type { TypeDefinition, TypeDefinitionObject } from "tanu/dist/type";

import { getOpenApiDependencyGraph } from "./getOpenApiDependencyGraph";
import type { EndpointDefinitionWithRefs } from "./getZodiosEndpointDefinitionList";
Expand Down Expand Up @@ -377,6 +378,15 @@ export type TemplateContextOptions = {
operation: OperationObject
) => EndpointDefinitionWithRefs;

/**
* A function to refine each tanu type definition. Mostly useful for adding fields from SchemaObject
* that aren't defined yet in the default type definition.
*/
typescriptDefinitionRefiner?: (
defaultTs: ts.Node | TypeDefinitionObject | string,
schema: SchemaObject
) => ts.Node | TypeDefinitionObject | string;

/**
* When true, all generated objects and arrays will be readonly.
*/
Expand Down
Loading