Skip to content

Commit e778f88

Browse files
committed
🏭 Add partial typing for the wretch options
Solves #253 and #251
1 parent db86a5f commit e778f88

File tree

7 files changed

+20
-22
lines changed

7 files changed

+20
-22
lines changed

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mix } from "./utils.js"
2-
import type { Config, ErrorType } from "./types.js"
2+
import type { Config, ErrorType, WretchOptions } from "./types.js"
33

44
declare const global
55

@@ -42,7 +42,7 @@ const config: Config = {
4242
* @param options Default options
4343
* @param replace If true, completely replaces the existing options instead of mixing in
4444
*/
45-
export function setOptions(options: object, replace = false) {
45+
export function setOptions(options: WretchOptions, replace = false) {
4646
config.options = replace ? options : mix(config.options, options)
4747
}
4848

src/index.cts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import factory from "./index.js";
1+
import factory from "./index.js"
22

3-
module.exports = factory.default;
3+
module.exports = factory.default

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { setOptions, setErrorType, setPolyfills } from "./config.js"
22
import { core } from "./core.js"
33
import { WretchError } from "./resolver.js"
4-
import type { Wretch } from "./types.js"
4+
import type { Wretch, WretchOptions } from "./types.js"
55

66
export type {
77
Wretch,
@@ -33,16 +33,13 @@ export type {
3333
* @param _options The base fetch options
3434
* @returns A fresh wretch instance
3535
*/
36-
function factory(_url = "", _options = {}): Wretch {
36+
function factory(_url = "", _options: WretchOptions = {}): Wretch {
3737
return { ...core, _url, _options }
3838
}
3939

4040
factory["default"] = factory
41-
/** {@inheritDoc setOptions} */
4241
factory.options = setOptions
43-
/** {@inheritDoc setErrorType} */
4442
factory.errorType = setErrorType
45-
/** {@inheritDoc setPolyfills} */
4643
factory.polyfills = setPolyfills
4744
factory.WretchError = WretchError
4845

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ export type Config = {
761761
/**
762762
* Fetch Request options with additional properties.
763763
*/
764-
export type WretchOptions = Record<string, any>
764+
export type WretchOptions = Record<string, any> & RequestInit
765765
/**
766766
* An Error enhanced with status, text and body.
767767
*/

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { CONTENT_TYPE_HEADER } from "./constants.js"
22

3-
export function extractContentType(headers: Record<string, string> = {}): string | undefined {
4-
return Object.entries(headers).find(([k]) =>
3+
export function extractContentType(headers: HeadersInit = {}): string | undefined {
4+
const normalizedHeaders = headers instanceof Array ? Object.fromEntries(headers) : headers
5+
return Object.entries(normalizedHeaders).find(([k]) =>
56
k.toLowerCase() === CONTENT_TYPE_HEADER.toLowerCase()
67
)?.[1]
78
}

test/deno/wretch_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
22
assertEquals,
33
fail
4-
} from "https://deno.land/[email protected]/testing/asserts.ts"
4+
} from "jsr:@std/assert"
55
import {
66
beforeAll,
77
describe,
88
it,
9-
} from "https://deno.land/[email protected]/testing/bdd.ts"
9+
} from "jsr:@std/testing/bdd"
1010

1111
import wretchFn from "../../dist/bundle/wretch.min.mjs"
1212
import BasicAuthAddon from "../../dist/bundle/addons/basicAuth.min.mjs"
@@ -117,7 +117,7 @@ describe("Wretch", function () {
117117
}
118118
// Ensure that the charset is preserved.
119119
const headerWithCharset = "application/json; charset=utf-16"
120-
assertEquals((wretch()).content(headerWithCharset).json({})._options.headers["Content-Type"], headerWithCharset)
120+
assertEquals((wretch()).content(headerWithCharset).json({})._options.headers?.["Content-Type" as keyof HeadersInit], headerWithCharset)
121121
})
122122

123123
it("should perform an url encoded form data round trip", async function () {
@@ -395,7 +395,7 @@ describe("Wretch", function () {
395395
await wretch(_URL + "/basicauth")
396396
.get()
397397
.res(_ => fail("Authenticated route should not respond without credentials."))
398-
} catch (e) {
398+
} catch (e: any) {
399399
assertEquals(e.status, 401)
400400
}
401401
})

test/node/middlewares/retry.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default describe("Retry Middleware", () => {
9999
true
100100
)
101101
.options({ a: 1 })
102-
await expect(w.get("/retry").res()).rejects.toThrowError(
102+
await expect(w.get("/retry").res()).rejects.toThrow(
103103
"Number of attempts exceeded."
104104
)
105105
expect(counter).toEqual(10)
@@ -114,17 +114,17 @@ export default describe("Retry Middleware", () => {
114114
delayTimer: 1,
115115
onRetry() {
116116
counter++
117-
return { url: `/${counter}`, options: { method: counter } }
117+
return { url: `/${counter}`, options: { method: `${counter}` } }
118118
},
119119
}),
120120
],
121121
true
122122
)
123-
await expect(w.options({ a: 0 }).get("/0").res()).rejects.toThrowError(
123+
await expect(w.options({ a: 0 }).get("/0").res()).rejects.toThrow(
124124
"Number of attempts exceeded."
125125
)
126126
logs.forEach((log, index) => {
127-
expect(log).toMatchObject([`/${index}`, index === 0 ? "GET" : index])
127+
expect(log).toMatchObject([`/${index}`, index === 0 ? "GET" : `${index}`])
128128
})
129129
})
130130

@@ -161,10 +161,10 @@ export default describe("Retry Middleware", () => {
161161
true
162162
)
163163

164-
await expect(wThrow.get("/retry").res()).rejects.toThrowError(
164+
await expect(wThrow.get("/retry").res()).rejects.toThrow(
165165
"Network Error"
166166
)
167-
await expect(wRetry.get("/retry").res()).rejects.toThrowError(
167+
await expect(wRetry.get("/retry").res()).rejects.toThrow(
168168
"Network Error"
169169
)
170170
expect(counter).toBe(10)

0 commit comments

Comments
 (0)