@oino-ts/common 0.18.1 → 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.
@@ -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.body = init.body;
54
+ this.body = init.body ?? null;
55
55
  this.multipartBoundary = "";
56
56
  this.lastModified = init.lastModified;
57
57
  if (init.multipartBoundary) {
@@ -122,10 +122,13 @@ class OINOHttpRequest extends OINORequest {
122
122
  *
123
123
  */
124
124
  bodyAsText() {
125
- if (this.body instanceof Uint8Array) {
125
+ if (this.body == null) {
126
+ return "";
127
+ }
128
+ else if (this.body instanceof Uint8Array) {
126
129
  return new TextDecoder().decode(this.body);
127
130
  }
128
- else if (this.body instanceof Object) {
131
+ else if (typeof this.body === "object") {
129
132
  return JSON.stringify(this.body);
130
133
  }
131
134
  else {
@@ -160,7 +163,7 @@ class OINOHttpRequest extends OINORequest {
160
163
  else if (this.body instanceof Uint8Array) {
161
164
  return node_buffer_1.Buffer.from(this.body);
162
165
  }
163
- else if (this.body instanceof Object) {
166
+ else if (typeof this.body === "object") {
164
167
  return node_buffer_1.Buffer.from(JSON.stringify(this.body), "utf-8");
165
168
  }
166
169
  else {
@@ -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.body = init.body;
50
+ this.body = init.body ?? null;
51
51
  this.multipartBoundary = "";
52
52
  this.lastModified = init.lastModified;
53
53
  if (init.multipartBoundary) {
@@ -118,10 +118,13 @@ export class OINOHttpRequest extends OINORequest {
118
118
  *
119
119
  */
120
120
  bodyAsText() {
121
- if (this.body instanceof Uint8Array) {
121
+ if (this.body == null) {
122
+ return "";
123
+ }
124
+ else if (this.body instanceof Uint8Array) {
122
125
  return new TextDecoder().decode(this.body);
123
126
  }
124
- else if (this.body instanceof Object) {
127
+ else if (typeof this.body === "object") {
125
128
  return JSON.stringify(this.body);
126
129
  }
127
130
  else {
@@ -156,7 +159,7 @@ export class OINOHttpRequest extends OINORequest {
156
159
  else if (this.body instanceof Uint8Array) {
157
160
  return Buffer.from(this.body);
158
161
  }
159
- else if (this.body instanceof Object) {
162
+ else if (typeof this.body === "object") {
160
163
  return Buffer.from(JSON.stringify(this.body), "utf-8");
161
164
  }
162
165
  else {
@@ -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
- body?: string | Buffer | Uint8Array | object | null | undefined;
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 body: string | Buffer | Uint8Array | object | null | undefined;
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
  *
@@ -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 {
@@ -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.18.1",
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.18.1",
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
- body?: string|Buffer|Uint8Array|object|null|undefined
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 body: string|Buffer|Uint8Array|object|null|undefined
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.body = init.body
73
+ this.body = init.body ?? null
72
74
  this.multipartBoundary = ""
73
75
  this.lastModified = init.lastModified
74
76
 
@@ -139,10 +141,13 @@ export class OINOHttpRequest extends OINORequest {
139
141
  *
140
142
  */
141
143
  bodyAsText(): string {
142
- if (this.body instanceof Uint8Array) {
144
+ if (this.body == null) {
145
+ return ""
146
+
147
+ } else if (this.body instanceof Uint8Array) {
143
148
  return new TextDecoder().decode(this.body)
144
149
 
145
- } else if (this.body instanceof Object) {
150
+ } else if (typeof this.body === "object") {
146
151
  return JSON.stringify(this.body)
147
152
 
148
153
  } else {
@@ -180,7 +185,7 @@ export class OINOHttpRequest extends OINORequest {
180
185
  } else if (this.body instanceof Uint8Array) {
181
186
  return Buffer.from(this.body)
182
187
 
183
- } else if (this.body instanceof Object) {
188
+ } else if (typeof this.body === "object") {
184
189
  return Buffer.from(JSON.stringify(this.body), "utf-8")
185
190
 
186
191
  } else {
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"