Skip to content

Commit 9de8306

Browse files
authored
feat: SP-1376 Adds Http transport layer
* feat: SP-1376 Adds http transport layer
1 parent 12f68dd commit 9de8306

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scanoss",
3-
"version": "0.15.0",
3+
"version": "0.15.1",
44
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

Diff for: src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ export * from './sdk/Services/Grpc/CryptographyService';
3939
export * from './sdk/Services/Grpc/DependencyService';
4040
export { PurlRequest, EchoRequest } from './sdk/Services/Grpc/BaseService';
4141

42+
// *** Http ***//
43+
export * from './sdk/Services/http/HttpClient'
44+
4245
export { logger } from './sdk/Logger';

Diff for: src/sdk/Services/Transport/Transport.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import FormData from 'form-data';
2+
3+
export abstract class Transport<T1> {
4+
5+
abstract get(url: string): Promise<T1>;
6+
7+
abstract post(url: string, body: FormData): Promise<T1>;
8+
9+
abstract put(url: string, body: FormData): Promise<T1>;
10+
11+
abstract delete(url: string): Promise<T1>;
12+
13+
}

Diff for: src/sdk/Services/http/HttpClient.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import fetch, { Response } from 'node-fetch';
2+
import { ProxyAgent } from 'proxy-agent';
3+
import { Transport } from '../Transport/Transport';
4+
import { Utils } from '../../Utils/Utils';
5+
import FormData from 'form-data';
6+
export interface HttpProxyConfig {
7+
PAC_PROXY?: string;
8+
API_KEY?: string;
9+
NO_PROXY?: Array<string>;
10+
HTTP_PROXY?: string;
11+
HTTPS_PROXY?: string;
12+
IGNORE_CERT_ERRORS?: boolean;
13+
CA_CERT?: string;
14+
15+
}
16+
export class HttpClient extends Transport<Response> {
17+
18+
private readonly proxyAgent: ProxyAgent;
19+
private cfg: HttpProxyConfig;
20+
21+
constructor(cfg?: HttpProxyConfig){
22+
super();
23+
this.cfg = cfg;
24+
this.init();
25+
this.proxyAgent = new ProxyAgent();
26+
}
27+
28+
private init(){
29+
const PAC_URL = this.cfg?.PAC_PROXY ? `pac+${this.cfg.PAC_PROXY.trim()}` : null;
30+
const proxyConfig = {
31+
HTTP_PROXY: PAC_URL || this.cfg?.HTTP_PROXY || '',
32+
HTTPS_PROXY: PAC_URL || this.cfg?.HTTPS_PROXY || '',
33+
NO_PROXY: this.cfg?.NO_PROXY ? this.cfg?.NO_PROXY.join(',') : null,
34+
CA_CERT: this.cfg?.CA_CERT || null,
35+
IGNORE_CERT_ERRORS: this.cfg?.IGNORE_CERT_ERRORS || false
36+
}
37+
38+
process.env.NO_PROXY = process.env.NO_PROXY || proxyConfig.NO_PROXY
39+
process.env.HTTP_PROXY = process.env.HTTP_PROXY || proxyConfig.HTTP_PROXY
40+
process.env.HTTPS_PROXY = process.env.HTTPS_PROXY ||proxyConfig.HTTPS_PROXY
41+
if (proxyConfig.IGNORE_CERT_ERRORS ) {
42+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
43+
} else {
44+
process.env.NODE_TLS_REJECT_UNAUTHORIZED='1';
45+
}
46+
const caCertPath =
47+
proxyConfig.CA_CERT || process.env.NODE_EXTRA_CA_CERTS;
48+
49+
if (caCertPath) Utils.loadCaCertFromFile(caCertPath);
50+
}
51+
52+
async get(url: string): Promise<Response> {
53+
return await fetch(url, {
54+
agent: this.proxyAgent,
55+
method: 'get',
56+
headers: {
57+
...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }),
58+
},
59+
});
60+
}
61+
62+
async post(url: string, body: FormData): Promise<Response> {
63+
return await fetch(url, {
64+
agent: this.proxyAgent,
65+
method: 'post',
66+
body: body,
67+
headers: {
68+
...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY })
69+
},
70+
});
71+
}
72+
73+
async delete(url: string): Promise<Response> {
74+
return await fetch(url, {
75+
agent: this.proxyAgent,
76+
method: 'delete',
77+
headers: {
78+
...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY }),
79+
},
80+
});
81+
}
82+
83+
async put(url: string, body: FormData): Promise<Response> {
84+
return await fetch(url, {
85+
agent: this.proxyAgent,
86+
method: 'put',
87+
body: body,
88+
headers: {
89+
...(this.cfg.API_KEY && { 'X-Session': this.cfg.API_KEY })
90+
},
91+
});
92+
}
93+
94+
95+
}

0 commit comments

Comments
 (0)