@oino-ts/types 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@
22
22
  ```
23
23
 
24
24
  ### Register database and logger
25
- Register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOConsoleLog.html) how to implement your own)
25
+ Register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/types_src.OINOConsoleLog.html) how to implement your own)
26
26
 
27
27
  ```
28
28
  OINOLog.setLogger(new OINOConsoleLog())
@@ -30,13 +30,13 @@
30
30
  ```
31
31
 
32
32
  ### Create a database
33
- Creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/core_src_OINODb.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/core_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/bunsqlite_OINODbBunSqlite.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
33
+ Creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/db_bunsqlite_src.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
34
34
  ```
35
35
  const db:OINODb = await OINOFactory.createDb( { type: "OINODbBunSqlite", url: "file://../localDb/northwind.sqlite" } )
36
36
  ```
37
37
 
38
38
  ### Create an API
39
- From a database you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/core_src_OINOApi.OINOApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/core_src.OINOApiParams.html) with table name and preferences to the factory method.
39
+ From a database you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbApiParams.html) with table name and preferences to the factory method.
40
40
  ```
41
41
  const api_employees:OINOApi = await OINOFactory.createApi(db, { tableName: "Employees", excludeFields:["BirthDate"] })
42
42
  ```
@@ -49,7 +49,7 @@
49
49
  ```
50
50
 
51
51
  ### Write results back to HTTP Response
52
- The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/core_src_OINOModelSet.OINOModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
52
+ The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
53
53
  ```
54
54
  return new Response(result.modelset.writeString(OINOContentType.json))
55
55
  ```
@@ -134,7 +134,7 @@
134
134
  To support tables with multipart primary keys OINO generates a composite key `_OINOID_` that is included in the result and can be used as the REST ID. For example in the example above table `OrderDetails` has two primary keys `OrderID` and `ProductID` making the `_OINOID_` of form `11077:99`.
135
135
 
136
136
  ## Power Of SQL
137
- Since OINO controls the SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOSqlFilter.html) and order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOSqlOrder.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
137
+ Since OINO controls the SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html) and order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
138
138
 
139
139
  ## Swagger Support
140
140
  Swagger is great as long as the definitions are updated and with OINO you can automatically get a Swagger definition including a data model schema.
@@ -149,7 +149,7 @@
149
149
  OINO is developped Typescript first but compiles to standard CommonJS and the NPM packages should work on either ESM / CommonJS. Checkout sample apps `readmeApp` (ESM) and `nodeApp` (CommonJS).
150
150
 
151
151
  ## HTMX support
