Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 14 additions & 27 deletions packages/better-fetch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down