@worker-tools/json-fetch
Advanced tools
Comparing version 2.1.0-pre.2 to 2.1.0-pre.3
@@ -16,8 +16,8 @@ /** | ||
constructor(input, init, replacer, space) { | ||
const { headers: h, body: b, ...i } = init || {}; | ||
const { headers: _headers, body: _body, ..._init } = init || {}; | ||
let isBI; | ||
const body = (isBI = isBodyInit(b)) | ||
? b | ||
: JSON.stringify(b, replacer, space); | ||
const headers = new Headers(h); | ||
const body = (isBI = isBodyInit(_body)) | ||
? _body | ||
: JSON.stringify(_body, replacer, space); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
@@ -27,3 +27,3 @@ headers.set('Content-Type', JSONRequest.contentType); | ||
headers.set('Accept', JSONRequest.accept); | ||
super(input instanceof URL ? input.href : input, { headers, body, ...i }); | ||
super(input instanceof URL ? input.href : input, { headers, body, ..._init }); | ||
} | ||
@@ -45,11 +45,11 @@ } | ||
constructor(body, init, replacer, space) { | ||
const { headers: h, ...i } = init || {}; | ||
const { headers: _headers, ..._init } = init || {}; | ||
let isBI; | ||
const b = (isBI = isBodyInit(body)) | ||
const _body = (isBI = isBodyInit(body)) | ||
? body | ||
: JSON.stringify(body, replacer, space); | ||
const headers = new Headers(h); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
headers.set('Content-Type', JSONResponse.contentType); | ||
super(b, { headers, ...i }); | ||
super(_body, { headers, ..._init }); | ||
} | ||
@@ -56,0 +56,0 @@ } |
// This could be it's own module... | ||
/** | ||
* Like `URL`, but accepts a `params` argument that is added to the search parameters/query string. | ||
*/ | ||
export class SearchParamsURL extends URL { | ||
@@ -7,5 +10,7 @@ constructor(url, params, base) { | ||
? params | ||
: Object.entries(params !== null && params !== void 0 ? params : {}); | ||
: typeof params === 'string' | ||
? new URLSearchParams(params) | ||
: Object.entries(params !== null && params !== void 0 ? params : {}); | ||
for (const [k, v] of iterable) | ||
this.searchParams.append(k, v); | ||
this.searchParams.append(k, v.toString()); | ||
} | ||
@@ -12,0 +17,0 @@ } |
@@ -6,3 +6,3 @@ { | ||
"name": "@worker-tools/json-fetch", | ||
"version": "2.1.0-pre.2", | ||
"version": "2.1.0-pre.3", | ||
"description": "A drop-in replacements for fetch, Request, and Response with first class support for JSON objects.", | ||
@@ -9,0 +9,0 @@ "license": "MIT", |
@@ -33,8 +33,8 @@ "use strict"; | ||
constructor(input, init, replacer, space) { | ||
const { headers: h, body: b, ...i } = init || {}; | ||
const { headers: _headers, body: _body, ..._init } = init || {}; | ||
let isBI; | ||
const body = (isBI = isBodyInit(b)) | ||
? b | ||
: JSON.stringify(b, replacer, space); | ||
const headers = new Headers(h); | ||
const body = (isBI = isBodyInit(_body)) | ||
? _body | ||
: JSON.stringify(_body, replacer, space); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
@@ -44,3 +44,3 @@ headers.set('Content-Type', JSONRequest.contentType); | ||
headers.set('Accept', JSONRequest.accept); | ||
super(input instanceof URL ? input.href : input, { headers, body, ...i }); | ||
super(input instanceof URL ? input.href : input, { headers, body, ..._init }); | ||
} | ||
@@ -63,11 +63,11 @@ } | ||
constructor(body, init, replacer, space) { | ||
const { headers: h, ...i } = init || {}; | ||
const { headers: _headers, ..._init } = init || {}; | ||
let isBI; | ||
const b = (isBI = isBodyInit(body)) | ||
const _body = (isBI = isBodyInit(body)) | ||
? body | ||
: JSON.stringify(body, replacer, space); | ||
const headers = new Headers(h); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
headers.set('Content-Type', JSONResponse.contentType); | ||
super(b, { headers, ...i }); | ||
super(_body, { headers, ..._init }); | ||
} | ||
@@ -74,0 +74,0 @@ } |
@@ -5,2 +5,5 @@ "use strict"; | ||
// This could be it's own module... | ||
/** | ||
* Like `URL`, but accepts a `params` argument that is added to the search parameters/query string. | ||
*/ | ||
class SearchParamsURL extends URL { | ||
@@ -11,5 +14,7 @@ constructor(url, params, base) { | ||
? params | ||
: Object.entries(params !== null && params !== void 0 ? params : {}); | ||
: typeof params === 'string' | ||
? new URLSearchParams(params) | ||
: Object.entries(params !== null && params !== void 0 ? params : {}); | ||
for (const [k, v] of iterable) | ||
this.searchParams.append(k, v); | ||
this.searchParams.append(k, v.toString()); | ||
} | ||
@@ -16,0 +21,0 @@ } |
// deno-lint-ignore-file no-cond-assign | ||
// deno-lint-ignore no-explicit-any | ||
export type JSONBodyInit = BodyInit | any; | ||
@@ -31,10 +32,10 @@ export type JSONRequestInit = { body?: JSONBodyInit | null } & Omit<RequestInit, 'body'>; | ||
) { | ||
const { headers: h, body: b, ...i } = init || {}; | ||
const { headers: _headers, body: _body, ..._init } = init || {}; | ||
let isBI: boolean | ||
const body = (isBI = isBodyInit(b)) | ||
? b | ||
: JSON.stringify(b, replacer, space); | ||
const body = (isBI = isBodyInit(_body)) | ||
? _body | ||
: JSON.stringify(_body, replacer, space); | ||
const headers = new Headers(h); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
@@ -45,3 +46,3 @@ headers.set('Content-Type', JSONRequest.contentType); | ||
super(input instanceof URL ? input.href : input, { headers, body, ...i }); | ||
super(input instanceof URL ? input.href : input, { headers, body, ..._init }); | ||
} | ||
@@ -59,14 +60,14 @@ } | ||
) { | ||
const { headers: h, ...i } = init || {}; | ||
const { headers: _headers, ..._init } = init || {}; | ||
let isBI: boolean | ||
const b = (isBI = isBodyInit(body)) | ||
const _body = (isBI = isBodyInit(body)) | ||
? body | ||
: JSON.stringify(body, replacer, space); | ||
const headers = new Headers(h); | ||
const headers = new Headers(_headers); | ||
if (!headers.has('Content-Type') && !isBI) | ||
headers.set('Content-Type', JSONResponse.contentType); | ||
super(b, { headers, ...i }); | ||
super(_body, { headers, ..._init }); | ||
} | ||
@@ -73,0 +74,0 @@ } |
@@ -1,5 +0,8 @@ | ||
export type Stringable = { toString(...args: any[]): string } | ||
export type SearchParamsInit = URLSearchParams | [string, Stringable][] | Record<string, Stringable>; | ||
// deno-lint-ignore no-explicit-any | ||
export type SearchParamsInit = [string, any][] | Record<string, any> | string | URLSearchParams; | ||
// This could be it's own module... | ||
/** | ||
* Like `URL`, but accepts a `params` argument that is added to the search parameters/query string. | ||
*/ | ||
export class SearchParamsURL extends URL { | ||
@@ -14,5 +17,7 @@ constructor( | ||
? params | ||
: Object.entries(params ?? {}); | ||
: typeof params === 'string' | ||
? new URLSearchParams(params) | ||
: Object.entries(params ?? {}) | ||
for (const [k, v] of iterable) | ||
this.searchParams.append(k, v as string); | ||
this.searchParams.append(k, v.toString()); | ||
} | ||
@@ -19,0 +24,0 @@ } |
@@ -1,5 +0,5 @@ | ||
export declare type Stringable = { | ||
toString(...args: any[]): string; | ||
}; | ||
export declare type SearchParamsInit = URLSearchParams | [string, Stringable][] | Record<string, Stringable>; | ||
export declare type SearchParamsInit = [string, any][] | Record<string, any> | string | URLSearchParams; | ||
/** | ||
* Like `URL`, but accepts a `params` argument that is added to the search parameters/query string. | ||
*/ | ||
export declare class SearchParamsURL extends URL { | ||
@@ -6,0 +6,0 @@ constructor(url: string | URL, params?: SearchParamsInit | null, base?: string | URL); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22326
312