152
- OINO is [htmx.org](https://htmx.org) friendly, allowing easy translation of [`OINODataRow`](https://pragmatta.github.io/oino-ts/types/core_src.OINODataRow.html) to HTML output using templates (cf. the [htmx sample app](https://github.com/pragmatta/oino-ts/tree/main/samples/htmxApp)).
152
+ OINO is [htmx.org](https://htmx.org) friendly, allowing easy translation of [`OINODataRow`](https://pragmatta.github.io/oino-ts/types/db_src.OINODataRow.html) to HTML output using templates (cf. the [htmx sample app](https://github.com/pragmatta/oino-ts/tree/main/samples/htmxApp)).
153
153
 
154
154
  ### Hashids
155
155
  Autoinc numeric id's are very pragmatic and fit well with OINO (e.g. using a form without primary key fields to insert new rows with database assigned ids). However it's not always sensible to share information about the sequence. Hashids solve this by masking the original values by encrypting the ids using AES-128 and some randomness. Length of the hashid can be chosen from 12-32 characters where longer ids provide more security. However this should not be considereded a cryptographic solution for keeping ids secret but rather making it infeasible to iterate all ids.
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OINOHtmlTemplate = void 0;
4
+ const _1 = require(".");
5
+ /**
6
+ * Class for rendering HTML from data.
7
+ */
8
+ class OINOHtmlTemplate {
9
+ /** HTML template string */
10
+ template;
11
+ /** Cache expiration value for template */
12
+ expires;
13
+ /**
14
+ * Creates HTML Response from a key-value-pair.
15
+ *
16
+ * @param template template string
17
+ * @param expires cache expiration value
18
+ *
19
+ */
20
+ constructor(template, expires) {
21
+ this.template = template;
22
+ if (expires) {
23
+ this.expires = expires;
24
+ }
25
+ else {
26
+ this.expires = -1;
27
+ }
28
+ }
29
+ /**
30
+ * @returns whether template is empty
31
+ */
32
+ isEmpty() {
33
+ return this.template == "";
34
+ }
35
+ /**
36
+ * Creates HTML Response from a key-value-pair.
37
+ *
38
+ * @param key key
39
+ * @param value value
40
+ *
41
+ */
42
+ renderFromKeyValue(key, value) {
43
+ const html = this.template.replaceAll('###' + key + '###', _1.OINOStr.encode(value, _1.OINOContentType.html));
44
+ const result = new _1.OINOHttpResult(html);
45
+ return result;
46
+ }
47
+ /**
48
+ * Creates HTML Response from object properties.
49
+ *
50
+ * @param object object
51
+ *
52
+ */
53
+ renderFromObject(object) {
54
+ let html = this.template;
55
+ if (object) {
56
+ for (let key in object) {
57
+ const value = object[key];
58
+ if (value) {
59
+ html = html.replaceAll('###' + key + '###', _1.OINOStr.encode(value.toString(), _1.OINOContentType.html));
60
+ }
61
+ }
62
+ }
63
+ html = html.replace(/###[^#]*###/g, "");
64
+ const result = new _1.OINOHttpResult(html);
65
+ if (this.expires >= 1) {
66
+ result.headers["Expires"] = Math.round(this.expires).toString();
67
+ }
68
+ return result;
69
+ }
70
+ /**
71
+ * Creates HTML Response from API result.
72
+ *
73
+ * @param result OINOResult-object
74
+ * @param includeErrorMessages include debug messages in result
75
+ * @param includeWarningMessages include debug messages in result
76
+ * @param includeInfoMessages include debug messages in result
77
+ * @param includeDebugMessages include debug messages in result
78
+ *
79
+ */
80
+ renderFromResult(result, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
81
+ let html = this.template;
82
+ html = html.replaceAll('###statusCode###', _1.OINOStr.encode(result.statusCode.toString(), _1.OINOContentType.html));
83
+ html = html.replaceAll('###statusMessage###', _1.OINOStr.encode(result.statusMessage.toString(), _1.OINOContentType.html));
84
+ let messages = "";
85
+ for (let i = 0; i < result.messages.length; i++) {
86
+ if (includeErrorMessages && result.messages[i].startsWith(_1.OINO_ERROR_PREFIX)) {
87
+ messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
88
+ }
89
+ if (includeWarningMessages && result.messages[i].startsWith(_1.OINO_WARNING_PREFIX)) {
90
+ messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
91
+ }
92
+ if (includeInfoMessages && result.messages[i].startsWith(_1.OINO_INFO_PREFIX)) {
93
+ messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
94
+ }
95
+ if (includeDebugMessages && result.messages[i].startsWith(_1.OINO_DEBUG_PREFIX)) {
96
+ messages += "<li>" + _1.OINOStr.encode(result.messages[i], _1.OINOContentType.html) + "</li>";
97
+ }
98
+ }
99
+ if (messages) {
100
+ html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>");
101
+ }
102
+ html = html.replace(/###[^#]*###/g, "");
103
+ const http_result = new _1.OINOHttpResult(html);
104
+ return http_result;
105
+ }
106
+ }
107
+ exports.OINOHtmlTemplate = OINOHtmlTemplate;
108
+ ;
@@ -5,7 +5,7 @@
5
5
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.OINOResult = void 0;
8
+ exports.OINOHttpResult = exports.OINOResult = void 0;
9
9
  const _1 = require(".");
10
10
  /**
11
11
  * OINO API request result object with returned data and/or http status code/message and
@@ -31,6 +31,17 @@ class OINOResult {
31
31
  this.statusMessage = "OK";
32
32
  this.messages = [];
33
33
  }
34
+ /**
35
+ * Copy values from different result.
36
+ *
37
+ * @param result source value
38
+ */
39
+ copy(result) {
40
+ this.success = result.success;
41
+ this.statusCode = result.statusCode;
42
+ this.statusMessage = result.statusMessage;
43
+ this.messages = result.messages.slice();
44
+ }
34
45
  /**
35
46
  * Set HTTP OK status (does not reset messages).
36
47
  *
@@ -41,7 +52,7 @@ class OINOResult {
41
52
  this.statusMessage = "OK";
42
53
  }
43
54
  /**
44
- * Set HTTP error status using given code and message.
55
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
45
56
  *
46
57
  * @param statusCode HTTP status code
47
58
  * @param statusMessage HTTP status message
@@ -60,9 +71,10 @@ class OINOResult {
60
71
  else {
61
72
  this.statusMessage = _1.OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage;
62
73
  }
74
+ return this;
63
75
  }
64
76
  /**
65
- * Add warning message.
77
+ * Add warning message. Returns self reference for chaining.
66
78
  *
67
79
  * @param message HTTP status message
68
80
  * @param operation operation where warning occured
@@ -73,9 +85,10 @@ class OINOResult {
73
85
  if (message) {
74
86
  this.messages.push(_1.OINO_WARNING_PREFIX + " (" + operation + "): " + message);
75
87
  }
88
+ return this;
76
89
  }
77
90
  /**
78
- * Add info message.
91
+ * Add info message. Returns self reference for chaining.
79
92
  *
80
93
  * @param message HTTP status message
81
94
  * @param operation operation where info occured
@@ -86,9 +99,10 @@ class OINOResult {
86
99
  if (message) {
87
100
  this.messages.push(_1.OINO_INFO_PREFIX + " (" + operation + "): " + message);
88
101
  }
102
+ return this;
89
103
  }
90
104
  /**
91
- * Add debug message.
105
+ * Add debug message. Returns self reference for chaining.
92
106
  *
93
107
  * @param message HTTP status message
94
108
  * @param operation operation where debug occured
@@ -99,6 +113,7 @@ class OINOResult {
99
113
  if (message) {
100
114
  this.messages.push(_1.OINO_DEBUG_PREFIX + " (" + operation + "): " + message);
101
115
  }
116
+ return this;
102
117
  }
103
118
  /**
104
119
  * Copy given messages to HTTP headers.
@@ -134,3 +149,42 @@ class OINOResult {
134
149
  }
135
150
  }
136
151
  exports.OINOResult = OINOResult;
152
+ /**
153
+ * Specialized result for HTTP responses.
154
+ */
155
+ class OINOHttpResult extends OINOResult {
156
+ /** HTTP body data */
157
+ body;
158
+ /** HTTP header values */
159
+ headers;
160
+ /**
161
+ * Constructor for a `OINOHttpResult`
162
+ *
163
+ * @param body HTTP body
164
+ * @param headers HTTP headers
165
+ */
166
+ constructor(body, headers) {
167
+ super();
168
+ this.body = body;
169
+ if (headers) {
170
+ this.headers = headers;
171
+ }
172
+ else {
173
+ this.headers = {};
174
+ }
175
+ }
176
+ /**
177
+ * Get a Response object from the result values.
178
+ *
179
+ * @param headers HTTP headers (overrides existing values)
180
+ */
181
+ getResponse(headers) {
182
+ if (!headers) {
183
+ headers = this.headers;
184
+ }
185
+ headers['Content-Length'] = this.body.length.toString();
186
+ const result = new Response(this.body, { status: this.statusCode, statusText: this.statusMessage, headers: headers });
187
+ return result;
188
+ }
189
+ }
190
+ exports.OINOHttpResult = OINOHttpResult;
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.OINOStr = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOBenchmark = void 0;
3
+ exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = 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
  var OINOLog_js_1 = require("./OINOLog.js");
@@ -9,8 +9,11 @@ Object.defineProperty(exports, "OINOLogLevel", { enumerable: true, get: function
9
9
  Object.defineProperty(exports, "OINOConsoleLog", { enumerable: true, get: function () { return OINOLog_js_1.OINOConsoleLog; } });
10
10
  var OINOResult_js_1 = require("./OINOResult.js");
11
11
  Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOResult; } });
12
+ Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOHttpResult; } });
12
13
  var OINOStr_js_1 = require("./OINOStr.js");
