nyro
Version:
A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.
502 lines (501 loc) • 21.5 kB
TypeScript
import * as http from 'http';
import { PassThrough } from 'stream';
interface ProxyOptions {
host: string;
port: number;
auth?: {
username: string;
password: string;
};
protocol?: ('http' | 'https' | 'socks' | 'socks4' | 'socks5' | 'socks4a' | 'socks5h' & string);
}
interface AuthOptions {
username: string;
password: string;
}
interface Headers {
'User-Agent'?: string;
'Response-Type'?: string;
'Content-Encoding'?: string;
'Content-Length'?: string;
'Content-Range'?: string;
'Content-Type'?: string;
'Authorization'?: string;
'Accept'?: string;
'Accept-Encoding'?: string;
'Accept-Language'?: string;
'Cache-Control'?: string;
'Connection'?: string;
'Cookie'?: string;
'DNT'?: string;
'Host'?: string;
'Origin'?: string;
'Pragma'?: string;
'Referer'?: string;
'TE'?: string;
'Upgrade-Insecure-Requests'?: string;
'Via'?: string;
'Warning'?: string;
'X-Requested-With'?: string;
'X-Forwarded-For'?: string;
'X-Forwarded-Host'?: string;
'X-Forwarded-Proto'?: string;
'Front-End-Https'?: string;
'X-Http-Method-Override'?: string;
'X-ATT-DeviceId'?: string;
'X-Wap-Profile'?: string;
'Proxy-Connection'?: string;
'X-UIDH'?: string;
'X-Csrf-Token'?: string;
'X-Request-ID'?: string;
'X-Correlation-ID'?: string;
'X-DeviceUserAgent'?: string;
'X-Device-ID'?: string;
'X-Device-OS'?: string;
'X-Device-OS-Version'?: string;
'X-Device-Model'?: string;
'X-Device-Brand'?: string;
'X-Device-Name'?: string;
'X-Device-Carrier'?: string;
'X-Device-Country'?: string;
'X-Device-Locale'?: string;
'X-Device-App'?: string;
'X-Device-App-Version'?: string;
'X-Device-App-Name'?: string;
'X-Device-App-Installer'?: string;
'X-Device-App-Install-Time'?: string;
'X-Device-App-Update-Time'?: string;
'X-Device-App-Store'?: string;
'X-Device-App-Store-Version'?: string;
'X-Device-App-Store-Name'?: string;
}
type InferBodySchema<T> = T extends Record<string, infer U> ? {
[K in keyof T]: T[K] extends NumberConstructor ? number : T[K] extends StringConstructor ? string : any;
} : any;
interface RequestOptions<B = any> {
method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'connect' | 'trace' & string);
port?: number;
url?: string;
path?: string;
headers?: (Headers & Record<string, string>);
body?: any;
timeout?: number;
params?: Record<string, string>;
baseURL?: string;
query?: Record<string, string | number | boolean>;
responseType?: ('json' | 'text' | 'blob' | 'stream' | 'arrayBuffer' | 'document' & string);
responseEncoding?: BufferEncoding;
timeoutErrorMessage?: string;
isStream?: boolean;
useHttp2?: boolean;
validateStatus?: (status: number) => boolean;
decompress?: boolean;
proxy?: ProxyOptions;
maxRedirects?: number;
auth?: AuthOptions;
maxBodyLength?: number;
maxContentLength?: number;
maxRate?: number;
signal?: AbortSignal;
bodySchema?: B;
retries?: number;
retryDelay?: number;
retryOn?: (req: http.RequestOptions, error: Error) => boolean;
onDownloadProgress?: (progress: {
percent: number;
transferredBytes: number;
totalBytes: number;
}) => void;
}
interface RequestInfo {
method?: string;
url?: string;
fullUrl: string;
headers: (Headers & Record<string, string>);
body?: BodyFromSchema<any>;
httpVersion?: string;
startTimestamp: number;
timeout?: number;
contentLength?: number;
}
type BodyFromSchema<B> = B extends typeof Number ? number : B extends typeof String ? string : B extends Record<string, infer T> ? {
[K in keyof B]: B[K] extends typeof Number ? number : B[K] extends typeof String ? string : T;
} : B extends typeof Array ? any[] : B;
interface HttpResponse<T, B = any> {
body: (BodyFromSchema<B> & PassThrough);
statusCode: number;
statusText: string;
headers: (Headers & Record<string, string | string[]>);
config: RequestOptions<B>;
request: http.ClientRequest;
requestInfo: RequestInfo;
response: http.IncomingMessage;
timestamp: {
startTimestamp: number;
endTimestamp: number;
};
responseTime: number;
responseSize: number;
serverIp?: string;
connectionReused: boolean;
}
type OmitedCreate = Omit<Core, 'create'>;
type OmitedExtend = Omit<Core, 'create'>;
declare class Core {
baseRequestOptions: RequestOptions;
constructor(baseRequestOptions?: RequestOptions);
/**
* The version of the Nyro library.
*/
static version: string;
/**
* The package.json file for the Nyro library.
*/
static pkg: any;
/**
* @param url
* @returns this
* @example Nyro.setURL('https://jsonplaceholder.typicode.com/posts');
* @description This function sets the URL for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setURL(url: string): this;
/**
* @param baseURL
* @returns this
* @example Nyro.setBaseURL('https://jsonplaceholder.typicode.com');
* @description This function sets the base URL for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBaseURL(baseURL: string): this;
/**
* @param path
* @returns this
* @example Nyro.setPath('/posts');
* @description This function sets the path for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setPath(path: string): this;
/**
* @param bodySchema
* @returns this
* @example Nyro.setBodySchema({ title: String, body: String });
* @description This function sets the body schema for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBodySchema(bodySchema: any): this;
/**
* @param auth
* @returns this
* @example Nyro.setAuth({ username: 'user', password: 'pass' });
* @description This function sets the authentication credentials for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setAuth(auth: AuthOptions): this;
/**
* @param port
* @returns this
* @example Nyro.setPort(443);
* @description This function sets the port for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setProxy(proxy: ProxyOptions): this;
/**
* @param port
* @returns this
* @example Nyro.setPort(443);
* @description This function sets the port for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMethod(method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE'): this;
/**
* @param headers
* @returns this
* @example Nyro.setHeaders({ 'Content-Type': 'application/json' });
* @description This function sets the headers for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setHeaders(headers: (Headers & Record<string, string>)): this;
/**
* @param params
* @returns this
* @example Nyro.setParams({ id: '1' });
* @description This function sets the query parameters for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setParams(params: Record<string, string>): this;
/**
* @param query
* @returns this
* @example Nyro.setQuery({ id: '1' });
* @description This function sets the query parameters for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setQuery(query: Record<string, string | number | boolean>): this;
/**
* @param body
* @returns this
* @example Nyro.setBody({ title: 'foo', body: 'bar', userId: 1 });
* @description This function sets the body for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBody(body: any): this;
/**
* @param timeout
* @returns this
* @example Nyro.setTimeout(5000);
* @description This function sets the timeout for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setTimeout(timeout: number): this;
/**
* @param responseType
* @returns this
* @example Nyro.setResponseType('json');
* @description This function sets the response type for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetryOn(retryOn: (req: http.RequestOptions, error: Error) => boolean): this;
/**
* @param retries
* @returns this
* @example Nyro.setRetries(3);
* @description This function sets the number of retries for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetries(retries: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setValidateStatus(validateStatus: (status: number) => boolean): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxBodyLength(maxBodyLength: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxContentLength(maxContentLength: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxRate(maxRate: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setSignal(signal: AbortSignal): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setOnDownloadProgress(onDownloadProgress: (progress: {
percent: number;
transferredBytes: number;
totalBytes: number;
}) => void): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setTimeoutErrorMessage(timeoutErrorMessage: string): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setResponseType(responseType: ('json' | 'text' | 'blob' | 'stream' | 'arrayBuffer' | 'document' & string)): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setResponseEncoding(responseEncoding: BufferEncoding): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxRedirects(maxRedirects: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetryDelay(retryDelay: number): this;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setDecompress(decompress: boolean): this;
/**
* Sends a GET request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.get('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a GET request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET|MDN web docs}
*/
get<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a POST request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.post('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a POST request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|MDN web docs}
*/
post<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a PUT request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.put('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a PUT request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT|MDN web docs}
*/
put<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a DELETE request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.delete('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a DELETE request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|MDN web docs}
*/
delete<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a PATCH request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.patch('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a PATCH request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH|MDN web docs}
*/
patch<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a HEAD request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.head('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a HEAD request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD|MDN web docs}
*/
head<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends an OPTIONS request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.options('https://jsonplaceholder.typicode.com/posts');
* @description This function sends an OPTIONS request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS|MDN web docs}
*/
options<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a CONNECT request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.connect('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a CONNECT request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT|MDN web docs}
*/
connect<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Sends a TRACE request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.trace('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a TRACE request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE|MDN web docs}
*/
trace<T, B>(url?: string, options?: RequestOptions<B>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
/**
* Extends the default request options with the provided options.
*
* @param extendOptions - The options to extend the default request options with.
* @returns An object with the execute function to make the request and the options used for the request.
* @example Nyro.extend({
* url: 'https://jsonplaceholder.typicode.com/posts',
* method: 'GET',
* headers: {
* 'Content-Type': 'application/json'
* }
* });
* @description This function allows you to create a new request with the provided options, while keeping the default options for future requests.
*/
extend<T, B>(extendOptions: RequestOptions<B>): Promise<OmitedExtend>;
/**
* Creates a new instance of the Nyro library with the provided options.
*
* @param options - The request options.
* @returns A new instance of the Nyro library with the provided options.
* @example Nyro.create({
* url: 'https://jsonplaceholder.typicode.com/posts',
* method: 'GET',
* headers: {
* 'Content-Type': 'application/json'
* }
* });
* @description This function creates a new instance of the Nyro library with the provided options.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_headers|MDN web docs}
*/
create<T, B>(options: RequestOptions<B>): Promise<OmitedCreate>;
/**
* Core function for handling HTTP requests.
*
* @param options - The request options.
* @param currentRedirects - The number of redirects that have occurred.
* @returns A promise that resolves with the HTTP response.
*/
request<T, B>(options?: RequestOptions<B>, currentRedirects?: number, attempt?: number, visitedUrls?: Set<string>): Promise<HttpResponse<T, BodyFromSchema<B>>>;
}
export default Core;
export { RequestInfo, RequestOptions, HttpResponse, Headers, ProxyOptions, AuthOptions, InferBodySchema, BodyFromSchema };