@oino-ts/common 0.17.5 → 0.19.0

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.
@@ -34,7 +34,7 @@ class OINOHttpRequest extends OINORequest {
34
34
  url;
35
35
  method;
36
36
  headers;
37
- data;
37
+ body;
38
38
  requestType;
39
39
  responseType;
40
40
  multipartBoundary;
@@ -48,10 +48,10 @@ class OINOHttpRequest extends OINORequest {
48
48
  */
49
49
  constructor(init) {
50
50
  super(init);
51
- this.url = init.url;
52
- this.method = init.method ?? "GET";
51
+ this.url = typeof init.url === "string" ? new URL(init.url) : init.url;
52
+ this.method = init.method?.toUpperCase() ?? "GET";
53
53
  this.headers = new _1.OINOHeaders(init.headers);
54
- this.data = init.data ?? "";
54
+ this.body = init.body ?? null;
55
55
  this.multipartBoundary = "";
56
56
  this.lastModified = init.lastModified;
57
57
  if (init.multipartBoundary) {
@@ -102,47 +102,72 @@ 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
- data: node_buffer_1.Buffer.from(body),
117
+ body: node_buffer_1.Buffer.from(body),
112
118
  });
113
119
  }
114
- dataAsText() {
115
- if (this.data instanceof Uint8Array) {
116
- return new TextDecoder().decode(this.data);
120
+ /**
121
+ * Returns the request data as a text string.
122
+ *
123
+ */
124
+ bodyAsText() {
125
+ if (this.body == null) {
126
+ return "";
117
127
  }
118
- else if (this.data instanceof Object) {
119
- return JSON.stringify(this.data);
128
+ else if (this.body instanceof Uint8Array) {
129
+ return new TextDecoder().decode(this.body);
130
+ }
131
+ else if (typeof this.body === "object") {
132
+ return JSON.stringify(this.body);
120
133
  }
121
134
  else {
122
- return this.data?.toString() || "";
135
+ return this.body?.toString() || "";
123
136
  }
124
137
  }
125
- dataAsParsedJson() {
126
- return this.data ? JSON.parse(this.dataAsText()) : {};
138
+ /**
139
+ * Returns the request data parsed as JSON object.
140
+ *
141
+ */
142
+ bodyAsParsedJson() {
143
+ return this.body ? JSON.parse(this.bodyAsText()) : {};
127
144
  }
128
- dataAsFormData() {
129
- return new URLSearchParams(this.dataAsText() || "");
145
+ /**
146
+ * Returns the request data as URLSearchParams (form data).
147
+ *
148
+ */
149
+ bodyAsFormData() {
150
+ return new URLSearchParams(this.bodyAsText() || "");
130
151
  }
131
- dataAsBuffer() {
132
- if (this.data === null) {
152
+ /**
153
+ * Returns the request data as Buffer.
154
+ *
155
+ */
156
+ bodyAsBuffer() {
157
+ if ((this.body === null) || (this.body === undefined)) {
133
158
  return node_buffer_1.Buffer.alloc(0);
134
159
  }
135
- else if (this.data instanceof node_buffer_1.Buffer) {
136
- return this.data;
160
+ else if (this.body instanceof node_buffer_1.Buffer) {
161
+ return this.body;
137
162
  }
138
- else if (this.data instanceof Uint8Array) {
139
- return node_buffer_1.Buffer.from(this.data);
163
+ else if (this.body instanceof Uint8Array) {
164
+ return node_buffer_1.Buffer.from(this.body);
140
165
  }
141
- else if (this.data instanceof Object) {
142
- return node_buffer_1.Buffer.from(JSON.stringify(this.data), "utf-8");
166
+ else if (typeof this.body === "object") {
167
+ return node_buffer_1.Buffer.from(JSON.stringify(this.body), "utf-8");
143
168
  }
144
169
  else {
145
- return node_buffer_1.Buffer.from(this.data, "utf-8");
170
+ return node_buffer_1.Buffer.from(this.body, "utf-8");
146
171
  }
147
172
  }
148
173
  }
@@ -231,5 +231,66 @@ class OINOHttpResult extends OINOResult {
231
231
  result.headers.set("ETag", this.getEtag());
232
232
  return result;
233
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
+ }
234
295
  }
235
296
  exports.OINOHttpResult = OINOHttpResult;
@@ -30,7 +30,7 @@ export class OINOHttpRequest extends OINORequest {
30
30
  url;
31
31
  method;
32
32
  headers;
33
- data;
33
+ body;
34
34
  requestType;
35
35
  responseType;
36
36
  multipartBoundary;
@@ -44,10 +44,10 @@ export class OINOHttpRequest extends OINORequest {
44
44
  */
45
45
  constructor(init) {
46
46
  super(init);
47
- this.url = init.url;
48
- this.method = init.method ?? "GET";
47
+ this.url = typeof init.url === "string" ? new URL(init.url) : init.url;
48
+ this.method = init.method?.toUpperCase() ?? "GET";
49
49
  this.headers = new OINOHeaders(init.headers);
50
- this.data = init.data ?? "";
50
+ this.body = init.body ?? null;
51
51
  this.multipartBoundary = "";
52
52
  this.lastModified = init.lastModified;
53
53
  if (init.multipartBoundary) {
@@ -98,47 +98,72 @@ 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
- data: Buffer.from(body),
113
+ body: Buffer.from(body),
108
114
  });
109
115
  }
110
- dataAsText() {
111
- if (this.data instanceof Uint8Array) {
112
- return new TextDecoder().decode(this.data);
116
+ /**
117
+ * Returns the request data as a text string.
118
+ *
119
+ */
120
+ bodyAsText() {
121
+ if (this.body == null) {
122
+ return "";
113
123
  }
114
- else if (this.data instanceof Object) {
115
- return JSON.stringify(this.data);
124
+ else if (this.body instanceof Uint8Array) {
125
+ return new TextDecoder().decode(this.body);
126
+ }
127
+ else if (typeof this.body === "object") {
128
+ return JSON.stringify(this.body);
116
129
  }
117
130
  else {
118
- return this.data?.toString() || "";
131
+ return this.body?.toString() || "";
119
132
  }
120
133
  }
121
- dataAsParsedJson() {
122
- return this.data ? JSON.parse(this.dataAsText()) : {};
134
+ /**
135
+ * Returns the request data parsed as JSON object.
136
+ *
137
+ */
138
+ bodyAsParsedJson() {
139
+ return this.body ? JSON.parse(this.bodyAsText()) : {};
123
140
  }
124
- dataAsFormData() {
125
- return new URLSearchParams(this.dataAsText() || "");
141
+ /**
142
+ * Returns the request data as URLSearchParams (form data).
143
+ *
144
+ */
145
+ bodyAsFormData() {
146
+ return new URLSearchParams(this.bodyAsText() || "");
126
147
  }
127
- dataAsBuffer() {
128
- if (this.data === null) {
148
+ /**
149
+ * Returns the request data as Buffer.
150
+ *
151
+ */
152
+ bodyAsBuffer() {
153
+ if ((this.body === null) || (this.body === undefined)) {
129
154
  return Buffer.alloc(0);
130
155
  }
131
- else if (this.data instanceof Buffer) {
132
- return this.data;
156
+ else if (this.body instanceof Buffer) {
157
+ return this.body;
133
158
  }
134
- else if (this.data instanceof Uint8Array) {
135
- return Buffer.from(this.data);
159
+ else if (this.body instanceof Uint8Array) {
160
+ return Buffer.from(this.body);
136
161
  }
137
- else if (this.data instanceof Object) {
138
- return Buffer.from(JSON.stringify(this.data), "utf-8");
162
+ else if (typeof this.body === "object") {
163
+ return Buffer.from(JSON.stringify(this.body), "utf-8");
139
164
  }
140
165
  else {
141
- return Buffer.from(this.data, "utf-8");
166
+ return Buffer.from(this.body, "utf-8");
142
167
  }
143
168
  }
144
169
  }
@@ -227,4 +227,65 @@ export class OINOHttpResult extends OINOResult {
227
227
  result.headers.set("ETag", this.getEtag());
228
228
  return result;
229
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
+ }
230
291
  }
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
1
3
  import { Buffer } from "node:buffer";
2
4
  import { OINOContentType, OINOHeaders, OINOHeadersInit } from ".";
3
5
  export interface OINORequestInit {
@@ -19,11 +21,12 @@ export declare class OINORequest {
19
21
  */
20
22
  constructor(init?: OINORequestInit);
21
23
  }
24
+ export type OINOHttpData = string | Buffer | Uint8Array | null;
22
25
  export interface OINOHttpRequestInit extends OINORequestInit {
23
- url?: URL;
26
+ url?: URL | string;
24
27
  method?: string;
25
28
  headers?: OINOHeadersInit;
26
- data?: string | Buffer | Uint8Array | object | null;
29
+ body?: OINOHttpData;
27
30
  requestType?: OINOContentType;
28
31
  responseType?: OINOContentType;
29
32
  multipartBoundary?: string;
@@ -33,15 +36,15 @@ export interface OINOHttpRequestInit extends OINORequestInit {
33
36
  * Specialized result for HTTP responses.
34
37
  */
35
38
  export declare class OINOHttpRequest extends OINORequest {
36
- readonly url?: URL;
37
- readonly method: string;
38
- readonly headers: OINOHeaders;
39
- readonly data: string | Buffer | Uint8Array | object | null;
40
- readonly requestType: OINOContentType;
41
- readonly responseType: OINOContentType;
42
- readonly multipartBoundary?: string;
43
- readonly lastModified?: number;
44
- readonly etags?: string[];
39
+ url?: URL;
40
+ method: string;
41
+ headers: OINOHeaders;
42
+ body: OINOHttpData;
43
+ requestType: OINOContentType;
44
+ responseType: OINOContentType;
45
+ multipartBoundary?: string;
46
+ lastModified?: number;
47
+ etags?: string[];
45
48
  /**
46
49
  * Constructor for a `OINOHttpRequest`
47
50
  *
@@ -49,9 +52,31 @@ export declare class OINOHttpRequest extends OINORequest {
49
52
  *
50
53
  */
51
54
  constructor(init: OINOHttpRequestInit);
55
+ /**
56
+ * Creates a `OINOHttpRequest` from a Fetch API `Request` object.
57
+ *
58
+ * @param request Fetch request
59
+ *
60
+ */
52
61
  static fromFetchRequest(request: Request): Promise<OINOHttpRequest>;
53
- dataAsText(): string;
54
- dataAsParsedJson(): any;
55
- dataAsFormData(): URLSearchParams;
56
- dataAsBuffer(): Buffer;
62
+ /**
63
+ * Returns the request data as a text string.
64
+ *
65
+ */
66
+ bodyAsText(): string;
67
+ /**
68
+ * Returns the request data parsed as JSON object.
69
+ *
70
+ */
71
+ bodyAsParsedJson(): any;
72
+ /**
73
+ * Returns the request data as URLSearchParams (form data).
74
+ *
75
+ */
76
+ bodyAsFormData(): URLSearchParams;
77
+ /**
78
+ * Returns the request data as Buffer.
79
+ *
80
+ */
81
+ bodyAsBuffer(): Buffer;
57
82
  }
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
1
3
  import { Buffer } from "node:buffer";
2
4
  import { OINOHeaders, OINOHeadersInit } from ".";
3
5
  export interface OINOResultInit {
@@ -127,4 +129,31 @@ export declare class OINOHttpResult extends OINOResult {
127
129
  * @param headers HTTP headers (overrides existing values)
128
130
  */
129
131
  getFetchResponse(headers?: OINOHeadersInit): Response;
132
+ /**
133
+ * Create from a Response object from the result values.
134
+ *
135
+ * @param response fetch Response object
136
+ *
137
+ */
138
+ static fromFetchResponse(response: Response): Promise<OINOHttpResult>;
139
+ /**
140
+ * Returns the request body as a text string.
141
+ *
142
+ */
143
+ bodyAsText(): string;
144
+ /**
145
+ * Returns the request body parsed as JSON object.
146
+ *
147
+ */
148
+ bodyAsParsedJson(): any;
149
+ /**
150
+ * Returns the request body as URLSearchParams (form body).
151
+ *
152
+ */
153
+ bodyAsFormData(): URLSearchParams;
154
+ /**
155
+ * Returns the request body as Buffer.
156
+ *
157
+ */
158
+ bodyAsBuffer(): Buffer;
130
159
  }
@@ -1,7 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
3
  export { OINOResult, OINOHttpResult, type OINOResultInit, type OINOHttpResultInit } from "./OINOResult.js";
4
- export { OINORequest, OINOHttpRequest, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js";
4
+ export { OINORequest, OINOHttpRequest, type OINOHttpData, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js";
5
5
  export { OINOStr } from "./OINOStr.js";
6
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
7
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/common",
3
- "version": "0.17.5",
3
+ "version": "0.19.0",
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.17.5",
22
+ "@oino-ts/types": "0.19.0",
23
23
  "@types/node": "^22.0.0",
24
24
  "typescript": "~5.9.0"
25
25
  },
@@ -32,11 +32,13 @@ export class OINORequest {
32
32
  }
33
33
  }
34
34
 
35
+ export type OINOHttpData = string|Buffer|Uint8Array|null
36
+
35
37
  export interface OINOHttpRequestInit extends OINORequestInit {
36
- url?: URL
38
+ url?: URL|string
37
39
  method?: string
38
40
  headers?: OINOHeadersInit
39
- data?: string|Buffer|Uint8Array|object|null
41
+ body?: OINOHttpData
40
42
  requestType?:OINOContentType
41
43
  responseType?:OINOContentType
42
44
  multipartBoundary?:string
@@ -47,15 +49,15 @@ export interface OINOHttpRequestInit extends OINORequestInit {
47
49
  * Specialized result for HTTP responses.
48
50
  */
49
51
  export class OINOHttpRequest extends OINORequest {
50
- readonly url?: URL
51
- readonly method: string
52
- readonly headers: OINOHeaders
53
- readonly data: string|Buffer|Uint8Array|object|null
54
- readonly requestType:OINOContentType
55
- readonly responseType:OINOContentType
56
- readonly multipartBoundary?:string
57
- readonly lastModified?:number
58
- readonly etags?:string[]
52
+ url?: URL
53
+ method: string
54
+ headers: OINOHeaders
55
+ body: OINOHttpData
56
+ requestType:OINOContentType
57
+ responseType:OINOContentType
58
+ multipartBoundary?:string
59
+ lastModified?:number
60
+ etags?:string[]
59
61
 
60
62
  /**
61
63
  * Constructor for a `OINOHttpRequest`
@@ -65,10 +67,10 @@ export class OINOHttpRequest extends OINORequest {
65
67
  */
66
68
  constructor(init: OINOHttpRequestInit) {
67
69
  super(init)
68
- this.url = init.url
69
- this.method = init.method ?? "GET"
70
+ this.url = typeof init.url === "string" ? new URL(init.url) : init.url
71
+ this.method = init.method?.toUpperCase() ?? "GET"
70
72
  this.headers = new OINOHeaders(init.headers)
71
- this.data = init.data ?? ""
73
+ this.body = init.body ?? null
72
74
  this.multipartBoundary = ""
73
75
  this.lastModified = init.lastModified
74
76
 
@@ -118,51 +120,76 @@ export class OINOHttpRequest extends OINORequest {
118
120
  }
119
121
  }
120
122
 
123
+ /**
124
+ * Creates a `OINOHttpRequest` from a Fetch API `Request` object.
125
+ *
126
+ * @param request Fetch request
127
+ *
128
+ */
121
129
  static async fromFetchRequest(request: Request): Promise<OINOHttpRequest> {
122
130
  const body = await request.arrayBuffer()
123
131
  return new OINOHttpRequest({
124
132
  url: new URL(request.url),
125
133
  method: request.method,
126
134
  headers: Object.fromEntries(request.headers as any),
127
- data: Buffer.from(body),
135
+ body: Buffer.from(body),
128
136
  })
129
137
  }
130
138
 
131
- dataAsText(): string {
132
- if (this.data instanceof Uint8Array) {
133
- return new TextDecoder().decode(this.data)
139
+ /**
140
+ * Returns the request data as a text string.
141
+ *
142
+ */
143
+ bodyAsText(): string {
144
+ if (this.body == null) {
145
+ return ""
146
+
147
+ } else if (this.body instanceof Uint8Array) {
148
+ return new TextDecoder().decode(this.body)
134
149
 
135
- } else if (this.data instanceof Object) {
136
- return JSON.stringify(this.data)
150
+ } else if (typeof this.body === "object") {
151
+ return JSON.stringify(this.body)
137
152
 
138
153
  } else {
139
- return this.data?.toString() || ""
154
+ return this.body?.toString() || ""
140
155
  }
141
156
  }
142
157
 
143
- dataAsParsedJson(): any {
144
- return this.data ? JSON.parse(this.dataAsText()) : {}
158
+ /**
159
+ * Returns the request data parsed as JSON object.
160
+ *
161
+ */
162
+ bodyAsParsedJson(): any {
163
+ return this.body ? JSON.parse(this.bodyAsText()) : {}
145
164
  }
146
165
 
147
- dataAsFormData(): URLSearchParams {
148
- return new URLSearchParams(this.dataAsText() || "")
166
+ /**
167
+ * Returns the request data as URLSearchParams (form data).
168
+ *
169
+ */
170
+ bodyAsFormData(): URLSearchParams {
171
+ return new URLSearchParams(this.bodyAsText() || "")
149
172
  }
150
-
151
- dataAsBuffer(): Buffer {
152
- if (this.data === null) {
173
+
174
+ /**
175
+ * Returns the request data as Buffer.
176
+ *
177
+ */
178
+ bodyAsBuffer(): Buffer {
179
+ if ((this.body === null) || (this.body === undefined)) {
153
180
  return Buffer.alloc(0)
154
181
 
155
- } else if (this.data instanceof Buffer) {
156
- return this.data
182
+ } else if (this.body instanceof Buffer) {
183
+ return this.body
157
184
 
158
- } else if (this.data instanceof Uint8Array) {
159
- return Buffer.from(this.data)
185
+ } else if (this.body instanceof Uint8Array) {
186
+ return Buffer.from(this.body)
160
187
 
161
- } else if (this.data instanceof Object) {
162
- return Buffer.from(JSON.stringify(this.data), "utf-8")
188
+ } else if (typeof this.body === "object") {
189
+ return Buffer.from(JSON.stringify(this.body), "utf-8")
163
190
 
164
191
  } else {
165
- return Buffer.from(this.data, "utf-8")
192
+ return Buffer.from(this.body, "utf-8")
166
193
  }
167
194
  }
168
195
 
package/src/OINOResult.ts CHANGED
@@ -262,4 +262,69 @@ export class OINOHttpResult extends OINOResult {
262
262
  return result
263
263
  }
264
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
+ }
265
330
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js"
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
3
  export { OINOResult, OINOHttpResult, type OINOResultInit, type OINOHttpResultInit } from "./OINOResult.js"
4
- export { OINORequest, OINOHttpRequest, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js"
4
+ export { OINORequest, OINOHttpRequest, type OINOHttpData, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js"
5
5
  export { OINOStr } from "./OINOStr.js"
6
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js"
7
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js"