13
14
  Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return OINOStr_js_1.OINOStr; } });
15
+ var OINOHtmlTemplate_js_1 = require("./OINOHtmlTemplate.js");
16
+ Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return OINOHtmlTemplate_js_1.OINOHtmlTemplate; } });
14
17
  /** OINO error message prefix */
15
18
  exports.OINO_ERROR_PREFIX = "OINO ERROR";
16
19
  /** OINO warning message prefix */
@@ -0,0 +1,104 @@
1
+ import { OINOStr, OINOContentType, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX } from ".";
2
+ /**
3
+ * Class for rendering HTML from data.
4
+ */
5
+ export class OINOHtmlTemplate {
6
+ /** HTML template string */
7
+ template;
8
+ /** Cache expiration value for template */
9
+ expires;
10
+ /**
11
+ * Creates HTML Response from a key-value-pair.
12
+ *
13
+ * @param template template string
14
+ * @param expires cache expiration value
15
+ *
16
+ */
17
+ constructor(template, expires) {
18
+ this.template = template;
19
+ if (expires) {
20
+ this.expires = expires;
21
+ }
22
+ else {
23
+ this.expires = -1;
24
+ }
25
+ }
26
+ /**
27
+ * @returns whether template is empty
28
+ */
29
+ isEmpty() {
30
+ return this.template == "";
31
+ }
32
+ /**
33
+ * Creates HTML Response from a key-value-pair.
34
+ *
35
+ * @param key key
36
+ * @param value value
37
+ *
38
+ */
39
+ renderFromKeyValue(key, value) {
40
+ const html = this.template.replaceAll('###' + key + '###', OINOStr.encode(value, OINOContentType.html));
41
+ const result = new OINOHttpResult(html);
42
+ return result;
43
+ }
44
+ /**
45
+ * Creates HTML Response from object properties.
46
+ *
47
+ * @param object object
48
+ *
49
+ */
50
+ renderFromObject(object) {
51
+ let html = this.template;
52
+ if (object) {
53
+ for (let key in object) {
54
+ const value = object[key];
55
+ if (value) {
56
+ html = html.replaceAll('###' + key + '###', OINOStr.encode(value.toString(), OINOContentType.html));
57
+ }
58
+ }
59
+ }
60
+ html = html.replace(/###[^#]*###/g, "");
61
+ const result = new OINOHttpResult(html);
62
+ if (this.expires >= 1) {
63
+ result.headers["Expires"] = Math.round(this.expires).toString();
64
+ }
65
+ return result;
66
+ }
67
+ /**
68
+ * Creates HTML Response from API result.
69
+ *
70
+ * @param result OINOResult-object
71
+ * @param includeErrorMessages include debug messages in result
72
+ * @param includeWarningMessages include debug messages in result
73
+ * @param includeInfoMessages include debug messages in result
74
+ * @param includeDebugMessages include debug messages in result
75
+ *
76
+ */
77
+ renderFromResult(result, includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
78
+ let html = this.template;
79
+ html = html.replaceAll('###statusCode###', OINOStr.encode(result.statusCode.toString(), OINOContentType.html));
80
+ html = html.replaceAll('###statusMessage###', OINOStr.encode(result.statusMessage.toString(), OINOContentType.html));
81
+ let messages = "";
82
+ for (let i = 0; i < result.messages.length; i++) {
83
+ if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
84
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
85
+ }
86
+ if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
87
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
88
+ }
89
+ if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
90
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
91
+ }
92
+ if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
93
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>";
94
+ }
95
+ }
96
+ if (messages) {
97
+ html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>");
98
+ }
99
+ html = html.replace(/###[^#]*###/g, "");
100
+ const http_result = new OINOHttpResult(html);
101
+ return http_result;
102
+ }
103
+ }
104
+ ;
@@ -28,6 +28,17 @@ export class OINOResult {
28
28
  this.statusMessage = "OK";
29
29
  this.messages = [];
30
30
  }
