@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
@@ -1,222 +1,225 @@
1
- import { OINOStr, OINOContentType, OINOResult, OINOHttpResult, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOBenchmark } from "."
2
- import { OINO_EMPTY_FORMATTER, OINOFormatter } from "./OINOFormatter"
3
-
4
- /**
5
- * Class for rendering HTML from data.
6
- */
7
- export class OINOHtmlTemplate {
8
- private _tagOpen:string
9
- private _tagClose:string
10
- private _variables:Record<string, string> = {}
11
- private _tagStart:number[] = []
12
- private _tagEnd:number[] = []
13
- private _tagVariable:string[] = []
14
- private _tagFormatters:OINOFormatter[] = []
15
- private _tagCount:number = 0
16
-
17
- /** HTML template string */
18
- template: string;
19
-
20
- /** Cache modified value for template */
21
- modified: number;
22
-
23
- /** Cache expiration value for template */
24
- expires: number;
25
-
26
- /**
27
- * Creates HTML Response from a key-value-pair.
28
- *
29
- * @param template template string
30
- * @param tagOpen tag to start variable in template
31
- * @param tagClose tag to end variables in template
32
- */
33
- constructor (template:string, tagOpen:string = "{{{", tagClose:string = "}}}") {
34
- this.template = template
35
- this.modified = 0
36
- this.expires = 0
37
- this._tagOpen = tagOpen
38
- this._tagClose = tagClose
39
- this._parseTemplate()
40
- }
41
-
42
- /**
43
- * @returns whether template is empty
44
- */
45
- isEmpty():boolean {
46
- return this.template == ""
47
- }
48
-
49
- protected _parseTemplate() {
50
- const tag_open_length = this._tagOpen.length
51
- const tag_close_length = this._tagClose.length
52
- let tag_start_pos = this.template.indexOf(this._tagOpen, 0)
53
- let tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
54
- while ((tag_start_pos >= 0) && (tag_end_pos > tag_start_pos)) {
55
- this._tagStart.push(tag_start_pos)
56
- this._tagEnd.push(tag_end_pos)
57
- let variable = this.template.slice(tag_start_pos+tag_open_length, tag_end_pos - tag_close_length)
58
- const variable_parts = variable.split("|")
59
- if (variable_parts.length > 1) {
60
- const formatter: OINOFormatter = OINOFormatter.parse(variable_parts.slice(1))
61
- this._tagFormatters.push(formatter)
62
- } else {
63
- this._tagFormatters.push(OINO_EMPTY_FORMATTER)
64
- }
65
- this._tagVariable.push(variable_parts[0])
66
- this._tagCount = this._tagCount + 1
67
- tag_start_pos = this.template.indexOf(this._tagOpen, tag_end_pos)
68
- tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
69
- }
70
- }
71
-
72
- protected _createHttpResult(html:string):OINOHttpResult {
73
- const result:OINOHttpResult = new OINOHttpResult({body: html})
74
- if (this.expires >= 1) {
75
- result.expires = Math.round(this.expires)
76
- }
77
- if (this.modified >= 1) {
78
- result.lastModified = this.modified
79
- }
80
- return result
81
- }
82
-
83
- protected _renderHtml():string {
84
- let html:string = ""
85
- let start_pos = 0
86
- let end_pos = 0
87
- for (let i=0; i<this._tagCount; i++) {
88
- end_pos = this._tagStart[i]
89
- const key = this._tagVariable[i]
90
- const value = this._tagFormatters[i].format(this._variables[key] || "")
91
- html += this.template.slice(start_pos, end_pos) + value
92
- start_pos = this._tagEnd[i]
93
- }
94
- html += this.template.slice(start_pos)
95
- // let html:string = this.template
96
- // for (let key in this._variables) {
97
- // const value = this._variables[key]
98
- // html = html.replaceAll(this._tag + key + this._tag, value)
99
- // }
100
- return html
101
- }
102
-
103
- /**
104
- * Clear template variables.
105
- *
106
- */
107
- clearVariables() {
108
- this._variables = {}
109
- }
110
-
111
- /**
112
- * Sets template variable from a key-value-pair.
113
- *
114
- * @param variable key
115
- * @param value value
116
- * @param escapeValue whether to escape value
117
- *
118
- */
119
- setVariableFromValue(variable:string, value:string, escapeValue:boolean = true) {
120
- if (escapeValue) {
121
- value = OINOStr.encode(value, OINOContentType.html)
122
- }
123
- this._variables[variable] = value
124
- }
125
-
126
- /**
127
- * Sets template variables from object properties.
128
- *
129
- * @param object any object
130
- * @param escapeValue whether to escape value
131
- *
132
- */
133
- setVariableFromProperties(object:any, escapeValue:boolean = true) {
134
- if (object) {
135
- for (let key in object) {
136
- if (escapeValue) {
137
- this._variables[key] = OINOStr.encode(object[key], OINOContentType.html)
138
- } else {
139
- this._variables[key] = object[key]
140
- }
141
- }
142
- }
143
- }
144
-
145
- /**
146
- * Creates HTML Response from set variables.
147
- *
148
- */
149
- render():OINOHttpResult {
150
- const html:string = this._renderHtml()
151
- this.clearVariables() // clear variables after rendering
152
- return this._createHttpResult(html)
153
- }
154
-
155
- /**
156
- * Creates HTML Response from a key-value-pair.
157
- *
158
- * @param key key
159
- * @param value value
160
- *
161
- */
162
- renderFromKeyValue(key:string, value:string):OINOHttpResult {
163
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
164
- this.setVariableFromValue(key, value)
165
- const result:OINOHttpResult = this.render()
166
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
167
- return result
168
- }
169
-
170
- /**
171
- * Creates HTML Response from object properties.
172
- *
173
- * @param object object
174
- *
175
- */
176
- renderFromObject(object:any = true):OINOHttpResult {
177
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
178
- this.setVariableFromProperties(object)
179
- const result:OINOHttpResult = this.render()
180
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
181
- return result
182
- }
183
-
184
- /**
185
- * Creates HTML Response from API result.
186
- *
187
- * @param result OINOResult-object
188
- * @param messageSeparator HTML separator for messages
189
- * @param includeErrorMessages include debug messages in result
190
- * @param includeWarningMessages include debug messages in result
191
- * @param includeInfoMessages include debug messages in result
192
- * @param includeDebugMessages include debug messages in result
193
- *
194
- */
195
- renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
196
- OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
197
- this.setVariableFromValue("status", result.status.toString())
198
- this.setVariableFromValue("statusText", result.statusText.toString())
199
- let messages:string[] = []
200
- for (let i:number = 0; i<result.messages.length; i++) {
201
- if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
202
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
203
- }
204
- if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
205
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
206
- }
207
- if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
208
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
209
- }
210
- if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
211
- messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
212
- }
213
-
214
- }
215
- if (messageSeparator && (messages.length > 0)) {
216
- this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
217
- }
218
- const http_result:OINOHttpResult = this.render()
219
- OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
220
- return http_result
221
- }
222
- };
1
+ import { OINOContentType, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX } from "./OINOConstants.js"
2
+ import { OINOStr } from "./OINOStr.js"
3
+ import { OINOResult, OINOHttpResult } from "./OINOResult.js"
4
+ import { OINOBenchmark } from "./OINOBenchmark.js"
5
+ import { OINO_EMPTY_FORMATTER, OINOFormatter } from "./OINOFormatter"
6
+
7
+ /**
8
+ * Class for rendering HTML from data.
9
+ */
10
+ export class OINOHtmlTemplate {
11
+ private _tagOpen:string
12
+ private _tagClose:string
13
+ private _variables:Record<string, string> = {}
14
+ private _tagStart:number[] = []
15
+ private _tagEnd:number[] = []
16
+ private _tagVariable:string[] = []
17
+ private _tagFormatters:OINOFormatter[] = []
18
+ private _tagCount:number = 0
19
+
20
+ /** HTML template string */
21
+ template: string;
22
+
23
+ /** Cache modified value for template */
24
+ modified: number;
25
+
26
+ /** Cache expiration value for template */
27
+ expires: number;
28
+
29
+ /**
30
+ * Creates HTML Response from a key-value-pair.
31
+ *
32
+ * @param template template string
33
+ * @param tagOpen tag to start variable in template
34
+ * @param tagClose tag to end variables in template
35
+ */
36
+ constructor (template:string, tagOpen:string = "{{{", tagClose:string = "}}}") {
37
+ this.template = template
38
+ this.modified = 0
39
+ this.expires = 0
40
+ this._tagOpen = tagOpen
41
+ this._tagClose = tagClose
42
+ this._parseTemplate()
43
+ }
44
+
45
+ /**
46
+ * @returns whether template is empty
47
+ */
48
+ isEmpty():boolean {
49
+ return this.template == ""
50
+ }
51
+
52
+ protected _parseTemplate() {
53
+ const tag_open_length = this._tagOpen.length
54
+ const tag_close_length = this._tagClose.length
55
+ let tag_start_pos = this.template.indexOf(this._tagOpen, 0)
56
+ let tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
57
+ while ((tag_start_pos >= 0) && (tag_end_pos > tag_start_pos)) {
58
+ this._tagStart.push(tag_start_pos)
59
+ this._tagEnd.push(tag_end_pos)
60
+ let variable = this.template.slice(tag_start_pos+tag_open_length, tag_end_pos - tag_close_length)
61
+ const variable_parts = variable.split("|")
62
+ if (variable_parts.length > 1) {
63
+ const formatter: OINOFormatter = OINOFormatter.parse(variable_parts.slice(1))
64
+ this._tagFormatters.push(formatter)
65
+ } else {
66
+ this._tagFormatters.push(OINO_EMPTY_FORMATTER)
67
+ }
68
+ this._tagVariable.push(variable_parts[0])
69
+ this._tagCount = this._tagCount + 1
70
+ tag_start_pos = this.template.indexOf(this._tagOpen, tag_end_pos)
71
+ tag_end_pos = this.template.indexOf(this._tagClose, tag_start_pos + tag_open_length) + tag_close_length
72
+ }
73
+ }
74
+
75
+ protected _createHttpResult(html:string):OINOHttpResult {
76
+ const result:OINOHttpResult = new OINOHttpResult({body: html})
77
+ if (this.expires >= 1) {
78
+ result.expires = Math.round(this.expires)
79
+ }
80
+ if (this.modified >= 1) {
81
+ result.lastModified = this.modified
82
+ }
83
+ return result
84
+ }
85
+
86
+ protected _renderHtml():string {
87
+ let html:string = ""
88
+ let start_pos = 0
89
+ let end_pos = 0
90
+ for (let i=0; i<this._tagCount; i++) {
91
+ end_pos = this._tagStart[i]
92
+ const key = this._tagVariable[i]
93
+ const value = this._tagFormatters[i].format(this._variables[key] || "")
94
+ html += this.template.slice(start_pos, end_pos) + value
95
+ start_pos = this._tagEnd[i]
96
+ }
97
+ html += this.template.slice(start_pos)
98
+ // let html:string = this.template
99
+ // for (let key in this._variables) {
100
+ // const value = this._variables[key]
101
+ // html = html.replaceAll(this._tag + key + this._tag, value)
102
+ // }
103
+ return html
104
+ }
105
+
106
+ /**
107
+ * Clear template variables.
108
+ *
109
+ */
110
+ clearVariables() {
111
+ this._variables = {}
112
+ }
113
+
114
+ /**
115
+ * Sets template variable from a key-value-pair.
116
+ *
117
+ * @param variable key
118
+ * @param value value
119
+ * @param escapeValue whether to escape value
120
+ *
121
+ */
122
+ setVariableFromValue(variable:string, value:string, escapeValue:boolean = true) {
123
+ if (escapeValue) {
124
+ value = OINOStr.encode(value, OINOContentType.html)
125
+ }
126
+ this._variables[variable] = value
127
+ }
128
+
129
+ /**
130
+ * Sets template variables from object properties.
131
+ *
132
+ * @param object any object
133
+ * @param escapeValue whether to escape value
134
+ *
135
+ */
136
+ setVariableFromProperties(object:any, escapeValue:boolean = true) {
137
+ if (object) {
138
+ for (let key in object) {
139
+ if (escapeValue) {
140
+ this._variables[key] = OINOStr.encode(object[key], OINOContentType.html)
141
+ } else {
142
+ this._variables[key] = object[key]
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Creates HTML Response from set variables.
150
+ *
151
+ */
152
+ render():OINOHttpResult {
153
+ const html:string = this._renderHtml()
154
+ this.clearVariables() // clear variables after rendering
155
+ return this._createHttpResult(html)
156
+ }
157
+
158
+ /**
159
+ * Creates HTML Response from a key-value-pair.
160
+ *
161
+ * @param key key
162
+ * @param value value
163
+ *
164
+ */
165
+ renderFromKeyValue(key:string, value:string):OINOHttpResult {
166
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
167
+ this.setVariableFromValue(key, value)
168
+ const result:OINOHttpResult = this.render()
169
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
170
+ return result
171
+ }
172
+
173
+ /**
174
+ * Creates HTML Response from object properties.
175
+ *
176
+ * @param object object
177
+ *
178
+ */
179
+ renderFromObject(object:any = true):OINOHttpResult {
180
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
181
+ this.setVariableFromProperties(object)
182
+ const result:OINOHttpResult = this.render()
183
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
184
+ return result
185
+ }
186
+
187
+ /**
188
+ * Creates HTML Response from API result.
189
+ *
190
+ * @param result OINOResult-object
191
+ * @param messageSeparator HTML separator for messages
192
+ * @param includeErrorMessages include debug messages in result
193
+ * @param includeWarningMessages include debug messages in result
194
+ * @param includeInfoMessages include debug messages in result
195
+ * @param includeDebugMessages include debug messages in result
196
+ *
197
+ */
198
+ renderFromResult(result:OINOResult, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
199
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
200
+ this.setVariableFromValue("status", result.status.toString())
201
+ this.setVariableFromValue("statusText", result.statusText.toString())
202
+ let messages:string[] = []
203
+ for (let i:number = 0; i<result.messages.length; i++) {
204
+ if (includeErrorMessages && result.messages[i].startsWith(OINO_ERROR_PREFIX)) {
205
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
206
+ }
207
+ if (includeWarningMessages && result.messages[i].startsWith(OINO_WARNING_PREFIX)) {
208
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
209
+ }
210
+ if (includeInfoMessages && result.messages[i].startsWith(OINO_INFO_PREFIX)) {
211
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
212
+ }
213
+ if (includeDebugMessages && result.messages[i].startsWith(OINO_DEBUG_PREFIX)) {
214
+ messages.push(OINOStr.encode(result.messages[i], OINOContentType.html))
215
+ }
216
+
217
+ }
218
+ if (messageSeparator && (messages.length > 0)) {
219
+ this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
220
+ }
221
+ const http_result:OINOHttpResult = this.render()
222
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
223
+ return http_result
224
+ }
225
+ };