@oino-ts/common 0.21.1 → 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 (72) hide show
  1. package/README.md +183 -0
  2. package/dist/cjs/OINOApi.js +322 -0
  3. package/dist/cjs/OINOBenchmark.js +3 -4
  4. package/dist/cjs/OINOConfig.js +104 -0
  5. package/dist/cjs/OINOConstants.js +42 -0
  6. package/dist/cjs/OINODataField.js +346 -0
  7. package/dist/cjs/OINODataModel.js +182 -0
  8. package/dist/cjs/OINODataSource.js +165 -0
  9. package/dist/cjs/OINOFormatter.js +6 -5
  10. package/dist/cjs/OINOHtmlTemplate.js +21 -18
  11. package/dist/cjs/OINOModelSet.js +333 -0
  12. package/dist/cjs/OINOParser.js +448 -0
  13. package/dist/cjs/OINOQueryParams.js +434 -0
  14. package/dist/cjs/OINORequest.js +21 -13
  15. package/dist/cjs/OINOResult.js +13 -12
  16. package/dist/cjs/OINOStr.js +11 -11
  17. package/dist/cjs/OINOSwagger.js +205 -0
  18. package/dist/cjs/index.js +57 -39
  19. package/dist/esm/OINOApi.js +315 -0
  20. package/dist/esm/OINOBenchmark.js +3 -4
  21. package/dist/esm/OINOConfig.js +100 -0
  22. package/dist/esm/OINOConstants.js +39 -0
  23. package/dist/esm/OINODataField.js +337 -0
  24. package/dist/esm/OINODataModel.js +178 -0
  25. package/dist/esm/OINODataSource.js +159 -0
  26. package/dist/esm/OINOFormatter.js +2 -1
  27. package/dist/esm/OINOHtmlTemplate.js +4 -1
  28. package/dist/esm/OINOModelSet.js +329 -0
  29. package/dist/esm/OINOParser.js +444 -0
  30. package/dist/esm/OINOQueryParams.js +426 -0
  31. package/dist/esm/OINORequest.js +9 -1
  32. package/dist/esm/OINOResult.js +2 -1
  33. package/dist/esm/OINOStr.js +1 -1
  34. package/dist/esm/OINOSwagger.js +201 -0
  35. package/dist/esm/index.js +14 -32
  36. package/dist/types/OINOApi.d.ts +191 -0
  37. package/dist/types/OINOBenchmark.d.ts +1 -1
  38. package/dist/types/OINOConfig.d.ts +63 -0
  39. package/dist/types/OINOConstants.d.ts +51 -0
  40. package/dist/types/OINODataField.d.ts +209 -0
  41. package/dist/types/OINODataModel.d.ts +78 -0
  42. package/dist/types/OINODataSource.d.ts +184 -0
  43. package/dist/types/OINOHtmlTemplate.d.ts +1 -1
  44. package/dist/types/OINOModelSet.d.ts +64 -0
  45. package/dist/types/OINOParser.d.ts +42 -0
  46. package/dist/types/OINOQueryParams.d.ts +270 -0
  47. package/dist/types/OINORequest.d.ts +4 -1
  48. package/dist/types/OINOResult.d.ts +1 -1
  49. package/dist/types/OINOStr.d.ts +1 -1
  50. package/dist/types/OINOSwagger.d.ts +25 -0
  51. package/dist/types/index.d.ts +14 -31
  52. package/package.json +32 -32
  53. package/src/OINOApi.ts +429 -0
  54. package/src/OINOBenchmark.ts +323 -324
  55. package/src/OINOConfig.ts +113 -0
  56. package/src/OINOConstants.ts +59 -0
  57. package/src/OINODataField.ts +371 -0
  58. package/src/OINODataModel.ts +187 -0
  59. package/src/OINODataSource.ts +280 -0
  60. package/src/OINOFormatter.ts +166 -165
  61. package/src/OINOHeaders.ts +51 -51
  62. package/src/OINOHtmlTemplate.test.ts +114 -114
  63. package/src/OINOHtmlTemplate.ts +225 -222
  64. package/src/OINOLog.ts +292 -292
  65. package/src/OINOModelSet.ts +359 -0
  66. package/src/OINOParser.ts +441 -0
  67. package/src/OINOQueryParams.ts +449 -0
  68. package/src/OINORequest.ts +204 -196
  69. package/src/OINOResult.ts +331 -330
  70. package/src/OINOStr.ts +254 -254
  71. package/src/OINOSwagger.ts +213 -0
  72. package/src/index.ts +18 -38