31
+ /**
32
+ * Copy values from different result.
33
+ *
34
+ * @param result source value
35
+ */
36
+ copy(result) {
37
+ this.success = result.success;
38
+ this.statusCode = result.statusCode;
39
+ this.statusMessage = result.statusMessage;
40
+ this.messages = result.messages.slice();
41
+ }
31
42
  /**
32
43
  * Set HTTP OK status (does not reset messages).
33
44
  *
@@ -38,7 +49,7 @@ export class OINOResult {
38
49
  this.statusMessage = "OK";
39
50
  }
40
51
  /**
41
- * Set HTTP error status using given code and message.
52
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
42
53
  *
43
54
  * @param statusCode HTTP status code
44
55
  * @param statusMessage HTTP status message
@@ -57,9 +68,10 @@ export class OINOResult {
57
68
  else {
58
69
  this.statusMessage = OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage;
59
70
  }
71
+ return this;
60
72
  }
61
73
  /**
62
- * Add warning message.
74
+ * Add warning message. Returns self reference for chaining.
63
75
  *
64
76
  * @param message HTTP status message
65
77
  * @param operation operation where warning occured
@@ -70,9 +82,10 @@ export class OINOResult {
70
82
  if (message) {
71
83
  this.messages.push(OINO_WARNING_PREFIX + " (" + operation + "): " + message);
72
84
  }
85
+ return this;
73
86
  }
74
87
  /**
75
- * Add info message.
88
+ * Add info message. Returns self reference for chaining.
76
89
  *
77
90
  * @param message HTTP status message
78
91
  * @param operation operation where info occured
@@ -83,9 +96,10 @@ export class OINOResult {
83
96
  if (message) {
84
97
  this.messages.push(OINO_INFO_PREFIX + " (" + operation + "): " + message);
85
98
  }
99
+ return this;
86
100
  }
87
101
  /**
88
- * Add debug message.
102
+ * Add debug message. Returns self reference for chaining.
89
103
  *
90
104
  * @param message HTTP status message
91
105
  * @param operation operation where debug occured
@@ -96,6 +110,7 @@ export class OINOResult {
96
110
  if (message) {
97
111
  this.messages.push(OINO_DEBUG_PREFIX + " (" + operation + "): " + message);
98
112
  }
113
+ return this;
99
114
  }
100
115
  /**
101
116
  * Copy given messages to HTTP headers.
@@ -130,3 +145,41 @@ export class OINOResult {
130
145
  }
131
146
  }
132
147
  }
148
+ /**
149
+ * Specialized result for HTTP responses.
150
+ */
151
+ export class OINOHttpResult extends OINOResult {
152
+ /** HTTP body data */
153
+ body;
154
+ /** HTTP header values */
155
+ headers;
156
+ /**
157
+ * Constructor for a `OINOHttpResult`
158
+ *
159
+ * @param body HTTP body
160
+ * @param headers HTTP headers
161
+ */
162
+ constructor(body, headers) {
163
+ super();
164
+ this.body = body;
165
+ if (headers) {
166
+ this.headers = headers;
167
+ }
168
+ else {
169
+ this.headers = {};
170
+ }
171
+ }
172
+ /**
173
+ * Get a Response object from the result values.
174
+ *
175
+ * @param headers HTTP headers (overrides existing values)
176
+ */
177
+ getResponse(headers) {
178
+ if (!headers) {
179
+ headers = this.headers;
180
+ }
181
+ headers['Content-Length'] = this.body.length.toString();
182
+ const result = new Response(this.body, { status: this.statusCode, statusText: this.statusMessage, headers: headers });
183
+ return result;
184
+ }
185
+ }
package/dist/esm/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  export { OINOBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
- export { OINOResult } from "./OINOResult.js";
3
+ export { OINOResult, OINOHttpResult } from "./OINOResult.js";
4
4
  export { OINOStr } from "./OINOStr.js";
5
+ export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
5
6
  /** OINO error message prefix */
6
7
  export const OINO_ERROR_PREFIX = "OINO ERROR";
7
8
  /** OINO warning message prefix */
@@ -0,0 +1,48 @@
1
+ import { OINOResult, OINOHttpResult } from ".";
2
+ /**
3
+ * Class for rendering HTML from data.
4
+ */
5
+ export declare class OINOHtmlTemplate {
6
+ /** HTML template string */
7
+ template: string;
8
+ /** Cache expiration value for template */
9
+ expires: number;
10
+ /**
11
+ * Creates HTML Response from a key-value-pair.
12
+ *
13
+ * @param template template string
14
+ * @param expires cache expiration value
15
+ *
16
+ */
17
+ constructor(template: string, expires?: number);
18
+ /**
19
+ * @returns whether template is empty
20
+ */
21
+ isEmpty(): boolean;
22
+ /**
23
+ * Creates HTML Response from a key-value-pair.
24
+ *
25
+ * @param key key
26
+ * @param value value
27
+ *
28
+ */
29
+ renderFromKeyValue(key: string, value: string): OINOHttpResult;
30
+ /**
31
+ * Creates HTML Response from object properties.
32
+ *
33
+ * @param object object
34
+ *
35
+ */
36
+ renderFromObject(object: any): OINOHttpResult;
37
+ /**
38
+ * Creates HTML Response from API result.
39
+ *
40
+ * @param result OINOResult-object
41
+ * @param includeErrorMessages include debug messages in result
42
+ * @param includeWarningMessages include debug messages in result
43
+ * @param includeInfoMessages include debug messages in result
44
+ * @param includeDebugMessages include debug messages in result
45
+ *
46
+ */
47
+ renderFromResult(result: OINOResult, includeErrorMessages?: boolean, includeWarningMessages?: boolean, includeInfoMessages?: boolean, includeDebugMessages?: boolean): OINOHttpResult;
48
+ }
@@ -17,44 +17,50 @@ export declare class OINOResult {
17
17
  *
18
18
  */
19
19
  constructor();
20
+ /**
21
+ * Copy values from different result.
22
+ *
23
+ * @param result source value
24
+ */
25
+ copy(result: OINOResult): void;
20
26
  /**
21
27
  * Set HTTP OK status (does not reset messages).
22
28
  *
23
29
  */
24
30
  setOk(): void;
25
31
  /**
26
- * Set HTTP error status using given code and message.
32
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
27
33
  *
28
34
  * @param statusCode HTTP status code
29
35
  * @param statusMessage HTTP status message
30
36
  * @param operation operation where error occured
31
37
  *
32
38
  */
33
- setError(statusCode: number, statusMessage: string, operation: string): void;
39
+ setError(statusCode: number, statusMessage: string, operation: string): OINOResult;
34
40
  /**
35
- * Add warning message.
41
+ * Add warning message. Returns self reference for chaining.
36
42
  *
37
43
  * @param message HTTP status message
38
44
  * @param operation operation where warning occured
39
45
  *
40
46
  */
41
- addWarning(message: string, operation: string): void;
47
+ addWarning(message: string, operation: string): OINOResult;
42
48
  /**
43
- * Add info message.
49
+ * Add info message. Returns self reference for chaining.
44
50
  *
45
51
  * @param message HTTP status message
46
52
  * @param operation operation where info occured
47
53
  *
48
54
  */
49
- addInfo(message: string, operation: string): void;
55
+ addInfo(message: string, operation: string): OINOResult;
50
56
  /**
51
- * Add debug message.
57
+ * Add debug message. Returns self reference for chaining.
52
58
  *
53
59
  * @param message HTTP status message
54
60
  * @param operation operation where debug occured
55
61
  *
56
62
  */
57
- addDebug(message: string, operation: string): void;
63
+ addDebug(message: string, operation: string): OINOResult;
58
64
  /**
59
65
  * Copy given messages to HTTP headers.
60
66
  *
@@ -67,3 +73,25 @@ export declare class OINOResult {
67
73
  */
68
74
  copyMessagesToHeaders(headers: Headers, copyErrors?: boolean, copyWarnings?: boolean, copyInfos?: boolean, copyDebug?: boolean): void;
69
75
  }
76
+ /**
77
+ * Specialized result for HTTP responses.
78
+ */
79
+ export declare class OINOHttpResult extends OINOResult {
80
+ /** HTTP body data */
81
+ body: string;
82
+ /** HTTP header values */
83
+ headers: Record<string, string>;
84
+ /**
85
+ * Constructor for a `OINOHttpResult`
86
+ *
87
+ * @param body HTTP body
88
+ * @param headers HTTP headers
89
+ */
90
+ constructor(body: string, headers?: Record<string, string>);
91
+ /**
92
+ * Get a Response object from the result values.
93
+ *
94
+ * @param headers HTTP headers (overrides existing values)
95
+ */
96
+ getResponse(headers?: Record<string, string>): Response;
97
+ }
@@ -1,7 +1,8 @@
1
1
  export { OINOBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
- export { OINOResult } from "./OINOResult.js";
3
+ export { OINOResult, OINOHttpResult } from "./OINOResult.js";
4
4
  export { OINOStr } from "./OINOStr.js";
5
+ export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
5
6
  /** OINO error message prefix */
6
7
  export declare const OINO_ERROR_PREFIX = "OINO ERROR";
7
8
  /** OINO warning message prefix */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/types",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "OINO TS package for types.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -0,0 +1,110 @@
1
+ import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX } from "."
2
+
3
+ /**
4
+ * Class for rendering HTML from data.
5
+ */
6
+ export class OINOHtmlTemplate {
7
+ /** HTML template string */
8
+ template: string;
9
+
10
+ /** Cache expiration value for template */
11
+ expires: number;
12
+
13
+ /**
14
+ * Creates HTML Response from a key-value-pair.
15
+ *
16
+ * @param template template string
17
+ * @param expires cache expiration value
18
+ *
19
+ */
20
+ constructor (template:string, expires?: number) {
21
+ this.template = template
22
+ if (expires) {
23
+ this.expires = expires
24
+ } else {
25
+ this.expires = -1
26
+ }
27
+ }
28
+
29
+ /**
30
+ * @returns whether template is empty
31
+ */
32
+ isEmpty():boolean {
33
+ return this.template == ""
34
+ }
35
+
36
+ /**
37
+ * Creates HTML Response from a key-value-pair.
38
+ *
39
+ * @param key key
40
+ * @param value value
41
+ *
42
+ */
43
+ renderFromKeyValue(key:string, value:string):OINOHttpResult {
44
+ const html:string = this.template.replaceAll('###' + key + '###', OINOStr.encode(value, OINOContentType.html))
45
+ const result:OINOHttpResult = new OINOHttpResult(html)
46
+ return result
47
+ }
48
+
49
+ /**
50
+ * Creates HTML Response from object properties.
51
+ *
52
+ * @param object object
53
+ *
54
+ */
55
+ renderFromObject(object:any):OINOHttpResult {
56
+ let html:string = this.template
57
+ if (object) {
58
+ for (let key in object) {
59
+ const value = object[key]
60
+ if (value) {
61
+ html = html.replaceAll('###' + key + '###', OINOStr.encode(value.toString(), OINOContentType.html))
62
+ }
63
+ }
64
+ }
65
+ html = html.replace(/###[^#]*###/g, "")
66
+ const result:OINOHttpResult = new OINOHttpResult(html)
67
+ if (this.expires >= 1) {
68
+ result.headers["Expires"] = Math.round(this.expires).toString()
69
+ }
70
+ return result
71
+ }
72
+
73
+ /**
74
+ * Creates HTML Response from API result.
75
+ *
76
+ * @param result OINOResult-object
77
+ * @param includeErrorMessages include debug messages in result
78
+ * @param includeWarningMessages include debug messages in result
79
+ * @param includeInfoMessages include debug messages in result
80
+ * @param includeDebugMessages include debug messages in result
81
+ *
82
+ */
83
+ renderFromResult(result:OINOResult, includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
84
+ let html:string = this.template
85
+ html = html.replaceAll('###statusCode###', OINOStr.encode(result.statusCode.toString(), OINOContentType.html))
86
+ html = html.replaceAll('###statusMessage###', OINOStr.encode(result.statusMessage.toString(), OINOContentType.html))
87
+ let messages = ""
88
+ for (let i:number = 0; i<result.messages.length; i++) {
89
+ if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
90
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
91
+ }
92
+ if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
93
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
94
+ }
95
+ if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
96
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
97
+ }
98
+ if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
99
+ messages += "<li>" + OINOStr.encode(result.messages[i], OINOContentType.html) + "</li>"
100
+ }
101
+
102
+ }
103
+ if (messages) {
104
+ html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>")
105
+ }
106
+ html = html.replace(/###[^#]*###/g, "")
107
+ const http_result:OINOHttpResult = new OINOHttpResult(html)
108
+ return http_result
109
+ }
110
+ };
package/src/OINOResult.ts CHANGED
@@ -35,6 +35,18 @@ export class OINOResult {
35
35
  this.messages = []
36
36
  }
