Skip to content

Commit

Permalink
Fix 2XX, 4XX, 5XX responses (#1251)
Browse files Browse the repository at this point in the history
* Fix 2XX, 4XX, 5XX responses

* Add 2XX, 4XX, 5XX test
  • Loading branch information
drwpow authored Jul 26, 2023
1 parent a33775b commit 80717a7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 2 deletions.
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";
// 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": {
content: {
"application/json": string;
};
};
/** @description OK */
"4XX": {
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description OK */
"5XX": {
content: {
"application/json": components["schemas"]["Error"];
};
};
};
}`);
});
});

0 comments on commit 80717a7

Please sign in to comment.