Skip to content

Commit ad7bcf5

Browse files
authored
Add Endpoint::getProbableRequestType() (#3507)
Complements #3504
1 parent 991b629 commit ad7bcf5

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

express-zod-api/src/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Diagnostics {
3636
if (entry[prop]) stack.push(...entry[prop]);
3737
}
3838
}
39-
if (endpoint.requestType === "json") {
39+
if (endpoint.getProbableRequestType() === "json") {
4040
const reason = findJsonIncompatible(endpoint.inputSchema, "input");
4141
if (reason) {
4242
this.logger.warn(

express-zod-api/src/documentation.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ export class Documentation extends OpenApiBuilder {
250250
request,
251251
paramNames: R.pluck("name", depictedParams),
252252
schema: inputSchema,
253-
mimeType:
254-
method === "query" // @todo consider moving into the inheritor of Endpoint::requestType() when made
255-
? contentTypes.form // conventionally recommended for QUERY
256-
: contentTypes[endpoint.requestType],
253+
mimeType: contentTypes[endpoint.getProbableRequestType(method)],
257254
description: descriptions?.requestBody?.({
258255
method,
259256
path,

express-zod-api/src/endpoint.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ export abstract class AbstractEndpoint {
7474
public abstract get scopes(): ReadonlyArray<string>;
7575
/** @internal */
7676
public abstract get tags(): ReadonlyArray<string>;
77-
/**
78-
* @internal
79-
* @todo reconsider the purpose of this method, since parsers are global now
80-
* */
81-
public abstract get requestType(): ContentType;
77+
/** @internal */
78+
public abstract getProbableRequestType(method?: ClientMethod): ContentType;
8279
/** @internal */
8380
public abstract get isDeprecated(): boolean;
8481
}
@@ -89,6 +86,7 @@ export class Endpoint<
8986
CTX extends FlatObject,
9087
> extends AbstractEndpoint {
9188
readonly #def: ConstructorParameters<typeof Endpoint<IN, OUT, CTX>>[0];
89+
#requestType?: ContentType;
9290

9391
/** considered an expensive operation, only required for generators */
9492
#ensureOutputExamples = R.once(() => {
@@ -161,15 +159,18 @@ export class Endpoint<
161159
}
162160

163161
/** @internal */
164-
public override get requestType() {
165-
const found = findRequestTypeDefiningSchema(this.#def.inputSchema);
166-
if (found) {
167-
const brand = getBrand(found);
168-
if (brand === ezUploadBrand) return "upload";
169-
if (brand === ezRawBrand) return "raw";
170-
if (brand === ezFormBrand) return "form";
171-
}
172-
return "json";
162+
public override getProbableRequestType(method?: ClientMethod) {
163+
if (method === "query") return "form";
164+
return (this.#requestType ??= (() => {
165+
const found = findRequestTypeDefiningSchema(this.#def.inputSchema);
166+
if (found) {
167+
const brand = getBrand(found);
168+
if (brand === ezUploadBrand) return "upload";
169+
if (brand === ezRawBrand) return "raw";
170+
if (brand === ezFormBrand) return "form";
171+
}
172+
return "json";
173+
})());
173174
}
174175

175176
/** @internal */

express-zod-api/tests/endpoint.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,17 @@ describe("Endpoint", () => {
367367
});
368368
});
369369

370-
describe(".requestType", () => {
370+
describe(".getProbableRequestType()", () => {
371+
test("should return 'form' for QUERY method %#", () => {
372+
const factory = new EndpointsFactory(defaultResultHandler);
373+
const endpoint = factory.build({
374+
input: ez.raw(),
375+
output: z.object({}),
376+
handler: vi.fn(),
377+
});
378+
expect(endpoint.getProbableRequestType()).toBe("raw");
379+
expect(endpoint.getProbableRequestType("query")).toBe("form");
380+
});
371381
test.each([
372382
{ input: z.object({}), expected: "json" },
373383
{ input: ez.raw(), expected: "raw" },
@@ -383,7 +393,7 @@ describe("Endpoint", () => {
383393
output: z.object({}),
384394
handler: vi.fn(),
385395
});
386-
expect(endpoint.requestType).toEqual(expected);
396+
expect(endpoint.getProbableRequestType()).toEqual(expected);
387397
},
388398
);
389399
});

0 commit comments

Comments
 (0)