@stackone/transport 1.4.1 → 1.5.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/customErrors/index.d.ts +3 -0
- package/dist/types/customErrors/schemas.d.ts +17 -0
- package/dist/types/customErrors/types.d.ts +3 -0
- package/dist/types/httpClient/types.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/requestClient/restClient.d.ts +3 -1
- package/dist/types/requestClient/types.d.ts +3 -1
- package/package.json +3 -2
package/dist/index.es.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"axios";import t from"qs";import{
|
|
1
|
+
import e from"axios";import t from"qs";import{safeEvaluate as r}from"@stackone/expressions";import{isMissing as a,notMissing as n}from"@stackone/utils";import{z as s}from"zod";const o=["get","post","put","delete","patch"];function i(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 c,u,h,d,l,p;"function"==typeof SuppressedError&&SuppressedError;const m=()=>e.create({maxBodyLength:1/0});class f{constructor({transportInstance:e=m()}={}){c.add(this),u.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,u,e,"f")}async request({headers:e={},url:t,method:r="get",queryParams:a,maxRedirects:n=0,responseType:s,cacheTTL:o,context:l,payload:m,httpsAgent:f}){try{const o=new URLSearchParams(a).toString(),l=i(this,c,"m",h).call(this,e),y=await i(this,u,"f").request({headers:l,url:o?`${t}?${o}`:t,method:r,maxRedirects:n,responseType:s,data:i(this,c,"m",p).call(this,m,l),httpsAgent:f});return{data:y.data,status:y.status,headers:i(this,c,"m",d).call(this,y.headers),requestUrl:t,responseType:y?.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})}}u=new WeakMap,c=new WeakSet,h=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r.toLowerCase()]=e[r]||""})),t},d=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]||""})),t},l=function(e){return"application/x-www-form-urlencoded"===e["content-type"]},p=function(e,r){if(e)return i(this,c,"m",l).call(this,r)?t.stringify(e):e};const y=()=>new f;class w{static async getInstance({getHttpClient:e=y}={}){return this.httpClientInstance??=e(),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}w.httpClientInstance=null;const g=e=>{switch(e.type){case"basic":return b(e);case"bearer":return T(e);default:throw new Error("Invalid authentication type")}},b=({username:e="",password:t="",encoding:r="base64"})=>{const a=r;return{authorization:`Basic ${Buffer.from(`${e}:${t}`).toString(a)}`}},T=({token:e,includeBearer:t=!0})=>({authorization:`${t?"Bearer ":""}${e}`}),v=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 x{async performRequest({httpClient:e,url:t,method:n,headers:s,body:o,customErrorConfigs:i}){return((e,t)=>{if(a(t)||0===t.length||a(e))return e;const n=t?.filter((t=>t.receivedStatus===e.status));if(n?.length)for(const t of n){const n=t.condition,s=r(n,e);if(a(n)||s)return{...e,status:t.targetStatus,message:t?.message}}return e})(await(e?.request({method:n,url:t,headers:s,maxRedirects:0,payload:o})),i)}}class q{static build(e="rest"){if("rest"===e)return new x;throw new Error(`Unknown request client type: ${e}`)}}const E=["query","body","headers"],S=e=>n(e)&&e>=200&&e<=299,C=e=>n(e)&&e>=400&&e<=599,R=e=>n(e)&&e>=100&&e<=199,k=s.object({receivedStatus:s.number(),targetStatus:s.number(),message:s.string().optional(),condition:s.string().optional()});export{k as CUSTOM_ERROR_CONFIG_SCHEMA,f as HttpClient,w as HttpClientManager,o as HttpMethods,q as RequestClientFactory,E as RequestParameterLocations,g as createAuthorizationHeaders,C as isFailedStatusCode,R as isInfoStatusCode,S as isSuccessStatusCode,v as parseRequestParameters};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("axios"),t=require("qs"),r=require("@stackone/utils");function
|
|
1
|
+
"use strict";var e=require("axios"),t=require("qs"),r=require("@stackone/expressions"),s=require("@stackone/utils"),a=require("zod");function n(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 o,i,c,u,h,d;"function"==typeof SuppressedError&&SuppressedError;const l=()=>e.create({maxBodyLength:1/0});class p{constructor({transportInstance:e=l()}={}){o.add(this),i.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,i,e,"f")}async request({headers:e={},url:t,method:r="get",queryParams:s,maxRedirects:a=0,responseType:h,cacheTTL:l,context:p,payload:m,httpsAgent:f}){try{const l=new URLSearchParams(s).toString(),p=n(this,o,"m",c).call(this,e),y=await n(this,i,"f").request({headers:p,url:l?`${t}?${l}`:t,method:r,maxRedirects:a,responseType:h,data:n(this,o,"m",d).call(this,m,p),httpsAgent:f});return{data:y.data,status:y.status,headers:n(this,o,"m",u).call(this,y.headers),requestUrl:t,responseType:y?.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})}}i=new WeakMap,o=new WeakSet,c=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r.toLowerCase()]=e[r]||""})),t},u=function(e){if(!e)return{};const t={};return Object.keys(e).forEach((r=>{t[r]=e[r]||""})),t},h=function(e){return"application/x-www-form-urlencoded"===e["content-type"]},d=function(e,r){if(e)return n(this,o,"m",h).call(this,r)?t.stringify(e):e};const m=()=>new p;class f{static async getInstance({getHttpClient:e=m}={}){return this.httpClientInstance??=e(),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}f.httpClientInstance=null;const y=({username:e="",password:t="",encoding:r="base64"})=>{const s=r;return{authorization:`Basic ${Buffer.from(`${e}:${t}`).toString(s)}`}},w=({token:e,includeBearer:t=!0})=>({authorization:`${t?"Bearer ":""}${e}`});class g{async performRequest({httpClient:e,url:t,method:a,headers:n,body:o,customErrorConfigs:i}){return((e,t)=>{if(s.isMissing(t)||0===t.length||s.isMissing(e))return e;const a=t?.filter((t=>t.receivedStatus===e.status));if(a?.length)for(const t of a){const a=t.condition,n=r.safeEvaluate(a,e);if(s.isMissing(a)||n)return{...e,status:t.targetStatus,message:t?.message}}return e})(await(e?.request({method:a,url:t,headers:n,maxRedirects:0,payload:o})),i)}}const x=a.z.object({receivedStatus:a.z.number(),targetStatus:a.z.number(),message:a.z.string().optional(),condition:a.z.string().optional()});exports.CUSTOM_ERROR_CONFIG_SCHEMA=x,exports.HttpClient=p,exports.HttpClientManager=f,exports.HttpMethods=["get","post","put","delete","patch"],exports.RequestClientFactory=class{static build(e="rest"){if("rest"===e)return new g;throw new Error(`Unknown request client type: ${e}`)}},exports.RequestParameterLocations=["query","body","headers"],exports.createAuthorizationHeaders=e=>{switch(e.type){case"basic":return y(e);case"bearer":return w(e);default:throw new Error("Invalid authentication type")}},exports.isFailedStatusCode=e=>s.notMissing(e)&&e>=400&&e<=599,exports.isInfoStatusCode=e=>s.notMissing(e)&&e>=100&&e<=199,exports.isSuccessStatusCode=e=>s.notMissing(e)&&e>=200&&e<=299,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,17 @@
|
|
|
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
|
+
}>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,3 +7,5 @@ export { RequestParameter } from './parsers/types';
|
|
|
7
7
|
export { RequestClientFactory } from './requestClient/requestClientFactory';
|
|
8
8
|
export { RequestParameterLocations } from './parsers/types';
|
|
9
9
|
export { isSuccessStatusCode, isFailedStatusCode, isInfoStatusCode, } from './validators/statusCodes';
|
|
10
|
+
export { CustomErrorConfig } from './customErrors/types';
|
|
11
|
+
export { CUSTOM_ERROR_CONFIG_SCHEMA } from './customErrors/schemas';
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { CustomErrorConfig } from '../customErrors/types';
|
|
1
2
|
import { HttpMethod, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
2
3
|
import { IRequestClient } from './types';
|
|
3
4
|
export declare class RestClient implements IRequestClient {
|
|
4
|
-
performRequest({ httpClient, url, method, headers, body, }: {
|
|
5
|
+
performRequest({ httpClient, url, method, headers, body, customErrorConfigs, }: {
|
|
5
6
|
httpClient: IHttpClient;
|
|
6
7
|
url: string;
|
|
7
8
|
method: HttpMethod;
|
|
8
9
|
headers: Record<string, string>;
|
|
9
10
|
body: unknown;
|
|
11
|
+
customErrorConfigs?: CustomErrorConfig[];
|
|
10
12
|
}): Promise<HttpResponse>;
|
|
11
13
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import { CustomErrorConfig } from '../customErrors/types';
|
|
1
2
|
import { HttpHeaders, HttpMethod, HttpQueryParams, HttpResponse, IHttpClient } from '../httpClient/types';
|
|
2
3
|
export interface IRequestClient {
|
|
3
|
-
performRequest: ({ httpClient, url, method, headers, body, }: {
|
|
4
|
+
performRequest: ({ httpClient, url, method, headers, body, customErrorConfigs, }: {
|
|
4
5
|
httpClient: IHttpClient;
|
|
5
6
|
url: string;
|
|
6
7
|
method: HttpMethod;
|
|
7
8
|
headers: HttpHeaders;
|
|
8
9
|
queryParams?: HttpQueryParams;
|
|
9
10
|
body: unknown;
|
|
11
|
+
customErrorConfigs?: CustomErrorConfig[];
|
|
10
12
|
}) => Promise<HttpResponse>;
|
|
11
13
|
}
|
|
12
14
|
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.5.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.es.mjs",
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"axios": "1.8.4",
|
|
36
36
|
"qs": "6.14.0",
|
|
37
|
-
"@stackone/utils": "*"
|
|
37
|
+
"@stackone/utils": "*",
|
|
38
|
+
"@stackone/expressions": "*"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"@types/qs": "6.9.18"
|