@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
@@ -0,0 +1,205 @@
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.OINOSwagger = void 0;
9
+ /**
10
+ * Static class for Swagger utilities
11
+ *
12
+ */
13
+ class OINOSwagger {
14
+ static _getSchemaApiMethodParamsQueryId() {
15
+ return {
16
+ "schema": {
17
+ "type": "string"
18
+ },
19
+ "in": "path",
20
+ "name": "id",
21
+ "required": true
22
+ };
23
+ }
24
+ static _getSchemaApiMethodParamsBody(tableName) {
25
+ return {
26
+ "required": true,
27
+ "content": {
28
+ "application/json": {
29
+ "schema": {
30
+ "$ref": "#/components/schemas/" + tableName
31
+ }
32
+ }
33
+ }
34
+ };
35
+ }
36
+ static _getSchemaApiMethodDescription(method, tableName, hasQueryIdParam) {
37
+ if (hasQueryIdParam) {
38
+ return method.toUpperCase() + " " + tableName + " object";
39
+ }
40
+ else {
41
+ return method.toUpperCase() + " " + tableName + " object array";
42
+ }
43
+ }
44
+ static _getSchemaApiMethodOperationId(method, tableName, hasQueryIdParam) {
45
+ if (hasQueryIdParam) {
46
+ return method + tableName;
47
+ }
48
+ else {
49
+ return method + tableName + "All";
50
+ }
51
+ }
52
+ static _getSchemaOinoResponse() {
53
+ return {
54
+ "type": "object",
55
+ "properties": {
56
+ "success": {
57
+ "type": "boolean"
58
+ },
59
+ "status": {
60
+ "type": "number"
61
+ },
62
+ "statusText": {
63
+ "type": "string"
64
+ },
65
+ "messages": {
66
+ "type": "array",
67
+ "items": {
68
+ "type": "string"
69
+ }
70
+ }
71
+ },
72
+ "required": [
73
+ "success",
74
+ "status",
75
+ "statusText",
76
+ "messages"
77
+ ]
78
+ };
79
+ }
80
+ static _getSchemaFieldType(field) {
81
+ let type_string;
82
+ if (field.type == "boolean") {
83
+ type_string = "boolean";
84
+ }
85
+ else if (field.type == "integer" || field.type == "number") {
86
+ type_string = "number";
87
+ }
88
+ else {
89
+ type_string = "string";
90
+ }
91
+ if (!field.fieldParams.isNotNull) {
92
+ return {
93
+ "anyOf": [
94
+ {
95
+ "type": type_string
96
+ },
97
+ {
98
+ "type": "null"
99
+ }
100
+ ]
101
+ };
102
+ }
103
+ else {
104
+ return { type: type_string };
105
+ }
106
+ }
107
+ static _getSwaggerApiType(api) {
108
+ let result = {
109
+ type: "object",
110
+ properties: {},
111
+ required: []
112
+ };
113
+ let field;
114
+ for (field of api.datamodel.fields) {
115
+ result.properties[field.name] = this._getSchemaFieldType(field);
116
+ if (field.fieldParams.isPrimaryKey) {
117
+ result.required.push(field.name);
118
+ }
119
+ }
120
+ return result;
121
+ }
122
+ static _getSchemaType(tableName, hasQueryIdParam, hasResultData) {
123
+ if (hasResultData && hasQueryIdParam) {
124
+ return { "$ref": "#/components/schemas/" + tableName };
125
+ }
126
+ else if (hasResultData) {
127
+ return { type: "array", items: { "$ref": "#/components/schemas/" + tableName } };
128
+ }
129
+ else if (hasQueryIdParam) {
130
+ return { "$ref": "#/components/schemas/OINOResponse" };
131
+ }
132
+ else {
133
+ return { "$ref": "#/components/schemas/OINOResponse" };
134
+ }
135
+ }
136
+ static _getSchemaApiMethodParams(hasQueryIdParam) {
137
+ if (hasQueryIdParam) {
138
+ return [this._getSchemaApiMethodParamsQueryId()];
139
+ }
140
+ else {
141
+ return [];
142
+ }
143
+ }
144
+ static _getSchemaApiMethod(method, tableName, hasQueryIdParam, hasBody, hasResultData) {
145
+ const result = {
146
+ responses: {
147
+ 200: { description: this._getSchemaApiMethodDescription(method, tableName, hasQueryIdParam), content: { "application/json": { schema: this._getSchemaType(tableName, hasQueryIdParam, hasResultData) } } }
148
+ },
149
+ "operationId": this._getSchemaApiMethodOperationId(method, tableName, hasQueryIdParam),
150
+ "parameters": this._getSchemaApiMethodParams(hasQueryIdParam)
151
+ };
152
+ if (hasBody) {
153
+ result["requestBody"] = this._getSchemaApiMethodParamsBody(tableName);
154
+ }
155
+ return result;
156
+ }
157
+ static _getSwaggerApiPath(tableName, hasQueryIdParam) {
158
+ if (hasQueryIdParam) {
159
+ return {
160
+ get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
161
+ put: this._getSchemaApiMethod("put", tableName, hasQueryIdParam, true, false),
162
+ delete: this._getSchemaApiMethod("delete", tableName, hasQueryIdParam, false, false)
163
+ };
164
+ }
165
+ else {
166
+ return {
167
+ get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
168
+ post: this._getSchemaApiMethod("post", tableName, hasQueryIdParam, true, false)
169
+ };
170
+ }
171
+ }
172
+ /**
173
+ * Returns swagger.json as object of the given API's.
174
+ *
175
+ * @param apis array of API's use for Swagger definition
176
+ *
177
+ */
178
+ static getApiDefinition(apis) {
179
+ let result = {
180
+ "openapi": "3.1.0",
181
+ "info": {
182
+ "title": "",
183
+ "description": "",
184
+ "version": ""
185
+ },
186
+ "paths": {},
187
+ "components": {
188
+ "schemas": {
189
+ OINOResponse: this._getSchemaOinoResponse()
190
+ }
191
+ }
192
+ };
193
+ for (let i = 0; i < apis.length; i++) {
194
+ if (!apis[i].initialized) {
195
+ throw new Error("API " + apis[i].params.tableName + " is not initialized, cannot create Swagger definition");
196
+ }
197
+ const table_name = apis[i].params.tableName;
198
+ result.paths["/" + table_name] = this._getSwaggerApiPath(table_name, false);
199
+ result.paths["/" + table_name + "/{id}"] = this._getSwaggerApiPath(table_name, true);
200
+ result.components.schemas[table_name] = this._getSwaggerApiType(apis[i]);
201
+ }
202
+ return result;
203
+ }
204
+ }
205
+ exports.OINOSwagger = OINOSwagger;
package/dist/cjs/index.js CHANGED
@@ -1,53 +1,71 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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.OINOHeaders = 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;
3
+ exports.OINOSwagger = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOHttpRequest = exports.OINORequest = exports.OINOQueryAggregate = exports.OINOQueryLimit = exports.OINOQueryOrder = exports.OINOQueryFilter = exports.OINOQuerySelect = exports.OINOQueryAggregateFunctions = exports.OINOQueryNullCheck = exports.OINOQueryComparison = exports.OINOQueryBooleanOperation = exports.OINOParser = exports.OINOModelSet = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOHtmlTemplate = exports.OINOHeaders = exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = exports.OINOBlobDataField = exports.OINODatetimeDataField = exports.OINOBooleanDataField = exports.OINONumberDataField = exports.OINOStringDataField = exports.OINODataField = exports.OINOMemoryDataset = exports.OINODataSet = exports.OINODataSource = exports.OINODataModel = exports.OINO_EMPTY_ROWS = exports.OINO_EMPTY_ROW = 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.OINOConfig = exports.OINOMemoryBenchmark = exports.OINOBenchmark = exports.OINOApiHtmlTemplate = exports.OINOApiResult = exports.OINOApiRequest = exports.OINOApi = void 0;
4
+ var OINOApi_js_1 = require("./OINOApi.js");
5
+ Object.defineProperty(exports, "OINOApi", { enumerable: true, get: function () { return OINOApi_js_1.OINOApi; } });
6
+ Object.defineProperty(exports, "OINOApiRequest", { enumerable: true, get: function () { return OINOApi_js_1.OINOApiRequest; } });
7
+ Object.defineProperty(exports, "OINOApiResult", { enumerable: true, get: function () { return OINOApi_js_1.OINOApiResult; } });
8
+ Object.defineProperty(exports, "OINOApiHtmlTemplate", { enumerable: true, get: function () { return OINOApi_js_1.OINOApiHtmlTemplate; } });
4
9
  var OINOBenchmark_js_1 = require("./OINOBenchmark.js");
