From e778f885a39a916a3452cc83f053536cae745d09 Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Tue, 29 Oct 2024 08:54:22 +0100 Subject: [PATCH] :factory: Add partial typing for the wretch options Solves #253 and #251 --- src/config.ts | 4 ++-- src/index.cts | 4 ++-- src/index.ts | 7 ++----- src/types.ts | 2 +- src/utils.ts | 5 +++-- test/deno/wretch_test.ts | 8 ++++---- test/node/middlewares/retry.spec.ts | 12 ++++++------ 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/config.ts b/src/config.ts index f9e67b0..bafa808 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,5 @@ import { mix } from "./utils.js" -import type { Config, ErrorType } from "./types.js" +import type { Config, ErrorType, WretchOptions } from "./types.js" declare const global @@ -42,7 +42,7 @@ const config: Config = { * @param options Default options * @param replace If true, completely replaces the existing options instead of mixing in */ -export function setOptions(options: object, replace = false) { +export function setOptions(options: WretchOptions, replace = false) { config.options = replace ? options : mix(config.options, options) } diff --git a/src/index.cts b/src/index.cts index b76d642..b193d8e 100644 --- a/src/index.cts +++ b/src/index.cts @@ -1,3 +1,3 @@ -import factory from "./index.js"; +import factory from "./index.js" -module.exports = factory.default; +module.exports = factory.default diff --git a/src/index.ts b/src/index.ts index 7ab175d..90b632e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -33,16 +33,13 @@ 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 } } factory["default"] = factory -/** {@inheritDoc setOptions} */ factory.options = setOptions -/** {@inheritDoc setErrorType} */ factory.errorType = setErrorType -/** {@inheritDoc setPolyfills} */ factory.polyfills = setPolyfills factory.WretchError = WretchError diff --git a/src/types.ts b/src/types.ts index e5914a6..da7fa33 100644 --- a/src/types.ts +++ b/src/types.ts @@ -761,7 +761,7 @@ export type Config = { /** * Fetch Request options with additional properties. */ -export type WretchOptions = Record +export type WretchOptions = Record & RequestInit /** * An Error enhanced with status, text and body. */ diff --git a/src/utils.ts b/src/utils.ts index ba7b4c8..094e2bc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,8 @@ import { CONTENT_TYPE_HEADER } from "./constants.js" -export function extractContentType(headers: Record = {}): 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] } diff --git a/test/deno/wretch_test.ts b/test/deno/wretch_test.ts index cd2c09f..b30c6ad 100644 --- a/test/deno/wretch_test.ts +++ b/test/deno/wretch_test.ts @@ -1,12 +1,12 @@ import { assertEquals, fail -} from "https://deno.land/std@0.147.0/testing/asserts.ts" +} from "jsr:@std/assert" import { beforeAll, describe, it, -} from "https://deno.land/std@0.147.0/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" @@ -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 () { @@ -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) } }) diff --git a/test/node/middlewares/retry.spec.ts b/test/node/middlewares/retry.spec.ts index e609c7f..2e52949 100644 --- a/test/node/middlewares/retry.spec.ts +++ b/test/node/middlewares/retry.spec.ts @@ -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) @@ -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}`]) }) }) @@ -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)