@stackone/transport 1.1.3 → 1.3.0
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.es.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/types/authorization/authorizationHeaders.d.ts +4 -0
- package/dist/types/authorization/types.d.ts +12 -0
- package/dist/types/{httpClient.d.ts → httpClient/httpClient.d.ts} +5 -3
- package/dist/types/{types.d.ts → httpClient/types.d.ts} +14 -4
- package/dist/types/index.d.ts +8 -3
- package/dist/types/parsers/requestParameters.d.ts +3 -0
- package/dist/types/parsers/types.d.ts +7 -0
- package/dist/types/requestClient/requestClientFactory.d.ts +4 -0
- package/dist/types/requestClient/restClient.d.ts +11 -0
- package/dist/types/requestClient/types.d.ts +12 -0
- package/package.json +6 -6
- /package/dist/types/{httpClientManager.d.ts → httpClient/httpClientManager.d.ts} +0 -0
package/dist/index.es.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import e from"axios";import t from"qs";const r=["get","post","put","delete","patch"];function a(e,t,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(e):a?a.value:t.get(e)}var n,s,o,c,i,u;"function"==typeof SuppressedError&&SuppressedError;const h=()=>e.create({maxBodyLength:1/0});class d{constructor({transportInstance:e=h()}={}){n.add(this),s.set(this,void 0),function(e,t,r,a,n){if("m"===a)throw new TypeError("Private method is not writable");if("a"===a&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===a?n.call(e,r):n?n.value=r:t.set(e,r)}(this,s,e,"f")}async request({headers:e={},url:t,method:r="get",queryParams:i,maxRedirects:h=0,responseType:d,cacheTTL:l,context:p,payload:m,httpsAgent:y}){try{const l=new URLSearchParams(i).toString(),p=a(this,n,"m",o).call(this,e),f=await a(this,s,"f").request({headers:p,url:l?`${t}?${l}`:t,method:r,maxRedirects:h,responseType:d,data:a(this,n,"m",u).call(this,m,p),httpsAgent:y});return{data:f.data,status:f.status,headers:a(this,n,"m",c).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:a,cacheTTL:n,context:s}){return this.request({url:t,method:"get",queryParams:r,headers:e,maxRedirects:a,cacheTTL:n,context:s})}async post({headers:e,url:t,maxRedirects:r,cacheTTL:a,context:n,payload:s}){return this.request({url:t,method:"post",headers:e,maxRedirects:r,cacheTTL:a,context:n,payload:s})}}s=new WeakMap,n=new WeakSet,o=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r.toLowerCase()]=e[r]||""})),t},c=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]||""})),t},i=function(e){return"application/x-www-form-urlencoded"===e["content-type"]},u=function(e,r){if(e)return a(this,n,"m",i).call(this,r)?t.stringify(e):e};const l=()=>new d;class p{static async getInstance({getHttpClient:e=l}={}){return this.httpClientInstance??=e(),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}p.httpClientInstance=null;const m=e=>{switch(e.type){case"basic":return y(e);case"bearer":return f(e);default:throw new Error("Invalid authentication type")}},y=({username:e,password:t,encoding:r="base64"})=>{const a=r;return{authorization:`Basic ${Buffer.from(`${e}:${t}`).toString(a)}`}},f=({token:e,includeBearer:t=!0})=>({authorization:`${t?"Bearer ":""}${e}`}),w=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:{}});class T{async performRequest({httpClient:e,url:t,method:r,headers:a,body:n}){return e?.request({method:r,url:t,headers:a,maxRedirects:0,payload:n})}}class b{static build(e="rest"){if("rest"===e)return new T;throw new Error(`Unknown request client type: ${e}`)}}const g=["query","body","headers"];export{d as HttpClient,p as HttpClientManager,r as HttpMethods,b as RequestClientFactory,g as RequestParameterLocations,m as createAuthorizationHeaders,w as parseRequestParameters};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var e=require("axios"),t=require("qs");function r(e,t,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(e):a?a.value:t.get(e)}var a,s,n,o,i,c;"function"==typeof SuppressedError&&SuppressedError;const u=()=>e.create({maxBodyLength:1/0});class h{constructor({transportInstance:e=u()}={}){a.add(this),s.set(this,void 0),function(e,t,r,a,s){if("m"===a)throw new TypeError("Private method is not writable");if("a"===a&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===a?s.call(e,r):s?s.value=r:t.set(e,r)}(this,s,e,"f")}async request({headers:e={},url:t,method:i="get",queryParams:u,maxRedirects:h=0,responseType:d,cacheTTL:p,context:l,payload:m,httpsAgent:y}){try{const p=new URLSearchParams(u).toString(),l=r(this,a,"m",n).call(this,e),f=await r(this,s,"f").request({headers:l,url:p?`${t}?${p}`:t,method:i,maxRedirects:h,responseType:d,data:r(this,a,"m",c).call(this,m,l),httpsAgent:y});return{data:f.data,status:f.status,headers:r(this,a,"m",o).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:a,cacheTTL:s,context:n}){return this.request({url:t,method:"get",queryParams:r,headers:e,maxRedirects:a,cacheTTL:s,context:n})}async post({headers:e,url:t,maxRedirects:r,cacheTTL:a,context:s,payload:n}){return this.request({url:t,method:"post",headers:e,maxRedirects:r,cacheTTL:a,context:s,payload:n})}}s=new WeakMap,a=new WeakSet,n=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r.toLowerCase()]=e[r]||""})),t},o=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]||""})),t},i=function(e){return"application/x-www-form-urlencoded"===e["content-type"]},c=function(e,s){if(e)return r(this,a,"m",i).call(this,s)?t.stringify(e):e};const d=()=>new h;class p{static async getInstance({getHttpClient:e=d}={}){return this.httpClientInstance??=e(),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}p.httpClientInstance=null;const l=({username:e,password:t,encoding:r="base64"})=>{const a=r;return{authorization:`Basic ${Buffer.from(`${e}:${t}`).toString(a)}`}},m=({token:e,includeBearer:t=!0})=>({authorization:`${t?"Bearer ":""}${e}`});class y{async performRequest({httpClient:e,url:t,method:r,headers:a,body:s}){return e?.request({method:r,url:t,headers:a,maxRedirects:0,payload:s})}}exports.HttpClient=h,exports.HttpClientManager=p,exports.HttpMethods=["get","post","put","delete","patch"],exports.RequestClientFactory=class{static build(e="rest"){if("rest"===e)return new y;throw new Error(`Unknown request client type: ${e}`)}},exports.RequestParameterLocations=["query","body","headers"],exports.createAuthorizationHeaders=e=>{switch(e.type){case"basic":return l(e);case"bearer":return m(e);default:throw new Error("Invalid authentication type")}},exports.parseRequestParameters=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:{}});
|
|
@@ -0,0 +1,12 @@
|
|
|
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,15 +1,16 @@
|
|
|
1
1
|
import https from 'https';
|
|
2
2
|
import { AxiosInstance } from 'axios';
|
|
3
|
-
import { HttpHeaders, HttpMethod, HttpResponse, IHttpClient } from './types';
|
|
3
|
+
import { HttpHeaders, HttpMethod, HttpQueryParams, HttpResponse, IHttpClient } from './types';
|
|
4
4
|
export declare class HttpClient implements IHttpClient {
|
|
5
5
|
#private;
|
|
6
6
|
constructor({ transportInstance, }?: {
|
|
7
7
|
transportInstance?: AxiosInstance;
|
|
8
8
|
});
|
|
9
|
-
request<P, T>({ headers, url, method, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
9
|
+
request<P, T>({ headers, url, method, queryParams, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
10
10
|
headers?: HttpHeaders;
|
|
11
11
|
url: string;
|
|
12
12
|
method?: HttpMethod;
|
|
13
|
+
queryParams?: HttpQueryParams;
|
|
13
14
|
maxRedirects?: number;
|
|
14
15
|
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
15
16
|
cacheTTL?: number;
|
|
@@ -17,9 +18,10 @@ export declare class HttpClient implements IHttpClient {
|
|
|
17
18
|
payload?: P;
|
|
18
19
|
httpsAgent?: https.Agent;
|
|
19
20
|
}): Promise<HttpResponse<T>>;
|
|
20
|
-
get<T>({ headers, url, maxRedirects, cacheTTL, context, }: {
|
|
21
|
+
get<T>({ headers, url, queryParams, maxRedirects, cacheTTL, context, }: {
|
|
21
22
|
headers?: HttpHeaders;
|
|
22
23
|
url: string;
|
|
24
|
+
queryParams?: HttpQueryParams;
|
|
23
25
|
maxRedirects?: number;
|
|
24
26
|
cacheTTL?: number;
|
|
25
27
|
context?: unknown;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import https from 'https';
|
|
2
|
-
export
|
|
2
|
+
export type HttpHeaders = {
|
|
3
3
|
[key: string]: string;
|
|
4
|
-
}
|
|
4
|
+
};
|
|
5
|
+
export type HttpQueryParams = {
|
|
6
|
+
[key: string]: string;
|
|
7
|
+
};
|
|
5
8
|
export interface CertOptions {
|
|
6
9
|
certs?: string[];
|
|
7
10
|
keys?: string[];
|
|
@@ -16,11 +19,17 @@ export type HttpResponse<T = any> = {
|
|
|
16
19
|
responseType?: string;
|
|
17
20
|
responseTime?: Date;
|
|
18
21
|
};
|
|
22
|
+
export type HttpParameters = {
|
|
23
|
+
query: HttpQueryParams;
|
|
24
|
+
body: Record<string, unknown>;
|
|
25
|
+
headers: HttpHeaders;
|
|
26
|
+
};
|
|
19
27
|
export interface IHttpClient {
|
|
20
|
-
request<P, T>({ headers, url, method, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
28
|
+
request<P, T>({ headers, url, method, queryParams, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
21
29
|
headers?: HttpHeaders;
|
|
22
30
|
url: string;
|
|
23
31
|
method?: HttpMethod;
|
|
32
|
+
queryParams?: HttpQueryParams;
|
|
24
33
|
maxRedirects?: number;
|
|
25
34
|
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
26
35
|
cacheTTL?: number;
|
|
@@ -28,9 +37,10 @@ export interface IHttpClient {
|
|
|
28
37
|
payload?: P;
|
|
29
38
|
httpsAgent?: https.Agent;
|
|
30
39
|
}): Promise<HttpResponse<T>>;
|
|
31
|
-
get<T>({ headers, url, maxRedirects, cacheTTL, context, }: {
|
|
40
|
+
get<T>({ headers, url, queryParams, maxRedirects, cacheTTL, context, }: {
|
|
32
41
|
headers?: HttpHeaders;
|
|
33
42
|
url: string;
|
|
43
|
+
queryParams?: HttpQueryParams;
|
|
34
44
|
maxRedirects?: number;
|
|
35
45
|
cacheTTL?: number;
|
|
36
46
|
context?: unknown;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
export { IHttpClient, HttpResponse, HttpMethods, HttpMethod } from './types';
|
|
2
|
-
export { HttpClient } from './httpClient';
|
|
3
|
-
export { HttpClientManager } from './httpClientManager';
|
|
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';
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HttpMethod, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
2
|
+
import { IRequestClient } from './types';
|
|
3
|
+
export declare class RestClient implements IRequestClient {
|
|
4
|
+
performRequest({ httpClient, url, method, headers, body, }: {
|
|
5
|
+
httpClient: IHttpClient;
|
|
6
|
+
url: string;
|
|
7
|
+
method: HttpMethod;
|
|
8
|
+
headers: Record<string, string>;
|
|
9
|
+
body: unknown;
|
|
10
|
+
}): Promise<HttpResponse>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HttpHeaders, HttpMethod, HttpQueryParams, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
2
|
+
export interface IRequestClient {
|
|
3
|
+
performRequest: ({ httpClient, url, method, headers, body, }: {
|
|
4
|
+
httpClient: IHttpClient;
|
|
5
|
+
url: string;
|
|
6
|
+
method: HttpMethod;
|
|
7
|
+
headers: HttpHeaders;
|
|
8
|
+
queryParams?: HttpQueryParams;
|
|
9
|
+
body: unknown;
|
|
10
|
+
}) => Promise<HttpResponse>;
|
|
11
|
+
}
|
|
12
|
+
export type RequestClientType = 'rest' | 'soap';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackone/transport",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.es.mjs",
|
|
@@ -24,18 +24,18 @@
|
|
|
24
24
|
"code:check:fix": "biome check --write ./src ./*.mjs",
|
|
25
25
|
"lint": "npm run code:check",
|
|
26
26
|
"lint:fix": "npm run code:check:fix",
|
|
27
|
-
"test": "vitest run",
|
|
28
|
-
"test:watch": "vitest watch",
|
|
27
|
+
"test": "FORCE_COLOR=1 vitest run",
|
|
28
|
+
"test:watch": "FORCE_COLOR=1 vitest watch --silent",
|
|
29
29
|
"publish-release": "npm publish --access=public"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [],
|
|
32
32
|
"author": "StackOne",
|
|
33
33
|
"license": "ISC",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"axios": "
|
|
36
|
-
"qs": "
|
|
35
|
+
"axios": "1.8.4",
|
|
36
|
+
"qs": "6.14.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/qs": "
|
|
39
|
+
"@types/qs": "6.9.18"
|
|
40
40
|
}
|
|
41
41
|
}
|
|
File without changes
|