Skip to content

Commit b737c9f

Browse files
committed
Using R.once().
1 parent fd2ff06 commit b737c9f

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

express-zod-api/src/endpoint.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,21 @@ export class Endpoint<
8383
OPT extends FlatObject,
8484
> extends AbstractEndpoint {
8585
readonly #def: ConstructorParameters<typeof Endpoint<IN, OUT, OPT>>[0];
86-
#didPull = false;
86+
87+
/** considered expensive operation, only required for generators */
88+
#ensureOutputExamples = R.once(() => {
89+
const meta = this.#def.outputSchema.meta();
90+
if (meta?.examples?.length) return; // has examples on the output schema, or pull up:
91+
if (!isSchema<$ZodObject>(this.#def.outputSchema, "object")) return;
92+
const examples = pullResponseExamples(this.#def.outputSchema as $ZodObject);
93+
if (!examples.length) return;
94+
globalRegistry
95+
.remove(this.#def.outputSchema) // reassign to avoid cloning
96+
.add(this.#def.outputSchema as $ZodObject, {
97+
...meta,
98+
examples,
99+
});
100+
});
87101

88102
constructor(def: {
89103
deprecated?: boolean;
@@ -187,23 +201,6 @@ export class Endpoint<
187201
return this.#def.getOperationId?.(method);
188202
}
189203

190-
/** considered expensive operation, only required for generators */
191-
#ensureOutputExamples() {
192-
if (this.#didPull) return;
193-
this.#didPull = true;
194-
const meta = this.#def.outputSchema.meta();
195-
if (meta?.examples?.length) return; // has examples on the output schema, or pull up:
196-
if (!isSchema<$ZodObject>(this.#def.outputSchema, "object")) return;
197-
const examples = pullResponseExamples(this.#def.outputSchema as $ZodObject);
198-
if (!examples.length) return;
199-
globalRegistry
200-
.remove(this.#def.outputSchema) // reassign to avoid cloning
201-
.add(this.#def.outputSchema as $ZodObject, {
202-
...meta,
203-
examples,
204-
});
205-
}
206-
207204
async #parseOutput(output: z.input<OUT>) {
208205
try {
209206
return (await this.#def.outputSchema.parseAsync(output)) as FlatObject;

0 commit comments

Comments
 (0)