dff-util 1.1.2 → 1.1.3

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/main/http.js CHANGED
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Http = exports.BaseHeaders = exports.HttpHeaders = exports.RespType = void 0;
13
+ const util_1 = require("./util");
13
14
  var RespType;
14
15
  (function (RespType) {
15
16
  RespType["JSON"] = "json";
@@ -31,7 +32,13 @@ class Http {
31
32
  static RespData(response_1) {
32
33
  return __awaiter(this, arguments, void 0, function* (response, type = RespType.JSON) {
33
34
  if (!response.ok) {
34
- throw { status: response.status || 0, statusText: response.statusText || response };
35
+ const data = yield response.text();
36
+ if ((data && data.startsWith('{')) || data.startsWith('[')) {
37
+ throw JSON.parse(data);
38
+ }
39
+ else {
40
+ throw data;
41
+ }
35
42
  }
36
43
  switch (type) {
37
44
  case RespType.JSON:
@@ -55,42 +62,45 @@ class Http {
55
62
  return __awaiter(this, arguments, void 0, function* (url, params, headers, type = RespType.JSON) {
56
63
  url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
57
64
  const fullUrl = params ? `${url}?${new URLSearchParams(params).toString()}` : url;
65
+ headers = headers || exports.HttpHeaders;
66
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
58
67
  try {
59
- const requestOptions = { method: 'GET', headers: headers || exports.HttpHeaders };
68
+ const requestOptions = { method: 'GET', headers: headers };
60
69
  const response = yield fetch(fullUrl, requestOptions);
61
70
  return yield Http.RespData(response, type);
62
71
  }
63
72
  catch (error) {
64
- console.error('HttpGet error: ', error);
65
- throw { url: fullUrl, requestId: exports.HttpHeaders['x-request-id'], error: error };
73
+ throw { url: fullUrl, requestId: headers['x-request-id'], error: error };
66
74
  }
67
75
  });
68
76
  }
69
77
  static Post(url_1, data_1, headers_1) {
70
78
  return __awaiter(this, arguments, void 0, function* (url, data, headers, type = RespType.JSON) {
71
79
  url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
80
+ headers = headers || exports.HttpHeaders;
81
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
72
82
  try {
73
- const requestOptions = { method: 'POST', body: JSON.stringify(data || {}), headers: headers || exports.HttpHeaders };
83
+ const requestOptions = { method: 'POST', body: JSON.stringify(data), headers: headers };
74
84
  const response = yield fetch(url, requestOptions);
75
85
  return yield Http.RespData(response, type);
76
86
  }
77
87
  catch (error) {
78
- console.error('HttpPost error: ', error);
79
- throw { url: url, requestId: exports.HttpHeaders['x-request-id'], body: data, error: error };
88
+ throw { url: url, requestId: headers['x-request-id'], body: data, error: error };
80
89
  }
81
90
  });
82
91
  }
83
92
  static Put(url_1, data_1, headers_1) {
84
93
  return __awaiter(this, arguments, void 0, function* (url, data, headers, type = RespType.JSON) {
85
94
  url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
95
+ headers = headers || exports.HttpHeaders;
96
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
86
97
  try {
87
- const requestOptions = { method: 'PUT', body: JSON.stringify(data || {}), headers: headers || exports.HttpHeaders };
98
+ const requestOptions = { method: 'PUT', body: JSON.stringify(data), headers: headers };
88
99
  const response = yield fetch(url, requestOptions);
89
100
  return yield Http.RespData(response, type);
90
101
  }
91
102
  catch (error) {
92
- console.error('HttpPut error: ', error);
93
- throw { url: url, requestId: exports.HttpHeaders['x-request-id'], body: data, error: error };
103
+ throw { url: url, requestId: headers['x-request-id'], body: data, error: error };
94
104
  }
95
105
  });
96
106
  }
@@ -98,14 +108,15 @@ class Http {
98
108
  return __awaiter(this, arguments, void 0, function* (url, params, headers, type = RespType.JSON) {
99
109
  url = url.startsWith('http') ? url : Http.API_BASE_URL + url;
100
110
  const fullUrl = params ? `${url}?${new URLSearchParams(params).toString()}` : url;
111
+ headers = headers || exports.HttpHeaders;
112
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
101
113
  try {
102
- const requestOptions = { method: 'DELETE', headers: headers || exports.HttpHeaders };
114
+ const requestOptions = { method: 'DELETE', headers: headers };
103
115
  const response = yield fetch(fullUrl, requestOptions);
104
116
  return yield Http.RespData(response, type);
105
117
  }
106
118
  catch (error) {
107
- console.error('HttpDelete error: ', error);
108
- throw { url: fullUrl, requestId: exports.HttpHeaders['x-request-id'], error: error };
119
+ throw { url: fullUrl, requestId: headers['x-request-id'], error: error };
109
120
  }
110
121
  });
111
122
  }
@@ -115,14 +126,14 @@ class Http {
115
126
  const params = { key: appKey, payload: JSON.stringify(sessionInfo) };
116
127
  const fullUrl = `${url}?${new URLSearchParams(params).toString()}`;
117
128
  const headers = Object.assign(Object.assign({}, Headers), { 'X-API-KEY': apiKey || '' });
129
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
118
130
  try {
119
131
  const requestOptions = { method: 'GET', headers: headers };
120
132
  const response = yield fetch(fullUrl, requestOptions);
121
133
  return yield Http.RespData(response, type);
122
134
  }
123
135
  catch (error) {
124
- console.error('HttpToken error: ', error);
125
- throw { url: fullUrl, requestId: exports.HttpHeaders['x-request-id'], error: error };
136
+ throw { url: fullUrl, requestId: headers['x-request-id'], error: error };
126
137
  }
127
138
  });
128
139
  }
@@ -139,19 +150,18 @@ class Http {
139
150
  if (isHeadersAddMultiForm) {
140
151
  headers = Object.assign(Object.assign({}, headers), { 'Content-Type': headers['Content-Type'] || 'multipart/form-data' });
141
152
  }
153
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
142
154
  try {
143
155
  const requestOptions = {
144
156
  method: method,
145
157
  body: formData,
146
158
  headers: Object.assign({}, headers),
147
159
  };
148
- console.log(requestOptions);
149
160
  const response = yield fetch(url, requestOptions);
150
161
  return yield Http.RespData(response, type);
151
162
  }
152
163
  catch (error) {
153
- console.error('HttpUpload error: ', error);
154
- throw { url: url, requestId: exports.HttpHeaders['x-request-id'], error: error };
164
+ throw { url: url, requestId: headers['x-request-id'], error: error };
155
165
  }
156
166
  });
157
167
  }
@@ -160,6 +170,7 @@ class Http {
160
170
  headers = headers || {
161
171
  'Content-Type': file.type,
162
172
  };
173
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
163
174
  try {
164
175
  const requestOptions = {
165
176
  method: method,
@@ -170,24 +181,24 @@ class Http {
170
181
  return response;
171
182
  }
172
183
  catch (error) {
173
- console.error('HttpUpload error: ', error);
174
- throw { url: fullUrl, requestId: exports.HttpHeaders['x-request-id'], error: error };
184
+ throw { url: fullUrl, requestId: headers['x-request-id'], error: error };
175
185
  }
176
186
  });
177
187
  }
178
188
  static FileDownload(fullUrl_1, params_1) {
179
189
  return __awaiter(this, arguments, void 0, function* (fullUrl, params, headers = exports.HttpHeaders, method = 'GET') {
180
190
  fullUrl = params ? `${fullUrl}?${new URLSearchParams(params).toString()}` : fullUrl;
191
+ headers = headers || exports.HttpHeaders;
192
+ headers['x-request-id'] = headers['x-request-id'] || (0, util_1.AppUUID4)();
181
193
  try {
182
- const requestOptions = { method: method, headers: headers || exports.HttpHeaders };
194
+ const requestOptions = { method: method, headers: headers };
183
195
  const response = yield fetch(fullUrl, requestOptions);
184
196
  const blob = yield response.blob();
185
197
  const buffer = Buffer.from(yield blob.arrayBuffer());
186
198
  return buffer;
187
199
  }
188
200
  catch (error) {
189
- console.error('HttpDownload error: ', error);
190
- throw { url: fullUrl, requestId: exports.HttpHeaders['x-request-id'], error: error };
201
+ throw { url: fullUrl, requestId: headers['x-request-id'], error: error };
191
202
  }
192
203
  });
193
204
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ // bun src/tests/http.ts
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ const index_1 = require("../index");
14
+ const url = '/auth/partner/signin';
15
+ const reqBody = { language: 'en-US', mobile: '111111111', provider: 'mobile', telCode: '+91' };
16
+ const runHttpExample = () => __awaiter(void 0, void 0, void 0, function* () {
17
+ try {
18
+ const res = yield index_1.Http.Post(url, reqBody, null);
19
+ console.log(res);
20
+ return res;
21
+ }
22
+ catch (error) {
23
+ console.log(error);
24
+ }
25
+ });
26
+ runHttpExample();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {