Skip to content

Commit 290c517

Browse files
authored
fix(openapi-fetch): treat default response as error (openapi-ts#1139)
1 parent 9ad9729 commit 290c517

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ All methods return an object with **data**, **error**, and **response**.
129129

130130
- **data** will contain that endpoint’s `2xx` response if the server returned `2xx`; otherwise it will be `undefined`
131131
- **error** likewise contains that endpoint’s `4xx`/`5xx` response if the server returned either; otherwise it will be `undefined`
132+
- _Note: `default` will also be interpreted as `error`, since its intent is handling unexpected HTTP codes_
132133
- **response** has response info like `status`, `headers`, etc. It is not typechecked.
133134

134135
## API

src/index.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ describe("client", () => {
194194
});
195195
expect((await client.get("/self", {})).data).toBe(data);
196196
});
197+
198+
it("treats `default` as an error", async () => {
199+
const client = createClient<paths>({ headers: { "Cache-Control": "max-age=10000000" } });
200+
fetchMocker.mockResponseOnce(() => ({ status: 500, headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code: 500, message: "An unexpected error occurred" }) }));
201+
const { error } = await client.get("/default-as-error", {});
202+
203+
// discard `data` object
204+
if (!error) throw new Error("treats `default` as an error: error response should be present");
205+
206+
// assert `error.message` doesn’t throw TS error
207+
expect(error.message).toBe("An unexpected error occurred");
208+
});
197209
});
198210

199211
describe("get()", () => {

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export interface OperationObject {
2424
}
2525
export type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";
2626
export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207;
27-
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;
27+
// prettier-ignore
28+
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";
2829

2930
// util
3031
/** Get a union of paths which have method */

test/v1.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ export interface paths {
141141
};
142142
};
143143
};
144+
"/default-as-error": {
145+
get: {
146+
responses: {
147+
default: components["responses"]["Error"];
148+
};
149+
};
150+
};
144151
"/anyMethod": {
145152
get: {
146153
responses: {

test/v1.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ paths:
124124
$ref: '#/components/responses/CreateTag'
125125
500:
126126
$ref: '#/components/responses/Error'
127+
/default-as-error:
128+
get:
129+
responses:
130+
default:
131+
$ref: '#/components/responses/Error'
127132
/anyMethod:
128133
get:
129134
responses:

0 commit comments

Comments
 (0)