@oino-ts/common 0.17.4 → 0.18.1
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/cjs/OINORequest.js +44 -22
- package/dist/cjs/OINOResult.js +70 -1
- package/dist/esm/OINORequest.js +44 -22
- package/dist/esm/OINOResult.js +70 -1
- package/dist/types/OINORequest.d.ts +28 -6
- package/dist/types/OINOResult.d.ts +30 -2
- package/package.json +2 -2
- package/src/OINORequest.ts +46 -24
- package/src/OINOResult.ts +75 -3
package/dist/cjs/OINORequest.js
CHANGED
|
@@ -34,7 +34,7 @@ class OINOHttpRequest extends OINORequest {
|
|
|
34
34
|
url;
|
|
35
35
|
method;
|
|
36
36
|
headers;
|
|
37
|
-
|
|
37
|
+
body;
|
|
38
38
|
requestType;
|
|
39
39
|
responseType;
|
|
40
40
|
multipartBoundary;
|
|
@@ -51,7 +51,7 @@ class OINOHttpRequest extends OINORequest {
|
|
|
51
51
|
this.url = init.url;
|
|
52
52
|
this.method = init.method ?? "GET";
|
|
53
53
|
this.headers = new _1.OINOHeaders(init.headers);
|
|
54
|
-
this.
|
|
54
|
+
this.body = init.body;
|
|
55
55
|
this.multipartBoundary = "";
|
|
56
56
|
this.lastModified = init.lastModified;
|
|
57
57
|
if (init.multipartBoundary) {
|
|
@@ -102,47 +102,69 @@ class OINOHttpRequest extends OINORequest {
|
|
|
102
102
|
this.etags = etags;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Creates a `OINOHttpRequest` from a Fetch API `Request` object.
|
|
107
|
+
*
|
|
108
|
+
* @param request Fetch request
|
|
109
|
+
*
|
|
110
|
+
*/
|
|
105
111
|
static async fromFetchRequest(request) {
|
|
106
112
|
const body = await request.arrayBuffer();
|
|
107
113
|
return new OINOHttpRequest({
|
|
108
114
|
url: new URL(request.url),
|
|
109
115
|
method: request.method,
|
|
110
116
|
headers: Object.fromEntries(request.headers),
|
|
111
|
-
|
|
117
|
+
body: node_buffer_1.Buffer.from(body),
|
|
112
118
|
});
|
|
113
119
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Returns the request data as a text string.
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
bodyAsText() {
|
|
125
|
+
if (this.body instanceof Uint8Array) {
|
|
126
|
+
return new TextDecoder().decode(this.body);
|
|
117
127
|
}
|
|
118
|
-
else if (this.
|
|
119
|
-
return JSON.stringify(this.
|
|
128
|
+
else if (this.body instanceof Object) {
|
|
129
|
+
return JSON.stringify(this.body);
|
|
120
130
|
}
|
|
121
131
|
else {
|
|
122
|
-
return this.
|
|
132
|
+
return this.body?.toString() || "";
|
|
123
133
|
}
|
|
124
134
|
}
|
|
125
|
-
|
|
126
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Returns the request data parsed as JSON object.
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
139
|
+
bodyAsParsedJson() {
|
|
140
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {};
|
|
127
141
|
}
|
|
128
|
-
|
|
129
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Returns the request data as URLSearchParams (form data).
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
bodyAsFormData() {
|
|
147
|
+
return new URLSearchParams(this.bodyAsText() || "");
|
|
130
148
|
}
|
|
131
|
-
|
|
132
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Returns the request data as Buffer.
|
|
151
|
+
*
|
|
152
|
+
*/
|
|
153
|
+
bodyAsBuffer() {
|
|
154
|
+
if ((this.body === null) || (this.body === undefined)) {
|
|
133
155
|
return node_buffer_1.Buffer.alloc(0);
|
|
134
156
|
}
|
|
135
|
-
else if (this.
|
|
136
|
-
return this.
|
|
157
|
+
else if (this.body instanceof node_buffer_1.Buffer) {
|
|
158
|
+
return this.body;
|
|
137
159
|
}
|
|
138
|
-
else if (this.
|
|
139
|
-
return node_buffer_1.Buffer.from(this.
|
|
160
|
+
else if (this.body instanceof Uint8Array) {
|
|
161
|
+
return node_buffer_1.Buffer.from(this.body);
|
|
140
162
|
}
|
|
141
|
-
else if (this.
|
|
142
|
-
return node_buffer_1.Buffer.from(JSON.stringify(this.
|
|
163
|
+
else if (this.body instanceof Object) {
|
|
164
|
+
return node_buffer_1.Buffer.from(JSON.stringify(this.body), "utf-8");
|
|
143
165
|
}
|
|
144
166
|
else {
|
|
145
|
-
return node_buffer_1.Buffer.from(this.
|
|
167
|
+
return node_buffer_1.Buffer.from(this.body, "utf-8");
|
|
146
168
|
}
|
|
147
169
|
}
|
|
148
170
|
}
|
package/dist/cjs/OINOResult.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINOHttpResult = exports.OINOResult = void 0;
|
|
9
9
|
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
const node_buffer_1 = require("node:buffer");
|
|
10
11
|
const _1 = require(".");
|
|
11
12
|
/**
|
|
12
13
|
* OINO API request result object with returned data and/or http status code/message and
|
|
@@ -209,7 +210,14 @@ class OINOHttpResult extends OINOResult {
|
|
|
209
210
|
if (headers) {
|
|
210
211
|
merged_headers.setHeaders(headers);
|
|
211
212
|
}
|
|
212
|
-
|
|
213
|
+
let body;
|
|
214
|
+
if (this.body instanceof node_buffer_1.Buffer) {
|
|
215
|
+
body = new Uint8Array(this.body);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
body = this.body;
|
|
219
|
+
}
|
|
220
|
+
const result = new Response(body, { status: this.status, statusText: this.statusText, headers: merged_headers });
|
|
213
221
|
result.headers.set('Content-Length', this.body.length.toString());
|
|
214
222
|
if (merged_headers['Last-Modified'] === undefined && this.lastModified > 0) {
|
|
215
223
|
result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString());
|
|
@@ -223,5 +231,66 @@ class OINOHttpResult extends OINOResult {
|
|
|
223
231
|
result.headers.set("ETag", this.getEtag());
|
|
224
232
|
return result;
|
|
225
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Create from a Response object from the result values.
|
|
236
|
+
*
|
|
237
|
+
* @param response fetch Response object
|
|
238
|
+
*
|
|
239
|
+
*/
|
|
240
|
+
static async fromFetchResponse(response) {
|
|
241
|
+
const body = node_buffer_1.Buffer.from(await response.arrayBuffer());
|
|
242
|
+
const last_modified = response.headers.get('Last-Modified');
|
|
243
|
+
const expires = response.headers.get('Expires');
|
|
244
|
+
const result = new OINOHttpResult({
|
|
245
|
+
status: response.status,
|
|
246
|
+
statusText: response.statusText,
|
|
247
|
+
body: body,
|
|
248
|
+
headers: response.headers,
|
|
249
|
+
lastModified: last_modified ? (new Date(last_modified).getTime()) : 0,
|
|
250
|
+
expires: expires ? (new Date(expires).getTime() - Date.now()) : 0
|
|
251
|
+
});
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Returns the request body as a text string.
|
|
256
|
+
*
|
|
257
|
+
*/
|
|
258
|
+
bodyAsText() {
|
|
259
|
+
if (this.body instanceof node_buffer_1.Buffer) {
|
|
260
|
+
return this.body.toString("utf-8");
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
return this.body || "";
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the request body parsed as JSON object.
|
|
268
|
+
*
|
|
269
|
+
*/
|
|
270
|
+
bodyAsParsedJson() {
|
|
271
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Returns the request body as URLSearchParams (form body).
|
|
275
|
+
*
|
|
276
|
+
*/
|
|
277
|
+
bodyAsFormData() {
|
|
278
|
+
return new URLSearchParams(this.bodyAsText() || "");
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns the request body as Buffer.
|
|
282
|
+
*
|
|
283
|
+
*/
|
|
284
|
+
bodyAsBuffer() {
|
|
285
|
+
if (this.body === null) {
|
|
286
|
+
return node_buffer_1.Buffer.alloc(0);
|
|
287
|
+
}
|
|
288
|
+
else if (this.body instanceof node_buffer_1.Buffer) {
|
|
289
|
+
return this.body;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
return node_buffer_1.Buffer.from(this.body, "utf-8");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
226
295
|
}
|
|
227
296
|
exports.OINOHttpResult = OINOHttpResult;
|
package/dist/esm/OINORequest.js
CHANGED
|
@@ -30,7 +30,7 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
30
30
|
url;
|
|
31
31
|
method;
|
|
32
32
|
headers;
|
|
33
|
-
|
|
33
|
+
body;
|
|
34
34
|
requestType;
|
|
35
35
|
responseType;
|
|
36
36
|
multipartBoundary;
|
|
@@ -47,7 +47,7 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
47
47
|
this.url = init.url;
|
|
48
48
|
this.method = init.method ?? "GET";
|
|
49
49
|
this.headers = new OINOHeaders(init.headers);
|
|
50
|
-
this.
|
|
50
|
+
this.body = init.body;
|
|
51
51
|
this.multipartBoundary = "";
|
|
52
52
|
this.lastModified = init.lastModified;
|
|
53
53
|
if (init.multipartBoundary) {
|
|
@@ -98,47 +98,69 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
98
98
|
this.etags = etags;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a `OINOHttpRequest` from a Fetch API `Request` object.
|
|
103
|
+
*
|
|
104
|
+
* @param request Fetch request
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
101
107
|
static async fromFetchRequest(request) {
|
|
102
108
|
const body = await request.arrayBuffer();
|
|
103
109
|
return new OINOHttpRequest({
|
|
104
110
|
url: new URL(request.url),
|
|
105
111
|
method: request.method,
|
|
106
112
|
headers: Object.fromEntries(request.headers),
|
|
107
|
-
|
|
113
|
+
body: Buffer.from(body),
|
|
108
114
|
});
|
|
109
115
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Returns the request data as a text string.
|
|
118
|
+
*
|
|
119
|
+
*/
|
|
120
|
+
bodyAsText() {
|
|
121
|
+
if (this.body instanceof Uint8Array) {
|
|
122
|
+
return new TextDecoder().decode(this.body);
|
|
113
123
|
}
|
|
114
|
-
else if (this.
|
|
115
|
-
return JSON.stringify(this.
|
|
124
|
+
else if (this.body instanceof Object) {
|
|
125
|
+
return JSON.stringify(this.body);
|
|
116
126
|
}
|
|
117
127
|
else {
|
|
118
|
-
return this.
|
|
128
|
+
return this.body?.toString() || "";
|
|
119
129
|
}
|
|
120
130
|
}
|
|
121
|
-
|
|
122
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Returns the request data parsed as JSON object.
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
bodyAsParsedJson() {
|
|
136
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {};
|
|
123
137
|
}
|
|
124
|
-
|
|
125
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Returns the request data as URLSearchParams (form data).
|
|
140
|
+
*
|
|
141
|
+
*/
|
|
142
|
+
bodyAsFormData() {
|
|
143
|
+
return new URLSearchParams(this.bodyAsText() || "");
|
|
126
144
|
}
|
|
127
|
-
|
|
128
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Returns the request data as Buffer.
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
bodyAsBuffer() {
|
|
150
|
+
if ((this.body === null) || (this.body === undefined)) {
|
|
129
151
|
return Buffer.alloc(0);
|
|
130
152
|
}
|
|
131
|
-
else if (this.
|
|
132
|
-
return this.
|
|
153
|
+
else if (this.body instanceof Buffer) {
|
|
154
|
+
return this.body;
|
|
133
155
|
}
|
|
134
|
-
else if (this.
|
|
135
|
-
return Buffer.from(this.
|
|
156
|
+
else if (this.body instanceof Uint8Array) {
|
|
157
|
+
return Buffer.from(this.body);
|
|
136
158
|
}
|
|
137
|
-
else if (this.
|
|
138
|
-
return Buffer.from(JSON.stringify(this.
|
|
159
|
+
else if (this.body instanceof Object) {
|
|
160
|
+
return Buffer.from(JSON.stringify(this.body), "utf-8");
|
|
139
161
|
}
|
|
140
162
|
else {
|
|
141
|
-
return Buffer.from(this.
|
|
163
|
+
return Buffer.from(this.body, "utf-8");
|
|
142
164
|
}
|
|
143
165
|
}
|
|
144
166
|
}
|
package/dist/esm/OINOResult.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
7
|
+
import { Buffer } from "node:buffer";
|
|
7
8
|
import { OINO_DEBUG_PREFIX, OINO_ERROR_PREFIX, OINO_INFO_PREFIX, OINO_WARNING_PREFIX, OINOHeaders } from ".";
|
|
8
9
|
/**
|
|
9
10
|
* OINO API request result object with returned data and/or http status code/message and
|
|
@@ -205,7 +206,14 @@ export class OINOHttpResult extends OINOResult {
|
|
|
205
206
|
if (headers) {
|
|
206
207
|
merged_headers.setHeaders(headers);
|
|
207
208
|
}
|
|
208
|
-
|
|
209
|
+
let body;
|
|
210
|
+
if (this.body instanceof Buffer) {
|
|
211
|
+
body = new Uint8Array(this.body);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
body = this.body;
|
|
215
|
+
}
|
|
216
|
+
const result = new Response(body, { status: this.status, statusText: this.statusText, headers: merged_headers });
|
|
209
217
|
result.headers.set('Content-Length', this.body.length.toString());
|
|
210
218
|
if (merged_headers['Last-Modified'] === undefined && this.lastModified > 0) {
|
|
211
219
|
result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString());
|
|
@@ -219,4 +227,65 @@ export class OINOHttpResult extends OINOResult {
|
|
|
219
227
|
result.headers.set("ETag", this.getEtag());
|
|
220
228
|
return result;
|
|
221
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Create from a Response object from the result values.
|
|
232
|
+
*
|
|
233
|
+
* @param response fetch Response object
|
|
234
|
+
*
|
|
235
|
+
*/
|
|
236
|
+
static async fromFetchResponse(response) {
|
|
237
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
238
|
+
const last_modified = response.headers.get('Last-Modified');
|
|
239
|
+
const expires = response.headers.get('Expires');
|
|
240
|
+
const result = new OINOHttpResult({
|
|
241
|
+
status: response.status,
|
|
242
|
+
statusText: response.statusText,
|
|
243
|
+
body: body,
|
|
244
|
+
headers: response.headers,
|
|
245
|
+
lastModified: last_modified ? (new Date(last_modified).getTime()) : 0,
|
|
246
|
+
expires: expires ? (new Date(expires).getTime() - Date.now()) : 0
|
|
247
|
+
});
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Returns the request body as a text string.
|
|
252
|
+
*
|
|
253
|
+
*/
|
|
254
|
+
bodyAsText() {
|
|
255
|
+
if (this.body instanceof Buffer) {
|
|
256
|
+
return this.body.toString("utf-8");
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
return this.body || "";
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Returns the request body parsed as JSON object.
|
|
264
|
+
*
|
|
265
|
+
*/
|
|
266
|
+
bodyAsParsedJson() {
|
|
267
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Returns the request body as URLSearchParams (form body).
|
|
271
|
+
*
|
|
272
|
+
*/
|
|
273
|
+
bodyAsFormData() {
|
|
274
|
+
return new URLSearchParams(this.bodyAsText() || "");
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Returns the request body as Buffer.
|
|
278
|
+
*
|
|
279
|
+
*/
|
|
280
|
+
bodyAsBuffer() {
|
|
281
|
+
if (this.body === null) {
|
|
282
|
+
return Buffer.alloc(0);
|
|
283
|
+
}
|
|
284
|
+
else if (this.body instanceof Buffer) {
|
|
285
|
+
return this.body;
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
return Buffer.from(this.body, "utf-8");
|
|
289
|
+
}
|
|
290
|
+
}
|
|
222
291
|
}
|
|
@@ -23,7 +23,7 @@ export interface OINOHttpRequestInit extends OINORequestInit {
|
|
|
23
23
|
url?: URL;
|
|
24
24
|
method?: string;
|
|
25
25
|
headers?: OINOHeadersInit;
|
|
26
|
-
|
|
26
|
+
body?: string | Buffer | Uint8Array | object | null | undefined;
|
|
27
27
|
requestType?: OINOContentType;
|
|
28
28
|
responseType?: OINOContentType;
|
|
29
29
|
multipartBoundary?: string;
|
|
@@ -36,7 +36,7 @@ export declare class OINOHttpRequest extends OINORequest {
|
|
|
36
36
|
readonly url?: URL;
|
|
37
37
|
readonly method: string;
|
|
38
38
|
readonly headers: OINOHeaders;
|
|
39
|
-
readonly
|
|
39
|
+
readonly body: string | Buffer | Uint8Array | object | null | undefined;
|
|
40
40
|
readonly requestType: OINOContentType;
|
|
41
41
|
readonly responseType: OINOContentType;
|
|
42
42
|
readonly multipartBoundary?: string;
|
|
@@ -49,9 +49,31 @@ export declare class OINOHttpRequest extends OINORequest {
|
|
|
49
49
|
*
|
|
50
50
|
*/
|
|
51
51
|
constructor(init: OINOHttpRequestInit);
|
|
52
|
+
/**
|
|
53
|
+
* Creates a `OINOHttpRequest` from a Fetch API `Request` object.
|
|
54
|
+
*
|
|
55
|
+
* @param request Fetch request
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
52
58
|
static fromFetchRequest(request: Request): Promise<OINOHttpRequest>;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Returns the request data as a text string.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
bodyAsText(): string;
|
|
64
|
+
/**
|
|
65
|
+
* Returns the request data parsed as JSON object.
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
bodyAsParsedJson(): any;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the request data as URLSearchParams (form data).
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
bodyAsFormData(): URLSearchParams;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the request data as Buffer.
|
|
76
|
+
*
|
|
77
|
+
*/
|
|
78
|
+
bodyAsBuffer(): Buffer;
|
|
57
79
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
1
2
|
import { OINOHeaders, OINOHeadersInit } from ".";
|
|
2
3
|
export interface OINOResultInit {
|
|
3
4
|
success?: boolean;
|
|
@@ -88,7 +89,7 @@ export declare class OINOResult {
|
|
|
88
89
|
printLog(): string;
|
|
89
90
|
}
|
|
90
91
|
export interface OINOHttpResultInit extends OINOResultInit {
|
|
91
|
-
body?: string;
|
|
92
|
+
body?: string | Buffer;
|
|
92
93
|
headers?: OINOHeadersInit;
|
|
93
94
|
expires?: number;
|
|
94
95
|
lastModified?: number;
|
|
@@ -99,7 +100,7 @@ export interface OINOHttpResultInit extends OINOResultInit {
|
|
|
99
100
|
export declare class OINOHttpResult extends OINOResult {
|
|
100
101
|
private _etag;
|
|
101
102
|
/** HTTP body data */
|
|
102
|
-
readonly body: string;
|
|
103
|
+
readonly body: string | Buffer;
|
|
103
104
|
/** HTTP headers */
|
|
104
105
|
readonly headers: OINOHeaders;
|
|
105
106
|
/** HTTP cache expiration value
|
|
@@ -126,4 +127,31 @@ export declare class OINOHttpResult extends OINOResult {
|
|
|
126
127
|
* @param headers HTTP headers (overrides existing values)
|
|
127
128
|
*/
|
|
128
129
|
getFetchResponse(headers?: OINOHeadersInit): Response;
|
|
130
|
+
/**
|
|
131
|
+
* Create from a Response object from the result values.
|
|
132
|
+
*
|
|
133
|
+
* @param response fetch Response object
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
static fromFetchResponse(response: Response): Promise<OINOHttpResult>;
|
|
137
|
+
/**
|
|
138
|
+
* Returns the request body as a text string.
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
bodyAsText(): string;
|
|
142
|
+
/**
|
|
143
|
+
* Returns the request body parsed as JSON object.
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
bodyAsParsedJson(): any;
|
|
147
|
+
/**
|
|
148
|
+
* Returns the request body as URLSearchParams (form body).
|
|
149
|
+
*
|
|
150
|
+
*/
|
|
151
|
+
bodyAsFormData(): URLSearchParams;
|
|
152
|
+
/**
|
|
153
|
+
* Returns the request body as Buffer.
|
|
154
|
+
*
|
|
155
|
+
*/
|
|
156
|
+
bodyAsBuffer(): Buffer;
|
|
129
157
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1",
|
|
4
4
|
"description": "OINO TS package for common classes.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@oino-ts/types": "0.
|
|
22
|
+
"@oino-ts/types": "0.18.1",
|
|
23
23
|
"@types/node": "^22.0.0",
|
|
24
24
|
"typescript": "~5.9.0"
|
|
25
25
|
},
|
package/src/OINORequest.ts
CHANGED
|
@@ -36,7 +36,7 @@ export interface OINOHttpRequestInit extends OINORequestInit {
|
|
|
36
36
|
url?: URL
|
|
37
37
|
method?: string
|
|
38
38
|
headers?: OINOHeadersInit
|
|
39
|
-
|
|
39
|
+
body?: string|Buffer|Uint8Array|object|null|undefined
|
|
40
40
|
requestType?:OINOContentType
|
|
41
41
|
responseType?:OINOContentType
|
|
42
42
|
multipartBoundary?:string
|
|
@@ -50,7 +50,7 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
50
50
|
readonly url?: URL
|
|
51
51
|
readonly method: string
|
|
52
52
|
readonly headers: OINOHeaders
|
|
53
|
-
readonly
|
|
53
|
+
readonly body: string|Buffer|Uint8Array|object|null|undefined
|
|
54
54
|
readonly requestType:OINOContentType
|
|
55
55
|
readonly responseType:OINOContentType
|
|
56
56
|
readonly multipartBoundary?:string
|
|
@@ -68,7 +68,7 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
68
68
|
this.url = init.url
|
|
69
69
|
this.method = init.method ?? "GET"
|
|
70
70
|
this.headers = new OINOHeaders(init.headers)
|
|
71
|
-
this.
|
|
71
|
+
this.body = init.body
|
|
72
72
|
this.multipartBoundary = ""
|
|
73
73
|
this.lastModified = init.lastModified
|
|
74
74
|
|
|
@@ -118,51 +118,73 @@ export class OINOHttpRequest extends OINORequest {
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Creates a `OINOHttpRequest` from a Fetch API `Request` object.
|
|
123
|
+
*
|
|
124
|
+
* @param request Fetch request
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
121
127
|
static async fromFetchRequest(request: Request): Promise<OINOHttpRequest> {
|
|
122
128
|
const body = await request.arrayBuffer()
|
|
123
129
|
return new OINOHttpRequest({
|
|
124
130
|
url: new URL(request.url),
|
|
125
131
|
method: request.method,
|
|
126
132
|
headers: Object.fromEntries(request.headers as any),
|
|
127
|
-
|
|
133
|
+
body: Buffer.from(body),
|
|
128
134
|
})
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Returns the request data as a text string.
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
bodyAsText(): string {
|
|
142
|
+
if (this.body instanceof Uint8Array) {
|
|
143
|
+
return new TextDecoder().decode(this.body)
|
|
134
144
|
|
|
135
|
-
} else if (this.
|
|
136
|
-
return JSON.stringify(this.
|
|
145
|
+
} else if (this.body instanceof Object) {
|
|
146
|
+
return JSON.stringify(this.body)
|
|
137
147
|
|
|
138
148
|
} else {
|
|
139
|
-
return this.
|
|
149
|
+
return this.body?.toString() || ""
|
|
140
150
|
}
|
|
141
151
|
}
|
|
142
152
|
|
|
143
|
-
|
|
144
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Returns the request data parsed as JSON object.
|
|
155
|
+
*
|
|
156
|
+
*/
|
|
157
|
+
bodyAsParsedJson(): any {
|
|
158
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {}
|
|
145
159
|
}
|
|
146
160
|
|
|
147
|
-
|
|
148
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Returns the request data as URLSearchParams (form data).
|
|
163
|
+
*
|
|
164
|
+
*/
|
|
165
|
+
bodyAsFormData(): URLSearchParams {
|
|
166
|
+
return new URLSearchParams(this.bodyAsText() || "")
|
|
149
167
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Returns the request data as Buffer.
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
173
|
+
bodyAsBuffer(): Buffer {
|
|
174
|
+
if ((this.body === null) || (this.body === undefined)) {
|
|
153
175
|
return Buffer.alloc(0)
|
|
154
176
|
|
|
155
|
-
} else if (this.
|
|
156
|
-
return this.
|
|
177
|
+
} else if (this.body instanceof Buffer) {
|
|
178
|
+
return this.body
|
|
157
179
|
|
|
158
|
-
} else if (this.
|
|
159
|
-
return Buffer.from(this.
|
|
180
|
+
} else if (this.body instanceof Uint8Array) {
|
|
181
|
+
return Buffer.from(this.body)
|
|
160
182
|
|
|
161
|
-
} else if (this.
|
|
162
|
-
return Buffer.from(JSON.stringify(this.
|
|
183
|
+
} else if (this.body instanceof Object) {
|
|
184
|
+
return Buffer.from(JSON.stringify(this.body), "utf-8")
|
|
163
185
|
|
|
164
186
|
} else {
|
|
165
|
-
return Buffer.from(this.
|
|
187
|
+
return Buffer.from(this.body, "utf-8")
|
|
166
188
|
}
|
|
167
189
|
}
|
|
168
190
|
|
package/src/OINOResult.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { createHash, Hash } from "node:crypto";
|
|
8
|
+
import { Buffer } from "node:buffer";
|
|
8
9
|
import { OINO_DEBUG_PREFIX, OINO_ERROR_PREFIX, OINO_INFO_PREFIX, OINO_WARNING_PREFIX, OINOHeaders, OINOHeadersInit } from ".";
|
|
9
10
|
|
|
10
11
|
export interface OINOResultInit {
|
|
@@ -177,7 +178,7 @@ export class OINOResult {
|
|
|
177
178
|
}
|
|
178
179
|
|
|
179
180
|
export interface OINOHttpResultInit extends OINOResultInit {
|
|
180
|
-
body?: string
|
|
181
|
+
body?: string|Buffer
|
|
181
182
|
headers?: OINOHeadersInit
|
|
182
183
|
expires?: number
|
|
183
184
|
lastModified?: number
|
|
@@ -190,7 +191,7 @@ export class OINOHttpResult extends OINOResult {
|
|
|
190
191
|
private _etag:string
|
|
191
192
|
|
|
192
193
|
/** HTTP body data */
|
|
193
|
-
readonly body: string
|
|
194
|
+
readonly body: string|Buffer
|
|
194
195
|
|
|
195
196
|
/** HTTP headers */
|
|
196
197
|
readonly headers: OINOHeaders
|
|
@@ -240,7 +241,13 @@ export class OINOHttpResult extends OINOResult {
|
|
|
240
241
|
if (headers) {
|
|
241
242
|
merged_headers.setHeaders(headers)
|
|
242
243
|
}
|
|
243
|
-
|
|
244
|
+
let body: string|BufferSource
|
|
245
|
+
if (this.body instanceof Buffer) {
|
|
246
|
+
body = new Uint8Array(this.body)
|
|
247
|
+
} else {
|
|
248
|
+
body = this.body as string
|
|
249
|
+
}
|
|
250
|
+
const result: Response = new Response(body, { status: this.status, statusText: this.statusText, headers: merged_headers })
|
|
244
251
|
result.headers.set('Content-Length', this.body.length.toString())
|
|
245
252
|
if (merged_headers['Last-Modified'] === undefined && this.lastModified > 0) {
|
|
246
253
|
result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString())
|
|
@@ -255,4 +262,69 @@ export class OINOHttpResult extends OINOResult {
|
|
|
255
262
|
return result
|
|
256
263
|
}
|
|
257
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Create from a Response object from the result values.
|
|
267
|
+
*
|
|
268
|
+
* @param response fetch Response object
|
|
269
|
+
*
|
|
270
|
+
*/
|
|
271
|
+
static async fromFetchResponse(response: Response): Promise<OINOHttpResult> {
|
|
272
|
+
const body = Buffer.from(await response.arrayBuffer())
|
|
273
|
+
const last_modified = response.headers.get('Last-Modified')
|
|
274
|
+
const expires = response.headers.get('Expires')
|
|
275
|
+
const result = new OINOHttpResult({
|
|
276
|
+
status: response.status,
|
|
277
|
+
statusText: response.statusText,
|
|
278
|
+
body: body,
|
|
279
|
+
headers: response.headers as unknown as OINOHeadersInit,
|
|
280
|
+
lastModified: last_modified ? (new Date(last_modified).getTime()) : 0,
|
|
281
|
+
expires: expires ? (new Date(expires).getTime() - Date.now()) : 0
|
|
282
|
+
})
|
|
283
|
+
return result
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Returns the request body as a text string.
|
|
288
|
+
*
|
|
289
|
+
*/
|
|
290
|
+
bodyAsText(): string {
|
|
291
|
+
if (this.body instanceof Buffer) {
|
|
292
|
+
return this.body.toString("utf-8")
|
|
293
|
+
|
|
294
|
+
} else {
|
|
295
|
+
return this.body as string || ""
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Returns the request body parsed as JSON object.
|
|
301
|
+
*
|
|
302
|
+
*/
|
|
303
|
+
bodyAsParsedJson(): any {
|
|
304
|
+
return this.body ? JSON.parse(this.bodyAsText()) : {}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Returns the request body as URLSearchParams (form body).
|
|
309
|
+
*
|
|
310
|
+
*/
|
|
311
|
+
bodyAsFormData(): URLSearchParams {
|
|
312
|
+
return new URLSearchParams(this.bodyAsText() || "")
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Returns the request body as Buffer.
|
|
317
|
+
*
|
|
318
|
+
*/
|
|
319
|
+
bodyAsBuffer(): Buffer {
|
|
320
|
+
if (this.body === null) {
|
|
321
|
+
return Buffer.alloc(0)
|
|
322
|
+
|
|
323
|
+
} else if (this.body instanceof Buffer) {
|
|
324
|
+
return this.body
|
|
325
|
+
|
|
326
|
+
} else {
|
|
327
|
+
return Buffer.from(this.body as string, "utf-8")
|
|
328
|
+
}
|
|
329
|
+
}
|
|
258
330
|
}
|