5
10
  Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOBenchmark; } });
6
11
  Object.defineProperty(exports, "OINOMemoryBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOMemoryBenchmark; } });
12
+ var OINOConfig_js_1 = require("./OINOConfig.js");
13
+ Object.defineProperty(exports, "OINOConfig", { enumerable: true, get: function () { return OINOConfig_js_1.OINOConfig; } });
14
+ var OINOConstants_js_1 = require("./OINOConstants.js");
15
+ Object.defineProperty(exports, "OINO_ERROR_PREFIX", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_ERROR_PREFIX; } });
16
+ Object.defineProperty(exports, "OINO_WARNING_PREFIX", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_WARNING_PREFIX; } });
17
+ Object.defineProperty(exports, "OINO_INFO_PREFIX", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_INFO_PREFIX; } });
18
+ Object.defineProperty(exports, "OINO_DEBUG_PREFIX", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_DEBUG_PREFIX; } });
19
+ Object.defineProperty(exports, "OINO_REQUEST_TYPE_PARAM", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_REQUEST_TYPE_PARAM; } });
20
+ Object.defineProperty(exports, "OINO_RESPONSE_TYPE_PARAM", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_RESPONSE_TYPE_PARAM; } });
21
+ Object.defineProperty(exports, "OINOContentType", { enumerable: true, get: function () { return OINOConstants_js_1.OINOContentType; } });
22
+ Object.defineProperty(exports, "OINO_EMPTY_ROW", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_EMPTY_ROW; } });
23
+ Object.defineProperty(exports, "OINO_EMPTY_ROWS", { enumerable: true, get: function () { return OINOConstants_js_1.OINO_EMPTY_ROWS; } });
24
+ var OINODataModel_js_1 = require("./OINODataModel.js");
25
+ Object.defineProperty(exports, "OINODataModel", { enumerable: true, get: function () { return OINODataModel_js_1.OINODataModel; } });
26
+ var OINODataSource_js_1 = require("./OINODataSource.js");
27
+ Object.defineProperty(exports, "OINODataSource", { enumerable: true, get: function () { return OINODataSource_js_1.OINODataSource; } });
28
+ Object.defineProperty(exports, "OINODataSet", { enumerable: true, get: function () { return OINODataSource_js_1.OINODataSet; } });
29
+ Object.defineProperty(exports, "OINOMemoryDataset", { enumerable: true, get: function () { return OINODataSource_js_1.OINOMemoryDataset; } });
30
+ var OINODataField_js_1 = require("./OINODataField.js");
31
+ Object.defineProperty(exports, "OINODataField", { enumerable: true, get: function () { return OINODataField_js_1.OINODataField; } });
32
+ Object.defineProperty(exports, "OINOStringDataField", { enumerable: true, get: function () { return OINODataField_js_1.OINOStringDataField; } });
33
+ Object.defineProperty(exports, "OINONumberDataField", { enumerable: true, get: function () { return OINODataField_js_1.OINONumberDataField; } });
34
+ Object.defineProperty(exports, "OINOBooleanDataField", { enumerable: true, get: function () { return OINODataField_js_1.OINOBooleanDataField; } });
35
+ Object.defineProperty(exports, "OINODatetimeDataField", { enumerable: true, get: function () { return OINODataField_js_1.OINODatetimeDataField; } });
36
+ Object.defineProperty(exports, "OINOBlobDataField", { enumerable: true, get: function () { return OINODataField_js_1.OINOBlobDataField; } });
37
+ var OINOFormatter_js_1 = require("./OINOFormatter.js");
38
+ Object.defineProperty(exports, "OINOFormatter", { enumerable: true, get: function () { return OINOFormatter_js_1.OINOFormatter; } });
39
+ Object.defineProperty(exports, "OINO_EMPTY_FORMATTER", { enumerable: true, get: function () { return OINOFormatter_js_1.OINO_EMPTY_FORMATTER; } });
40
+ var OINOHeaders_js_1 = require("./OINOHeaders.js");
41
+ Object.defineProperty(exports, "OINOHeaders", { enumerable: true, get: function () { return OINOHeaders_js_1.OINOHeaders; } });
42
+ var OINOHtmlTemplate_js_1 = require("./OINOHtmlTemplate.js");
43
+ Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return OINOHtmlTemplate_js_1.OINOHtmlTemplate; } });
7
44
  var OINOLog_js_1 = require("./OINOLog.js");
