Skip to content

Commit

Permalink
fix: unintentional shared context -> make a new object
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed Aug 11, 2022
1 parent 704d28f commit 5e80cee
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/generateZodClientFromOpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { topologicalSort } from "./topologicalSort";

export const getZodClientTemplateContext = (openApiDoc: GenerateZodClientFromOpenApiArgs["openApiDoc"]) => {
const result = getZodiosEndpointDescriptionFromOpenApiDoc(openApiDoc);
const data = { ...initialContext };
const data = makeInitialContext();

const replaceRefTokenWithVariableRef = (code: string) =>
code.replaceAll(tokens.refTokenHashRegex, (match) => tokens.rmToken(match, tokens.refToken));
Expand Down Expand Up @@ -105,22 +105,23 @@ export const generateZodClientFromOpenAPI = async ({
};

/** @see https://github.dev/stephenh/ts-poet/blob/5ea0dbb3c9f1f4b0ee51a54abb2d758102eda4a2/src/Code.ts#L231 */
function maybePretty(input: string, options?: Options | null): string {
export function maybePretty(input: string, options?: Options | null): string {
try {
return prettier.format(input.trim(), { parser: "typescript", plugins: [parserTypescript], ...options });
} catch (e) {
return input; // assume it's invalid syntax and ignore
}
}

const initialContext: TemplateContext = {
variables: {},
schemas: {},
endpoints: [],
options: {
withAlias: false,
},
};
const makeInitialContext = () =>
({
variables: {},
schemas: {},
endpoints: [],
options: {
withAlias: false,
},
} as TemplateContext);

interface TemplateContext {
variables: Record<string, string>;
Expand Down

0 comments on commit 5e80cee

Please sign in to comment.