@oino-ts/common 0.16.2 → 0.17.2

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.
@@ -66,7 +66,7 @@ class OINOHtmlTemplate {
66
66
  }
67
67
  }
68
68
  _createHttpResult(html) {
69
- const result = new _1.OINOHttpResult(html);
69
+ const result = new _1.OINOHttpResult({ body: html });
70
70
  if (this.expires >= 1) {
71
71
  result.expires = Math.round(this.expires);
72
72
  }
@@ -183,8 +183,8 @@ class OINOHtmlTemplate {
183
183
  */
184
184
  renderFromResult(result, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
185
185
  _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
186
- this.setVariableFromValue("statusCode", result.statusCode.toString());
187
- this.setVariableFromValue("statusMessage", result.statusMessage.toString());
186
+ this.setVariableFromValue("status", result.status.toString());
187
+ this.setVariableFromValue("statusText", result.statusText.toString());
188
188
  let messages = [];
189
189
  for (let i = 0; i < result.messages.length; i++) {
190
190
  if (includeErrorMessages && result.messages[i].startsWith(_1.OINO_ERROR_PREFIX)) {
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINOHttpRequest = exports.OINORequest = void 0;
9
+ const _1 = require(".");
10
+ const index_js_1 = require("./index.js");
11
+ /**
12
+ * OINO API request result object with returned data and/or http status code/message and
13
+ * error / warning messages.
14
+ *
15
+ */
16
+ class OINORequest {
17
+ /** Key-value parameters */
18
+ params;
19
+ /**
20
+ * Constructor of OINORequest.
21
+ *
22
+ * @param init initialization values
23
+ *
24
+ */
25
+ constructor(init) {
26
+ this.params = init?.params ?? {};
27
+ }
28
+ /**
29
+ * Copy values from different result.
30
+ *
31
+ * @param request source value
32
+ */
33
+ copy(request) {
34
+ this.params = { ...request.params };
35
+ }
36
+ }
37
+ exports.OINORequest = OINORequest;
38
+ /**
39
+ * Specialized result for HTTP responses.
40
+ */
41
+ class OINOHttpRequest extends OINORequest {
42
+ url;
43
+ method;
44
+ headers;
45
+ data;
46
+ requestType;
47
+ responseType;
48
+ multipartBoundary;
49
+ lastModified;
50
+ etags;
51
+ /**
52
+ * Constructor for a `OINOHttpRequest`
53
+ *
54
+ * @param init initialization values
55
+ *
56
+ */
57
+ constructor(init) {
58
+ super(init);
59
+ this.url = init.url;
60
+ this.method = init.method ?? "GET";
61
+ this.headers = init.headers ?? {};
62
+ this.data = init.data ?? "";
63
+ this.multipartBoundary = "";
64
+ this.lastModified = init.lastModified;
65
+ if (init.multipartBoundary) {
66
+ this.multipartBoundary = init.multipartBoundary;
67
+ }
68
+ if (init.requestType) {
69
+ this.requestType = init.requestType;
70
+ }
71
+ else {
72
+ const request_type_param = this.url?.searchParams.get(index_js_1.OINO_REQUEST_TYPE_PARAM) || this.headers["content-type"]; // content-type header can be overridden by query parameter
73
+ if (request_type_param == _1.OINOContentType.csv) {
74
+ this.requestType = _1.OINOContentType.csv;
75
+ }
76
+ else if (request_type_param == _1.OINOContentType.urlencode) {
77
+ this.requestType = _1.OINOContentType.urlencode;
78
+ }
79
+ else if (request_type_param?.startsWith(_1.OINOContentType.formdata)) {
80
+ this.requestType = _1.OINOContentType.formdata;
81
+ if (!this.multipartBoundary) {
82
+ this.multipartBoundary = request_type_param.split('boundary=')[1] || "";
83
+ }
84
+ }
85
+ else {
86
+ this.requestType = _1.OINOContentType.json;
87
+ }
88
+ }
89
+ if (init.responseType) {
90
+ this.responseType = init.responseType;
91
+ }
92
+ else {
93
+ const response_type_param = this.url?.searchParams.get(index_js_1.OINO_RESPONSE_TYPE_PARAM) || this.headers["accept"]; // accept header can be overridden by query parameter
94
+ const accept_types = response_type_param?.split(', ') || [];
95
+ let response_type = undefined;
96
+ for (let i = 0; i < accept_types.length; i++) {
97
+ if (Object.values(_1.OINOContentType).includes(accept_types[i])) {
98
+ response_type = accept_types[i];
99
+ break;
100
+ }
101
+ }
102
+ this.responseType = response_type ?? _1.OINOContentType.json;
103
+ }
104
+ const last_modified = this.headers["if-modified-since"];
105
+ if (last_modified) {
106
+ this.lastModified = new Date(last_modified).getTime();
107
+ }
108
+ const etags = this.headers["if-none-match"]?.split(',').map(e => e.trim());
109
+ if (etags) {
110
+ this.etags = etags;
111
+ }
112
+ }
113
+ static async fromRequest(request) {
114
+ const body = await request.arrayBuffer();
115
+ return new OINOHttpRequest({
116
+ url: new URL(request.url),
117
+ method: request.method,
118
+ headers: Object.fromEntries(request.headers),
119
+ data: Buffer.from(body),
120
+ });
121
+ }
122
+ }
123
+ exports.OINOHttpRequest = OINOHttpRequest;
@@ -17,20 +17,22 @@ class OINOResult {
17
17
  /** Wheter request was successfully executed */
18
18
  success;
19
19
  /** HTTP status code */
20
- statusCode;
20
+ status;
21
21
  /** HTTP status message */
22
- statusMessage;
22
+ statusText;
23
23
  /** Error / warning messages */
24
24
  messages;
25
25
  /**
26
26
  * Constructor of OINOResult.
27
27
  *
28
+ * @param init initialization values
29
+ *
28
30
  */
29
- constructor() {
30
- this.success = true;
31
- this.statusCode = 200;
32
- this.statusMessage = "OK";
33
- this.messages = [];
31
+ constructor(init) {
32
+ this.success = init?.success ?? true;
33
+ this.status = init?.status ?? 200;
34
+ this.statusText = init?.statusText ?? "OK";
35
+ this.messages = init?.messages ?? [];
34
36
  }
35
37
  /**
36
38
  * Copy values from different result.
@@ -39,8 +41,8 @@ class OINOResult {
39
41
  */
40
42
  copy(result) {
41
43
  this.success = result.success;
42
- this.statusCode = result.statusCode;
43
- this.statusMessage = result.statusMessage;
44
+ this.status = result.status;
45
+ this.statusText = result.statusText;
44
46
  this.messages = result.messages.slice();
45
47
  }
46
48
  /**
@@ -49,28 +51,28 @@ class OINOResult {
49
51
  */
50
52
  setOk() {
51
53
  this.success = true;
52
- this.statusCode = 200;
53
- this.statusMessage = "OK";
54
+ this.status = 200;
55
+ this.statusText = "OK";
54
56
  }
55
57
  /**
56
58
  * Set HTTP error status using given code and message. Returns self reference for chaining.
57
59
  *
58
- * @param statusCode HTTP status code
59
- * @param statusMessage HTTP status message
60
+ * @param status HTTP status code
61
+ * @param statusText HTTP status message
60
62
  * @param operation operation where error occured
61
63
  *
62
64
  */
63
- setError(statusCode, statusMessage, operation) {
65
+ setError(status, statusText, operation) {
64
66
  this.success = false;
65
- this.statusCode = statusCode;
66
- if (this.statusMessage != "OK") {
67
- this.messages.push(this.statusMessage); // latest error becomes status, but if there was something non-trivial, add it to the messages
67
+ this.status = status;
68
+ if (this.statusText != "OK") {
69
+ this.messages.push(this.statusText); // latest error becomes status, but if there was something non-trivial, add it to the messages
68
70
  }
69
- if (statusMessage.startsWith(_1.OINO_ERROR_PREFIX)) {
70
- this.statusMessage = statusMessage;
71
+ if (statusText.startsWith(_1.OINO_ERROR_PREFIX)) {
72
+ this.statusText = statusText;
71
73
  }
72
74
  else {
73
- this.statusMessage = _1.OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage;
75
+ this.statusText = _1.OINO_ERROR_PREFIX + " (" + operation + "): " + statusText;
74
76
  }
75
77
  return this;
76
78
  }
@@ -153,17 +155,7 @@ class OINOResult {
153
155
  *
154
156
  */
155
157
  printLog() {
156
- return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]";
157
- }
158
- /**
159
- * Get a Response object from the result values.
160
- *
161
- * @param headers HTTP headers (overrides existing values)
162
- */
163
- getStatusResponse(headers) {
164
- const result = new Response(this.statusMessage, { status: this.statusCode, headers: headers });
165
- result.headers.set('Content-Length', this.statusMessage.length.toString());
166
- return result;
158
+ return "OINOResult: status=" + this.status + ", statusText=" + this.statusText + ", messages=[" + this.messages.join(", ") + "]";
167
159
  }
168
160
  }
169
161
  exports.OINOResult = OINOResult;
@@ -174,21 +166,23 @@ class OINOHttpResult extends OINOResult {
174
166
  _etag;
175
167
  /** HTTP body data */
176
168
  body;
177
- /** HTTP cache expiration value */
169
+ /** HTTP cache expiration value
170
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
171
+ */
178
172
  expires;
179
173
  /** HTTP cache last-modified value */
180
174
  lastModified;
181
175
  /**
182
176
  * Constructor for a `OINOHttpResult`
183
177
  *
184
- * @param body HTTP body
178
+ * @param init initialization values
185
179
  *
186
180
  */
187
- constructor(body) {
188
- super();
189
- this.body = body;
190
- this.expires = 0;
191
- this.lastModified = 0;
181
+ constructor(init) {
182
+ super(init);
183
+ this.body = init?.body ?? "";
184
+ this.expires = init?.expires ?? 0;
185
+ this.lastModified = init?.lastModified ?? 0;
192
186
  this._etag = "";
193
187
  }
194
188
  /**
@@ -208,7 +202,7 @@ class OINOHttpResult extends OINOResult {
208
202
  * @param headers HTTP headers (overrides existing values)
209
203
  */
210
204
  getHttpResponse(headers) {
211
- const result = new Response(this.body, { status: this.statusCode, statusText: this.statusMessage, headers: headers });
205
+ const result = new Response(this.body, { status: this.status, statusText: this.statusText, headers: headers });
212
206
  result.headers.set('Content-Length', this.body.length.toString());
213
207
  if (this.lastModified > 0) {
214
208
  result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString());
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
3
+ exports.OINOContentType = exports.OINO_RESPONSE_TYPE_PARAM = exports.OINO_REQUEST_TYPE_PARAM = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpRequest = exports.OINORequest = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
4
4
  var OINOBenchmark_js_1 = require("./OINOBenchmark.js");
5
5
  Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOBenchmark; } });
6
6
  Object.defineProperty(exports, "OINOMemoryBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOMemoryBenchmark; } });
@@ -11,6 +11,9 @@ Object.defineProperty(exports, "OINOConsoleLog", { enumerable: true, get: functi
11
11
  var OINOResult_js_1 = require("./OINOResult.js");
12
12
  Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOResult; } });
13
13
  Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOHttpResult; } });
14
+ var OINORequest_js_1 = require("./OINORequest.js");
15
+ Object.defineProperty(exports, "OINORequest", { enumerable: true, get: function () { return OINORequest_js_1.OINORequest; } });
16
+ Object.defineProperty(exports, "OINOHttpRequest", { enumerable: true, get: function () { return OINORequest_js_1.OINOHttpRequest; } });
14
17
  var OINOStr_js_1 = require("./OINOStr.js");
15
18
  Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return OINOStr_js_1.OINOStr; } });
16
19
  var OINOHtmlTemplate_js_1 = require("./OINOHtmlTemplate.js");
@@ -26,6 +29,10 @@ exports.OINO_WARNING_PREFIX = "OINO WARNING";
26
29
  exports.OINO_INFO_PREFIX = "OINO INFO";
27
30
  /** OINO debug message prefix */
28
31
  exports.OINO_DEBUG_PREFIX = "OINO DEBUG";
32
+ /** Name of the OINOContentType-parameter request */
33
+ exports.OINO_REQUEST_TYPE_PARAM = "oinorequesttype";
34
+ /** Name of the OINOContentType-parameter request */
35
+ exports.OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype";
29
36
  /**
30
37
  * Supported content format mime-types
31
38
  */
@@ -63,7 +63,7 @@ export class OINOHtmlTemplate {
63
63
  }
64
64
  }
65
65
  _createHttpResult(html) {
66
- const result = new OINOHttpResult(html);
66
+ const result = new OINOHttpResult({ body: html });
67
67
  if (this.expires >= 1) {
68
68
  result.expires = Math.round(this.expires);
69
69
  }
@@ -180,8 +180,8 @@ export class OINOHtmlTemplate {
180
180
  */
181
181
  renderFromResult(result, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
182
182
  OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
183
- this.setVariableFromValue("statusCode", result.statusCode.toString());
184
- this.setVariableFromValue("statusMessage", result.statusMessage.toString());
183
+ this.setVariableFromValue("status", result.status.toString());
184
+ this.setVariableFromValue("statusText", result.statusText.toString());
185
185
  let messages = [];
186
186
  for (let i = 0; i < result.messages.length; i++) {
187
187
  if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
@@ -0,0 +1,118 @@
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
+ import { OINOContentType } from ".";
7
+ import { OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM } from "./index.js";
8
+ /**
9
+ * OINO API request result object with returned data and/or http status code/message and
10
+ * error / warning messages.
11
+ *
12
+ */
13
+ export class OINORequest {
14
+ /** Key-value parameters */
15
+ params;
16
+ /**
17
+ * Constructor of OINORequest.
18
+ *
19
+ * @param init initialization values
20
+ *
21
+ */
22
+ constructor(init) {
23
+ this.params = init?.params ?? {};
24
+ }
25
+ /**
26
+ * Copy values from different result.
27
+ *
28
+ * @param request source value
29
+ */
30
+ copy(request) {
31
+ this.params = { ...request.params };
32
+ }
33
+ }
34
+ /**
35
+ * Specialized result for HTTP responses.
36
+ */
37
+ export class OINOHttpRequest extends OINORequest {
38
+ url;
39
+ method;
40
+ headers;
41
+ data;
42
+ requestType;
43
+ responseType;
44
+ multipartBoundary;
45
+ lastModified;
46
+ etags;
47
+ /**
48
+ * Constructor for a `OINOHttpRequest`
49
+ *
50
+ * @param init initialization values
51
+ *
52
+ */
53
+ constructor(init) {
54
+ super(init);
55
+ this.url = init.url;
56
+ this.method = init.method ?? "GET";
57
+ this.headers = init.headers ?? {};
58
+ this.data = init.data ?? "";
59
+ this.multipartBoundary = "";
60
+ this.lastModified = init.lastModified;
61
+ if (init.multipartBoundary) {
62
+ this.multipartBoundary = init.multipartBoundary;
63
+ }
64
+ if (init.requestType) {
65
+ this.requestType = init.requestType;
66
+ }
67
+ else {
68
+ const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers["content-type"]; // content-type header can be overridden by query parameter
69
+ if (request_type_param == OINOContentType.csv) {
70
+ this.requestType = OINOContentType.csv;
71
+ }
72
+ else if (request_type_param == OINOContentType.urlencode) {
73
+ this.requestType = OINOContentType.urlencode;
74
+ }
75
+ else if (request_type_param?.startsWith(OINOContentType.formdata)) {
76
+ this.requestType = OINOContentType.formdata;
77
+ if (!this.multipartBoundary) {
78
+ this.multipartBoundary = request_type_param.split('boundary=')[1] || "";
79
+ }
80
+ }
81
+ else {
82
+ this.requestType = OINOContentType.json;
83
+ }
84
+ }
85
+ if (init.responseType) {
86
+ this.responseType = init.responseType;
87
+ }
88
+ else {
89
+ const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers["accept"]; // accept header can be overridden by query parameter
90
+ const accept_types = response_type_param?.split(', ') || [];
91
+ let response_type = undefined;
92
+ for (let i = 0; i < accept_types.length; i++) {
93
+ if (Object.values(OINOContentType).includes(accept_types[i])) {
94
+ response_type = accept_types[i];
95
+ break;
96
+ }
97
+ }
98
+ this.responseType = response_type ?? OINOContentType.json;
99
+ }
100
+ const last_modified = this.headers["if-modified-since"];
101
+ if (last_modified) {
102
+ this.lastModified = new Date(last_modified).getTime();
103
+ }
104
+ const etags = this.headers["if-none-match"]?.split(',').map(e => e.trim());
105
+ if (etags) {
106
+ this.etags = etags;
107
+ }
108
+ }
109
+ static async fromRequest(request) {
110
+ const body = await request.arrayBuffer();
111
+ return new OINOHttpRequest({
112
+ url: new URL(request.url),
113
+ method: request.method,
114
+ headers: Object.fromEntries(request.headers),
115
+ data: Buffer.from(body),
116
+ });
117
+ }
118
+ }
@@ -14,20 +14,22 @@ export class OINOResult {
14
14
  /** Wheter request was successfully executed */
15
15
  success;
16
16
  /** HTTP status code */
17
- statusCode;
17
+ status;
18
18
  /** HTTP status message */
19
- statusMessage;
19
+ statusText;
20
20
  /** Error / warning messages */
21
21
  messages;
22
22
  /**
23
23
  * Constructor of OINOResult.
24
24
  *
25
+ * @param init initialization values
26
+ *
25
27
  */
26
- constructor() {
27
- this.success = true;
28
- this.statusCode = 200;
29
- this.statusMessage = "OK";
30
- this.messages = [];
28
+ constructor(init) {
29
+ this.success = init?.success ?? true;
30
+ this.status = init?.status ?? 200;
31
+ this.statusText = init?.statusText ?? "OK";
32
+ this.messages = init?.messages ?? [];
31
33
  }
32
34
  /**
33
35
  * Copy values from different result.
@@ -36,8 +38,8 @@ export class OINOResult {
36
38
  */
37
39
  copy(result) {
38
40
  this.success = result.success;
39
- this.statusCode = result.statusCode;
40
- this.statusMessage = result.statusMessage;
41
+ this.status = result.status;
42
+ this.statusText = result.statusText;
41
43
  this.messages = result.messages.slice();
42
44
  }
43
45
  /**
@@ -46,28 +48,28 @@ export class OINOResult {
46
48
  */
47
49
  setOk() {
48
50
  this.success = true;
49
- this.statusCode = 200;
50
- this.statusMessage = "OK";
51
+ this.status = 200;
52
+ this.statusText = "OK";
51
53
  }
52
54
  /**
53
55
  * Set HTTP error status using given code and message. Returns self reference for chaining.
54
56
  *
55
- * @param statusCode HTTP status code
56
- * @param statusMessage HTTP status message
57
+ * @param status HTTP status code
58
+ * @param statusText HTTP status message
57
59
  * @param operation operation where error occured
58
60
  *
59
61
  */
60
- setError(statusCode, statusMessage, operation) {
62
+ setError(status, statusText, operation) {
61
63
  this.success = false;
62
- this.statusCode = statusCode;
63
- if (this.statusMessage != "OK") {
64
- this.messages.push(this.statusMessage); // latest error becomes status, but if there was something non-trivial, add it to the messages
64
+ this.status = status;
65
+ if (this.statusText != "OK") {
66
+ this.messages.push(this.statusText); // latest error becomes status, but if there was something non-trivial, add it to the messages
65
67
  }
66
- if (statusMessage.startsWith(OINO_ERROR_PREFIX)) {
67
- this.statusMessage = statusMessage;
68
+ if (statusText.startsWith(OINO_ERROR_PREFIX)) {
69
+ this.statusText = statusText;
68
70
  }
69
71
  else {
70
- this.statusMessage = OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage;
72
+ this.statusText = OINO_ERROR_PREFIX + " (" + operation + "): " + statusText;
71
73
  }
72
74
  return this;
73
75
  }
@@ -150,17 +152,7 @@ export class OINOResult {
150
152
  *
151
153
  */
152
154
  printLog() {
153
- return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]";
154
- }
155
- /**
156
- * Get a Response object from the result values.
157
- *
158
- * @param headers HTTP headers (overrides existing values)
159
- */
160
- getStatusResponse(headers) {
161
- const result = new Response(this.statusMessage, { status: this.statusCode, headers: headers });
162
- result.headers.set('Content-Length', this.statusMessage.length.toString());
163
- return result;
155
+ return "OINOResult: status=" + this.status + ", statusText=" + this.statusText + ", messages=[" + this.messages.join(", ") + "]";
164
156
  }
165
157
  }
166
158
  /**
@@ -170,21 +162,23 @@ export class OINOHttpResult extends OINOResult {
170
162
  _etag;
171
163
  /** HTTP body data */
172
164
  body;
173
- /** HTTP cache expiration value */
165
+ /** HTTP cache expiration value
166
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
167
+ */
174
168
  expires;
175
169
  /** HTTP cache last-modified value */
176
170
  lastModified;
177
171
  /**
178
172
  * Constructor for a `OINOHttpResult`
179
173
  *
180
- * @param body HTTP body
174
+ * @param init initialization values
181
175
  *
182
176
  */
183
- constructor(body) {
184
- super();
185
- this.body = body;
186
- this.expires = 0;
187
- this.lastModified = 0;
177
+ constructor(init) {
178
+ super(init);
179
+ this.body = init?.body ?? "";
180
+ this.expires = init?.expires ?? 0;
181
+ this.lastModified = init?.lastModified ?? 0;
188
182
  this._etag = "";
189
183
  }
190
184
  /**
@@ -204,7 +198,7 @@ export class OINOHttpResult extends OINOResult {
204
198
  * @param headers HTTP headers (overrides existing values)
205
199
  */
206
200
  getHttpResponse(headers) {
207
- const result = new Response(this.body, { status: this.statusCode, statusText: this.statusMessage, headers: headers });
201
+ const result = new Response(this.body, { status: this.status, statusText: this.statusText, headers: headers });
208
202
  result.headers.set('Content-Length', this.body.length.toString());
209
203
  if (this.lastModified > 0) {
210
204
  result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString());
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
3
  export { OINOResult, OINOHttpResult } from "./OINOResult.js";
4
+ export { OINORequest, OINOHttpRequest } from "./OINORequest.js";
4
5
  export { OINOStr } from "./OINOStr.js";
5
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
6
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js";
@@ -12,6 +13,10 @@ export const OINO_WARNING_PREFIX = "OINO WARNING";
12
13
  export const OINO_INFO_PREFIX = "OINO INFO";
13
14
  /** OINO debug message prefix */
14
15
  export const OINO_DEBUG_PREFIX = "OINO DEBUG";
16
+ /** Name of the OINOContentType-parameter request */
17
+ export const OINO_REQUEST_TYPE_PARAM = "oinorequesttype";
18
+ /** Name of the OINOContentType-parameter request */
19
+ export const OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype";
15
20
  /**
16
21
  * Supported content format mime-types
17
22
  */
@@ -0,0 +1,58 @@
1
+ import { OINOContentType } from ".";
2
+ export interface OINORequestInit {
3
+ params?: Record<string, string>;
4
+ }
5
+ /**
6
+ * OINO API request result object with returned data and/or http status code/message and
7
+ * error / warning messages.
8
+ *
9
+ */
10
+ export declare class OINORequest {
11
+ /** Key-value parameters */
12
+ params?: Record<string, string>;
13
+ /**
14
+ * Constructor of OINORequest.
15
+ *
16
+ * @param init initialization values
17
+ *
18
+ */
19
+ constructor(init?: OINORequestInit);
20
+ /**
21
+ * Copy values from different result.
22
+ *
23
+ * @param request source value
24
+ */
25
+ copy(request: OINORequest): void;
26
+ }
27
+ export interface OINOHttpRequestInit extends OINORequestInit {
28
+ url?: URL;
29
+ method?: string;
30
+ headers?: Record<string, string>;
31
+ data?: string | Buffer | Uint8Array | object | null;
32
+ requestType?: OINOContentType;
33
+ responseType?: OINOContentType;
34
+ multipartBoundary?: string;
35
+ lastModified?: number;
36
+ }
37
+ /**
38
+ * Specialized result for HTTP responses.
39
+ */
40
+ export declare class OINOHttpRequest extends OINORequest {
41
+ readonly url?: URL;
42
+ readonly method: string;
43
+ readonly headers: Record<string, string>;
44
+ readonly data: string | Buffer | Uint8Array | object | null;
45
+ readonly requestType: OINOContentType;
46
+ readonly responseType: OINOContentType;
47
+ readonly multipartBoundary?: string;
48
+ readonly lastModified?: number;
49
+ readonly etags?: string[];
50
+ /**
51
+ * Constructor for a `OINOHttpRequest`
52
+ *
53
+ * @param init initialization values
54
+ *
55
+ */
56
+ constructor(init: OINOHttpRequestInit);
57
+ static fromRequest(request: Request): Promise<OINOHttpRequest>;
58
+ }
@@ -1,3 +1,9 @@
1
+ export interface OINOResultInit {
2
+ success?: boolean;
3
+ status?: number;
4
+ statusText?: string;
5
+ messages?: string[];
6
+ }
1
7
  /**
2
8
  * OINO API request result object with returned data and/or http status code/message and
3
9
  * error / warning messages.
@@ -7,16 +13,18 @@ export declare class OINOResult {
7
13
  /** Wheter request was successfully executed */
8
14
  success: boolean;
9
15
  /** HTTP status code */
10
- statusCode: number;
16
+ status: number;
11
17
  /** HTTP status message */
12
- statusMessage: string;
18
+ statusText: string;
13
19
  /** Error / warning messages */
14
20
  messages: string[];
15
21
  /**
16
22
  * Constructor of OINOResult.
17
23
  *
24
+ * @param init initialization values
25
+ *
18
26
  */
19
- constructor();
27
+ constructor(init?: OINOResultInit);
20
28
  /**
21
29
  * Copy values from different result.
22
30
  *
@@ -31,12 +39,12 @@ export declare class OINOResult {
31
39
  /**
32
40
  * Set HTTP error status using given code and message. Returns self reference for chaining.
33
41
  *
34
- * @param statusCode HTTP status code
35
- * @param statusMessage HTTP status message
42
+ * @param status HTTP status code
43
+ * @param statusText HTTP status message
36
44
  * @param operation operation where error occured
37
45
  *
38
46
  */
39
- setError(statusCode: number, statusMessage: string, operation: string): OINOResult;
47
+ setError(status: number, statusText: string, operation: string): OINOResult;
40
48
  /**
41
49
  * Add warning message. Returns self reference for chaining.
42
50
  *
@@ -77,12 +85,11 @@ export declare class OINOResult {
77
85
  *
78
86
  */
79
87
  printLog(): string;
80
- /**
81
- * Get a Response object from the result values.
82
- *
83
- * @param headers HTTP headers (overrides existing values)
84
- */
85
- getStatusResponse(headers?: Record<string, string>): Response;
88
+ }
89
+ export interface OINOHttpResultInit extends OINOResultInit {
90
+ body?: string;
91
+ expires?: number;
92
+ lastModified?: number;
86
93
  }
87
94
  /**
88
95
  * Specialized result for HTTP responses.
@@ -91,17 +98,19 @@ export declare class OINOHttpResult extends OINOResult {
91
98
  private _etag;
92
99
  /** HTTP body data */
93
100
  readonly body: string;
94
- /** HTTP cache expiration value */
101
+ /** HTTP cache expiration value
102
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
103
+ */
95
104
  expires: number;
96
105
  /** HTTP cache last-modified value */
97
106
  lastModified: number;
98
107
  /**
99
108
  * Constructor for a `OINOHttpResult`
100
109
  *
101
- * @param body HTTP body
110
+ * @param init initialization values
102
111
  *
103
112
  */
104
- constructor(body: string);
113
+ constructor(init?: OINOHttpResultInit);
105
114
  /**
106
115
  * Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
107
116
  *
@@ -1,6 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
- export { OINOResult, OINOHttpResult } from "./OINOResult.js";
3
+ export { OINOResult, OINOHttpResult, type OINOResultInit, type OINOHttpResultInit } from "./OINOResult.js";
4
+ export { OINORequest, OINOHttpRequest, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js";
4
5
  export { OINOStr } from "./OINOStr.js";
5
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
6
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js";
@@ -12,6 +13,10 @@ export declare const OINO_WARNING_PREFIX = "OINO WARNING";
12
13
  export declare const OINO_INFO_PREFIX = "OINO INFO";
13
14
  /** OINO debug message prefix */
14
15
  export declare const OINO_DEBUG_PREFIX = "OINO DEBUG";
16
+ /** Name of the OINOContentType-parameter request */
17
+ export declare const OINO_REQUEST_TYPE_PARAM = "oinorequesttype";
18
+ /** Name of the OINOContentType-parameter request */
19
+ export declare const OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype";
15
20
  /**
16
21
  * Supported content format mime-types
17
22
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/common",
3
- "version": "0.16.2",
3
+ "version": "0.17.2",
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.16.2",
22
+ "@oino-ts/types": "0.17.2",
23
23
  "@types/node": "^22.0.0",
24
24
  "typescript": "~5.9.0"
25
25
  },
@@ -70,7 +70,7 @@ export class OINOHtmlTemplate {
70
70
  }
71
71
 
72
72
  protected _createHttpResult(html:string):OINOHttpResult {
73
- const result:OINOHttpResult = new OINOHttpResult(html)
73
+ const result:OINOHttpResult = new OINOHttpResult({body: html})
74
74
  if (this.expires >= 1) {
75
75
  result.expires = Math.round(this.expires)
76
76
  }
@@ -194,8 +194,8 @@ export class OINOHtmlTemplate {
194
194
  */
195
195
  renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
196
196
  OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
197
- this.setVariableFromValue("statusCode", result.statusCode.toString())
198
- this.setVariableFromValue("statusMessage", result.statusMessage.toString())
197
+ this.setVariableFromValue("status", result.status.toString())
198
+ this.setVariableFromValue("statusText", result.statusText.toString())
199
199
  let messages:string[] = []
200
200
  for (let i:number = 0; i<result.messages.length; i++) {
201
201
  if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
@@ -0,0 +1,139 @@
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 { OINOContentType } from "."
8
+ import { OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM } from "./index.js"
9
+
10
+ export interface OINORequestInit {
11
+ params?: Record<string, string>
12
+ }
13
+
14
+ /**
15
+ * OINO API request result object with returned data and/or http status code/message and
16
+ * error / warning messages.
17
+ *
18
+ */
19
+ export class OINORequest {
20
+ /** Key-value parameters */
21
+ params?: Record<string, string>
22
+
23
+ /**
24
+ * Constructor of OINORequest.
25
+ *
26
+ * @param init initialization values
27
+ *
28
+ */
29
+ constructor (init?: OINORequestInit) {
30
+ this.params = init?.params ?? {}
31
+ }
32
+
33
+ /**
34
+ * Copy values from different result.
35
+ *
36
+ * @param request source value
37
+ */
38
+ copy(request: OINORequest) {
39
+ this.params = {...request.params}
40
+ }
41
+ }
42
+
43
+ export interface OINOHttpRequestInit extends OINORequestInit {
44
+ url?: URL
45
+ method?: string
46
+ headers?: Record<string, string>
47
+ data?: string|Buffer|Uint8Array|object|null
48
+ requestType?:OINOContentType
49
+ responseType?:OINOContentType
50
+ multipartBoundary?:string
51
+ lastModified?:number
52
+ }
53
+
54
+ /**
55
+ * Specialized result for HTTP responses.
56
+ */
57
+ export class OINOHttpRequest extends OINORequest {
58
+ readonly url?: URL
59
+ readonly method: string
60
+ readonly headers: Record<string, string>
61
+ readonly data: string|Buffer|Uint8Array|object|null
62
+ readonly requestType:OINOContentType
63
+ readonly responseType:OINOContentType
64
+ readonly multipartBoundary?:string
65
+ readonly lastModified?:number
66
+ readonly etags?:string[]
67
+
68
+ /**
69
+ * Constructor for a `OINOHttpRequest`
70
+ *
71
+ * @param init initialization values
72
+ *
73
+ */
74
+ constructor(init: OINOHttpRequestInit) {
75
+ super(init)
76
+ this.url = init.url
77
+ this.method = init.method ?? "GET"
78
+ this.headers = init.headers ?? {}
79
+ this.data = init.data ?? ""
80
+ this.multipartBoundary = ""
81
+ this.lastModified = init.lastModified
82
+
83
+ if (init.multipartBoundary) {
84
+ this.multipartBoundary = init.multipartBoundary
85
+ }
86
+ if (init.requestType) {
87
+ this.requestType = init.requestType
88
+ } else {
89
+ const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers["content-type"] // content-type header can be overridden by query parameter
90
+ if (request_type_param == OINOContentType.csv) {
91
+ this.requestType = OINOContentType.csv
92
+
93
+ } else if (request_type_param == OINOContentType.urlencode) {
94
+ this.requestType = OINOContentType.urlencode
95
+
96
+ } else if (request_type_param?.startsWith(OINOContentType.formdata)) {
97
+ this.requestType = OINOContentType.formdata
98
+ if (!this.multipartBoundary) {
99
+ this.multipartBoundary = request_type_param.split('boundary=')[1] || ""
100
+ }
101
+ } else {
102
+ this.requestType = OINOContentType.json
103
+ }
104
+ }
105
+ if (init.responseType) {
106
+ this.responseType = init.responseType
107
+ } else {
108
+ const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers["accept"] // accept header can be overridden by query parameter
109
+ const accept_types = response_type_param?.split(', ') || []
110
+ let response_type:OINOContentType|undefined = undefined
111
+ for (let i=0; i<accept_types.length; i++) {
112
+ if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
113
+ response_type = accept_types[i] as OINOContentType
114
+ break
115
+ }
116
+ }
117
+ this.responseType = response_type ?? OINOContentType.json
118
+ }
119
+ const last_modified = this.headers["if-modified-since"]
120
+ if (last_modified) {
121
+ this.lastModified = new Date(last_modified).getTime()
122
+ }
123
+ const etags = this.headers["if-none-match"]?.split(',').map(e => e.trim())
124
+ if (etags) {
125
+ this.etags = etags
126
+ }
127
+ }
128
+
129
+ static async fromRequest(request: Request): Promise<OINOHttpRequest> {
130
+ const body = await request.arrayBuffer()
131
+ return new OINOHttpRequest({
132
+ url: new URL(request.url),
133
+ method: request.method,
134
+ headers: Object.fromEntries(request.headers as any),
135
+ data: Buffer.from(body),
136
+ })
137
+ }
138
+
139
+ }
package/src/OINOResult.ts CHANGED
@@ -7,6 +7,13 @@
7
7
  import { createHash, Hash } from "node:crypto";
8
8
  import { OINO_DEBUG_PREFIX, OINO_ERROR_PREFIX, OINO_INFO_PREFIX, OINO_WARNING_PREFIX } from ".";
9
9
 
10
+ export interface OINOResultInit {
11
+ success?: boolean
12
+ status?: number
13
+ statusText?: string
14
+ messages?: string[]
15
+ }
16
+
10
17
  /**
11
18
  * OINO API request result object with returned data and/or http status code/message and
12
19
  * error / warning messages.
@@ -17,10 +24,10 @@ export class OINOResult {
17
24
  success: boolean
18
25
 
19
26
  /** HTTP status code */
20
- statusCode: number;
27
+ status: number;
21
28
 
22
29
  /** HTTP status message */
23
- statusMessage: string;
30
+ statusText: string;
24
31
 
25
32
  /** Error / warning messages */
26
33
  messages: string[];
@@ -28,12 +35,14 @@ export class OINOResult {
28
35
  /**
29
36
  * Constructor of OINOResult.
30
37
  *
38
+ * @param init initialization values
39
+ *
31
40
  */
32
- constructor () {
33
- this.success = true
34
- this.statusCode = 200
35
- this.statusMessage = "OK"
36
- this.messages = []
41
+ constructor (init?: OINOResultInit) {
42
+ this.success = init?.success ?? true
43
+ this.status = init?.status ?? 200
44
+ this.statusText = init?.statusText ?? "OK"
45
+ this.messages = init?.messages ?? []
37
46
  }
38
47
 
39
48
  /**
@@ -43,8 +52,8 @@ export class OINOResult {
43
52
  */
44
53
  copy(result: OINOResult) {
45
54
  this.success = result.success
46
- this.statusCode = result.statusCode
47
- this.statusMessage = result.statusMessage
55
+ this.status = result.status
56
+ this.statusText = result.statusText
48
57
  this.messages = result.messages.slice()
49
58
  }
50
59
 
@@ -54,28 +63,28 @@ export class OINOResult {
54
63
  */
55
64
  setOk() {
56
65
  this.success = true
57
- this.statusCode = 200
58
- this.statusMessage = "OK"
66
+ this.status = 200
67
+ this.statusText = "OK"
59
68
  }
60
69
 
61
70
  /**
62
71
  * Set HTTP error status using given code and message. Returns self reference for chaining.
63
72
  *
64
- * @param statusCode HTTP status code
65
- * @param statusMessage HTTP status message
73
+ * @param status HTTP status code
74
+ * @param statusText HTTP status message
66
75
  * @param operation operation where error occured
67
76
  *
68
77
  */
69
- setError(statusCode:number, statusMessage:string, operation:string):OINOResult {
78
+ setError(status:number, statusText:string, operation:string):OINOResult {
70
79
  this.success = false
71
- this.statusCode = statusCode
72
- if (this.statusMessage != "OK") {
73
- this.messages.push(this.statusMessage) // latest error becomes status, but if there was something non-trivial, add it to the messages
80
+ this.status = status
81
+ if (this.statusText != "OK") {
82
+ this.messages.push(this.statusText) // latest error becomes status, but if there was something non-trivial, add it to the messages
74
83
  }
75
- if (statusMessage.startsWith(OINO_ERROR_PREFIX)) {
76
- this.statusMessage = statusMessage
84
+ if (statusText.startsWith(OINO_ERROR_PREFIX)) {
85
+ this.statusText = statusText
77
86
  } else {
78
- this.statusMessage = OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage
87
+ this.statusText = OINO_ERROR_PREFIX + " (" + operation + "): " + statusText
79
88
  }
80
89
  return this
81
90
  }
@@ -163,20 +172,14 @@ export class OINOResult {
163
172
  *
164
173
  */
165
174
  printLog() {
166
- return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]"
167
- }
168
-
169
- /**
170
- * Get a Response object from the result values.
171
- *
172
- * @param headers HTTP headers (overrides existing values)
173
- */
174
- getStatusResponse(headers?:Record<string, string>):Response {
175
- const result:Response = new Response(this.statusMessage, {status:this.statusCode, headers: headers})
176
- result.headers.set('Content-Length', this.statusMessage.length.toString())
177
- return result
175
+ return "OINOResult: status=" + this.status + ", statusText=" + this.statusText + ", messages=[" + this.messages.join(", ") + "]"
178
176
  }
177
+ }
179
178
 
179
+ export interface OINOHttpResultInit extends OINOResultInit {
180
+ body?: string
181
+ expires?: number
182
+ lastModified?: number
180
183
  }
181
184
 
182
185
  /**
@@ -188,7 +191,9 @@ export class OINOHttpResult extends OINOResult {
188
191
  /** HTTP body data */
189
192
  readonly body: string
190
193
 
191
- /** HTTP cache expiration value */
194
+ /** HTTP cache expiration value
195
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
196
+ */
192
197
  expires: number
193
198
 
194
199
  /** HTTP cache last-modified value */
@@ -197,14 +202,14 @@ export class OINOHttpResult extends OINOResult {
197
202
  /**
198
203
  * Constructor for a `OINOHttpResult`
199
204
  *
200
- * @param body HTTP body
205
+ * @param init initialization values
201
206
  *
202
207
  */
203
- constructor(body:string) {
204
- super()
205
- this.body = body
206
- this.expires = 0
207
- this.lastModified = 0
208
+ constructor(init?: OINOHttpResultInit) {
209
+ super(init)
210
+ this.body = init?.body ?? ""
211
+ this.expires = init?.expires ?? 0
212
+ this.lastModified = init?.lastModified ?? 0
208
213
  this._etag = ""
209
214
  }
210
215
 
@@ -226,7 +231,7 @@ export class OINOHttpResult extends OINOResult {
226
231
  * @param headers HTTP headers (overrides existing values)
227
232
  */
228
233
  getHttpResponse(headers?:Record<string, string>):Response {
229
- const result:Response = new Response(this.body, {status:this.statusCode, statusText: this.statusMessage, headers: headers})
234
+ const result:Response = new Response(this.body, {status:this.status, statusText: this.statusText, headers: headers})
230
235
  result.headers.set('Content-Length', this.body.length.toString())
231
236
  if (this.lastModified > 0) {
232
237
  result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString())
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js"
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
- export { OINOResult, OINOHttpResult } from "./OINOResult.js"
3
+ export { OINOResult, OINOHttpResult, type OINOResultInit, type OINOHttpResultInit } from "./OINOResult.js"
4
+ export { OINORequest, OINOHttpRequest, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js"
4
5
  export { OINOStr } from "./OINOStr.js"
5
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js"
6
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js"
@@ -13,6 +14,10 @@ export const OINO_WARNING_PREFIX = "OINO WARNING"
13
14
  export const OINO_INFO_PREFIX = "OINO INFO"
14
15
  /** OINO debug message prefix */
15
16
  export const OINO_DEBUG_PREFIX = "OINO DEBUG"
17
+ /** Name of the OINOContentType-parameter request */
18
+ export const OINO_REQUEST_TYPE_PARAM = "oinorequesttype"
19
+ /** Name of the OINOContentType-parameter request */
20
+ export const OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype"
16
21
 
17
22
  /**
18
23
  * Supported content format mime-types