8
45
  Object.defineProperty(exports, "OINOLog", { enumerable: true, get: function () { return OINOLog_js_1.OINOLog; } });
9
46
  Object.defineProperty(exports, "OINOLogLevel", { enumerable: true, get: function () { return OINOLog_js_1.OINOLogLevel; } });
10
47
  Object.defineProperty(exports, "OINOConsoleLog", { enumerable: true, get: function () { return OINOLog_js_1.OINOConsoleLog; } });
11
- var OINOResult_js_1 = require("./OINOResult.js");
12
- Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOResult; } });
13
- Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOHttpResult; } });
48
+ var OINOModelSet_js_1 = require("./OINOModelSet.js");
49
+ Object.defineProperty(exports, "OINOModelSet", { enumerable: true, get: function () { return OINOModelSet_js_1.OINOModelSet; } });
50
+ var OINOParser_js_1 = require("./OINOParser.js");
51
+ Object.defineProperty(exports, "OINOParser", { enumerable: true, get: function () { return OINOParser_js_1.OINOParser; } });
52
+ var OINOQueryParams_js_1 = require("./OINOQueryParams.js");
53
+ Object.defineProperty(exports, "OINOQueryBooleanOperation", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryBooleanOperation; } });
54
+ Object.defineProperty(exports, "OINOQueryComparison", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryComparison; } });
55
+ Object.defineProperty(exports, "OINOQueryNullCheck", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryNullCheck; } });
56
+ Object.defineProperty(exports, "OINOQueryAggregateFunctions", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryAggregateFunctions; } });
57
+ Object.defineProperty(exports, "OINOQuerySelect", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQuerySelect; } });
58
+ Object.defineProperty(exports, "OINOQueryFilter", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryFilter; } });
59
+ Object.defineProperty(exports, "OINOQueryOrder", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryOrder; } });
60
+ Object.defineProperty(exports, "OINOQueryLimit", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryLimit; } });
61
+ Object.defineProperty(exports, "OINOQueryAggregate", { enumerable: true, get: function () { return OINOQueryParams_js_1.OINOQueryAggregate; } });
14
62
  var OINORequest_js_1 = require("./OINORequest.js");
