@stackone/transport 1.6.2 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +399 -0
- package/dist/index.d.ts +399 -0
- package/dist/index.js +793 -1
- package/dist/index.mjs +755 -0
- package/package.json +24 -5
- package/dist/index.es.mjs +0 -1
- package/dist/types/authorization/authorizationHeaders.d.ts +0 -4
- package/dist/types/authorization/types.d.ts +0 -12
- package/dist/types/customErrors/index.d.ts +0 -3
- package/dist/types/customErrors/schemas.d.ts +0 -17
- package/dist/types/customErrors/types.d.ts +0 -4
- package/dist/types/errors/httpResponseError.d.ts +0 -6
- package/dist/types/httpClient/httpClient.d.ts +0 -37
- package/dist/types/httpClient/httpClientManager.d.ts +0 -10
- package/dist/types/httpClient/types.d.ts +0 -57
- package/dist/types/index.d.ts +0 -13
- package/dist/types/parsers/requestParameters.d.ts +0 -3
- package/dist/types/parsers/types.d.ts +0 -7
- package/dist/types/requestClient/requestClientFactory.d.ts +0 -4
- package/dist/types/requestClient/restClient.d.ts +0 -13
- package/dist/types/requestClient/types.d.ts +0 -14
- package/dist/types/validators/statusCodes.d.ts +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackone/transport",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.es.mjs",
|
|
@@ -10,12 +10,20 @@
|
|
|
10
10
|
"package.json",
|
|
11
11
|
"README.md"
|
|
12
12
|
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/types/index.d.ts",
|
|
16
|
+
"import": "./dist/index.es.mjs",
|
|
17
|
+
"require": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
13
21
|
"scripts": {
|
|
14
22
|
"clean": "rimraf dist",
|
|
15
23
|
"prebuild": "npm run clean",
|
|
16
|
-
"build": "
|
|
24
|
+
"build": "tsdown --env.NODE_ENV=production",
|
|
17
25
|
"prebuild:dev": "npm run clean",
|
|
18
|
-
"build:dev": "
|
|
26
|
+
"build:dev": "tsdown --format esm --format cjs --dts --env.NODE_ENV=development",
|
|
19
27
|
"code:format": "biome format ./src ./*.mjs",
|
|
20
28
|
"code:format:fix": "biome format --write ./src ./*.mjs",
|
|
21
29
|
"code:lint": "biome lint --error-on-warnings ./src ./*.mjs",
|
|
@@ -34,10 +42,21 @@
|
|
|
34
42
|
"dependencies": {
|
|
35
43
|
"axios": "1.8.4",
|
|
36
44
|
"qs": "6.14.0",
|
|
45
|
+
"@stackone/logger": "*",
|
|
37
46
|
"@stackone/utils": "*",
|
|
38
|
-
"@stackone/expressions": "*"
|
|
47
|
+
"@stackone/expressions": "*",
|
|
48
|
+
"redis": "^4.7.0",
|
|
49
|
+
"zod": "^3.24.1"
|
|
39
50
|
},
|
|
40
51
|
"devDependencies": {
|
|
41
|
-
"@types/qs": "6.9.18"
|
|
52
|
+
"@types/qs": "6.9.18",
|
|
53
|
+
"tsdown": "^0.12.9"
|
|
54
|
+
},
|
|
55
|
+
"tsdown": {
|
|
56
|
+
"dts": true,
|
|
57
|
+
"format": [
|
|
58
|
+
"esm",
|
|
59
|
+
"cjs"
|
|
60
|
+
]
|
|
42
61
|
}
|
|
43
62
|
}
|
package/dist/index.es.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import e from"axios";import t from"qs";import{isMissing as r,notMissing as s}from"@stackone/utils";import{safeEvaluate as a}from"@stackone/expressions";import{z as n}from"zod";const o=["get","post","put","delete","patch"];function i(e,t,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(e):s?s.value:t.get(e)}var c,u,d,l,p,h;"function"==typeof SuppressedError&&SuppressedError;const m=()=>e.create({maxBodyLength:1/0});class y{constructor({transportInstance:e=m()}={}){c.add(this),u.set(this,void 0),function(e,t,r,s,a){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===s?a.call(e,r):a?a.value=r:t.set(e,r)}(this,u,e,"f")}async request({headers:e={},url:t,method:r="get",queryParams:s,maxRedirects:a=0,responseType:n,cacheTTL:o,context:p,payload:m,httpsAgent:y}){try{const o=new URLSearchParams(s).toString(),p=i(this,c,"m",d).call(this,e),f=await i(this,u,"f").request({headers:p,url:o?`${t}?${o}`:t,method:r,maxRedirects:a,responseType:n,data:i(this,c,"m",h).call(this,m,p),httpsAgent:y});return{data:f.data,status:f.status,headers:i(this,c,"m",l).call(this,f.headers),requestUrl:t,responseType:f?.config?.responseType,responseTime:new Date}}catch(e){throw e}}async get({headers:e,url:t,queryParams:r,maxRedirects:s,cacheTTL:a,context:n}){return this.request({url:t,method:"get",queryParams:r,headers:e,maxRedirects:s,cacheTTL:a,context:n})}async post({headers:e,url:t,maxRedirects:r,cacheTTL:s,context:a,payload:n}){return this.request({url:t,method:"post",headers:e,maxRedirects:r,cacheTTL:s,context:a,payload:n})}}u=new WeakMap,c=new WeakSet,d=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r.toLowerCase()]=e[r]||""})),t},l=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]||""})),t},p=function(e){return"application/x-www-form-urlencoded"===e["content-type"]},h=function(e,r){if(e)return i(this,c,"m",p).call(this,r)?t.stringify(e):e};const f=()=>new y;class w{static async getInstance({getHttpClient:e=f}={}){return this.httpClientInstance??=e(),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}w.httpClientInstance=null;const g=e=>{switch(e.type){case"basic":return T(e);case"bearer":return q(e);default:throw new Error("Invalid authentication type")}},T=({username:e="",password:t="",encoding:r="base64"})=>{const s=r;return{authorization:`Basic ${Buffer.from(`${e}:${t}`).toString(s)}`}},q=({token:e,includeBearer:t=!0})=>({authorization:`${t?"Bearer ":""}${e}`}),R=e=>e.reduce(((e,t)=>(Array.isArray(e[t.in][t.name])?e[t.in][t.name]=e[t.in][t.name].concat(t.value):"object"==typeof e[t.in][t.name]&&"object"==typeof t.value?e[t.in][t.name]={...e[t.in][t.name],...t.value}:e[t.in][t.name]="body"===t.in?t.value:String(t.value),e)),{query:{},body:{},headers:{}}),b={100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Too Early",426:"Upgrade Required",428:"Precondition Required",429:"Too many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",499:"Client Closed Request",500:"Internal server error",501:"Not Implemented",502:"Bad Gateway",503:"Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",510:"Not Extended",511:"Network Authentication Required"};class v extends Error{constructor(e,t){super(t),this.name="HttpResponseError",this.response=e,this.message=t??`HTTP error: ${e.status}`,Error.captureStackTrace&&Error.captureStackTrace(this,v)}toString(){const e=this.message?`: ${this.message}`:"";return`${this.name} [${this.response.status}]${e}`}}const x=e=>s(e)&&e>=200&&e<=299,S=e=>s(e)&&e>=400&&e<=599,E=e=>s(e)&&e>=100&&e<=199;class P{async performRequest({httpClient:e,url:t,method:s,headers:n,body:o,customErrorConfigs:i}){let c;try{c=await(e?.request({method:s,url:t,headers:n,maxRedirects:0,payload:o}))}catch(e){if(r(e?.response))throw e;c=e.response}const u=((e,t)=>{if(r(t)||0===t.length||r(e))return{data:e.data,status:e.status,headers:e.headers,requestUrl:e.requestUrl,responseType:e.responseType,responseTime:e.responseTime,message:e.message};const s=t?.filter((t=>t.receivedStatus===e.status));if(s?.length)for(const t of s){const s=t.condition,n=!0===a(s,e);if(r(s)||n)return{data:e.data,headers:e.headers,requestUrl:e.requestUrl,responseType:e.responseType,responseTime:e.responseTime,status:t.targetStatus,message:t?.message??b[t.targetStatus]??"Unknown error"}}return e})(c,i);if(S(u?.status))throw new v(u,u.message);return u}}class C{static build(e="rest"){if("rest"===e)return new P;throw new Error(`Unknown request client type: ${e}`)}}const U=["query","body","headers"],I=n.object({receivedStatus:n.number(),targetStatus:n.number(),message:n.string().optional(),condition:n.string().optional()});export{I as CUSTOM_ERROR_CONFIG_SCHEMA,y as HttpClient,w as HttpClientManager,b as HttpErrorMessages,o as HttpMethods,v as HttpResponseError,C as RequestClientFactory,U as RequestParameterLocations,g as createAuthorizationHeaders,S as isFailedStatusCode,E as isInfoStatusCode,x as isSuccessStatusCode,R as parseRequestParameters};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type AuthenticationType = 'basic' | 'bearer';
|
|
2
|
-
export type BasicAuthorizationParams = {
|
|
3
|
-
type: 'basic';
|
|
4
|
-
username?: string;
|
|
5
|
-
password?: string;
|
|
6
|
-
encoding?: string;
|
|
7
|
-
};
|
|
8
|
-
export type BearerAuthorizationParams = {
|
|
9
|
-
type: 'bearer';
|
|
10
|
-
token: string;
|
|
11
|
-
includeBearer?: boolean;
|
|
12
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export declare const CUSTOM_ERROR_CONFIG_SCHEMA: z.ZodObject<{
|
|
3
|
-
receivedStatus: z.ZodNumber;
|
|
4
|
-
targetStatus: z.ZodNumber;
|
|
5
|
-
message: z.ZodOptional<z.ZodString>;
|
|
6
|
-
condition: z.ZodOptional<z.ZodString>;
|
|
7
|
-
}, "strip", z.ZodTypeAny, {
|
|
8
|
-
receivedStatus: number;
|
|
9
|
-
targetStatus: number;
|
|
10
|
-
message?: string | undefined;
|
|
11
|
-
condition?: string | undefined;
|
|
12
|
-
}, {
|
|
13
|
-
receivedStatus: number;
|
|
14
|
-
targetStatus: number;
|
|
15
|
-
message?: string | undefined;
|
|
16
|
-
condition?: string | undefined;
|
|
17
|
-
}>;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import https from 'https';
|
|
2
|
-
import { AxiosInstance } from 'axios';
|
|
3
|
-
import { HttpHeaders, HttpMethod, HttpQueryParams, HttpResponse, IHttpClient } from './types';
|
|
4
|
-
export declare class HttpClient implements IHttpClient {
|
|
5
|
-
#private;
|
|
6
|
-
constructor({ transportInstance, }?: {
|
|
7
|
-
transportInstance?: AxiosInstance;
|
|
8
|
-
});
|
|
9
|
-
request<P, T>({ headers, url, method, queryParams, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
10
|
-
headers?: HttpHeaders;
|
|
11
|
-
url: string;
|
|
12
|
-
method?: HttpMethod;
|
|
13
|
-
queryParams?: HttpQueryParams;
|
|
14
|
-
maxRedirects?: number;
|
|
15
|
-
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
16
|
-
cacheTTL?: number;
|
|
17
|
-
context?: unknown;
|
|
18
|
-
payload?: P;
|
|
19
|
-
httpsAgent?: https.Agent;
|
|
20
|
-
}): Promise<HttpResponse<T>>;
|
|
21
|
-
get<T>({ headers, url, queryParams, maxRedirects, cacheTTL, context, }: {
|
|
22
|
-
headers?: HttpHeaders;
|
|
23
|
-
url: string;
|
|
24
|
-
queryParams?: HttpQueryParams;
|
|
25
|
-
maxRedirects?: number;
|
|
26
|
-
cacheTTL?: number;
|
|
27
|
-
context?: unknown;
|
|
28
|
-
}): Promise<HttpResponse<T>>;
|
|
29
|
-
post<P, T>({ headers, url, maxRedirects, cacheTTL, context, payload, }: {
|
|
30
|
-
headers?: HttpHeaders;
|
|
31
|
-
url: string;
|
|
32
|
-
maxRedirects?: number;
|
|
33
|
-
cacheTTL?: number;
|
|
34
|
-
context?: unknown;
|
|
35
|
-
payload?: P;
|
|
36
|
-
}): Promise<HttpResponse<T>>;
|
|
37
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { IHttpClient } from './types';
|
|
2
|
-
declare const buildHttpClientInstance: () => IHttpClient;
|
|
3
|
-
export declare class HttpClientManager {
|
|
4
|
-
private static httpClientInstance;
|
|
5
|
-
static getInstance({ getHttpClient, }?: {
|
|
6
|
-
getHttpClient?: typeof buildHttpClientInstance;
|
|
7
|
-
}): Promise<IHttpClient>;
|
|
8
|
-
static resetInstance(): void;
|
|
9
|
-
}
|
|
10
|
-
export {};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import https from 'https';
|
|
2
|
-
export type HttpHeaders = {
|
|
3
|
-
[key: string]: string;
|
|
4
|
-
};
|
|
5
|
-
export type HttpQueryParams = {
|
|
6
|
-
[key: string]: string;
|
|
7
|
-
};
|
|
8
|
-
export interface CertOptions {
|
|
9
|
-
certs?: string[];
|
|
10
|
-
keys?: string[];
|
|
11
|
-
}
|
|
12
|
-
export declare const HttpMethods: readonly ["get", "post", "put", "delete", "patch"];
|
|
13
|
-
export type HttpMethod = (typeof HttpMethods)[number];
|
|
14
|
-
export type HttpResponse<T = any> = {
|
|
15
|
-
data: T;
|
|
16
|
-
status: number;
|
|
17
|
-
headers: HttpHeaders;
|
|
18
|
-
requestUrl: string;
|
|
19
|
-
responseType?: string;
|
|
20
|
-
responseTime?: Date;
|
|
21
|
-
message?: string;
|
|
22
|
-
};
|
|
23
|
-
export type HttpParameters = {
|
|
24
|
-
query: HttpQueryParams;
|
|
25
|
-
body: Record<string, unknown>;
|
|
26
|
-
headers: HttpHeaders;
|
|
27
|
-
};
|
|
28
|
-
export interface IHttpClient {
|
|
29
|
-
request<P, T>({ headers, url, method, queryParams, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
30
|
-
headers?: HttpHeaders;
|
|
31
|
-
url: string;
|
|
32
|
-
method?: HttpMethod;
|
|
33
|
-
queryParams?: HttpQueryParams;
|
|
34
|
-
maxRedirects?: number;
|
|
35
|
-
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
36
|
-
cacheTTL?: number;
|
|
37
|
-
context?: unknown;
|
|
38
|
-
payload?: P;
|
|
39
|
-
httpsAgent?: https.Agent;
|
|
40
|
-
}): Promise<HttpResponse<T>>;
|
|
41
|
-
get<T>({ headers, url, queryParams, maxRedirects, cacheTTL, context, }: {
|
|
42
|
-
headers?: HttpHeaders;
|
|
43
|
-
url: string;
|
|
44
|
-
queryParams?: HttpQueryParams;
|
|
45
|
-
maxRedirects?: number;
|
|
46
|
-
cacheTTL?: number;
|
|
47
|
-
context?: unknown;
|
|
48
|
-
}): Promise<HttpResponse<T>>;
|
|
49
|
-
post<P, T>({ headers, url, maxRedirects, cacheTTL, context, payload, }: {
|
|
50
|
-
headers?: HttpHeaders;
|
|
51
|
-
url: string;
|
|
52
|
-
maxRedirects?: number;
|
|
53
|
-
cacheTTL?: number;
|
|
54
|
-
context?: unknown;
|
|
55
|
-
payload?: P;
|
|
56
|
-
}): Promise<HttpResponse<T>>;
|
|
57
|
-
}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { IHttpClient, HttpResponse, HttpMethods, HttpParameters, HttpMethod, HttpHeaders, HttpQueryParams, } from './httpClient/types';
|
|
2
|
-
export { HttpClient } from './httpClient/httpClient';
|
|
3
|
-
export { HttpClientManager } from './httpClient/httpClientManager';
|
|
4
|
-
export { createAuthorizationHeaders } from './authorization/authorizationHeaders';
|
|
5
|
-
export { parseRequestParameters } from './parsers/requestParameters';
|
|
6
|
-
export { RequestParameter } from './parsers/types';
|
|
7
|
-
export { RequestClientFactory } from './requestClient/requestClientFactory';
|
|
8
|
-
export { RequestParameterLocations } from './parsers/types';
|
|
9
|
-
export { isSuccessStatusCode, isFailedStatusCode, isInfoStatusCode, } from './validators/statusCodes';
|
|
10
|
-
export { CustomErrorConfig } from './customErrors/types';
|
|
11
|
-
export { CUSTOM_ERROR_CONFIG_SCHEMA } from './customErrors/schemas';
|
|
12
|
-
export { HttpResponseError } from './errors/httpResponseError';
|
|
13
|
-
export { HttpErrorMessages } from './customErrors/types';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare const RequestParameterLocations: readonly ["query", "body", "headers"];
|
|
2
|
-
export type RequestParameterLocation = (typeof RequestParameterLocations)[number];
|
|
3
|
-
export type RequestParameter = {
|
|
4
|
-
name: string;
|
|
5
|
-
in: RequestParameterLocation;
|
|
6
|
-
value?: unknown;
|
|
7
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { CustomErrorConfig } from '../customErrors/types';
|
|
2
|
-
import { HttpMethod, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
3
|
-
import { IRequestClient } from './types';
|
|
4
|
-
export declare class RestClient implements IRequestClient {
|
|
5
|
-
performRequest({ httpClient, url, method, headers, body, customErrorConfigs, }: {
|
|
6
|
-
httpClient: IHttpClient;
|
|
7
|
-
url: string;
|
|
8
|
-
method: HttpMethod;
|
|
9
|
-
headers: Record<string, string>;
|
|
10
|
-
body: unknown;
|
|
11
|
-
customErrorConfigs?: CustomErrorConfig[];
|
|
12
|
-
}): Promise<HttpResponse>;
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { CustomErrorConfig } from '../customErrors/types';
|
|
2
|
-
import { HttpHeaders, HttpMethod, HttpQueryParams, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
3
|
-
export interface IRequestClient {
|
|
4
|
-
performRequest: ({ httpClient, url, method, headers, body, customErrorConfigs, }: {
|
|
5
|
-
httpClient: IHttpClient;
|
|
6
|
-
url: string;
|
|
7
|
-
method: HttpMethod;
|
|
8
|
-
headers: HttpHeaders;
|
|
9
|
-
queryParams?: HttpQueryParams;
|
|
10
|
-
body: unknown;
|
|
11
|
-
customErrorConfigs?: CustomErrorConfig[];
|
|
12
|
-
}) => Promise<HttpResponse>;
|
|
13
|
-
}
|
|
14
|
-
export type RequestClientType = 'rest' | 'soap';
|