Skip to content

Commit

Permalink
🏭 Add partial typing for the wretch options
Browse files Browse the repository at this point in the history
Solves #253 and #251
  • Loading branch information
elbywan committed Oct 29, 2024
1 parent db86a5f commit d43960e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/index.cts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import factory from "./index.js";
import factory from "./index.js"

module.exports = factory.default;
module.exports = factory.default
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setOptions, setErrorType, setPolyfills } from "./config.js"
import { core } from "./core.js"
import { WretchError } from "./resolver.js"
import type { Wretch } from "./types.js"
import type { Wretch, WretchOptions } from "./types.js"

export type {
Wretch,
Expand Down Expand Up @@ -33,7 +33,7 @@ export type {
* @param _options The base fetch options
* @returns A fresh wretch instance
*/
function factory(_url = "", _options = {}): Wretch {
function factory(_url = "", _options: WretchOptions = {}): Wretch {
return { ...core, _url, _options }
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ export type Config = {
/**
* Fetch Request options with additional properties.
*/
export type WretchOptions = Record<string, any>
export type WretchOptions = Record<string, any> & RequestInit
/**
* An Error enhanced with status, text and body.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CONTENT_TYPE_HEADER } from "./constants.js"

export function extractContentType(headers: Record<string, string> = {}): string | undefined {
return Object.entries(headers).find(([k]) =>
export function extractContentType(headers: HeadersInit = {}): string | undefined {
const normalizedHeaders = headers instanceof Array ? Object.fromEntries(headers) : headers
return Object.entries(normalizedHeaders).find(([k]) =>
k.toLowerCase() === CONTENT_TYPE_HEADER.toLowerCase()
)?.[1]
}
Expand Down
10 changes: 5 additions & 5 deletions test/deno/wretch_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
assertEquals,
fail
} from "https://deno.land/[email protected]/testing/asserts.ts"
} from "jsr:@std/assert"
import {
beforeAll,
describe,
it,
} from "https://deno.land/[email protected]/testing/bdd.ts"
} from "jsr:@std/testing/bdd"

import wretchFn from "../../dist/bundle/wretch.min.mjs"
import BasicAuthAddon from "../../dist/bundle/addons/basicAuth.min.mjs"
Expand All @@ -15,7 +15,7 @@ import FormDataAddon from "../../dist/bundle/addons/formData.min.mjs"
import QueryAddon from "../../dist/bundle/addons/queryString.min.mjs"
import AbortAddon from "../../dist/bundle/addons/abort.min.mjs"
// import PerfsAddon from "../../dist/bundle/addons/perfs.min.mjs"
import type { Wretch, WretchResponseChain, WretchResponse, Middleware } from "../../dist/types.d.ts"
import type { Wretch, WretchResponseChain, WretchResponse, Middleware, WretchError } from "../../dist/types.d.ts"

Check failure on line 18 in test/deno/wretch_test.ts

View workflow job for this annotation

GitHub Actions / browsers-test

'WretchError' is defined but never used

Check failure on line 18 in test/deno/wretch_test.ts

View workflow job for this annotation

GitHub Actions / deno-tests

'WretchError' is defined but never used

Check failure on line 18 in test/deno/wretch_test.ts

View workflow job for this annotation

GitHub Actions / nodejs-test (16)

'WretchError' is defined but never used

Check failure on line 18 in test/deno/wretch_test.ts

View workflow job for this annotation

GitHub Actions / nodejs-test (18)

'WretchError' is defined but never used

Check failure on line 18 in test/deno/wretch_test.ts

View workflow job for this annotation

GitHub Actions / nodejs-test (20)

'WretchError' is defined but never used

declare type factory = {
(_url?: string, _options?: object): Wretch;
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("Wretch", function () {
}
// Ensure that the charset is preserved.
const headerWithCharset = "application/json; charset=utf-16"
assertEquals((wretch()).content(headerWithCharset).json({})._options.headers["Content-Type"], headerWithCharset)
assertEquals((wretch()).content(headerWithCharset).json({})._options.headers?.["Content-Type" as keyof HeadersInit], headerWithCharset)
})

it("should perform an url encoded form data round trip", async function () {
Expand Down Expand Up @@ -395,7 +395,7 @@ describe("Wretch", function () {
await wretch(_URL + "/basicauth")
.get()
.res(_ => fail("Authenticated route should not respond without credentials."))
} catch (e) {
} catch (e: any) {
assertEquals(e.status, 401)
}
})
Expand Down
12 changes: 6 additions & 6 deletions test/node/middlewares/retry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default describe("Retry Middleware", () => {
true
)
.options({ a: 1 })
await expect(w.get("/retry").res()).rejects.toThrowError(
await expect(w.get("/retry").res()).rejects.toThrow(
"Number of attempts exceeded."
)
expect(counter).toEqual(10)
Expand All @@ -114,17 +114,17 @@ export default describe("Retry Middleware", () => {
delayTimer: 1,
onRetry() {
counter++
return { url: `/${counter}`, options: { method: counter } }
return { url: `/${counter}`, options: { method: `${counter}` } }
},
}),
],
true
)
await expect(w.options({ a: 0 }).get("/0").res()).rejects.toThrowError(
await expect(w.options({ a: 0 }).get("/0").res()).rejects.toThrow(
"Number of attempts exceeded."
)
logs.forEach((log, index) => {
expect(log).toMatchObject([`/${index}`, index === 0 ? "GET" : index])
expect(log).toMatchObject([`/${index}`, index === 0 ? "GET" : `${index}`])
})
})

Expand Down Expand Up @@ -161,10 +161,10 @@ export default describe("Retry Middleware", () => {
true
)

await expect(wThrow.get("/retry").res()).rejects.toThrowError(
await expect(wThrow.get("/retry").res()).rejects.toThrow(
"Network Error"
)
await expect(wRetry.get("/retry").res()).rejects.toThrowError(
await expect(wRetry.get("/retry").res()).rejects.toThrow(
"Network Error"
)
expect(counter).toBe(10)
Expand Down

0 comments on commit d43960e

Please sign in to comment.