@oino-ts/common 0.17.1 → 0.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/OINORequest.js +11 -2
- package/dist/esm/OINORequest.js +11 -2
- package/dist/types/OINORequest.d.ts +3 -2
- package/package.json +32 -32
- package/src/OINOBenchmark.ts +250 -250
- package/src/OINOFormatter.ts +165 -165
- package/src/OINOHtmlTemplate.test.ts +114 -114
- package/src/OINOHtmlTemplate.ts +222 -222
- package/src/OINOLog.ts +292 -292
- package/src/OINORequest.ts +139 -129
- package/src/OINOResult.ts +249 -249
- package/src/OINOStr.ts +254 -254
- package/src/index.ts +37 -37
- package/README.md +0 -183
package/src/OINORequest.ts
CHANGED
|
@@ -1,129 +1,139 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { OINOContentType } from "."
|
|
8
|
-
import { OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM } from "./index.js"
|
|
9
|
-
|
|
10
|
-
export interface OINORequestInit {
|
|
11
|
-
params?: Record<string, string>
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* OINO API request result object with returned data and/or http status code/message and
|
|
16
|
-
* error / warning messages.
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
export class OINORequest {
|
|
20
|
-
/** Key-value parameters */
|
|
21
|
-
params?: Record<string, string>
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Constructor of OINORequest.
|
|
25
|
-
*
|
|
26
|
-
* @param init initialization values
|
|
27
|
-
*
|
|
28
|
-
*/
|
|
29
|
-
constructor (init?: OINORequestInit) {
|
|
30
|
-
this.params = init?.params ?? {}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Copy values from different result.
|
|
35
|
-
*
|
|
36
|
-
* @param request source value
|
|
37
|
-
*/
|
|
38
|
-
copy(request: OINORequest) {
|
|
39
|
-
this.params = {...request.params}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface OINOHttpRequestInit extends OINORequestInit {
|
|
44
|
-
url?: URL
|
|
45
|
-
method?: string
|
|
46
|
-
headers?: Record<string, string>
|
|
47
|
-
|
|
48
|
-
requestType?:OINOContentType
|
|
49
|
-
responseType?:OINOContentType
|
|
50
|
-
multipartBoundary?:string
|
|
51
|
-
lastModified?:number
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Specialized result for HTTP responses.
|
|
56
|
-
*/
|
|
57
|
-
export class OINOHttpRequest extends OINORequest {
|
|
58
|
-
readonly url?: URL
|
|
59
|
-
readonly method: string
|
|
60
|
-
readonly headers: Record<string, string>
|
|
61
|
-
readonly
|
|
62
|
-
readonly requestType:OINOContentType
|
|
63
|
-
readonly responseType:OINOContentType
|
|
64
|
-
readonly multipartBoundary?:string
|
|
65
|
-
readonly lastModified?:number
|
|
66
|
-
readonly etags?:string[]
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Constructor for a `OINOHttpRequest`
|
|
70
|
-
*
|
|
71
|
-
* @param init initialization values
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
constructor(init: OINOHttpRequestInit) {
|
|
75
|
-
super(init)
|
|
76
|
-
this.url = init.url
|
|
77
|
-
this.method = init.method ?? "GET"
|
|
78
|
-
this.headers = init.headers ?? {}
|
|
79
|
-
this.
|
|
80
|
-
this.multipartBoundary = ""
|
|
81
|
-
this.lastModified = init.lastModified
|
|
82
|
-
|
|
83
|
-
if (init.multipartBoundary) {
|
|
84
|
-
this.multipartBoundary = init.multipartBoundary
|
|
85
|
-
}
|
|
86
|
-
if (init.requestType) {
|
|
87
|
-
this.requestType = init.requestType
|
|
88
|
-
} else {
|
|
89
|
-
const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers["content-type"] // content-type header can be overridden by query parameter
|
|
90
|
-
if (request_type_param == OINOContentType.csv) {
|
|
91
|
-
this.requestType = OINOContentType.csv
|
|
92
|
-
|
|
93
|
-
} else if (request_type_param == OINOContentType.urlencode) {
|
|
94
|
-
this.requestType = OINOContentType.urlencode
|
|
95
|
-
|
|
96
|
-
} else if (request_type_param?.startsWith(OINOContentType.formdata)) {
|
|
97
|
-
this.requestType = OINOContentType.formdata
|
|
98
|
-
if (!this.multipartBoundary) {
|
|
99
|
-
this.multipartBoundary = request_type_param.split('boundary=')[1] || ""
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
this.requestType = OINOContentType.json
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (init.responseType) {
|
|
106
|
-
this.responseType = init.responseType
|
|
107
|
-
} else {
|
|
108
|
-
const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers["accept"] // accept header can be overridden by query parameter
|
|
109
|
-
const accept_types = response_type_param?.split(', ') || []
|
|
110
|
-
let response_type:OINOContentType|undefined = undefined
|
|
111
|
-
for (let i=0; i<accept_types.length; i++) {
|
|
112
|
-
if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
|
|
113
|
-
response_type = accept_types[i] as OINOContentType
|
|
114
|
-
break
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
this.responseType = response_type ?? OINOContentType.json
|
|
118
|
-
}
|
|
119
|
-
const last_modified = this.headers["if-modified-since"]
|
|
120
|
-
if (last_modified) {
|
|
121
|
-
this.lastModified = new Date(last_modified).getTime()
|
|
122
|
-
}
|
|
123
|
-
const etags = this.headers["if-none-match"]?.split(',').map(e => e.trim())
|
|
124
|
-
if (etags) {
|
|
125
|
-
this.etags = etags
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { OINOContentType } from "."
|
|
8
|
+
import { OINO_REQUEST_TYPE_PARAM, OINO_RESPONSE_TYPE_PARAM } from "./index.js"
|
|
9
|
+
|
|
10
|
+
export interface OINORequestInit {
|
|
11
|
+
params?: Record<string, string>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* OINO API request result object with returned data and/or http status code/message and
|
|
16
|
+
* error / warning messages.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export class OINORequest {
|
|
20
|
+
/** Key-value parameters */
|
|
21
|
+
params?: Record<string, string>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Constructor of OINORequest.
|
|
25
|
+
*
|
|
26
|
+
* @param init initialization values
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
constructor (init?: OINORequestInit) {
|
|
30
|
+
this.params = init?.params ?? {}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Copy values from different result.
|
|
35
|
+
*
|
|
36
|
+
* @param request source value
|
|
37
|
+
*/
|
|
38
|
+
copy(request: OINORequest) {
|
|
39
|
+
this.params = {...request.params}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface OINOHttpRequestInit extends OINORequestInit {
|
|
44
|
+
url?: URL
|
|
45
|
+
method?: string
|
|
46
|
+
headers?: Record<string, string>
|
|
47
|
+
data?: string|Buffer|Uint8Array|object|null
|
|
48
|
+
requestType?:OINOContentType
|
|
49
|
+
responseType?:OINOContentType
|
|
50
|
+
multipartBoundary?:string
|
|
51
|
+
lastModified?:number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Specialized result for HTTP responses.
|
|
56
|
+
*/
|
|
57
|
+
export class OINOHttpRequest extends OINORequest {
|
|
58
|
+
readonly url?: URL
|
|
59
|
+
readonly method: string
|
|
60
|
+
readonly headers: Record<string, string>
|
|
61
|
+
readonly data: string|Buffer|Uint8Array|object|null
|
|
62
|
+
readonly requestType:OINOContentType
|
|
63
|
+
readonly responseType:OINOContentType
|
|
64
|
+
readonly multipartBoundary?:string
|
|
65
|
+
readonly lastModified?:number
|
|
66
|
+
readonly etags?:string[]
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Constructor for a `OINOHttpRequest`
|
|
70
|
+
*
|
|
71
|
+
* @param init initialization values
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
constructor(init: OINOHttpRequestInit) {
|
|
75
|
+
super(init)
|
|
76
|
+
this.url = init.url
|
|
77
|
+
this.method = init.method ?? "GET"
|
|
78
|
+
this.headers = init.headers ?? {}
|
|
79
|
+
this.data = init.data ?? ""
|
|
80
|
+
this.multipartBoundary = ""
|
|
81
|
+
this.lastModified = init.lastModified
|
|
82
|
+
|
|
83
|
+
if (init.multipartBoundary) {
|
|
84
|
+
this.multipartBoundary = init.multipartBoundary
|
|
85
|
+
}
|
|
86
|
+
if (init.requestType) {
|
|
87
|
+
this.requestType = init.requestType
|
|
88
|
+
} else {
|
|
89
|
+
const request_type_param = this.url?.searchParams.get(OINO_REQUEST_TYPE_PARAM) || this.headers["content-type"] // content-type header can be overridden by query parameter
|
|
90
|
+
if (request_type_param == OINOContentType.csv) {
|
|
91
|
+
this.requestType = OINOContentType.csv
|
|
92
|
+
|
|
93
|
+
} else if (request_type_param == OINOContentType.urlencode) {
|
|
94
|
+
this.requestType = OINOContentType.urlencode
|
|
95
|
+
|
|
96
|
+
} else if (request_type_param?.startsWith(OINOContentType.formdata)) {
|
|
97
|
+
this.requestType = OINOContentType.formdata
|
|
98
|
+
if (!this.multipartBoundary) {
|
|
99
|
+
this.multipartBoundary = request_type_param.split('boundary=')[1] || ""
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
this.requestType = OINOContentType.json
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (init.responseType) {
|
|
106
|
+
this.responseType = init.responseType
|
|
107
|
+
} else {
|
|
108
|
+
const response_type_param = this.url?.searchParams.get(OINO_RESPONSE_TYPE_PARAM) || this.headers["accept"] // accept header can be overridden by query parameter
|
|
109
|
+
const accept_types = response_type_param?.split(', ') || []
|
|
110
|
+
let response_type:OINOContentType|undefined = undefined
|
|
111
|
+
for (let i=0; i<accept_types.length; i++) {
|
|
112
|
+
if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
|
|
113
|
+
response_type = accept_types[i] as OINOContentType
|
|
114
|
+
break
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.responseType = response_type ?? OINOContentType.json
|
|
118
|
+
}
|
|
119
|
+
const last_modified = this.headers["if-modified-since"]
|
|
120
|
+
if (last_modified) {
|
|
121
|
+
this.lastModified = new Date(last_modified).getTime()
|
|
122
|
+
}
|
|
123
|
+
const etags = this.headers["if-none-match"]?.split(',').map(e => e.trim())
|
|
124
|
+
if (etags) {
|
|
125
|
+
this.etags = etags
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static async fromRequest(request: Request): Promise<OINOHttpRequest> {
|
|
130
|
+
const body = await request.arrayBuffer()
|
|
131
|
+
return new OINOHttpRequest({
|
|
132
|
+
url: new URL(request.url),
|
|
133
|
+
method: request.method,
|
|
134
|
+
headers: Object.fromEntries(request.headers as any),
|
|
135
|
+
data: Buffer.from(body),
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
}
|