Skip to content

Fix 2XX, 4XX, 5XX responses #1251

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

Merged
merged 2 commits into from
Jul 26, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/stale-shoes-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix 2XX, 4XX, 5XX responses
15 changes: 15 additions & 0 deletions packages/openapi-fetch/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@ describe("client", () => {
// assert array type (and only array type) was inferred
expect(data.length).toBe(0);
});

it("handles literal 2XX and 4XX codes", async () => {
const client = createClient<paths>();
mockFetch({ status: 201, body: '{"status": "success"}' });
const { data, error } = await client.PUT("/media", { body: { media: "base64", name: "myImage" } });

if (data) {
// assert 2XX type inferred correctly
expect(data.status).toBe("success");
} else {
// assert 4XX type inferred correctly
// (this should be a dead code path but tests TS types)
expect(error.message).toBe("Error");
}
});
});

describe("POST()", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export interface OperationObject {
responses: any;
}
export type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";
export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207;
export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Random fact: the status codes are numbers because that’s what openapi-typescript actually exports. See #380

// prettier-ignore
export type ErrorStatus = 500 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 429 | 431 | 444 | 450 | 451 | 497 | 498 | 499 | "default";
export type ErrorStatus = 500 | '5XX' | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 429 | 431 | 444 | 450 | 451 | 497 | 498 | 499 | '4XX' | "default";

// util
/** Get a union of paths which have method */
Expand Down
23 changes: 23 additions & 0 deletions packages/openapi-fetch/test/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ export interface paths {
"/header-params": {
get: operations["getHeaderParams"];
};
"/media": {
put: {
requestBody: {
content: {
"application/json": {
/** Format: blob */
media: string;
name: string;
};
};
};
responses: {
"2XX": {
content: {
"application/json": {
status: string;
};
};
};
"4XX": components["responses"]["Error"];
};
};
};
"/self": {
get: {
responses: {
Expand Down
30 changes: 30 additions & 0 deletions packages/openapi-fetch/test/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ paths:
- status
500:
$ref: '#/components/responses/Error'
/media:
put:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
media:
type: string
format: blob
name:
type: string
required:
- media
- name
responses:
2XX:
content:
application/json:
schema:
type: object
properties:
status:
type: string
required:
- status
4XX:
$ref: '#/components/responses/Error'
/self:
get:
responses:
Expand Down
72 changes: 72 additions & 0 deletions packages/openapi-typescript/test/operation-object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { GlobalContext, OperationObject } from "../src/types.js";
import transformOperationObject from "../src/transform/operation-object.js";

const ctx: GlobalContext = {
additionalProperties: false,
alphabetize: false,
defaultNonNullable: false,
discriminators: {},
emptyObjectsUnknown: false,
immutableTypes: false,
indentLv: 0,
operations: {},
parameters: {},
pathParamsAsTypes: false,
postTransform: undefined,
silent: true,
supportArrayLength: false,
transform: undefined,
excludeDeprecated: false,
};

describe("Operation Object", () => {
it("allows 2XX codes", () => {
const schema: OperationObject = {
responses: {
"2XX": {
description: "OK",
content: {
"application/json": {
schema: { type: "string" },
},
},
},
"4XX": {
description: "OK",
content: {
"application/json": { schema: { $ref: 'components["schemas"]["Error"]' } },
},
},
"5XX": {
description: "OK",
content: {
"application/json": { schema: { $ref: 'components["schemas"]["Error"]' } },
},
},
},
};
const generated = transformOperationObject(schema, { ctx, path: "#/paths/~get-item" });
expect(generated).toBe(`{
responses: {
/** @description OK */
"2XX": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

😅 Glad this worked! Added a test just cuz I got scared

content: {
"application/json": string;
};
};
/** @description OK */
"4XX": {
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description OK */
"5XX": {
content: {
"application/json": components["schemas"]["Error"];
};
};
};
}`);
});
});