Skip to content

Commit 10bf404

Browse files
authored
Merge pull request #8 from cuppachino/stricter-types-1
fix: coerce undefined body to null
2 parents 67844e3 + 0194f01 commit 10bf404

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

.changeset/mighty-rings-whisper.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cuppachino/openapi-fetch": patch
3+
---
4+
5+
fix: coerce undefined body to null (stricter dev types)

src/index.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,23 @@ function wrapMiddlewares(middlewares: Middleware[], fetch: Fetch): Fetch {
328328
return (url, init) => handler(0, url, init)
329329
}
330330

331-
async function fetchUrl<R>(request: Request) {
332-
const { url, init } = getFetchParams(request)
333-
334-
const response = await request.fetch(url, init)
331+
const preferNull = <T>(
332+
maybe: T | undefined | null,
333+
): Exclude<T, undefined> | null => {
334+
if (maybe === undefined) return null
335+
return maybe as Exclude<T, undefined>
336+
}
335337

336-
return response as ApiResponse<R>
338+
async function fetchUrl<R>(request: Request) {
339+
const {
340+
init: { body, ...init },
341+
url,
342+
} = getFetchParams(request)
343+
344+
return (await request.fetch(url, {
345+
...init,
346+
body: preferNull(body),
347+
})) as ApiResponse<R>
337348
}
338349

339350
function createFetch<OP>(fetch: _TypedFetch<OP>): TypedFetch<OP> {

test/fetch.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe('fetch', () => {
266266
})
267267

268268
expect(data.body.list).toEqual(['b', 'c', 'mw2', 'mw1'])
269-
expect(data.headers.mw1).toEqual('true')
269+
expect(data.headers['mw1']).toEqual('true')
270270
expect(captured.url).toEqual('https://api.backend.dev/bodyquery/1?scalar=a')
271271
expect(captured.body).toEqual('{"list":["b","c"]}')
272272
})

test/mocks/handlers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const methods = {
5151
}),
5252
withError: [
5353
rest.get(`${HOST}/error/:status`, (req, res, ctx) => {
54-
const status = Number(req.params.status)
54+
const status = Number(req.params['status'])
5555
const detail = req.url.searchParams.get('detail') === 'true'
5656
return detail
5757
? res(
@@ -64,13 +64,13 @@ const methods = {
6464
)
6565
: res(ctx.status(status))
6666
}),
67-
rest.post(`${HOST}/nocontent`, (req, res, ctx) => {
67+
rest.post(`${HOST}/nocontent`, (_, res, ctx) => {
6868
return res(ctx.status(204))
6969
}),
70-
rest.get(`${HOST}/defaulterror`, (req, res, ctx) => {
70+
rest.get(`${HOST}/defaulterror`, (_, res, ctx) => {
7171
return res(ctx.status(500), ctx.body('internal server error'))
7272
}),
73-
rest.get(`${HOST}/networkerror`, (req, res) => {
73+
rest.get(`${HOST}/networkerror`, (_, res) => {
7474
return res.networkError('failed to connect')
7575
}),
7676
],

tsconfig.base.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
"sourceMap": false,
1212
"strict": true,
1313
"target": "ES2015",
14-
"useDefineForClassFields": true
14+
"useDefineForClassFields": true,
15+
"exactOptionalPropertyTypes": true,
16+
"noImplicitOverride": true,
17+
"noPropertyAccessFromIndexSignature": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"isolatedModules": true,
1521
},
1622
"include": ["src"]
1723
}

0 commit comments

Comments
 (0)