-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
143 lines (134 loc) · 4.32 KB
/
index.ts
File metadata and controls
143 lines (134 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright (c) 2022 Skyflow, Inc.
*/
import { ContentType, SKY_METADATA_HEADER } from '../../utils/constants';
import logs from '../../utils/logs';
import SkyflowError from '../../utils/skyflow-error';
import SKYFLOW_ERROR_CODE from '../../utils/skyflow-error-code';
import { getMetaObject } from '../../utils/helpers';
import { Platform } from 'react-native';
import { sdkDetails } from '../../sdkDetails';
export interface IClientRequest {
body?: any;
headers?: Record<string, string>;
requestMethod:
| 'GET'
| 'HEAD'
| 'POST'
| 'PUT'
| 'DELETE'
| 'CONNECT'
| 'OPTIONS'
| 'PATCH';
url: string;
}
class Client {
constructor() {}
request = (request: IClientRequest) => {
return new Promise((resolve, reject) => {
const httpRequest = new XMLHttpRequest();
if (!httpRequest) {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.CONNECTION_ERROR, [], true));
return;
}
httpRequest.open(request.requestMethod, request.url);
if (request.headers) {
const metaDataObject = getMetaObject(Platform, sdkDetails);
request.headers[SKY_METADATA_HEADER] = JSON.stringify(metaDataObject);
const { headers } = request;
Object.keys(request.headers).forEach((key) => {
if (
!(
key === 'content-type' &&
headers[key] &&
headers[key]?.includes(ContentType.FORMDATA)
)
) {
httpRequest.setRequestHeader(key, headers[key] as string);
}
});
}
if (request.body) {
if (
request.headers?.['content-type']?.includes(
ContentType.FORMURLENCODED
) ||
request.headers?.['content-type']?.includes(ContentType.FORMDATA)
) {
httpRequest.send(request.body);
} else {
httpRequest.send(JSON.stringify({ ...request.body }));
}
} else {
httpRequest.send();
}
httpRequest.onload = () => {
const responseHeaders = httpRequest.getAllResponseHeaders();
const headersList = responseHeaders.trim().split(/[\r\n]+/);
const headerMap: Record<string, string> = {};
headersList.forEach((line) => {
const parts = line.split(': ');
const header = parts.shift()?.toLowerCase() || '';
const value = parts.join(': ');
headerMap[header] = value;
});
const contentType = headerMap['content-type'];
const requestId = headerMap['x-request-id'];
if (httpRequest.status < 200 || httpRequest.status >= 400) {
if (contentType && contentType.includes('application/json')) {
let description = JSON.parse(httpRequest.response);
if (description?.error?.message) {
description = requestId
? `${description?.error?.message} - requestId: ${requestId}`
: description?.error?.message;
}
reject(
new SkyflowError(
{
code: httpRequest.status,
description,
},
[],
true
)
);
} else if (contentType && contentType.includes('text/plain')) {
reject(
new SkyflowError(
{
code: httpRequest.status,
description: requestId
? `${httpRequest.response} - requestId: ${requestId}`
: httpRequest.response,
},
[],
true
)
);
} else {
reject(
new SkyflowError(
{
code: httpRequest.status,
description: requestId
? `${logs.errorLogs.ERROR_OCCURED} - requestId: ${requestId}`
: logs.errorLogs.ERROR_OCCURED,
},
[],
true
)
);
}
}
if (contentType && contentType.includes('application/json')) {
resolve(JSON.parse(httpRequest.response));
}
resolve(httpRequest.response);
};
httpRequest.onerror = () => {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.NETWORK_ERROR, [], true));
};
});
};
}
export default Client;