Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Fix bad type lookups (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow authored May 6, 2023
1 parent a138f87 commit 5d1fb7d
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-yaks-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-fetch': patch
---

Fix bad HTTP method lookup causing polymorphsim
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"openapi-typescript": "^6.2.4",
"prettier": "^2.8.8",
"typescript": "^5.0.4",
"vite": "^4.3.3",
"vitest": "^0.30.1",
"vitest": "^0.31.0",
"vitest-fetch-mock": "^0.2.2"
}
}
111 changes: 51 additions & 60 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ afterEach(() => {
fetchMocker.resetMocks();
});

describe('createClient', () => {
describe('client', () => {
it('generates all proper functions', () => {
const client = createClient<paths>();

Expand Down Expand Up @@ -113,7 +113,9 @@ describe('createClient', () => {

// expect present body to be good enough (all fields optional)
// (no error)
await client.put('/post', { body: { title: 'Foo', body: 'Bar', publish_date: new Date('2023-04-01T12:00:00Z').getTime() } });
await client.put('/post', {
body: { title: 'Foo', body: 'Bar', publish_date: new Date('2023-04-01T12:00:00Z').getTime() },
});
});

it('skips optional requestBody', async () => {
Expand Down Expand Up @@ -167,7 +169,7 @@ describe('createClient', () => {
it('allows override headers', async () => {
const client = createClient<paths>({ headers: { 'Cache-Control': 'max-age=10000000' } });
fetchMocker.mockResponseOnce(JSON.stringify({ email: 'user@user.com' }));
await client.get('/self', { headers: { 'Cache-Control': 'no-cache' } });
await client.get('/self', { params: {}, headers: { 'Cache-Control': 'no-cache' } });

// assert default headers were passed
const options = fetchMocker.mock.calls[0][1];
Expand Down Expand Up @@ -232,6 +234,17 @@ describe('get()', () => {
expect(data).toBe(undefined);
});

// note: this was a previous bug in the type inference
it('handles array-type responses', async () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 200, body: '[]' }));
const { data } = await client.get('/posts', { params: {} });
if (!data) throw new Error('data empty');

// assert array type (and only array type) was inferred
expect(data.length).toBe(0);
});

it('escapes URLs properly', async () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 200, body: '{}' }));
Expand Down Expand Up @@ -284,6 +297,7 @@ describe('post()', () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 201, body: JSON.stringify(mockData) }));
const { data, error, response } = await client.put('/post', {
params: {},
body: {
title: 'New Post',
body: '<p>Best post yet</p>',
Expand All @@ -307,6 +321,7 @@ describe('post()', () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 201, body: JSON.stringify(mockData) }));
const { data, error, response } = await client.put('/comment', {
params: {},
body: {
message: 'My reply',
replied_at: new Date('2023-03-31T12:00:00Z').getTime(),
Expand Down
Loading

0 comments on commit 5d1fb7d

Please sign in to comment.