37
37
 
38
+ /**
39
+ * Copy values from different result.
40
+ *
41
+ * @param result source value
42
+ */
43
+ copy(result: OINOResult) {
44
+ this.success = result.success
45
+ this.statusCode = result.statusCode
46
+ this.statusMessage = result.statusMessage
47
+ this.messages = result.messages.slice()
48
+ }
49
+
38
50
  /**
39
51
  * Set HTTP OK status (does not reset messages).
40
52
  *
@@ -46,14 +58,14 @@ export class OINOResult {
46
58
  }
47
59
 
48
60
  /**
49
- * Set HTTP error status using given code and message.
61
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
50
62
  *
51
63
  * @param statusCode HTTP status code
52
64
  * @param statusMessage HTTP status message
53
65
  * @param operation operation where error occured
54
66
  *
55
67
  */
56
- setError(statusCode:number, statusMessage:string, operation:string) {
68
+ setError(statusCode:number, statusMessage:string, operation:string):OINOResult {
57
69
  this.success = false
58
70
  this.statusCode = statusCode
59
71
  if (this.statusMessage != "OK") {
@@ -64,48 +76,52 @@ export class OINOResult {
64
76
  } else {
65
77
  this.statusMessage = OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage
66
78
  }
79
+ return this
67
80
  }
68
81
 
69
82
  /**
70
- * Add warning message.
83
+ * Add warning message. Returns self reference for chaining.
71
84
  *
72
85
  * @param message HTTP status message
73
86
  * @param operation operation where warning occured
74
87
  *
75
88
  */
76
- addWarning(message:string, operation:string) {
89
+ addWarning(message:string, operation:string):OINOResult {
77
90
  message = message.trim()
78
91
  if (message) {
79
92
  this.messages.push(OINO_WARNING_PREFIX + " (" + operation + "): " + message)
80
93
  }
94
+ return this
81
95
  }
82
96
 
83
97
  /**
84
- * Add info message.
98
+ * Add info message. Returns self reference for chaining.
85
99
  *
86
100
  * @param message HTTP status message
87
101
  * @param operation operation where info occured
88
102
  *
89
103
  */
90
- addInfo(message:string, operation:string) {
104
+ addInfo(message:string, operation:string):OINOResult {
91
105
  message = message.trim()
92
106
  if (message) {
93
107
  this.messages.push(OINO_INFO_PREFIX + " (" + operation + "): " + message)
94
108
  }
109
+ return this
95
110
  }
96
111
 
97
112
  /**
98
- * Add debug message.
113
+ * Add debug message. Returns self reference for chaining.
99
114
  *
100
115
  * @param message HTTP status message
101
116
  * @param operation operation where debug occured
102
117
  *
103
118
  */
104
- addDebug(message:string, operation:string) {
119
+ addDebug(message:string, operation:string):OINOResult {
105
120
  message = message.trim()
106
121
  if (message) {
107
122
  this.messages.push(OINO_DEBUG_PREFIX + " (" + operation + "): " + message)
108
123
  }
124
+ return this
109
125
  }
110
126
 
111
127
  /**
@@ -141,3 +157,47 @@ export class OINOResult {
141
157
  }
142
158
  }
143
159
  }
160
+
161
+ /**
162
+ * Specialized result for HTTP responses.
163
+ */
164
+ export class OINOHttpResult extends OINOResult {
165
+ /** HTTP body data */
166
+ body: string
167
+
168
+ /** HTTP header values */
169
+ headers: Record<string, string>
170
+
171
+ /**
172
+ * Constructor for a `OINOHttpResult`
173
+ *
174
+ * @param body HTTP body
175
+ * @param headers HTTP headers
176
+ */
177
+ constructor(body:string, headers?:Record<string, string>) {
178
+ super()
179
+ this.body = body
180
+ if (headers) {
181
+ this.headers = headers
182
+ } else {
183
+ this.headers = {}
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Get a Response object from the result values.
189
+ *
190
+ * @param headers HTTP headers (overrides existing values)
191
+ */
192
+ getResponse(headers?:Record<string, string>):Response {
193
+ if (!headers) {
194
+ headers = this.headers
195
+ }
196
+ headers['Content-Length'] = this.body.length.toString()
197
+ const result = new Response(this.body, {status:this.statusCode, statusText: this.statusMessage, headers: headers})
198
+ return result
199
+ }
200
+
201
+
202
+
203
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export { OINOBenchmark } from "./OINOBenchmark.js"
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
- export { OINOResult } from "./OINOResult.js"
3
+ export { OINOResult, OINOHttpResult } from "./OINOResult.js"
4
4
  export { OINOStr } from "./OINOStr.js"
5
+ export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js"
5
6
 
6
7
  /** OINO error message prefix */
7
8
  export const OINO_ERROR_PREFIX = "OINO ERROR"