@stackone/transport 1.0.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/README.md +50 -0
- package/dist/httpClient.d.ts +36 -0
- package/dist/httpClientManager.d.ts +10 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.es.mjs +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +50 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @stackone/transport
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
This package contains the transport layer and communication utils used by the **StackOne** packages.
|
|
6
|
+
|
|
7
|
+
This package uses the tech stack provided by the **Connect Monorepo** global setup. Please check the [root README](../../README.md) for more information.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
Please check the [root README](../../README.md) for requirements.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# install dependencies
|
|
17
|
+
$ npm run install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Available commands
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# clean build output
|
|
24
|
+
$ npm run clean
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# build package
|
|
29
|
+
$ npm run build
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# run tests
|
|
34
|
+
$ npm run test
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# run tests on watch mode
|
|
39
|
+
$ npm run test:watch
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# run linter
|
|
44
|
+
$ npm run lint
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# run linter and try to fix any error
|
|
49
|
+
$ npm run lint:fix
|
|
50
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
import { HttpHeaders, HttpResponse, IHttpClient, RequestContext, HttpMethod } from './types';
|
|
4
|
+
import https from 'https';
|
|
5
|
+
export declare class HttpClient implements IHttpClient {
|
|
6
|
+
#private;
|
|
7
|
+
constructor({ transportInstance, }?: {
|
|
8
|
+
transportInstance?: AxiosInstance;
|
|
9
|
+
});
|
|
10
|
+
request<P, T>({ headers, url, method, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
11
|
+
headers?: HttpHeaders;
|
|
12
|
+
url: string;
|
|
13
|
+
method?: HttpMethod;
|
|
14
|
+
maxRedirects?: number;
|
|
15
|
+
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
16
|
+
cacheTTL?: number;
|
|
17
|
+
context?: RequestContext;
|
|
18
|
+
payload?: P;
|
|
19
|
+
httpsAgent?: https.Agent;
|
|
20
|
+
}): Promise<HttpResponse<T>>;
|
|
21
|
+
get<T>({ headers, url, maxRedirects, cacheTTL, context, }: {
|
|
22
|
+
headers?: HttpHeaders;
|
|
23
|
+
url: string;
|
|
24
|
+
maxRedirects?: number;
|
|
25
|
+
cacheTTL?: number;
|
|
26
|
+
context?: RequestContext;
|
|
27
|
+
}): Promise<HttpResponse<T>>;
|
|
28
|
+
post<P, T>({ headers, url, maxRedirects, cacheTTL, context, payload, }: {
|
|
29
|
+
headers?: HttpHeaders;
|
|
30
|
+
url: string;
|
|
31
|
+
maxRedirects?: number;
|
|
32
|
+
cacheTTL?: number;
|
|
33
|
+
context?: RequestContext;
|
|
34
|
+
payload?: P;
|
|
35
|
+
}): Promise<HttpResponse<T>>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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 {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"axios";import e from"qs";const r=["get","post","put","delete","patch","job"];function s(t,e,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(t):s?s.value:e.get(t)}var n,a,o,c,i,h;"function"==typeof SuppressedError&&SuppressedError;const p=()=>t.create({maxBodyLength:1/0});class d{constructor({transportInstance:t=p()}={}){n.add(this),a.set(this,void 0),function(t,e,r,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===s?n.call(t,r):n?n.value=r:e.set(t,r)}(this,a,t,"f")}async request({headers:t={},url:e,method:r="get",maxRedirects:i=0,responseType:p,cacheTTL:d,context:l,payload:u,httpsAgent:f}){try{const d=s(this,n,"m",o).call(this,t),l=await s(this,a,"f").request({headers:d,url:e,method:r,maxRedirects:i,responseType:p,data:s(this,n,"m",h).call(this,u,d),httpsAgent:f});return{data:l.data,status:l.status,headers:s(this,n,"m",c).call(this,l.headers),requestUrl:e,responseType:l?.config?.responseType,responseTime:new Date}}catch(t){throw t}}async get({headers:t,url:e,maxRedirects:r,cacheTTL:s,context:n}){return this.request({url:e,method:"get",headers:t,maxRedirects:r,cacheTTL:s,context:n})}async post({headers:t,url:e,maxRedirects:r,cacheTTL:s,context:n,payload:a}){return this.request({url:e,method:"post",headers:t,maxRedirects:r,cacheTTL:s,context:n,payload:a})}}a=new WeakMap,n=new WeakSet,o=function(t){if(!t)return{};const e={};return Object.keys(t).forEach((r=>{e[r.toLowerCase()]=t[r]||""})),e},c=function(t){if(!t)return{};const e={};return Object.keys(t).forEach((r=>{e[r]=t[r]||""})),e},i=function(t){return"application/x-www-form-urlencoded"===t["content-type"]},h=function(t,r){if(t)return s(this,n,"m",i).call(this,r)?e.stringify(t):t};const l=()=>new d;class u{static async getInstance({getHttpClient:t=l}={}){return this.httpClientInstance||(this.httpClientInstance=t()),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}u.httpClientInstance=null;export{d as HttpClient,u as HttpClientManager,r as HttpMethods};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("axios"),e=require("qs");function r(t,e,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(t):s?s.value:e.get(t)}var s,a,n,o,c,i;"function"==typeof SuppressedError&&SuppressedError;const h=()=>t.create({maxBodyLength:1/0});class p{constructor({transportInstance:t=h()}={}){s.add(this),a.set(this,void 0),function(t,e,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 e?t!==e||!a:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===s?a.call(t,r):a?a.value=r:e.set(t,r)}(this,a,t,"f")}async request({headers:t={},url:e,method:c="get",maxRedirects:h=0,responseType:p,cacheTTL:u,context:d,payload:l,httpsAgent:f}){try{const u=r(this,s,"m",n).call(this,t),d=await r(this,a,"f").request({headers:u,url:e,method:c,maxRedirects:h,responseType:p,data:r(this,s,"m",i).call(this,l,u),httpsAgent:f});return{data:d.data,status:d.status,headers:r(this,s,"m",o).call(this,d.headers),requestUrl:e,responseType:d?.config?.responseType,responseTime:new Date}}catch(t){throw t}}async get({headers:t,url:e,maxRedirects:r,cacheTTL:s,context:a}){return this.request({url:e,method:"get",headers:t,maxRedirects:r,cacheTTL:s,context:a})}async post({headers:t,url:e,maxRedirects:r,cacheTTL:s,context:a,payload:n}){return this.request({url:e,method:"post",headers:t,maxRedirects:r,cacheTTL:s,context:a,payload:n})}}a=new WeakMap,s=new WeakSet,n=function(t){if(!t)return{};const e={};return Object.keys(t).forEach((r=>{e[r.toLowerCase()]=t[r]||""})),e},o=function(t){if(!t)return{};const e={};return Object.keys(t).forEach((r=>{e[r]=t[r]||""})),e},c=function(t){return"application/x-www-form-urlencoded"===t["content-type"]},i=function(t,a){if(t)return r(this,s,"m",c).call(this,a)?e.stringify(t):t};const u=()=>new p;class d{static async getInstance({getHttpClient:t=u}={}){return this.httpClientInstance||(this.httpClientInstance=t()),this.httpClientInstance}static resetInstance(){this.httpClientInstance=null}}d.httpClientInstance=null,exports.HttpClient=p,exports.HttpClientManager=d,exports.HttpMethods=["get","post","put","delete","patch","job"];
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import https from 'https';
|
|
3
|
+
export interface HttpHeaders {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
}
|
|
6
|
+
export interface CertOptions {
|
|
7
|
+
certs?: string[];
|
|
8
|
+
keys?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare const HttpMethods: readonly ["get", "post", "put", "delete", "patch", "job"];
|
|
11
|
+
export type HttpMethod = (typeof HttpMethods)[number];
|
|
12
|
+
export type RequestContext = {
|
|
13
|
+
accountId: string;
|
|
14
|
+
};
|
|
15
|
+
export type HttpResponse<T = any> = {
|
|
16
|
+
data: T;
|
|
17
|
+
status: number;
|
|
18
|
+
headers: HttpHeaders;
|
|
19
|
+
requestUrl: string;
|
|
20
|
+
responseType?: string;
|
|
21
|
+
responseTime?: Date;
|
|
22
|
+
};
|
|
23
|
+
export interface IHttpClient {
|
|
24
|
+
request<P, T>({ headers, url, method, maxRedirects, responseType, cacheTTL, context, payload, httpsAgent, }: {
|
|
25
|
+
headers?: HttpHeaders;
|
|
26
|
+
url: string;
|
|
27
|
+
method?: HttpMethod;
|
|
28
|
+
maxRedirects?: number;
|
|
29
|
+
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
30
|
+
cacheTTL?: number;
|
|
31
|
+
context?: RequestContext;
|
|
32
|
+
payload?: P;
|
|
33
|
+
httpsAgent?: https.Agent;
|
|
34
|
+
}): Promise<HttpResponse<T>>;
|
|
35
|
+
get<T>({ headers, url, maxRedirects, cacheTTL, context, }: {
|
|
36
|
+
headers?: HttpHeaders;
|
|
37
|
+
url: string;
|
|
38
|
+
maxRedirects?: number;
|
|
39
|
+
cacheTTL?: number;
|
|
40
|
+
context?: RequestContext;
|
|
41
|
+
}): Promise<HttpResponse<T>>;
|
|
42
|
+
post<P, T>({ headers, url, maxRedirects, cacheTTL, context, payload, }: {
|
|
43
|
+
headers?: HttpHeaders;
|
|
44
|
+
url: string;
|
|
45
|
+
maxRedirects?: number;
|
|
46
|
+
cacheTTL?: number;
|
|
47
|
+
context?: RequestContext;
|
|
48
|
+
payload?: P;
|
|
49
|
+
}): Promise<HttpResponse<T>>;
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stackone/transport",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.es.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"package.json",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rimraf dist",
|
|
15
|
+
"prebuild": "npm run clean",
|
|
16
|
+
"build": "rollup -c",
|
|
17
|
+
"lint": "tsc --project tsconfig.json && eslint \"src/**/*.ts\"",
|
|
18
|
+
"lint:fix": "npm run lint -- --fix",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"test:watch": "vitest watch",
|
|
21
|
+
"publish": "npm publish --access=public"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [],
|
|
24
|
+
"author": "StackOne",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"axios": "^1.6.2",
|
|
28
|
+
"qs": "^6.11.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/qs": "^6.9.10"
|
|
32
|
+
}
|
|
33
|
+
}
|