15
63
  Object.defineProperty(exports, "OINORequest", { enumerable: true, get: function () { return OINORequest_js_1.OINORequest; } });
16
64
  Object.defineProperty(exports, "OINOHttpRequest", { enumerable: true, get: function () { return OINORequest_js_1.OINOHttpRequest; } });
65
+ var OINOResult_js_1 = require("./OINOResult.js");
66
+ Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOResult; } });
67
+ Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return OINOResult_js_1.OINOHttpResult; } });
17
68
  var OINOStr_js_1 = require("./OINOStr.js");
18
69
  Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return OINOStr_js_1.OINOStr; } });
19
- var OINOHtmlTemplate_js_1 = require("./OINOHtmlTemplate.js");
20
- Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return OINOHtmlTemplate_js_1.OINOHtmlTemplate; } });
21
- var OINOFormatter_js_1 = require("./OINOFormatter.js");
22
- Object.defineProperty(exports, "OINOFormatter", { enumerable: true, get: function () { return OINOFormatter_js_1.OINOFormatter; } });
23
- Object.defineProperty(exports, "OINO_EMPTY_FORMATTER", { enumerable: true, get: function () { return OINOFormatter_js_1.OINO_EMPTY_FORMATTER; } });
24
- var OINOHeaders_js_1 = require("./OINOHeaders.js");
25
- Object.defineProperty(exports, "OINOHeaders", { enumerable: true, get: function () { return OINOHeaders_js_1.OINOHeaders; } });
26
- /** OINO error message prefix */
27
- exports.OINO_ERROR_PREFIX = "OINO ERROR";
28
- /** OINO warning message prefix */
29
- exports.OINO_WARNING_PREFIX = "OINO WARNING";
30
- /** OINO info message prefix */
31
- exports.OINO_INFO_PREFIX = "OINO INFO";
32
- /** OINO debug message prefix */
33
- exports.OINO_DEBUG_PREFIX = "OINO DEBUG";
34
- /** Name of the OINOContentType-parameter request */
35
- exports.OINO_REQUEST_TYPE_PARAM = "oinorequesttype";
36
- /** Name of the OINOContentType-parameter request */
37
- exports.OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype";
38
- /**
39
- * Supported content format mime-types
40
- */
41
- var OINOContentType;
42
- (function (OINOContentType) {
43
- /** JSON encoded data */
44
- OINOContentType["json"] = "application/json";
45
- /** CSV encoded data */
46
- OINOContentType["csv"] = "text/csv";
47
- /** Multipart encoded form data */
48
- OINOContentType["formdata"] = "multipart/form-data";
49
- /** URL encoded form data */
50
- OINOContentType["urlencode"] = "application/x-www-form-urlencoded";
51
- /** HTML encoded data (output only) */
52
- OINOContentType["html"] = "text/html";
53
- })(OINOContentType || (exports.OINOContentType = OINOContentType = {}));
70
+ var OINOSwagger_js_1 = require("./OINOSwagger.js");
71
+ Object.defineProperty(exports, "OINOSwagger", { enumerable: true, get: function () { return OINOSwagger_js_1.OINOSwagger; } });
@@ -0,0 +1,315 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { OINOHashid } from "@oino-ts/hashid";
3
+ import { OINOQueryFilter, OINOQueryOrder, OINOQueryAggregate, OINOQueryLimit, OINOQuerySelect, OINOQueryBooleanOperation } from "./OINOQueryParams.js";
4
+ import { OINOConfig } from "./OINOConfig.js";
5
+ import { OINOHttpRequest } from "./OINORequest.js";
6
+ import { OINOResult } from "./OINOResult.js";
7
+ import { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
8
+ import { OINODatetimeDataField, OINONumberDataField } from "./OINODataField.js";
9
+ import { OINOBenchmark } from "./OINOBenchmark.js";
10
+ export class OINOApiRequest extends OINOHttpRequest {
11
+ rowId;
12
+ rowData;
13
+ queryParams;
14
+ constructor(init) {
15
+ super(init);
16
+ this.rowId = init?.rowId || "";
17
+ this.rowData = init?.rowData || null; // rowData is not compatible with OINOHttpRequest body so it's not automatically set, caller can set both if needed
18
+ this.queryParams = init?.queryParams || {};
19
+ if (init?.filter) {
20
+ if (init.filter instanceof OINOQueryFilter) {
21
+ this.queryParams.filter = init.filter;
22
+ }
23
+ else {
24
+ this.queryParams.filter = OINOQueryFilter.parse(init.filter);
25
+ }
26
+ }
27
+ if (!this.queryParams.filter) {
28
+ const filter_params = this.url?.searchParams.getAll(OINOConfig.OINO_QUERY_FILTER_PARAM) || [];
29
+ for (let i = 0; i < filter_params.length; i++) {
30
+ const f = OINOQueryFilter.parse(filter_params[i]);
31
+ if (i > 0) {
32
+ this.queryParams.filter = OINOQueryFilter.combine(this.queryParams.filter, OINOQueryBooleanOperation.and, f);
33
+ }
34
+ else {
35
+ this.queryParams.filter = f;
36
+ }
37
+ }
38
+ }
39
+ if (init?.order) {
40
+ if (init.order instanceof OINOQueryOrder) {
41
+ this.queryParams.order = init.order;
42
+ }
43
+ else {
44
+ this.queryParams.order = OINOQueryOrder.parse(init.order);
45
+ }
46
+ }
47
+ if (!this.queryParams.order) {
48
+ const order_param = this.url?.searchParams.get(OINOConfig.OINO_QUERY_ORDER_PARAM);
49
+ if (order_param) {
50
+ this.queryParams.order = OINOQueryOrder.parse(order_param);
51
+ }
52
+ }
53
+ if (init?.limit) {
54
+ if (init.limit instanceof OINOQueryLimit) {
55
+ this.queryParams.limit = init.limit;
56
+ }
57
+ else {
58
+ this.queryParams.limit = OINOQueryLimit.parse(init.limit);
59
+ }
60
+ }
61
+ if (!this.queryParams.limit) {
62
+ const limit_param = this.url?.searchParams.get(OINOConfig.OINO_QUERY_LIMIT_PARAM);
63
+ if (limit_param) {
64
+ this.queryParams.limit = OINOQueryLimit.parse(limit_param);
65
+ }
66
+ }
67
+ if (init?.aggregate) {
68
+ if (init.aggregate instanceof OINOQueryAggregate) {
69
+ this.queryParams.aggregate = init.aggregate;
70
+ }
71
+ else {
72
+ this.queryParams.aggregate = OINOQueryAggregate.parse(init.aggregate);
73
+ }
74
+ }
75
+ if (!this.queryParams.aggregate) {
76
+ const aggregate_param = this.url?.searchParams.get(OINOConfig.OINO_QUERY_AGGREGATE_PARAM);
77
+ if (aggregate_param) {
78
+ this.queryParams.aggregate = OINOQueryAggregate.parse(aggregate_param);
79
+ }
80
+ }
81
+ if (init?.select) {
82
+ if (init.select instanceof OINOQuerySelect) {
83
+ this.queryParams.select = init.select;
84
+ }
85
+ else {
86
+ this.queryParams.select = OINOQuerySelect.parse(init.select);
87
+ }
88
+ }
89
+ if (!this.queryParams.select) {
90
+ const select_param = this.url?.searchParams.get(OINOConfig.OINO_QUERY_SELECT_PARAM);
91
+ if (select_param) {
92
+ this.queryParams.select = OINOQuerySelect.parse(select_param);
93
+ }
94
+ }
95
+ }
96
+ static async fromFetchRequest(request, rowId, rowData, queryParams) {
97
+ return new OINOApiRequest({
98
+ url: new URL(request.url),
99
+ method: request.method,
100
+ headers: Object.fromEntries(request.headers),
101
+ rowId: rowId,
102
+ rowData: rowData ?? Buffer.from(await request.arrayBuffer()),
103
+ queryParams: queryParams
104
+ });
105
+ }
106
+ static fromHttpRequest(request, rowId, rowData, queryParams) {
107
+ return new OINOApiRequest({
108
+ url: typeof request.url === "string" ? new URL(request.url) : request.url,
109
+ method: request.method,
110
+ headers: Object.fromEntries(request.headers),
111
+ rowId: rowId,
112
+ rowData: rowData ?? request.bodyAsBuffer(),
113
+ requestType: request.requestType,
114
+ responseType: request.responseType,
115
+ multipartBoundary: request.multipartBoundary,
116
+ lastModified: request.lastModified,
117
+ queryParams: queryParams
118
+ });
119
+ }
120
+ }
121
+ /**
122
+ * OINO API request result object with returned data and/or http status code/message and
123
+ * error / warning messages.
124
+ *
125
+ */
126
+ export class OINOApiResult extends OINOResult {
127
+ /** DbApi request params */
128
+ request;
129
+ /** Returned data if any */
130
+ data;
131
+ /**
132
+ * Constructor of OINOApiResult.
133
+ *
134
+ * @param request DbApi request parameters
135
+ * @param data result data
136
+ *
137
+ */
138
+ constructor(request, data) {
139
+ super();
140
+ this.request = request;
141
+ this.data = data;
142
+ }
143
+ /**
144
+ * Creates a HTTP Response from API results.
145
+ *
146
+ * @param headers Headers to include in the response
147
+ *
148
+ */
149
+ async writeApiResponse(headers = {}) {
150
+ let response = null;
151
+ if (this.success && this.data) {
152
+ const body = await this.data.writeString(this.request.responseType);
153
+ response = new Response(body, { status: this.status, statusText: this.statusText, headers: headers });
154
+ }
155
+ else {
156
+ response = new Response(JSON.stringify(this, null, 3), { status: this.status, statusText: this.statusText, headers: headers });
157
+ }
158
+ if (this.request.responseDownload) {
159
+ response.headers.set('Content-Disposition', 'attachment; filename="' + this.request.responseDownload + '"');
160
+ }
161
+ else {
162
+ response.headers.set('Content-Disposition', 'inline');
163
+ }
164
+ for (let i = 0; i < this.messages.length; i++) {
165
+ response.headers.set('X-OINO-MESSAGE-' + i, this.messages[i]);
166
+ }
167
+ return Promise.resolve(response);
168
+ }
169
+ }
170
+ /**
171
+ * Specialized HTML template that can render ´OINOApiResult´.
172
+ *
173
+ */
174
+ export class OINOApiHtmlTemplate extends OINOHtmlTemplate {
175
+ /** Locale validation regex */
176
+ static LOCALE_REGEX = /^(\w\w)(\-\w\w)?$/;
177
+ /** Locale formatter */
178
+ _locale;
179
+ _numberDecimals = -1;
180
+ /**
181
+ * Constructor of OINOApiHtmlTemplate.
182
+ *
183
+ * @param template HTML template string
184
+ * @param numberDecimals Number of decimals to use for numbers, -1 for no formatting
185
+ * @param dateLocaleStr Datetime format string, either "iso" for ISO8601 or "default" for system default or valid locale string
186
+ * @param dateLocaleStyle Datetime format style, either "short/medium/long/full" or Intl.DateTimeFormat options
187
+ *
188
+ */
189
+ constructor(template, numberDecimals = -1, dateLocaleStr = "", dateLocaleStyle = "") {
190
+ super(template);
191
+ let locale_opts;
192
+ if ((dateLocaleStyle == null) || (dateLocaleStyle == "")) {
193
+ locale_opts = { dateStyle: "medium", timeStyle: "medium" };
194
+ }
195
+ else if (typeof dateLocaleStyle == "string") {
196
+ locale_opts = { dateStyle: dateLocaleStyle, timeStyle: dateLocaleStyle };
197
+ }
198
+ else {
199
+ locale_opts = dateLocaleStyle;
200
+ }
201
+ this._locale = null;
202
+ this._numberDecimals = numberDecimals;
203
+ if ((dateLocaleStr != null) && (dateLocaleStr != "") && OINOApiHtmlTemplate.LOCALE_REGEX.test(dateLocaleStr)) {
204
+ try {
205
+ this._locale = new Intl.DateTimeFormat(dateLocaleStr, locale_opts);
206
+ }
207
+ catch (e) { }
208
+ }
209
+ }
210
+ /**
211
+ * Creates HTML Response from API modelset.
212
+ *
213
+ * @param modelset OINO API dataset
214
+ * @param overrideValues values to override in the data
215
+ *
216
+ */
217
+ async renderFromDbData(modelset, overrideValues) {
218
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromDbData");
219
+ let html = "";
220
+ const dataset = modelset.dataset;
221
+ const datamodel = modelset.datamodel;
222
+ const api = modelset.datamodel.api;
223
+ const modified_index = datamodel.findFieldIndexByName(api.params.cacheModifiedField || "");
224
+ let last_modified = this.modified;
225
+ while (!dataset.isEof()) {
226
+ const row = dataset.getRow();
227
+ if (modified_index >= 0) {
228
+ last_modified = Math.max(last_modified, new Date(row[modified_index]).getTime());
229
+ }
230
+ let row_id_seed = datamodel.getRowPrimarykeyValues(row).join(' ');
231
+ let primary_key_values = [];
232
+ this.clearVariables();
233
+ this.setVariableFromValue(OINOConfig.OINO_ID_FIELD, "");
234
+ for (let i = 0; i < datamodel.fields.length; i++) {
235
+ const f = datamodel.fields[i];
236
+ let value;
237
+ if ((f instanceof OINODatetimeDataField) && (this._locale != null)) {
238
+ value = f.serializeCellWithLocale(row[i], this._locale);
239
+ }
240
+ else if ((f instanceof OINONumberDataField) && (this._numberDecimals >= 0) && (typeof row[i] === "number")) {
241
+ // console.debug("renderFromDbData number decimals", { field: f.name, value: row[i], type: typeof row[i] });
242
+ value = row[i].toFixed(this._numberDecimals);
243
+ }
244
+ else {
245
+ value = f.serializeCell(row[i]);
246
+ }
247
+ if (f.fieldParams.isPrimaryKey || f.fieldParams.isForeignKey) {
248
+ if (value && (f instanceof OINONumberDataField) && (datamodel.api.hashid)) {
249
+ value = datamodel.api.hashid.encode(value, f.name + " " + row_id_seed);
250
+ }
251
+ if (f.fieldParams.isPrimaryKey) {
252
+ primary_key_values.push(value || "");
253
+ }
254
+ }
255
+ this.setVariableFromValue(f.name, value || "");
256
+ }
257
+ this.setVariableFromProperties(overrideValues);
258
+ this.setVariableFromValue(OINOConfig.OINO_ID_FIELD, OINOConfig.printOINOId(primary_key_values));
259
+ html += this._renderHtml() + "\r\n";
260
+ await dataset.next();
261
+ }
262
+ this.modified = last_modified;
263
+ const result = this._createHttpResult(html);
264
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromDbData");
265
+ return result;
266
+ }
267
+ }
268
+ /**
269
+ * API class with method to process HTTP REST requests.
270
+ *
271
+ */
272
+ export class OINOApi {
273
+ /** Enable debug output on errors */
274
+ _debugOnError = false;
275
+ /** API database reference */
276
+ datasource;
277
+ /** API parameters */
278
+ params;
279
+ /** API hashid */
280
+ hashid;
281
+ /** Is API initialized */
282
+ initialized = false;
283
+ /** API datamodel */
284
+ datamodel = null;
285
+ constructor(datasource, params) {
286
+ this.datasource = datasource;
287
+ this.params = params;
288
+ if (this.params.hashidKey) {
289
+ this.hashid = new OINOHashid(this.params.hashidKey, this.params.apiName, this.params.hashidLength, this.params.hashidStaticIds);
290
+ }
291
+ else {
292
+ this.hashid = null;
293
+ }
294
+ }
295
+ /**
296
+ * Enable or disable debug output on errors.
297
+ *
298
+ * @param debugOnError true to enable debug output on errors, false to disable
299
+ */
300
+ setDebugOnError(debugOnError) {
301
+ this._debugOnError = debugOnError;
302
+ }
303
+ /**
304
+ * Method to check if a field is included in the API params.
305
+ *
306
+ * @param fieldName name of the field
307
+ *
308
+ */
309
+ isFieldIncluded(fieldName) {
310
+ const params = this.params;
311
+ return (((params.excludeFieldPrefix == undefined) || (params.excludeFieldPrefix == "") || (fieldName.startsWith(params.excludeFieldPrefix) == false)) &&
312
+ ((params.excludeFields == undefined) || (params.excludeFields.length == 0) || (params.excludeFields.indexOf(fieldName) < 0)) &&
313
+ ((params.includeFields == undefined) || (params.includeFields.length == 0) || (params.includeFields.indexOf(fieldName) >= 0)));
314
+ }
315
+ }