@@ -1,196 +1,204 @@
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 { Buffer } from "node:buffer"
8
-
9
- import { OINOContentType, OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM, OINOHeaders, OINOHeadersInit } from "."
10
-
11
- export interface OINORequestInit {
12
- params?: Record<string, string>
13
- }
14
-
15
- /**
16
- * OINO API request result object with returned data and/or http status code/message and
17
- * error / warning messages.
18
- *
19
- */
20
- export class OINORequest {
21
- /** Key-value parameters */
22
- params?: Record<string, string>
23
-
24
- /**
25
- * Constructor of OINORequest.
26
- *
27
- * @param init initialization values
28
- *
29
- */
30
- constructor (init?: OINORequestInit) {
31
- this.params = init?.params ?? {}
32
- }
33
- }
34
-
35
- export type OINOHttpData = string|Buffer|Uint8Array|null
36
-
37
- export interface OINOHttpRequestInit extends OINORequestInit {
38
- url?: URL|string
39
- method?: string
40
- headers?: OINOHeadersInit
41
- body?: OINOHttpData
42
- requestType?:OINOContentType
43
- responseType?:OINOContentType
44
- multipartBoundary?:string
45
- lastModified?:number
46
- }
47
-
48
- /**
49
- * Specialized result for HTTP responses.
50
- */
51
- export class OINOHttpRequest extends OINORequest {
52
- url?: URL
53
- method: string
54
- headers: OINOHeaders
55
- body: OINOHttpData
56
- requestType:OINOContentType
57
- responseType:OINOContentType
58
- multipartBoundary?:string
59
- lastModified?:number
60
- etags?:string[]
61
-
62
- /**
63
- * Constructor for a `OINOHttpRequest`
64
- *
65
- * @param init initialization values
66
- *
67
- */
68
- constructor(init: OINOHttpRequestInit) {
69
- super(init)
70
- this.url = typeof init.url === "string" ? new URL(init.url) : init.url
71
- this.method = init.method?.toUpperCase() ?? "GET"
72
- this.headers = new OINOHeaders(init.headers)
73
- this.body = init.body ?? null
74
- this.multipartBoundary = ""
75
- this.lastModified = init.lastModified
76
-
77
- if (init.multipartBoundary) {
78
- this.multipartBoundary = init.multipartBoundary
79
- }
80
- if (init.requestType) {
81
- this.requestType = init.requestType
82
- } else {
83
- const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers.get("content-type") // content-type header can be overridden by query parameter
84
- if (request_type_param == OINOContentType.csv) {
85
- this.requestType = OINOContentType.csv
86
-
87
- } else if (request_type_param == OINOContentType.urlencode) {
88
- this.requestType = OINOContentType.urlencode
89
-
90
- } else if (request_type_param?.startsWith(OINOContentType.formdata)) {
91
- this.requestType = OINOContentType.formdata
92
- if (!this.multipartBoundary) {
93
- this.multipartBoundary = request_type_param.split('boundary=')[1] || ""
94
- }
95
- } else {
96
- this.requestType = OINOContentType.json
97
- }
98
- }
99
- if (init.responseType) {
100
- this.responseType = init.responseType
101
- } else {
102
- const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers.get("accept") // accept header can be overridden by query parameter
103
- const accept_types = response_type_param?.split(', ') || []
104
- let response_type:OINOContentType|undefined = undefined
105
- for (let i=0; i<accept_types.length; i++) {
106
- if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
107
- response_type = accept_types[i] as OINOContentType
108
- break
109
- }
110
- }
111
- this.responseType = response_type ?? OINOContentType.json
112
- }
113
- const last_modified = this.headers.get("if-modified-since")
114
- if (last_modified) {
115
- this.lastModified = new Date(last_modified).getTime()
116
- }
117
- const etags = this.headers.get("if-none-match")?.split(',').map(e => e.trim())
118
- if (etags) {
119
- this.etags = etags
120
- }
121
- }
122
-
123
- /**
124
- * Creates a `OINOHttpRequest` from a Fetch API `Request` object.
125
- *
126
- * @param request Fetch request
127
- *
128
- */
129
- static async fromFetchRequest(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
- body: Buffer.from(body),
136
- })
137
- }
138
-
139
- /**
140
- * Returns the request data as a text string.
141
- *
142
- */
143
- bodyAsText(): string {
144
- if (this.body == null) {
145
- return ""
146
-
147
- } else if (this.body instanceof Uint8Array) {
148
- return new TextDecoder().decode(this.body)
149
-
150
- } else if (typeof this.body === "object") {
151
- return JSON.stringify(this.body)
152
-
153
- } else {
154
- return this.body?.toString() || ""
155
- }
156
- }
157
-
158
- /**
159
- * Returns the request data parsed as JSON object.
160
- *
161
- */
162
- bodyAsParsedJson(): any {
163
- return this.body ? JSON.parse(this.bodyAsText()) : {}
164
- }
165
-
166
- /**
167
- * Returns the request data as URLSearchParams (form data).
168
- *
169
- */
170
- bodyAsFormData(): URLSearchParams {
171
- return new URLSearchParams(this.bodyAsText() || "")
172
- }
173
-
174
- /**
175
- * Returns the request data as Buffer.
176
- *
177
- */
178
- bodyAsBuffer(): Buffer {
179
- if ((this.body === null) || (this.body === undefined)) {
180
- return Buffer.alloc(0)
181
-
182
- } else if (this.body instanceof Buffer) {
183
- return this.body
184
-
185
- } else if (this.body instanceof Uint8Array) {
186
- return Buffer.from(this.body)
187
-
188
- } else if (typeof this.body === "object") {
189
- return Buffer.from(JSON.stringify(this.body), "utf-8")
190
-
191
- } else {
192
- return Buffer.from(this.body, "utf-8")
193
- }
194
- }
195
-
196
- }
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 { Buffer } from "node:buffer"
8
+
9
+ import { OINOContentType, OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM, OINO_RESPONSE_DOWNLOAD_PARAM } from "./OINOConstants.js"
10
+ import { OINOHeaders, type OINOHeadersInit } from "./OINOHeaders.js"
11
+
12
+ export interface OINORequestInit {
13
+ params?: Record<string, string>
14
+ }
15
+
16
+ /**
17
+ * OINO API request result object with returned data and/or http status code/message and
18
+ * error / warning messages.
19
+ *
20
+ */
21
+ export class OINORequest {
22
+ /** Key-value parameters */
23
+ params?: Record<string, string>
24
+
25
+ /**
26
+ * Constructor of OINORequest.
27
+ *
28
+ * @param init initialization values
29
+ *
30
+ */
31
+ constructor (init?: OINORequestInit) {
32
+ this.params = init?.params ?? {}
33
+ }
34
+ }
35
+
36
+ export type OINOHttpData = string|Buffer|Uint8Array|null
37
+
38
+ export interface OINOHttpRequestInit extends OINORequestInit {
39
+ url?: URL|string
40
+ method?: string
41
+ headers?: OINOHeadersInit
42
+ body?: OINOHttpData
43
+ requestType?:OINOContentType
44
+ responseType?:OINOContentType
45
+ responseDownload?:string
46
+ multipartBoundary?:string
47
+ lastModified?:number
48
+ }
49
+
50
+ /**
51
+ * Specialized result for HTTP responses.
52
+ */
53
+ export class OINOHttpRequest extends OINORequest {
54
+ url?: URL
55
+ method: string
56
+ headers: OINOHeaders
57
+ body: OINOHttpData
58
+ requestType:OINOContentType
59
+ responseType:OINOContentType
60
+ responseDownload?:string
61
+ multipartBoundary?:string
62
+ lastModified?:number
63
+ etags?:string[]
64
+
65
+ /**
66
+ * Constructor for a `OINOHttpRequest`
67
+ *
68
+ * @param init initialization values
69
+ *
70
+ */
71
+ constructor(init: OINOHttpRequestInit) {
72
+ super(init)
73
+ this.url = typeof init.url === "string" ? new URL(init.url) : init.url
74
+ this.method = init.method?.toUpperCase() ?? "GET"
75
+ this.headers = new OINOHeaders(init.headers)
76
+ this.body = init.body ?? null
77
+ this.multipartBoundary = ""
78
+ this.lastModified = init.lastModified
79
+
80
+ if (init.multipartBoundary) {
81
+ this.multipartBoundary = init.multipartBoundary
82
+ }
83
+ if (init.requestType) {
84
+ this.requestType = init.requestType
85
+ } else {
86
+ const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers.get("content-type") // content-type header can be overridden by query parameter
87
+ if (request_type_param == OINOContentType.csv) {
88
+ this.requestType = OINOContentType.csv
89
+
90
+ } else if (request_type_param == OINOContentType.urlencode) {
91
+ this.requestType = OINOContentType.urlencode
92
+
93
+ } else if (request_type_param?.startsWith(OINOContentType.formdata)) {
94
+ this.requestType = OINOContentType.formdata
95
+ if (!this.multipartBoundary) {
96
+ this.multipartBoundary = request_type_param.split('boundary=')[1] || ""
97
+ }
98
+ } else {
99
+ this.requestType = OINOContentType.json
100
+ }
101
+ }
102
+ if (init.responseType) {
103
+ this.responseType = init.responseType
104
+ } else {
105
+ const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers.get("accept") // accept header can be overridden by query parameter
106
+ const accept_types = response_type_param?.split(', ') || []
107
+ let response_type:OINOContentType|undefined = undefined
108
+ for (let i=0; i<accept_types.length; i++) {
109
+ if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
110
+ response_type = accept_types[i] as OINOContentType
111
+ break
112
+ }
113
+ }
114
+ this.responseType = response_type ?? OINOContentType.json
115
+ }
116
+ if (init.responseDownload) {
117
+ this.responseDownload = init.responseDownload
118
+ } else {
119
+ this.responseDownload = this.url?.searchParams.get(OINO_RESPONSE_DOWNLOAD_PARAM) ?? ""
120
+ }
121
+ const last_modified = this.headers.get("if-modified-since")
122
+ if (last_modified) {
123
+ this.lastModified = new Date(last_modified).getTime()
124
+ }
125
+ const etags = this.headers.get("if-none-match")?.split(',').map(e => e.trim())
126
+ if (etags) {
127
+ this.etags = etags
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Creates a `OINOHttpRequest` from a Fetch API `Request` object.
133
+ *
134
+ * @param request Fetch request
135
+ *
136
+ */
137
+ static async fromFetchRequest(request: Request): Promise<OINOHttpRequest> {
138
+ const body = await request.arrayBuffer()
139
+ return new OINOHttpRequest({
140
+ url: new URL(request.url),
141
+ method: request.method,
142
+ headers: Object.fromEntries(request.headers as any),
143
+ body: Buffer.from(body),
144
+ })
145
+ }
146
+
147
+ /**
148
+ * Returns the request data as a text string.
149
+ *
150
+ */
151
+ bodyAsText(): string {
152
+ if (this.body == null) {
153
+ return ""
154
+
155
+ } else if (this.body instanceof Uint8Array) {
156
+ return new TextDecoder().decode(this.body)
157
+
158
+ } else if (typeof this.body === "object") {
159
+ return JSON.stringify(this.body)
160
+
161
+ } else {
162
+ return this.body?.toString() || ""
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Returns the request data parsed as JSON object.
168
+ *
169
+ */
170
+ bodyAsParsedJson(): any {
171
+ return this.body ? JSON.parse(this.bodyAsText()) : {}
172
+ }
173
+
174
+ /**
175
+ * Returns the request data as URLSearchParams (form data).
176
+ *
177
+ */
178
+ bodyAsFormData(): URLSearchParams {
179
+ return new URLSearchParams(this.bodyAsText() || "")
180
+ }
181
+
182
+ /**
183
+ * Returns the request data as Buffer.
184
+ *
185
+ */
186
+ bodyAsBuffer(): Buffer {
187
+ if ((this.body === null) || (this.body === undefined)) {
188
+ return Buffer.alloc(0)
189
+
190
+ } else if (this.body instanceof Buffer) {
191
+ return this.body
192
+
193
+ } else if (this.body instanceof Uint8Array) {
194
+ return Buffer.from(this.body)
195
+
196
+ } else if (typeof this.body === "object") {
197
+ return Buffer.from(JSON.stringify(this.body), "utf-8")
198
+
199
+ } else {
200
+ return Buffer.from(this.body, "utf-8")
201
+ }
202
+ }
203
+
204
+ }