Skip to content

Commit 960c7f8

Browse files
author
Ruslan Terekhov
committed
Started refactoring due to remote node-fetch dependency
1 parent ed149dd commit 960c7f8

10 files changed

Lines changed: 3873 additions & 129 deletions

File tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
},
2828
"homepage": "https://github.com/AsyncLegs/wayforpay-typescript#readme",
2929
"dependencies": {
30-
"lodash": "^4.17.11",
31-
"node-fetch": "^2.6.0"
30+
"lodash": "^4.17.11"
3231
},
3332
"devDependencies": {
3433
"@types/jest": "^24.0.13",

src/fields.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export enum HttpMethod {
2+
'POST' = 'POST',
3+
'PUT' = 'PUT',
4+
'PATCH' = 'PATCH',
5+
'DELETE' = 'DELETE',
6+
'HEAD' = 'HEAD',
7+
'OPTIONS' = 'OPTIONS',
8+
'GET' = 'GET'
9+
}

src/transport/enums/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './http.methods.enum';

src/transport/http.request.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as https from 'https';
2+
import { parse } from 'url';
3+
import { HttpMethod } from './enums';
4+
5+
export const request = (url: string, requestData: any) => {
6+
const { port, hostname, path } = parse(url);
7+
return new Promise((resolve, reject) => {
8+
const httpRequest = https.request(
9+
{
10+
headers: requestData.headers,
11+
hostname,
12+
method: requestData.method as HttpMethod,
13+
path,
14+
port
15+
},
16+
response => {
17+
if (
18+
response &&
19+
response.statusCode &&
20+
(response.statusCode < 200 || response.statusCode > 299)
21+
) {
22+
reject(
23+
new Error(
24+
`Request failed, status code: ${
25+
response.statusCode
26+
}, message is : ${response.statusMessage}`
27+
)
28+
);
29+
}
30+
31+
const body: any[] = [];
32+
response.on('data', chunk => body.push(chunk));
33+
response.on('end', () => resolve(body.join('')));
34+
response.on('error', err => reject(err));
35+
}
36+
);
37+
httpRequest.on('error', err => reject(err));
38+
if (requestData.body) {
39+
httpRequest.write(requestData.body);
40+
}
41+
httpRequest.end();
42+
});
43+
};

src/transport/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './http.request';

src/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ export const serialize = (obj: any) => {
22
const str = [];
33
for (const p in obj) {
44
if (obj.hasOwnProperty(p)) {
5-
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
5+
str.push(`${encodeURIComponent(p)}=${encodeURIComponent(obj[p])}`);
66
}
77
}
88
return str.join("&");
99
};
1010
export const propertyExists = (key: string, search: any) => {
11-
if (
12-
!search ||
13-
(search.constructor !== Array && search.constructor !== Object)
14-
) {
11+
if (!Array.isArray(search) && search.constructor !== Object) {
1512
return false;
1613
}
1714

0 commit comments

Comments
 (0)