@nanotime/http-please
Advanced tools
Comparing version 2.0.0 to 2.1.0
type Resolver = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData'; | ||
type Query = { | ||
[key: string]: string; | ||
}; | ||
type RequestParams = { | ||
path: string; | ||
query?: Query; | ||
opts?: Omit<RequestInit, 'method' | 'body'>; | ||
@@ -10,2 +14,6 @@ resolver?: Resolver; | ||
}; | ||
interface QP { | ||
params: Query; | ||
url: URL; | ||
} | ||
interface HttpPleaseResponse<T = unknown> extends Response { | ||
@@ -27,6 +35,2 @@ data?: T; | ||
* Sends an asynchronous HTTP request to the specified URL with optional request options. | ||
* | ||
* @param {string | URL} url - The URL to send the request to. | ||
* @param {RequestInit} [opts] - Optional request options. | ||
* @return {Promise<Response>} A promise that resolves with the response to the request. | ||
*/ | ||
@@ -36,59 +40,24 @@ request(url: string | URL, opts?: RequestInit): Promise<Response>; | ||
* Sends a GET request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the GET request. | ||
* @prop {string} param.path - The path for the GET request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the GET request. Defaults to null. | ||
* @return {Promise} - The response of the GET request. | ||
*/ | ||
get<R>({ path, resolver, opts }: RequestParams): Promise<HttpPleaseResponse<R>>; | ||
get<R>({ path, query, resolver, opts }: RequestParams): Promise<HttpPleaseResponse<R>>; | ||
/** | ||
* Sends a POST request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the POST request. | ||
* @prop {string} param.path - The path for the POST request. Defaults to an empty string. | ||
* @prop {Function} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the POST request. Defaults to null. | ||
* @prop {any} param.body - The body of the POST request. | ||
* @return {Promise} - The response of the POST request. | ||
*/ | ||
post<R>({ path, resolver, opts, body }: PostRequestParams): Promise<HttpPleaseResponse<R>>; | ||
post<R>({ path, query, resolver, opts, body, }: PostRequestParams): Promise<HttpPleaseResponse<R>>; | ||
/** | ||
* Sends a PUT request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the PUT request. | ||
* @prop {string} param.path - The path for the PUT request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the PUT request. Defaults to null. | ||
* @prop {any} param.body - The body of the PUT request. | ||
* @return {Promise} - The response of the PUT request. | ||
*/ | ||
put<R>({ path, resolver, opts, body }: PostRequestParams): Promise<HttpPleaseResponse<R>>; | ||
put<R>({ path, query, resolver, opts, body, }: PostRequestParams): Promise<HttpPleaseResponse<R>>; | ||
/** | ||
* Sends a DELETE request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the DELETE request. | ||
* @prop {string} param.path - The path for the DELETE request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the DELETE request. Defaults to null. | ||
* @return {Promise} - The response of the DELETE request. | ||
*/ | ||
delete<R>({ path, resolver, opts }: RequestParams): Promise<HttpPleaseResponse<R>>; | ||
delete<R>({ path, query, resolver, opts }: RequestParams): Promise<HttpPleaseResponse<R>>; | ||
/** | ||
* Generates a query string by combining the existing search parameters of the URL | ||
* with the provided parameters. | ||
* | ||
* @param {Object} params - An object containing key-value pairs of parameters. | ||
* @return {Object} - A new object with the updated URL. | ||
*/ | ||
query(params: { | ||
[key: string]: string; | ||
}): this & { | ||
url: string; | ||
}; | ||
queryFactory({ params, url }: QP): URL; | ||
/** | ||
* Creates a new URL by appending the given path to the base URL. | ||
* | ||
* @param {string} path - The path to be appended to the base URL. | ||
* @return {URL} - The new URL object with the appended path. | ||
*/ | ||
@@ -95,0 +64,0 @@ pathFactory(path: string): URL; |
@@ -1,22 +0,18 @@ | ||
var h = Object.defineProperty; | ||
var c = (o, s, t) => s in o ? h(o, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[s] = t; | ||
var i = (o, s, t) => (c(o, typeof s != "symbol" ? s + "" : s, t), t); | ||
var c = Object.defineProperty; | ||
var u = (i, t, r) => t in i ? c(i, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[t] = r; | ||
var h = (i, t, r) => (u(i, typeof t != "symbol" ? t + "" : t, r), r); | ||
class p { | ||
constructor({ url: s, options: t, resolver: r }) { | ||
i(this, "url"); | ||
i(this, "options"); | ||
i(this, "resolver", "json"); | ||
this.url = new URL(s), this.options = t, this.resolver = r || this.resolver; | ||
constructor({ url: t, options: r, resolver: s }) { | ||
h(this, "url"); | ||
h(this, "options"); | ||
h(this, "resolver", "json"); | ||
this.url = new URL(t), this.options = r, this.resolver = s || this.resolver; | ||
} | ||
/** | ||
* Sends an asynchronous HTTP request to the specified URL with optional request options. | ||
* | ||
* @param {string | URL} url - The URL to send the request to. | ||
* @param {RequestInit} [opts] - Optional request options. | ||
* @return {Promise<Response>} A promise that resolves with the response to the request. | ||
*/ | ||
async request(s, t) { | ||
return fetch(s, { | ||
async request(t, r) { | ||
return fetch(t, { | ||
...this.options, | ||
...t | ||
...r | ||
}); | ||
@@ -26,51 +22,52 @@ } | ||
* Sends a GET request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the GET request. | ||
* @prop {string} param.path - The path for the GET request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the GET request. Defaults to null. | ||
* @return {Promise} - The response of the GET request. | ||
*/ | ||
async get({ path: s = "", resolver: t, opts: r }) { | ||
const a = this.pathFactory(s), e = await this.request(a, { | ||
async get({ path: t = "", query: r = {}, resolver: s, opts: o }) { | ||
const a = this.queryFactory({ | ||
params: r, | ||
url: this.pathFactory(t) | ||
}), e = await this.request(a, { | ||
method: "GET", | ||
...r | ||
...o | ||
}); | ||
return e.data = await e[t || this.resolver](), e; | ||
return e.data = await e[s || this.resolver](), e; | ||
} | ||
/** | ||
* Sends a POST request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the POST request. | ||
* @prop {string} param.path - The path for the POST request. Defaults to an empty string. | ||
* @prop {Function} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the POST request. Defaults to null. | ||
* @prop {any} param.body - The body of the POST request. | ||
* @return {Promise} - The response of the POST request. | ||
*/ | ||
async post({ path: s = "", resolver: t, opts: r, body: a }) { | ||
const e = this.pathFactory(s), n = await this.request(e, { | ||
async post({ | ||
path: t = "", | ||
query: r = {}, | ||
resolver: s, | ||
opts: o, | ||
body: a | ||
}) { | ||
const e = this.queryFactory({ | ||
params: r, | ||
url: this.pathFactory(t) | ||
}), n = await this.request(e, { | ||
method: "POST", | ||
body: JSON.stringify(a), | ||
...r | ||
...o | ||
}); | ||
return n.data = await n[t || this.resolver](), n; | ||
return n.data = await n[s || this.resolver](), n; | ||
} | ||
/** | ||
* Sends a PUT request to the specified path with the provided body and options. | ||
* | ||
* @param {Object} - The parameters for the PUT request. | ||
* @prop {string} param.path - The path for the PUT request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the PUT request. Defaults to null. | ||
* @prop {any} param.body - The body of the PUT request. | ||
* @return {Promise} - The response of the PUT request. | ||
*/ | ||
async put({ path: s = "", resolver: t, opts: r, body: a }) { | ||
const e = this.pathFactory(s), n = await this.request(e, { | ||
async put({ | ||
path: t = "", | ||
query: r = {}, | ||
resolver: s, | ||
opts: o, | ||
body: a | ||
}) { | ||
const e = this.queryFactory({ | ||
params: r, | ||
url: this.pathFactory(t) | ||
}), n = await this.request(e, { | ||
method: "PUT", | ||
body: JSON.stringify(a), | ||
...r | ||
...o | ||
}); | ||
return n.data = await n[t || this.resolver](), n; | ||
return n.data = await n[s || this.resolver](), n; | ||
} | ||
@@ -80,14 +77,12 @@ /** | ||
* | ||
* @param {Object} - The parameters for the DELETE request. | ||
* @prop {string} param.path - The path for the DELETE request. Defaults to an empty string. | ||
* @prop {string} param.resolver - The resolver function to be used for parsing the response. Defaults to null. | ||
* @prop {any} param.opts - Additional options for the DELETE request. Defaults to null. | ||
* @return {Promise} - The response of the DELETE request. | ||
*/ | ||
async delete({ path: s = "", resolver: t, opts: r }) { | ||
const a = this.pathFactory(s), e = await this.request(a, { | ||
async delete({ path: t = "", query: r = {}, resolver: s, opts: o }) { | ||
const a = this.queryFactory({ | ||
params: r, | ||
url: this.pathFactory(t) | ||
}), e = await this.request(a, { | ||
method: "DELETE", | ||
...r | ||
...o | ||
}); | ||
return e.data = await e[t || this.resolver](), e; | ||
return e.data = await e[s || this.resolver](), e; | ||
} | ||
@@ -97,22 +92,15 @@ /** | ||
* with the provided parameters. | ||
* | ||
* @param {Object} params - An object containing key-value pairs of parameters. | ||
* @return {Object} - A new object with the updated URL. | ||
*/ | ||
query(s) { | ||
const t = Array.from(this.url.searchParams.entries()), r = new URLSearchParams([ | ||
...t, | ||
...Object.entries(s) | ||
]).toString(); | ||
return { ...this, url: `${this.url.origin}?${r}` }; | ||
queryFactory({ params: t, url: r }) { | ||
const s = Array.from(this.url.searchParams.entries()), o = new URLSearchParams([...s, ...Object.entries(t)]), a = new URL(r); | ||
for (const [e, n] of o) | ||
a.searchParams.set(e, n); | ||
return a; | ||
} | ||
/** | ||
* Creates a new URL by appending the given path to the base URL. | ||
* | ||
* @param {string} path - The path to be appended to the base URL. | ||
* @return {URL} - The new URL object with the appended path. | ||
*/ | ||
pathFactory(s) { | ||
const t = new URL(this.url); | ||
return t.pathname = s, t; | ||
pathFactory(t) { | ||
const r = new URL(this.url); | ||
return r.pathname = t, r; | ||
} | ||
@@ -119,0 +107,0 @@ } |
{ | ||
"name": "@nanotime/http-please", | ||
"private": false, | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "exports": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
16902
170