@nanotime/http-please
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -1,202 +0,119 @@ | ||
var g = Object.defineProperty; | ||
var f = (s, t, e) => t in s ? g(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e; | ||
var h = (s, t, e) => (f(s, typeof t != "symbol" ? t + "" : t, e), e), w = (s, t, e) => { | ||
if (!t.has(s)) | ||
throw TypeError("Cannot " + e); | ||
}; | ||
var c = (s, t, e) => (w(s, t, "read from private field"), e ? e.call(s) : t.get(s)), p = (s, t, e) => { | ||
if (t.has(s)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
t instanceof WeakSet ? t.add(s) : t.set(s, e); | ||
}; | ||
var u; | ||
class m { | ||
constructor({ url: t, options: e, resolver: n = "json", plugins: r }) { | ||
h(this, "url"); | ||
h(this, "options"); | ||
h(this, "resolver"); | ||
p(this, u, ["get", "post", "put", "delete", "fetch", "formatUrl"]); | ||
this.url = new URL(t), this.options = e, this.resolver = n, this.registerPlugins(r); | ||
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); | ||
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; | ||
} | ||
/** | ||
* Registers external plugins. | ||
* Sends an asynchronous HTTP request to the specified URL with optional request options. | ||
* | ||
* @param {array} externalPlugins - An array of external plugins to be registered. | ||
* @throws {Error} Throws an error if a plugin is already registered. | ||
* @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. | ||
*/ | ||
registerPlugins(t) { | ||
t.forEach((e) => { | ||
let n; | ||
if (Object.keys(e()).some((a) => (n = a, c(this, u).find((i) => i === a)))) | ||
throw new Error(`${n} is already registered`); | ||
c(this, u).push(n); | ||
async request(s, t) { | ||
return fetch(s, { | ||
...this.options, | ||
...t | ||
}); | ||
} | ||
get plugins() { | ||
return c(this, u); | ||
/** | ||
* 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, { | ||
method: "GET", | ||
...r | ||
}); | ||
return e.data = await e[t || this.resolver](), e; | ||
} | ||
} | ||
u = new WeakMap(); | ||
class d { | ||
constructor() { | ||
this.handlers = []; | ||
/** | ||
* 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, { | ||
method: "POST", | ||
body: JSON.stringify(a), | ||
...r | ||
}); | ||
return n.data = await n[t || this.resolver](), n; | ||
} | ||
/** | ||
* Adds a new handler to the list of handlers. | ||
* Sends a PUT request to the specified path with the provided body and options. | ||
* | ||
* @param {function} fullfilled - The function to be called when the promise is fulfilled. | ||
* @param {function} rejected - The function to be called when the promise is rejected. | ||
* @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. | ||
*/ | ||
use(t, e) { | ||
this.handlers.push([t, e]); | ||
async put({ path: s = "", resolver: t, opts: r, body: a }) { | ||
const e = this.pathFactory(s), n = await this.request(e, { | ||
method: "PUT", | ||
body: JSON.stringify(a), | ||
...r | ||
}); | ||
return n.data = await n[t || this.resolver](), n; | ||
} | ||
/** | ||
* Retrieves the value of the handlers property. | ||
* Sends a DELETE request to the specified path with the provided body and options. | ||
* | ||
* @return {Array} The handlers array. | ||
* @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. | ||
*/ | ||
get() { | ||
return this.handlers; | ||
async delete({ path: s = "", resolver: t, opts: r }) { | ||
const a = this.pathFactory(s), e = await this.request(a, { | ||
method: "DELETE", | ||
...r | ||
}); | ||
return e.data = await e[t || this.resolver](), e; | ||
} | ||
/** | ||
* 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(s) { | ||
const t = Array.from(this.url.searchParams.entries()), r = new URLSearchParams([ | ||
...t, | ||
...Object.entries(s) | ||
]).toString(); | ||
return { ...this, url: `${this.url.origin}?${r}` }; | ||
} | ||
/** | ||
* 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; | ||
} | ||
} | ||
function P() { | ||
return { | ||
// Create a new InterceptorManager for handling request/reponse interceptors | ||
// This objects will manage the list of interceptors and provide methods for adding and removing interceptors | ||
// It will be assigned to the property 'requestInterceptors' and 'responseInterceptors' | ||
requestInterceptors: new d(), | ||
responseInterceptors: new d(), | ||
/** | ||
* Asynchronously sends a request to the specified URL with the given options. | ||
* | ||
* @param {string} url - The URL to send the request to. | ||
* @param {object} opts - The options for the request. Default is an empty object. | ||
* @return {Promise} A promise that resolves with the response from the server. | ||
*/ | ||
async request(s, t = {}) { | ||
let e; | ||
const n = this.requestInterceptors.get().reverse(), r = this.responseInterceptors.get(), o = (i) => fetch(s, { | ||
...this.options, | ||
...i | ||
}), a = [...n, [o, void 0], ...r]; | ||
for (e = Promise.resolve({ | ||
url: this.url, | ||
resolver: this.resolver, | ||
options: { ...this.options, ...t } | ||
}); a.length; ) { | ||
const [i, l] = a.shift(); | ||
e = e.then(i, l); | ||
} | ||
return e; | ||
} | ||
}; | ||
} | ||
function y() { | ||
return { | ||
/** | ||
* Retrieves data from a specified path with optional query parameters. | ||
* | ||
* @param {object} params - An object containing the following properties: | ||
* - path {string} - The path to retrieve data from. | ||
* - opts {object} - Additional options for the request. | ||
* @return {Promise<object>} - A promise that resolves to the response object. | ||
*/ | ||
async get({ path: s = "", opts: t, resolver: e = "" }) { | ||
const n = new URL(this.url); | ||
n.pathname = s; | ||
const r = await this.request(n, { | ||
method: "GET", | ||
...t | ||
}); | ||
return r.data = await r[e || this.resolver](), r; | ||
} | ||
}; | ||
} | ||
function U() { | ||
return { | ||
/** | ||
* Sends a POST request to the specified path with the given query, body, and options. | ||
* | ||
* @param {Object} params - The options object. | ||
* - path {string} - The path for the request. | ||
* - body {object} - The body of the request. | ||
* - opts {object} - Additional options for the request. | ||
* @return {Promise<Object>} A promise that resolves to the response object. | ||
*/ | ||
async post({ path: s = "", body: t, opts: e, resolver: n = "" }) { | ||
const r = new URL(this.url); | ||
r.pathname = s; | ||
const o = await this.request(r, { | ||
method: "POST", | ||
body: JSON.stringify(t), | ||
...e | ||
}); | ||
return o.data = await o[n || this.resolver](), o; | ||
} | ||
}; | ||
} | ||
function q() { | ||
return { | ||
/** | ||
* Sends a PUT request to the specified path with the given query parameters, request body, and options. | ||
* | ||
* @param {object} params - An object containing the path, query parameters, request body, and options. | ||
* - {string} path - The path for the PUT request. | ||
* - {object} body - The request body. | ||
* - {object} opts - The options for the request. | ||
* @return {Promise<object>} A Promise that resolves to the response object with the data from the PUT request. | ||
*/ | ||
async put({ path: s = "", body: t, opts: e, resolver: n = "" }) { | ||
const r = new URL(this.url); | ||
r.pathname = s; | ||
const o = await this.request(r, { | ||
method: "PUT", | ||
body: JSON.stringify(t), | ||
...e | ||
}); | ||
return o.data = await o[n || this.resolver](), o; | ||
} | ||
}; | ||
} | ||
function I() { | ||
return { | ||
/** | ||
* Deletes a resource using the specified path, query, and options. | ||
* | ||
* @param {Object} parama - An object containing the following properties: | ||
* - {string} path - The path of the resource to delete. | ||
* - {Object} query - The query parameters for the request. | ||
* - {Object} opts - Additional options for the request. | ||
* @return {Promise<Object>} A Promise that resolves to the response object. | ||
*/ | ||
async delete({ path: s = "", opts: t, resolver: e = "" }) { | ||
const n = new URL(this.url); | ||
n.pathname = s; | ||
const r = await this.request(n, { | ||
method: "DELETE", | ||
...t | ||
}); | ||
return r.data = await r[e || this.resolver](), r; | ||
} | ||
}; | ||
} | ||
function E({ | ||
options: s, | ||
url: t, | ||
resolver: e, | ||
plugins: n = [] | ||
}) { | ||
const r = new m({ url: t, options: s, resolver: e, plugins: n }), o = n.reduce((i, l) => ({ ...i, ...l() }), {}); | ||
return { | ||
...r, | ||
...P(), | ||
...y(), | ||
...U(), | ||
...q(), | ||
...I(), | ||
...o | ||
}; | ||
} | ||
export { | ||
E as default | ||
p as default | ||
}; |
{ | ||
"name": "@nanotime/http-please", | ||
"private": false, | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js" | ||
}, | ||
"./plugins": { | ||
"import": "./dist/plugins.js" | ||
"import": "./dist/index.js", | ||
"types": "./dist/types/index.d.ts" | ||
} | ||
@@ -19,2 +17,3 @@ }, | ||
"dev": "vite", | ||
"clean": "rm -rf dist", | ||
"build": "vite build", | ||
@@ -32,3 +31,2 @@ "preview": "vite preview", | ||
"@commitlint/config-conventional": "^17.6.6", | ||
"@types/node": "^20.4.10", | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
@@ -49,3 +47,3 @@ "@typescript-eslint/parser": "^6.0.0", | ||
"vite": "^4.4.0", | ||
"vite-plugin-dts": "^3.3.0", | ||
"vite-plugin-dts": "^3.5.3", | ||
"vitest": "^0.33.0" | ||
@@ -52,0 +50,0 @@ }, |
@@ -85,7 +85,7 @@ <!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 --> | ||
http-please is a Fetch API wrapper library written in vanilla JS that was born as an educational project to learn how to apply design patterns and build npm packages with all the guarantees you would expect from a production-ready package. | ||
HttpPlease is a wrapper for Fetch, a TypeScript library that was born as an educational project to learn how to apply design patterns and create NPM packages that are as close to production standards as possible. | ||
The goal is not to create a package used by everyone that dethrones the big ones, but a contained package that is easy to extend where any programmer can contribute and understand more about how this world of libraries works. | ||
The goal of this project is to deploy a small, personal tool that is easy to use and extend, and in which anyone can contribute. | ||
Oh yes! And it works, you can use it for your personal projects, after all it's fetch... | ||
Oh yes! And it works, you can use it for your personal projects, after all it's just fetch... | ||
@@ -129,13 +129,12 @@ <p align="right">(<a href="#readme-top">back to top</a>)</p> | ||
Using this library is not different to use any other NPM library, just import it after the install | ||
and call the compositor: | ||
Using this library is not different to use any other NPM library, just import it after the install and instance the class: | ||
```js | ||
// Import the lib | ||
import CreateHttpPlease from '@nanotime/http-please'; | ||
import HttpPlease from '@nanotime/http-please'; | ||
// Create an instance | ||
const http = CreateHttpPlease({ | ||
const http = new HttpPlease({ | ||
url: 'http://example.com', | ||
options: { ... } | ||
options: { ... } // fetch options | ||
}) | ||
@@ -150,7 +149,4 @@ | ||
const response = await http.get({ path: 'foo' }); | ||
console.log(response); | ||
return response; | ||
} | ||
getFoo(); // log response | ||
``` | ||
@@ -157,0 +153,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21128
19
213
245
2