Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,2 @@ | ||
import { RequestsOptions, RequestOptions, RequestResponse } from './types.js'; | ||
import { RequestlyOptions, RequestOptions, RequestlyResponse, MaybePromise, OnResponse } from './types.js'; | ||
@@ -11,5 +11,5 @@ declare class Requestly { | ||
private _onResponse?; | ||
constructor(opts?: RequestsOptions | string); | ||
constructor(opts?: RequestlyOptions | string); | ||
private _request; | ||
create(opts?: RequestsOptions): Requestly; | ||
create(opts?: RequestlyOptions | string): Requestly; | ||
get params(): Record<string, string> | undefined; | ||
@@ -32,11 +32,12 @@ set params(params: Record<string, string> | undefined); | ||
}; | ||
get<T>(url: string, options?: Omit<RequestOptions, "body">): Promise<RequestResponse<T>>; | ||
post<T>(url: string, options?: RequestOptions): Promise<RequestResponse<T>>; | ||
put<T>(url: string, options?: RequestOptions): Promise<RequestResponse<T>>; | ||
delete<T>(url: string, options?: RequestOptions): Promise<RequestResponse<T>>; | ||
patch<T>(url: string, options?: RequestOptions): Promise<RequestResponse<T>>; | ||
intercept({ onRequest, onResponse, }?: RequestsOptions["interceptors"]): void; | ||
get<T>(url: string, options?: Omit<RequestOptions, "body">): Promise<RequestlyResponse<T>>; | ||
post<T>(url: string, options?: RequestOptions): Promise<RequestlyResponse<T>>; | ||
put<T>(url: string, options?: RequestOptions): Promise<RequestlyResponse<T>>; | ||
delete<T>(url: string, options?: RequestOptions): Promise<RequestlyResponse<T>>; | ||
patch<T>(url: string, options?: RequestOptions): Promise<RequestlyResponse<T>>; | ||
onRequest(fn: (url: string, init: RequestInit) => MaybePromise<RequestInit | void>): void; | ||
onResponse(fn: OnResponse): void; | ||
} | ||
declare const requests: Requestly; | ||
declare const requestly: Requestly; | ||
export { Requestly, requests as default }; | ||
export { Requestly, requestly as default, requestly }; |
@@ -1,1 +0,1 @@ | ||
var p="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36";function g(n){let e=new URLSearchParams(n).toString();return e?`?${e}`:""}function l(n){let e={},s=n.split(",").map(t=>t.trim().split(";")[0]).filter(t=>t?.includes("="));for(let t of s){let[u,r]=t.split("=");!u||!r||(e[u.trim()]=r.trim())}return e}function R(n){return Object.entries(n).map(([s,t])=>encodeURI(`${s}=${t}`)).join("; ")}var d=class n{constructor(e){this._headers={};this._cookies={};this._params={};this.headers={set:(e,s)=>{this._headers[e]=s},get:e=>this._headers[e],getAll:()=>this._headers,remove:e=>{delete this._headers[e]},update:e=>{this._headers={...e,...this._headers}}};this.cookies={set:(e,s)=>{this._cookies[e]=s},get:e=>this._cookies[e],getAll:()=>this._cookies,remove:e=>{delete this._cookies[e]},update:e=>{this._cookies={...e,...this._cookies}},toString:()=>R(this.cookies.getAll())};if(typeof e=="string"){this._baseUrl=e;return}this._baseUrl=e?.baseUrl??"",this._headers=e?.headers??{},this._cookies=e?.cookies??{},this._params=e?.params??{},this._storeCookies=e?.storeCookies??!0,this._headers["User-Agent"]=e?.userAgent??p,this._onRequest=e?.interceptors?.onRequest,this._onResponse=e?.interceptors?.onResponse}async _request(e,s,t){if(e.startsWith("/")&&this._baseUrl==null)throw new Error("Cannot make request with relative URL without a base URL");let u=g({...t?.params,...this._params}),r=`${this._baseUrl}${e}${u}`;r.endsWith("/")&&(r=r.slice(0,-1));let _={...t?.body&&{"Content-Type":t?.contentType||"application/json"},...t?.headers},o={method:s,headers:{...this.headers.getAll(),..._,Cookie:this.cookies.toString()},body:typeof t?.body=="object"&&!(t?.body instanceof FormData)&&!(t?.body instanceof URLSearchParams)&&!(t?.body instanceof Blob)&&!(t?.body instanceof ArrayBuffer)&&!(t?.body instanceof ReadableStream)&&!(t?.body instanceof URL)&&!(t?.body instanceof Uint8Array)?JSON.stringify(t?.body):t?.body},h=await this._onRequest?.(r,o);o={...o,...h,headers:{...o.headers,...h?.headers}};let c=await fetch(r,o).then(async i=>{let{headers:a}=i,b=a.get("Content-Type")?.includes("application/json"),f=l(a.get("Set-Cookie")||"");return{...i,url:i.url,ok:i.ok,redirected:i.redirected,status:i.status,statusText:i.statusText,request:{url:e,method:s,options:o},headers:a,cookies:f,data:await(b?i.json():i.text())}});return t?.ignoreCookies!==!1&&this._storeCookies&&Object.entries(c.cookies).forEach(([i,a])=>this.cookies.set(i,a)),this._onResponse&&await this._onResponse(r,o,c),c}create(e){return new n(e)}get params(){return this._params}set params(e){this._params=e}get(e,s){return this._request(e,"GET",s)}post(e,s){return this._request(e,"POST",s)}put(e,s){return this._request(e,"PUT",s)}delete(e,s){return this._request(e,"DELETE",s)}patch(e,s){return this._request(e,"PATCH",s)}intercept({onRequest:e,onResponse:s}={}){this._onRequest=e??this._onRequest,this._onResponse=s??this._onResponse}},m=new d,q=m;export{d as Requestly,q as default}; | ||
var g="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36";function h(n){let e=new URLSearchParams(n).toString();return e?`?${e}`:""}function p(n){let e={},s=n.split(",").map(t=>t.trim().split(";")[0]).filter(t=>t?.includes("="));for(let t of s){let[u,r]=t.split("=");!u||!r||(e[u.trim()]=r.trim())}return e}function R(n){return Object.entries(n).map(([s,t])=>encodeURI(`${s}=${t}`)).join("; ")}var d=class n{constructor(e){this._headers={};this._cookies={};this._params={};this.headers={set:(e,s)=>{this._headers[e]=s},get:e=>this._headers[e],getAll:()=>this._headers,remove:e=>{delete this._headers[e]},update:e=>{this._headers={...e,...this._headers}}};this.cookies={set:(e,s)=>{this._cookies[e]=s},get:e=>this._cookies[e],getAll:()=>this._cookies,remove:e=>{delete this._cookies[e]},update:e=>{this._cookies={...e,...this._cookies}},toString:()=>R(this.cookies.getAll())};if(typeof e=="string"){this._baseUrl=e;return}this._baseUrl=e?.baseUrl??"",this._headers=e?.headers??{},this._cookies=e?.cookies??{},this._params=e?.params??{},this._storeCookies=e?.storeCookies??!0,this._headers["User-Agent"]=e?.userAgent??g,this._onRequest=e?.onRequest}async _request(e,s,t){if(e.startsWith("/")&&this._baseUrl==null)throw new Error("Cannot make request with relative URL without a base URL");let u=h({...t?.params,...this._params}),r=`${this._baseUrl}${e}${u}`;r.endsWith("/")&&(r=r.slice(0,-1));let _={...t?.body&&{"Content-Type":t?.contentType||"application/json"},...t?.headers},o={method:s,headers:{...this.headers.getAll(),..._,Cookie:this.cookies.toString()},body:typeof t?.body=="object"&&!(t?.body instanceof FormData)&&!(t?.body instanceof URLSearchParams)&&!(t?.body instanceof Blob)&&!(t?.body instanceof ArrayBuffer)&&!(t?.body instanceof ReadableStream)&&!(t?.body instanceof URL)&&!(t?.body instanceof Uint8Array)?JSON.stringify(t?.body):t?.body},l=await this._onRequest?.(r,o);o={...o,...l,headers:{...o.headers,...l?.headers}};let c=await fetch(r,o).then(async i=>{let{headers:a}=i,b=a.get("Content-Type")?.includes("application/json"),f=p(a.get("Set-Cookie")||"");return{...i,url:i.url,ok:i.ok,redirected:i.redirected,status:i.status,statusText:i.statusText,request:{url:e,method:s,options:o},headers:a,cookies:f,data:await(b?i.json():i.text())}});if(t?.ignoreCookies!==!1&&this._storeCookies&&Object.entries(c.cookies).forEach(([i,a])=>this.cookies.set(i,a)),this._onResponse){let i=await this._onResponse(r,o,c);if(i)return i?.headers instanceof Headers?i:{...c,data:i}}return c}create(e){return new n(e)}get params(){return this._params}set params(e){this._params=e}get(e,s){return this._request(e,"GET",s)}post(e,s){return this._request(e,"POST",s)}put(e,s){return this._request(e,"PUT",s)}delete(e,s){return this._request(e,"DELETE",s)}patch(e,s){return this._request(e,"PATCH",s)}onRequest(e){this._onRequest=e}onResponse(e){this._onResponse=e}},m=new d,k=m;export{d as Requestly,k as default,m as requestly}; |
@@ -1,2 +0,4 @@ | ||
interface RequestsOptions { | ||
type RequestMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; | ||
type OnResponse<T = any> = (url: string, init: RequestInit, response: RequestlyResponse<T>) => RequestlyResponse<T> | Promise<RequestlyResponse<T>> | Promise<T> | T | void; | ||
interface RequestlyOptions { | ||
baseUrl?: string; | ||
@@ -8,6 +10,4 @@ userAgent?: string; | ||
storeCookies?: boolean; | ||
interceptors?: { | ||
onRequest?: (url: string, init: RequestInit) => MaybePromise<RequestInit | void>; | ||
onResponse?: <T>(url: string, init: RequestInit, response: RequestResponse<T>) => MaybePromise<void>; | ||
}; | ||
onRequest?: (url: string, init: RequestInit) => MaybePromise<RequestInit | void>; | ||
onResponse?: OnResponse; | ||
} | ||
@@ -23,3 +23,3 @@ type MaybePromise<T> = T | Promise<T>; | ||
} | ||
interface RequestResponse<T> extends Omit<Response, "text" | "json"> { | ||
interface RequestlyResponse<T> extends Omit<Response, "text" | "json"> { | ||
request: { | ||
@@ -34,2 +34,2 @@ url: string; | ||
export type { MaybePromise, RequestOptions, RequestResponse, RequestsOptions }; | ||
export type { MaybePromise, OnResponse, RequestMethod, RequestOptions, RequestlyOptions, RequestlyResponse }; |
{ | ||
"name": "requestly", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A minimal, but powerful HTTP Client for Node.js", | ||
@@ -9,2 +9,4 @@ "publishConfig": { | ||
}, | ||
"main": "lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"type": "module", | ||
@@ -23,3 +25,2 @@ "exports": { | ||
}, | ||
"types": "./lib/index.d.ts", | ||
"files": [ | ||
@@ -26,0 +27,0 @@ "lib" |
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
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
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
18839
98