diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03f995b..d0b9045 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,35 +2,32 @@ name: CI on: pull_request: - branches: - - main + branches: + - main push: - branches: - - main + branches: + - main merge_group: {} jobs: - test: - name: test & typecheck + ci: + name: Build, Typecheck & Test runs-on: ubuntu-latest steps: - name: Checkout repo uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - name: Install pnpm - with: - version: 9 - run_install: false + - name: Install pnpm + uses: pnpm/action-setup@v4 - name: Install Node.js uses: actions/setup-node@v4 with: node-version: 20 - cache: 'pnpm' + cache: pnpm - name: Install dependencies - run: pnpm install + run: pnpm install --frozen-lockfile - name: Build run: pnpm build diff --git a/packages/better-fetch/src/utils.ts b/packages/better-fetch/src/utils.ts index fb05686..6b7e1f4 100644 --- a/packages/better-fetch/src/utils.ts +++ b/packages/better-fetch/src/utils.ts @@ -105,17 +105,12 @@ export async function getHeaders(opts?: BetterFetchOption) { const headers = new Headers(); if (opts?.headers) { - if (opts.headers instanceof Headers) { - opts.headers.forEach((value, key) => { - headers.set(key, value); - }); - } else { - for (const [key, value] of Object.entries(opts.headers)) { - if (value !== null && value !== undefined) { - headers.set(key, value); - } - } - } + const source = opts.headers instanceof Headers + ? opts.headers + : new Headers(opts.headers as HeadersInit); + source.forEach((value, key) => { + headers.set(key, value); + }); } const authHeader = await getAuthHeader(opts); @@ -212,18 +207,13 @@ export function getBody(options?: BetterFetchOption) { if (!options?.body) { return null; } - - let hasContentType = false; - if (options?.headers) { - if (options.headers instanceof Headers) { - hasContentType = options.headers.has("content-type"); - } else if (typeof options.headers === "object") { - hasContentType = "content-type" in options.headers && - options.headers["content-type"] !== null && - options.headers["content-type"] !== undefined; - } - } - + + const headers = options?.headers + ? new Headers(options.headers as HeadersInit) + : null; + + const hasContentType = headers?.has("content-type") ?? false; + if (isJSONSerializable(options.body) && !hasContentType) { for (const [key, value] of Object.entries(options?.body)) { if (value instanceof Date) { @@ -233,10 +223,7 @@ export function getBody(options?: BetterFetchOption) { return JSON.stringify(options.body); } - if ( - headers.has("content-type") && - headers.get("content-type") === "application/x-www-form-urlencoded" - ) { + if (headers?.get("content-type") === "application/x-www-form-urlencoded") { if (isJSONSerializable(options.body)) { return new URLSearchParams(options.body).toString(); }