@oino-ts/common 0.21.2 → 1.0.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.
Files changed (69) hide show
  1. package/README.md +183 -0
  2. package/dist/cjs/OINOApi.js +322 -0
  3. package/dist/cjs/OINOConfig.js +104 -0
  4. package/dist/cjs/OINOConstants.js +42 -0
  5. package/dist/cjs/OINODataField.js +346 -0
  6. package/dist/cjs/OINODataModel.js +182 -0
  7. package/dist/cjs/OINODataSource.js +165 -0
  8. package/dist/cjs/OINOFormatter.js +6 -5
  9. package/dist/cjs/OINOHtmlTemplate.js +21 -18
  10. package/dist/cjs/OINOModelSet.js +333 -0
  11. package/dist/cjs/OINOParser.js +448 -0
  12. package/dist/cjs/OINOQueryParams.js +434 -0
  13. package/dist/cjs/OINORequest.js +21 -13
  14. package/dist/cjs/OINOResult.js +13 -12
  15. package/dist/cjs/OINOStr.js +11 -11
  16. package/dist/cjs/OINOSwagger.js +205 -0
  17. package/dist/cjs/index.js +57 -39
  18. package/dist/esm/OINOApi.js +315 -0
  19. package/dist/esm/OINOConfig.js +100 -0
  20. package/dist/esm/OINOConstants.js +39 -0
  21. package/dist/esm/OINODataField.js +337 -0
  22. package/dist/esm/OINODataModel.js +178 -0
  23. package/dist/esm/OINODataSource.js +159 -0
  24. package/dist/esm/OINOFormatter.js +2 -1
  25. package/dist/esm/OINOHtmlTemplate.js +4 -1
  26. package/dist/esm/OINOModelSet.js +329 -0
  27. package/dist/esm/OINOParser.js +444 -0
  28. package/dist/esm/OINOQueryParams.js +426 -0
  29. package/dist/esm/OINORequest.js +9 -1
  30. package/dist/esm/OINOResult.js +2 -1
  31. package/dist/esm/OINOStr.js +1 -1
  32. package/dist/esm/OINOSwagger.js +201 -0
  33. package/dist/esm/index.js +14 -32
  34. package/dist/types/OINOApi.d.ts +191 -0
  35. package/dist/types/OINOConfig.d.ts +63 -0
  36. package/dist/types/OINOConstants.d.ts +51 -0
  37. package/dist/types/OINODataField.d.ts +209 -0
  38. package/dist/types/OINODataModel.d.ts +78 -0
  39. package/dist/types/OINODataSource.d.ts +184 -0
  40. package/dist/types/OINOHtmlTemplate.d.ts +1 -1
  41. package/dist/types/OINOModelSet.d.ts +64 -0
  42. package/dist/types/OINOParser.d.ts +42 -0
  43. package/dist/types/OINOQueryParams.d.ts +270 -0
  44. package/dist/types/OINORequest.d.ts +4 -1
  45. package/dist/types/OINOResult.d.ts +1 -1
  46. package/dist/types/OINOStr.d.ts +1 -1
  47. package/dist/types/OINOSwagger.d.ts +25 -0
  48. package/dist/types/index.d.ts +14 -31
  49. package/package.json +32 -32
  50. package/src/OINOApi.ts +429 -0
  51. package/src/OINOBenchmark.ts +323 -323
  52. package/src/OINOConfig.ts +113 -0
  53. package/src/OINOConstants.ts +59 -0
  54. package/src/OINODataField.ts +371 -0
  55. package/src/OINODataModel.ts +187 -0
  56. package/src/OINODataSource.ts +280 -0
  57. package/src/OINOFormatter.ts +166 -165
  58. package/src/OINOHeaders.ts +51 -51
  59. package/src/OINOHtmlTemplate.test.ts +114 -114
  60. package/src/OINOHtmlTemplate.ts +225 -222
  61. package/src/OINOLog.ts +292 -292
  62. package/src/OINOModelSet.ts +359 -0
  63. package/src/OINOParser.ts +441 -0
  64. package/src/OINOQueryParams.ts +449 -0
  65. package/src/OINORequest.ts +204 -196
  66. package/src/OINOResult.ts +331 -330
  67. package/src/OINOStr.ts +254 -254
  68. package/src/OINOSwagger.ts +213 -0
  69. package/src/index.ts +18 -38
