Skip to content

Commit

Permalink
Merge pull request #6 from koh110/dev
Browse files Browse the repository at this point in the history
fix defaultFetcher
  • Loading branch information
koh110 authored Dec 14, 2023
2 parents ef3296f + c845bfc commit a0e0f12
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tiny-type-api/client",
"version": "1.2.0",
"version": "1.2.1",
"type": "module",
"main": "./dist/esm/src/index.js",
"types": "./dist/esm/src/index.d.ts",
Expand Down
9 changes: 6 additions & 3 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ async function defaultFetcher<T>(options: {
init.body = options.form
}
const res = await fetch(options.url, init)
const body = options.headers['Content-Type']?.includes('application/json')
? await res.json()
: await res.text()
let body = undefined
if ((Number(res.headers.get('content-length')) ?? 0) > 0) {
body = options.headers['Content-Type']?.includes('application/json')
? await res.json()
: await res.text()
}
return {
ok: res.ok,
status: res.status,
Expand Down
59 changes: 54 additions & 5 deletions packages/client/src/indext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ const { apis } = defineApis({
}
}
}
}
},
'/api/void': {
PUT: {
request: {},
response: {
200: {
body: define<void>()
}
}
}
},
})

import { createClients } from './index.js'
Expand All @@ -68,9 +78,12 @@ test('clients', async () => {
const resMock = {
ok: true,
status: 200,
headers: {
get: vi.fn().mockReturnValue('64')
},
json: async () => ({ name: 'user-name' })
} satisfies Pick<Response, 'ok' | 'status' | 'json'>
requestMock.mockResolvedValue(resMock as Response)
} satisfies Pick<Response, 'ok' | 'status' | 'json'> & { headers: Pick<Response['headers'], 'get'> }
requestMock.mockResolvedValue(resMock as unknown as Response)

const res = await clients['/api/user/:user_id'].POST.client({
headers: {
Expand All @@ -96,15 +109,51 @@ test('clients', async () => {
)
})

test('clients: void response body', async () => {
const requestMock = vi.mocked(fetch).mockReset()
type Response = Awaited<ReturnType<typeof fetch>>
const jsonFn = vi.fn()
const textFn = vi.fn()
const resMock = {
ok: true,
status: 200,
headers: {
get: vi.fn().mockReturnValue('0')
},
json: jsonFn,
text: textFn
} satisfies Pick<Response, 'ok' | 'status' | 'json' | 'text'> & { headers: Pick<Response['headers'], 'get'> }
requestMock.mockResolvedValue(resMock as unknown as Response)

const res = await clients['/api/void'].PUT.client({})

expect(res.ok).toStrictEqual(true)
expect(res.status).toStrictEqual(200)
expect(requestMock).toBeCalledTimes(1)
expect(requestMock).toBeCalledWith(
'https://localhost:8000/api/void',
{
method: 'PUT',
headers: { 'Content-Type': 'application/json' }
}
)
expect(jsonFn).toBeCalledTimes(0)
expect(textFn).toBeCalledTimes(0)
})


test('clients: FormData', async () => {
const requestMock = vi.mocked(fetch).mockReset()
type Response = Awaited<ReturnType<typeof fetch>>
const resMock = {
ok: true,
status: 200,
headers: {
get: vi.fn().mockReturnValue('64')
},
text: async () => 'user-name'
} satisfies Pick<Response, 'ok' | 'status' | 'text'>
requestMock.mockResolvedValue(resMock as Response)
} satisfies Pick<Response, 'ok' | 'status' | 'text'> & { headers: Pick<Response['headers'], 'get'> }
requestMock.mockResolvedValue(resMock as unknown as Response)

const iconData = new Blob()

Expand Down
2 changes: 1 addition & 1 deletion packages/examples/dist/src/cjs/client.cjs.map

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

2 changes: 1 addition & 1 deletion packages/examples/dist/src/cjs/server.cjs.map

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

15 changes: 3 additions & 12 deletions packages/examples/dist/src/client.js

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

2 changes: 1 addition & 1 deletion packages/examples/dist/src/client.js.map

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

3 changes: 3 additions & 0 deletions packages/examples/dist/src/server.js

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

2 changes: 1 addition & 1 deletion packages/examples/dist/src/server.js.map

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

10 changes: 10 additions & 0 deletions packages/examples/dist/src/universal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,14 @@ export declare const apis: {
};
};
};
"/api/void": {
PUT: {
request: {};
response: {
200: {
body: (args: void) => void;
};
};
};
};
};
12 changes: 11 additions & 1 deletion packages/examples/dist/src/universal.js

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

2 changes: 1 addition & 1 deletion packages/examples/dist/src/universal.js.map

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

2 changes: 1 addition & 1 deletion packages/examples/dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

Loading

0 comments on commit a0e0f12

Please sign in to comment.