package/src/OINOResult.ts CHANGED
@@ -1,330 +1,331 @@
1
- /*
2
- * This Source Code Form is subject to the terms of the Mozilla Public
3
- * License, v. 2.0. If a copy of the MPL was not distributed with this
4
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
- */
6
-
7
- import { createHash, Hash } from "node:crypto";
8
- import { Buffer } from "node:buffer";
9
- import { OINO_DEBUG_PREFIX, OINO_ERROR_PREFIX, OINO_INFO_PREFIX, OINO_WARNING_PREFIX, OINOHeaders, OINOHeadersInit } from ".";
10
-
11
- export interface OINOResultInit {
12
- success?: boolean
13
- status?: number
14
- statusText?: string
15
- messages?: string[]
16
- }
17
-
18
- /**
19
- * OINO API request result object with returned data and/or http status code/message and
20
- * error / warning messages.
21
- *
22
- */
23
- export class OINOResult {
24
- /** Wheter request was successfully executed */
25
- success: boolean
26
-
27
- /** HTTP status code */
28
- status: number;
29
-
30
- /** HTTP status message */
31
- statusText: string;
32
-
33
- /** Error / warning messages */
34
- messages: string[];
35
-
36
- /**
37
- * Constructor of OINOResult.
38
- *
39
- * @param init initialization values
40
- *
41
- */
42
- constructor (init?: OINOResultInit) {
43
- this.success = init?.success ?? true
44
- this.status = init?.status ?? 200
45
- this.statusText = init?.statusText ?? "OK"
46
- this.messages = init?.messages ?? []
47
- }
48
-
49
- /**
50
- * Copy values from different result.
51
- *
52
- * @param result source value
53
- */
54
- copy(result: OINOResult) {
55
- this.success = result.success
56
- this.status = result.status
57
- this.statusText = result.statusText
58
- this.messages = result.messages.slice()
59
- }
60
-
61
- /**
62
- * Set HTTP OK status (does not reset messages).
63
- *
64
- */
65
- setOk() {
66
- this.success = true
67
- this.status = 200
68
- this.statusText = "OK"
69
- }
70
-
71
- /**
72
- * Set HTTP error status using given code and message. Returns self reference for chaining.
73
- *
74
- * @param status HTTP status code
75
- * @param statusText HTTP status message
76
- * @param operation operation where error occured
77
- *
78
- */
79
- setError(status:number, statusText:string, operation:string):OINOResult {
80
- this.success = false
81
- this.status = status
82
- if (this.statusText != "OK") {
83
- this.messages.push(this.statusText) // latest error becomes status, but if there was something non-trivial, add it to the messages
84
- }
85
- if (statusText.startsWith(OINO_ERROR_PREFIX)) {
86
- this.statusText = statusText
87
- } else {
88
- this.statusText = OINO_ERROR_PREFIX + " (" + operation + "): " + statusText
89
- }
90
- return this
91
- }
92
-
93
- /**
94
- * Add warning message. Returns self reference for chaining.
95
- *
96
- * @param message HTTP status message
97
- * @param operation operation where warning occured
98
- *
99
- */
100
- addWarning(message:string, operation:string):OINOResult {
101
- message = message.trim()
102
- if (message) {
103
- this.messages.push(OINO_WARNING_PREFIX + " (" + operation + "): " + message)
104
- }
105
- return this
106
- }
107
-
108
- /**
109
- * Add info message. Returns self reference for chaining.
110
- *
111
- * @param message HTTP status message
112
- * @param operation operation where info occured
113
- *
114
- */
115
- addInfo(message:string, operation:string):OINOResult {
116
- message = message.trim()
117
- if (message) {
118
- this.messages.push(OINO_INFO_PREFIX + " (" + operation + "): " + message)
119
- }
120
- return this
121
- }
122
-
123
- /**
124
- * Add debug message. Returns self reference for chaining.
125
- *
126
- * @param message HTTP status message
127
- * @param operation operation where debug occured
128
- *
129
- */
130
- addDebug(message:string, operation:string):OINOResult {
131
- message = message.trim()
132
- if (message) {
133
- this.messages.push(OINO_DEBUG_PREFIX + " (" + operation + "): " + message)
134
- }
135
- return this
136
- }
137
-
138
- /**
139
- * Copy given messages to HTTP headers.
140
- *
141
- * @param headers HTTP headers
142
- * @param copyErrors wether error messages should be copied (default true)
143
- * @param copyWarnings wether warning messages should be copied (default false)
144
- * @param copyInfos wether info messages should be copied (default false)
145
- * @param copyDebug wether debug messages should be copied (default false)
146
- *
147
- */
148
- copyMessagesToHeaders(headers:OINOHeaders, copyErrors:boolean = true, copyWarnings:boolean = false, copyInfos:boolean = false, copyDebug:boolean = false) {
149
- let j=1
150
- for(let i=0; i<this.messages.length; i++) {
151
- const message = this.messages[i].replaceAll("\r", " ").replaceAll("\n", " ")
152
- if (copyErrors && message.startsWith(OINO_ERROR_PREFIX)) {
153
- headers.set('X-OINO-MESSAGE-'+j, message)
154
- j++
155
- }
156
- if (copyWarnings && message.startsWith(OINO_WARNING_PREFIX)) {
157
- headers.set('X-OINO-MESSAGE-'+j, message)
158
- j++
159
- }
160
- if (copyInfos && message.startsWith(OINO_INFO_PREFIX)) {
161
- headers.set('X-OINO-MESSAGE-'+j, message)
162
- j++
163
- }
164
- if (copyDebug && message.startsWith(OINO_DEBUG_PREFIX)) {
165
- headers.set('X-OINO-MESSAGE-'+j, message)
166
- j++
167
- }
168
- }
169
- }
170
-
171
- /**
172
- * Print result for logging.
173
- *
174
- */
175
- printLog() {
176
- return "OINOResult: status=" + this.status + ", statusText=" + this.statusText + ", messages=[" + this.messages.join(", ") + "]"
177
- }
178
- }
179
-
180
- export interface OINOHttpResultInit extends OINOResultInit {
181
- body?: string|Buffer
182
- headers?: OINOHeadersInit
183
- expires?: number
184
- lastModified?: number
185
- }
186
-
187
- /**
188
- * Specialized result for HTTP responses.
189
- */
190
- export class OINOHttpResult extends OINOResult {
191
- private _etag:string
192
-
193
- /** HTTP body data */
194
- readonly body: string|Buffer
195
-
196
- /** HTTP headers */
197
- readonly headers: OINOHeaders
198
-
199
- /** HTTP cache expiration value
200
- * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
201
- */
202
- expires: number
203
-
204
- /** HTTP cache last-modified value */
205
- lastModified: number
206
-
207
- /**
208
- * Constructor for a `OINOHttpResult`
209
- *
210
- * @param init initialization values
211
- *
212
- */
213
- constructor(init?: OINOHttpResultInit) {
214
- super(init)
215
- this.body = init?.body ?? ""
216
- this.headers = new OINOHeaders(init?.headers)
217
- this.expires = init?.expires ?? 0
218
- this.lastModified = init?.lastModified ?? 0
219
- this._etag = ""
220
- }
221
-
222
- /**
223
- * Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
224
- *
225
- */
226
- getEtag():string {
227
- if (this._etag == "") {
228
- const hash:Hash = createHash("sha256")
229
- this._etag = hash.update(this.body).digest("hex")
230
- }
231
- return this._etag
232
- }
233
-
234
- /**
235
- * Get a Response object from the result values.
236
- *
237
- * @param headers HTTP headers (overrides existing values)
238
- */
239
- getFetchResponse(headers?:OINOHeadersInit):Response {
240
- const merged_headers = new OINOHeaders(this.headers)
241
- if (headers) {
242
- merged_headers.setHeaders(headers)
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 })
251
- result.headers.set('Content-Length', this.body.length.toString())
252
- if (merged_headers['Last-Modified'] === undefined && this.lastModified > 0) {
253
- result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString())
254
- }
255
- if (merged_headers['Expires'] === undefined && this.expires >= 0) {
256
- result.headers.set('Expires', new Date(Date.now() + Math.round(this.expires)).toUTCString())
257
- if (this.expires == 0) {
258
- result.headers.set('Pragma', 'no-cache')
259
- }
260
- }
261
- result.headers.set("ETag", this.getEtag())
262
- return result
263
- }
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
- }
330
- }
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ import { createHash, Hash } from "node:crypto";
8
+ import { Buffer } from "node:buffer";
9
+ import { OINO_DEBUG_PREFIX, OINO_ERROR_PREFIX, OINO_INFO_PREFIX, OINO_WARNING_PREFIX, OINOContentType } from "./OINOConstants.js"
10
+ import { OINOHeaders, type OINOHeadersInit } from "./OINOHeaders.js"
11
+
12
+ export interface OINOResultInit {
13
+ success?: boolean
14
+ status?: number
15
+ statusText?: string
16
+ messages?: string[]
17
+ }
18
+
19
+ /**
20
+ * OINO API request result object with returned data and/or http status code/message and
21
+ * error / warning messages.
22
+ *
23
+ */
24
+ export class OINOResult {
25
+ /** Wheter request was successfully executed */
26
+ success: boolean
27
+
28
+ /** HTTP status code */
29
+ status: number;
30
+
31
+ /** HTTP status message */
32
+ statusText: string;
33
+
34
+ /** Error / warning messages */
35
+ messages: string[];
36
+
37
+ /**
38
+ * Constructor of OINOResult.
39
+ *
40
+ * @param init initialization values
41
+ *
42
+ */
43
+ constructor (init?: OINOResultInit) {
44
+ this.success = init?.success ?? true
45
+ this.status = init?.status ?? 200
46
+ this.statusText = init?.statusText ?? "OK"
47
+ this.messages = init?.messages ?? []
48
+ }
49
+
50
+ /**
51
+ * Copy values from different result.
52
+ *
53
+ * @param result source value
54
+ */
55
+ copy(result: OINOResult) {
56
+ this.success = result.success
57
+ this.status = result.status
58
+ this.statusText = result.statusText
59
+ this.messages = result.messages.slice()
60
+ }
61
+
62
+ /**
63
+ * Set HTTP OK status (does not reset messages).
64
+ *
65
+ */
66
+ setOk() {
67
+ this.success = true
68
+ this.status = 200
69
+ this.statusText = "OK"
70
+ }
71
+
72
+ /**
73
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
74
+ *
75
+ * @param status HTTP status code
76
+ * @param statusText HTTP status message
77
+ * @param operation operation where error occured
78
+ *
79
+ */
80
+ setError(status:number, statusText:string, operation:string):OINOResult {
81
+ this.success = false
82
+ this.status = status
83
+ if (this.statusText != "OK") {
84
+ this.messages.push(this.statusText) // latest error becomes status, but if there was something non-trivial, add it to the messages
85
+ }
86
+ if (statusText.startsWith(OINO_ERROR_PREFIX)) {
87
+ this.statusText = statusText
88
+ } else {
89
+ this.statusText = OINO_ERROR_PREFIX + " (" + operation + "): " + statusText
90
+ }
91
+ return this
92
+ }
93
+
94
+ /**
95
+ * Add warning message. Returns self reference for chaining.
96
+ *
97
+ * @param message HTTP status message
98
+ * @param operation operation where warning occured
99
+ *
100
+ */
101
+ addWarning(message:string, operation:string):OINOResult {
102
+ message = message.trim()
103
+ if (message) {
104
+ this.messages.push(OINO_WARNING_PREFIX + " (" + operation + "): " + message)
105
+ }
106
+ return this
107
+ }
108
+
109
+ /**
110
+ * Add info message. Returns self reference for chaining.
111
+ *
112
+ * @param message HTTP status message
113
+ * @param operation operation where info occured
114
+ *
115
+ */
116
+ addInfo(message:string, operation:string):OINOResult {
117
+ message = message.trim()
118
+ if (message) {
119
+ this.messages.push(OINO_INFO_PREFIX + " (" + operation + "): " + message)
120
+ }
121
+ return this
122
+ }
123
+
124
+ /**
125
+ * Add debug message. Returns self reference for chaining.
126
+ *
127
+ * @param message HTTP status message
128
+ * @param operation operation where debug occured
129
+ *
130
+ */
131
+ addDebug(message:string, operation:string):OINOResult {
132
+ message = message.trim()
133
+ if (message) {
134
+ this.messages.push(OINO_DEBUG_PREFIX + " (" + operation + "): " + message)
135
+ }
136
+ return this
137
+ }
138
+
139
+ /**
140
+ * Copy given messages to HTTP headers.
141
+ *
142
+ * @param headers HTTP headers
143
+ * @param copyErrors wether error messages should be copied (default true)
144
+ * @param copyWarnings wether warning messages should be copied (default false)
145
+ * @param copyInfos wether info messages should be copied (default false)
146
+ * @param copyDebug wether debug messages should be copied (default false)
147
+ *
148
+ */
149
+ copyMessagesToHeaders(headers:OINOHeaders, copyErrors:boolean = true, copyWarnings:boolean = false, copyInfos:boolean = false, copyDebug:boolean = false) {
150
+ let j=1
151
+ for(let i=0; i<this.messages.length; i++) {
152
+ const message = this.messages[i].replaceAll("\r", " ").replaceAll("\n", " ")
153
+ if (copyErrors && message.startsWith(OINO_ERROR_PREFIX)) {
154
+ headers.set('X-OINO-MESSAGE-'+j, message)
155
+ j++
156
+ }
157
+ if (copyWarnings && message.startsWith(OINO_WARNING_PREFIX)) {
158
+ headers.set('X-OINO-MESSAGE-'+j, message)
159
+ j++
160
+ }
161
+ if (copyInfos && message.startsWith(OINO_INFO_PREFIX)) {
162
+ headers.set('X-OINO-MESSAGE-'+j, message)
163
+ j++
164
+ }
165
+ if (copyDebug && message.startsWith(OINO_DEBUG_PREFIX)) {
166
+ headers.set('X-OINO-MESSAGE-'+j, message)
167
+ j++
168
+ }
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Print result for logging.
174
+ *
175
+ */
176
+ printLog() {
177
+ return "OINOResult: status=" + this.status + ", statusText=" + this.statusText + ", messages=[" + this.messages.join(", ") + "]"
178
+ }
179
+ }
180
+
181
+ export interface OINOHttpResultInit extends OINOResultInit {
182
+ body?: string|Buffer
183
+ headers?: OINOHeadersInit
184
+ expires?: number
185
+ lastModified?: number
186
+ }
187
+
188
+ /**
189
+ * Specialized result for HTTP responses.
190
+ */
191
+ export class OINOHttpResult extends OINOResult {
192
+ private _etag:string
193
+
194
+ /** HTTP body data */
195
+ readonly body: string|Buffer
196
+
197
+ /** HTTP headers */
198
+ readonly headers: OINOHeaders
199
+
200
+ /** HTTP cache expiration value
201
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
202
+ */
203
+ expires: number
204
+
205
+ /** HTTP cache last-modified value */
206
+ lastModified: number
207
+
208
+ /**
209
+ * Constructor for a `OINOHttpResult`
210
+ *
211
+ * @param init initialization values
212
+ *
213
+ */
214
+ constructor(init?: OINOHttpResultInit) {
215
+ super(init)
216
+ this.body = init?.body ?? ""
217
+ this.headers = new OINOHeaders(init?.headers)
218
+ this.expires = init?.expires ?? 0
219
+ this.lastModified = init?.lastModified ?? 0
220
+ this._etag = ""
221
+ }
222
+
223
+ /**
224
+ * Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
225
+ *
226
+ */
227
+ getEtag():string {
228
+ if (this._etag == "") {
229
+ const hash:Hash = createHash("sha256")
230
+ this._etag = hash.update(this.body).digest("hex")
231
+ }
232
+ return this._etag
233
+ }
234
+
235
+ /**
236
+ * Get a Response object from the result values.
237
+ *
238
+ * @param headers HTTP headers (overrides existing values)
239
+ */
240
+ getFetchResponse(headers?:OINOHeadersInit):Response {
241
+ const merged_headers = new OINOHeaders(this.headers)
242
+ if (headers) {
243
+ merged_headers.setHeaders(headers)
244
+ }
245
+ let body: string|BufferSource
246
+ if (this.body instanceof Buffer) {
247
+ body = new Uint8Array(this.body)
248
+ } else {
249
+ body = this.body as string
250
+ }
251
+ const result: Response = new Response(body, { status: this.status, statusText: this.statusText, headers: merged_headers })
252
+ result.headers.set('Content-Length', this.body.length.toString())
253
+ if (merged_headers['Last-Modified'] === undefined && this.lastModified > 0) {
254
+ result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString())
255
+ }
256
+ if (merged_headers['Expires'] === undefined && this.expires >= 0) {
257
+ result.headers.set('Expires', new Date(Date.now() + Math.round(this.expires)).toUTCString())
258
+ if (this.expires == 0) {
259
+ result.headers.set('Pragma', 'no-cache')
260
+ }
261
+ }
262
+ result.headers.set("ETag", this.getEtag())
263
+ return result
264
+ }
265
+
266
+ /**
267
+ * Create from a Response object from the result values.
268
+ *
269
+ * @param response fetch Response object
270
+ *
271
+ */
272
+ static async fromFetchResponse(response: Response): Promise<OINOHttpResult> {
273
+ const body = Buffer.from(await response.arrayBuffer())
274
+ const last_modified = response.headers.get('Last-Modified')
275
+ const expires = response.headers.get('Expires')
276
+ const result = new OINOHttpResult({
277
+ status: response.status,
278
+ statusText: response.statusText,
279
+ body: body,
280
+ headers: response.headers as unknown as OINOHeadersInit,
281
+ lastModified: last_modified ? (new Date(last_modified).getTime()) : 0,
282
+ expires: expires ? (new Date(expires).getTime() - Date.now()) : 0
283
+ })
284
+ return result
285
+ }
286
+
287
+ /**
288
+ * Returns the request body as a text string.
289
+ *
290
+ */
291
+ bodyAsText(): string {
292
+ if (this.body instanceof Buffer) {
293
+ return this.body.toString("utf-8")
294
+
295
+ } else {
296
+ return this.body as string || ""
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Returns the request body parsed as JSON object.
302
+ *
303
+ */
304
+ bodyAsParsedJson(): any {
305
+ return this.body ? JSON.parse(this.bodyAsText()) : {}
306
+ }
307
+
308
+ /**
309
+ * Returns the request body as URLSearchParams (form body).
310
+ *
311
+ */
312
+ bodyAsFormData(): URLSearchParams {
313
+ return new URLSearchParams(this.bodyAsText() || "")
314
+ }
315
+
316
+ /**
317
+ * Returns the request body as Buffer.
318
+ *
319
+ */
320
+ bodyAsBuffer(): Buffer {
321
+ if (this.body === null) {
322
+ return Buffer.alloc(0)
323
+
324
+ } else if (this.body instanceof Buffer) {
325
+ return this.body
326
+
327
+ } else {
328
+ return Buffer.from(this.body as string, "utf-8")
329
+ }
330
+ }
331
+ }