miolo-cli 3.0.0-beta.155 → 3.0.0-beta.159
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/package.json +11 -16
- package/src/catcher/index.mjs +19 -21
- package/src/fetcher/fetcher.mjs +66 -68
- package/src/fetcher/index.mjs +2 -3
- package/src/fetcher/utils.mjs +7 -8
- package/src/index.mjs +13 -16
- package/src/socket/index.mjs +2 -2
- package/dist/miolo-cli.mjs +0 -469
- package/dist/miolo-cli.mjs.map +0 -1
- package/dist/miolo-cli.umd.js +0 -699
- package/dist/miolo-cli.umd.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miolo-cli",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.159",
|
|
4
4
|
"description": "cli utils for miolo",
|
|
5
5
|
"author": "Donato Lorenzo <donato@afialapis.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -17,28 +17,23 @@
|
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"type": "module",
|
|
19
19
|
"exports": {
|
|
20
|
-
".":
|
|
21
|
-
"development": "./src/index.mjs",
|
|
22
|
-
"import": "./dist/miolo-cli.mjs",
|
|
23
|
-
"default": "./dist/miolo-cli.umd.js"
|
|
24
|
-
}
|
|
20
|
+
".": "./src/index.mjs"
|
|
25
21
|
},
|
|
26
22
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"
|
|
23
|
+
"src",
|
|
24
|
+
"package.json"
|
|
29
25
|
],
|
|
30
26
|
"scripts": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"dist": "npm run clean && npm run bundle"
|
|
27
|
+
"reset": "rm -fr package-lock.json npm-lock.yaml node_modules && npm i",
|
|
28
|
+
"lint": "biome check ./src --reporter=github",
|
|
29
|
+
"lint:fix": "biome check --write ./src --reporter=github",
|
|
30
|
+
"prepublishOnly": "npm run lint"
|
|
36
31
|
},
|
|
37
32
|
"dependencies": {
|
|
38
33
|
"qs": "^6.15.0",
|
|
39
34
|
"socket.io-client": "^4.8.3"
|
|
40
35
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=24"
|
|
43
38
|
}
|
|
44
|
-
}
|
|
39
|
+
}
|
package/src/catcher/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
function init_catcher
|
|
2
|
-
if (typeof window
|
|
1
|
+
function init_catcher(catcher_url, fetcher) {
|
|
2
|
+
if (typeof window === "undefined") {
|
|
3
3
|
return
|
|
4
|
-
}
|
|
4
|
+
}
|
|
5
5
|
|
|
6
|
-
if (window.miolo_listeners===true) {
|
|
6
|
+
if (window.miolo_listeners === true) {
|
|
7
7
|
return
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -23,53 +23,51 @@ function init_catcher (catcher_url, fetcher) {
|
|
|
23
23
|
// }
|
|
24
24
|
// }
|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
window.addEventListener("error", (event) => {
|
|
28
27
|
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
|
|
29
28
|
|
|
30
29
|
try {
|
|
31
|
-
const params= {
|
|
32
|
-
|
|
33
|
-
msg: event?.message ||
|
|
30
|
+
const params = {
|
|
31
|
+
error: {
|
|
32
|
+
msg: event?.message || "Client error",
|
|
34
33
|
file: event?.filename,
|
|
35
34
|
line: event?.lineno,
|
|
36
35
|
col: event?.colno,
|
|
37
36
|
error: event?.error
|
|
38
37
|
},
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
path: window.location.pathname,
|
|
39
|
+
agent: "UserAgent" + navigator.userAgent
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
fetcher.post(catcher_url, params)
|
|
44
|
-
} catch(e) {
|
|
43
|
+
} catch (e) {
|
|
45
44
|
console.error(e)
|
|
46
45
|
}
|
|
47
|
-
})
|
|
46
|
+
})
|
|
48
47
|
|
|
49
48
|
window.addEventListener("unhandledrejection", (event) => {
|
|
50
49
|
// https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
|
|
51
50
|
|
|
52
51
|
try {
|
|
53
|
-
const params= {
|
|
54
|
-
|
|
55
|
-
msg: event?.reason ||
|
|
52
|
+
const params = {
|
|
53
|
+
warning: {
|
|
54
|
+
msg: event?.reason || "Client Unhandled rejection",
|
|
56
55
|
file: undefined,
|
|
57
56
|
line: undefined,
|
|
58
57
|
col: undefined,
|
|
59
58
|
error: event?.reason
|
|
60
59
|
},
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
path: window.location.pathname,
|
|
61
|
+
agent: "UserAgent" + navigator.userAgent
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
fetcher.post(catcher_url, params)
|
|
66
|
-
} catch(e) {
|
|
65
|
+
} catch (e) {
|
|
67
66
|
console.error(e)
|
|
68
67
|
}
|
|
69
|
-
})
|
|
68
|
+
})
|
|
70
69
|
|
|
71
70
|
window.miolo_listeners = true
|
|
72
|
-
|
|
73
71
|
}
|
|
74
72
|
|
|
75
|
-
export {init_catcher}
|
|
73
|
+
export { init_catcher }
|
package/src/fetcher/fetcher.mjs
CHANGED
|
@@ -1,120 +1,119 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { json_to_query_string, null_to_undefined, omit_nil, trim_left } from "./utils.mjs"
|
|
2
2
|
|
|
3
3
|
class Fetcher {
|
|
4
4
|
/**
|
|
5
5
|
* @param {*} config {hostname, port, force_hostname, silent_fail: false}
|
|
6
6
|
*/
|
|
7
7
|
constructor(config) {
|
|
8
|
-
this.config= config
|
|
8
|
+
this.config = config
|
|
9
9
|
this.http_auth = undefined
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
set_http_auth(auth) {
|
|
13
13
|
if (auth) {
|
|
14
|
-
const {username, password}= auth
|
|
15
|
-
this.http_auth= {username, password}
|
|
14
|
+
const { username, password } = auth
|
|
15
|
+
this.http_auth = { username, password }
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
get_headers() {
|
|
20
|
-
|
|
20
|
+
const headers = {}
|
|
21
21
|
|
|
22
22
|
if (this.http_auth) {
|
|
23
|
-
let {username, password}= this.http_auth
|
|
24
|
-
username= username ||
|
|
25
|
-
password= password ||
|
|
23
|
+
let { username, password } = this.http_auth
|
|
24
|
+
username = username || ""
|
|
25
|
+
password = password || ""
|
|
26
26
|
|
|
27
27
|
let sauth
|
|
28
28
|
try {
|
|
29
|
-
sauth=
|
|
30
|
-
} catch(_) {
|
|
31
|
-
sauth=
|
|
29
|
+
sauth = "Basic " + Buffer.from(username + ":" + password).toString("base64")
|
|
30
|
+
} catch (_) {
|
|
31
|
+
sauth = "Basic " + btoa(username + ":" + password)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
headers
|
|
34
|
+
headers.Authorization = sauth
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
return headers
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
_prepare_url
|
|
41
|
-
const endpoint =
|
|
42
|
-
|
|
43
|
-
const {hostname, port, force_hostname} = this.config || {}
|
|
40
|
+
_prepare_url(url) {
|
|
41
|
+
const endpoint = "/" + trim_left(url, "/")
|
|
42
|
+
|
|
43
|
+
const { hostname, port, force_hostname } = this.config || {}
|
|
44
44
|
if (hostname && force_hostname) {
|
|
45
45
|
return `http://${hostname}:${port}${endpoint}`
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
return endpoint
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
async _fetch
|
|
51
|
+
async _fetch(method, url, params, http_auth = undefined) {
|
|
52
52
|
this.set_http_auth(http_auth)
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
const request = {
|
|
55
55
|
method,
|
|
56
|
-
mode:
|
|
57
|
-
credentials:
|
|
56
|
+
mode: "cors",
|
|
57
|
+
credentials: "include",
|
|
58
58
|
headers: {
|
|
59
|
-
|
|
60
|
-
...this.get_headers() || {}
|
|
59
|
+
"content-type": "application/json",
|
|
60
|
+
...(this.get_headers() || {})
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
let wurl = this._prepare_url(url)
|
|
65
|
-
|
|
66
|
-
if (method ===
|
|
67
|
-
request.body = JSON.stringify(params || {}, (
|
|
68
|
-
} else if (method ===
|
|
65
|
+
|
|
66
|
+
if (method === "POST") {
|
|
67
|
+
request.body = JSON.stringify(params || {}, (_k, v) => (v === undefined ? null : v))
|
|
68
|
+
} else if (method === "GET") {
|
|
69
69
|
if (params) {
|
|
70
|
-
wurl+= json_to_query_string(params)
|
|
70
|
+
wurl += json_to_query_string(params)
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
const response = await fetch(wurl, request)
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
if (response.redirected) {
|
|
77
|
-
const isBrowser = typeof window
|
|
77
|
+
const isBrowser = typeof window === "object"
|
|
78
78
|
if (isBrowser) {
|
|
79
79
|
// JSDOM does not support navigation, so lets skip it for tests
|
|
80
|
-
const isTest = typeof navigator !== "undefined" &&
|
|
81
|
-
navigator.userAgent.includes("Node.js");
|
|
80
|
+
const isTest = typeof navigator !== "undefined" && navigator.userAgent.includes("Node.js")
|
|
82
81
|
if (!isTest) {
|
|
83
82
|
window.location.replace(response.url)
|
|
84
83
|
return Promise.resolve(response)
|
|
85
84
|
} else {
|
|
86
|
-
console.error(
|
|
85
|
+
console.error(
|
|
86
|
+
`Response for ${wurl} is a redirect to ${response.url}. But you are in a test environment, where redirects cannot be done. Unexpected results are coming...`
|
|
87
|
+
)
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
|
-
|
|
91
|
-
if (response.headers.get(
|
|
92
|
-
let resp= await response.json()
|
|
91
|
+
|
|
92
|
+
if (response.headers.get("content-type").indexOf("json") >= 0) {
|
|
93
|
+
let resp = await response.json()
|
|
93
94
|
resp = null_to_undefined(resp)
|
|
94
|
-
|
|
95
|
+
|
|
95
96
|
return {
|
|
96
97
|
...resp,
|
|
97
98
|
status: response.status,
|
|
98
99
|
response
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
|
-
|
|
102
|
-
const data= await response.text()
|
|
102
|
+
|
|
103
|
+
const data = await response.text()
|
|
103
104
|
return {
|
|
104
105
|
ok: true,
|
|
105
106
|
data,
|
|
106
107
|
status: response.status,
|
|
107
108
|
response
|
|
108
109
|
}
|
|
109
|
-
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
async get(url, params, http_auth= undefined) {
|
|
113
|
-
/* eslint no-unused-vars:0 */
|
|
111
|
+
|
|
112
|
+
async get(url, params, http_auth = undefined) {
|
|
114
113
|
try {
|
|
115
|
-
const resp = await this._fetch(
|
|
114
|
+
const resp = await this._fetch("GET", url, omit_nil(params), http_auth)
|
|
116
115
|
return resp
|
|
117
|
-
} catch(e) {
|
|
116
|
+
} catch (e) {
|
|
118
117
|
if (this.config?.silent_fail !== true) {
|
|
119
118
|
console.error(`Error on GET ${url}`)
|
|
120
119
|
console.error(e)
|
|
@@ -123,16 +122,16 @@ class Fetcher {
|
|
|
123
122
|
return {
|
|
124
123
|
ok: false,
|
|
125
124
|
error: e,
|
|
126
|
-
status: -1
|
|
125
|
+
status: -1
|
|
127
126
|
}
|
|
128
127
|
}
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
async post(url, data, http_auth= undefined) {
|
|
130
|
+
async post(url, data, http_auth = undefined) {
|
|
132
131
|
try {
|
|
133
|
-
const resp = await this._fetch(
|
|
132
|
+
const resp = await this._fetch("POST", url, data, http_auth)
|
|
134
133
|
return resp
|
|
135
|
-
} catch(e) {
|
|
134
|
+
} catch (e) {
|
|
136
135
|
if (this.config?.silent_fail !== true) {
|
|
137
136
|
console.error(`Error on POST ${url}`)
|
|
138
137
|
console.error(e)
|
|
@@ -146,15 +145,15 @@ class Fetcher {
|
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
147
|
|
|
149
|
-
async read(url, params, http_auth= undefined) {
|
|
148
|
+
async read(url, params, http_auth = undefined) {
|
|
150
149
|
return await this.get(`${url}/read`, params, http_auth)
|
|
151
150
|
}
|
|
152
151
|
|
|
153
|
-
async key_list(url, params, http_auth= undefined) {
|
|
152
|
+
async key_list(url, params, http_auth = undefined) {
|
|
154
153
|
return await this.get(`${url}/key_list`, params, http_auth)
|
|
155
|
-
}
|
|
154
|
+
}
|
|
156
155
|
|
|
157
|
-
async name_list(url, params, http_auth= undefined) {
|
|
156
|
+
async name_list(url, params, http_auth = undefined) {
|
|
158
157
|
const resp = await this.key_list(url, params, http_auth)
|
|
159
158
|
if (resp?.data) {
|
|
160
159
|
resp.data = Object.values(resp.data)
|
|
@@ -162,38 +161,37 @@ class Fetcher {
|
|
|
162
161
|
return resp
|
|
163
162
|
}
|
|
164
163
|
|
|
165
|
-
async find(url, id, http_auth= undefined) {
|
|
164
|
+
async find(url, id, http_auth = undefined) {
|
|
166
165
|
return await this.get(`${url}/find`, { id: id }, http_auth)
|
|
167
166
|
}
|
|
168
167
|
|
|
169
|
-
async distinct(url, field, params, http_auth= undefined) {
|
|
170
|
-
const nparams= {
|
|
168
|
+
async distinct(url, field, params, http_auth = undefined) {
|
|
169
|
+
const nparams = {
|
|
171
170
|
...params,
|
|
172
171
|
distinct_field: field
|
|
173
172
|
}
|
|
174
|
-
return await this.get(`${url}/distinct`, nparams, http_auth)
|
|
173
|
+
return await this.get(`${url}/distinct`, nparams, http_auth)
|
|
175
174
|
}
|
|
176
175
|
|
|
177
|
-
async upsave(url, data, http_auth= undefined) {
|
|
176
|
+
async upsave(url, data, http_auth = undefined) {
|
|
178
177
|
let resp
|
|
179
|
-
if (data.id
|
|
178
|
+
if (data.id === undefined) {
|
|
180
179
|
delete data.id
|
|
181
|
-
resp= await this.post(`${url}/save`, data, http_auth)
|
|
180
|
+
resp = await this.post(`${url}/save`, data, http_auth)
|
|
182
181
|
} else {
|
|
183
|
-
resp= await this.post(`${url}/update`, data, http_auth)
|
|
182
|
+
resp = await this.post(`${url}/update`, data, http_auth)
|
|
184
183
|
}
|
|
185
|
-
|
|
184
|
+
|
|
186
185
|
return resp
|
|
187
|
-
}
|
|
186
|
+
}
|
|
188
187
|
|
|
189
|
-
async remove(url, id, http_auth= undefined) {
|
|
188
|
+
async remove(url, id, http_auth = undefined) {
|
|
190
189
|
const data = { id: id }
|
|
191
190
|
return await this.post(`${url}/delete`, data, http_auth)
|
|
192
191
|
}
|
|
193
192
|
}
|
|
194
193
|
|
|
195
|
-
Fetcher.keyList= Fetcher.key_list
|
|
196
|
-
Fetcher.nameList= Fetcher.name_list
|
|
194
|
+
Fetcher.keyList = Fetcher.key_list
|
|
195
|
+
Fetcher.nameList = Fetcher.name_list
|
|
197
196
|
|
|
198
197
|
export { Fetcher }
|
|
199
|
-
|
package/src/fetcher/index.mjs
CHANGED
package/src/fetcher/utils.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import qs from
|
|
1
|
+
import qs from "qs"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Transform an JSON object to a query string
|
|
@@ -10,7 +10,7 @@ import qs from 'qs'
|
|
|
10
10
|
// return value
|
|
11
11
|
// }
|
|
12
12
|
// }
|
|
13
|
-
//
|
|
13
|
+
//
|
|
14
14
|
// export function json_to_query_string(obj) {
|
|
15
15
|
// if (obj && (Object.keys(obj).length>0)) {
|
|
16
16
|
// const uparams = new URLSearchParams()
|
|
@@ -30,17 +30,16 @@ import qs from 'qs'
|
|
|
30
30
|
// }
|
|
31
31
|
|
|
32
32
|
export function json_to_query_string(obj) {
|
|
33
|
-
if (!obj || Object.keys(obj).length === 0) return
|
|
34
|
-
return `?${qs.stringify(obj, { arrayFormat:
|
|
33
|
+
if (!obj || Object.keys(obj).length === 0) return ""
|
|
34
|
+
return `?${qs.stringify(obj, { arrayFormat: "repeat" })}`
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export function trim_left(str, what) {
|
|
38
|
-
return str.replace(new RegExp(`^${what ||
|
|
38
|
+
return str.replace(new RegExp(`^${what || "\\s"}+`), "")
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
41
|
export function omit_nil(obj) {
|
|
43
|
-
if (typeof obj !==
|
|
42
|
+
if (typeof obj !== "object") return obj
|
|
44
43
|
return Object.keys(obj).reduce((acc, v) => {
|
|
45
44
|
if (obj[v] !== undefined) acc[v] = omit_nil(obj[v])
|
|
46
45
|
return acc
|
|
@@ -50,7 +49,7 @@ export function omit_nil(obj) {
|
|
|
50
49
|
export function null_to_undefined(value) {
|
|
51
50
|
if (Array.isArray(value)) {
|
|
52
51
|
return value.map(null_to_undefined)
|
|
53
|
-
} else if (value && typeof value ===
|
|
52
|
+
} else if (value && typeof value === "object") {
|
|
54
53
|
const data = {}
|
|
55
54
|
for (const [key, val] of Object.entries(value)) {
|
|
56
55
|
data[key] = val === null ? undefined : null_to_undefined(val)
|
package/src/index.mjs
CHANGED
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
import {init_catcher} from
|
|
2
|
-
import {init_fetcher} from
|
|
1
|
+
import { init_catcher } from "./catcher/index.mjs"
|
|
2
|
+
import { init_fetcher } from "./fetcher/index.mjs"
|
|
3
3
|
// import {init_socket} from './socket/index.mjs'
|
|
4
4
|
|
|
5
|
-
function miolo_client(context) {
|
|
6
|
-
|
|
7
|
-
const {config} = context
|
|
5
|
+
export function miolo_client(context) {
|
|
6
|
+
const { config } = context
|
|
8
7
|
|
|
9
8
|
const fetcher = init_fetcher(config)
|
|
10
9
|
|
|
11
10
|
if (config?.catcher_url) {
|
|
12
|
-
init_catcher(config?.catcher_url, fetcher)
|
|
11
|
+
init_catcher(config?.catcher_url, fetcher)
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
// let socket
|
|
16
|
-
// if (config?.socket?.enabled===true) {
|
|
17
|
-
// const domain = config?.socket?.domain
|
|
18
|
-
// const options = config?.socket?.options
|
|
19
|
-
// socket = init_socket(domain, options)
|
|
20
|
-
// }
|
|
14
|
+
// let socket
|
|
15
|
+
// if (config?.socket?.enabled===true) {
|
|
16
|
+
// const domain = config?.socket?.domain
|
|
17
|
+
// const options = config?.socket?.options
|
|
18
|
+
// socket = init_socket(domain, options)
|
|
19
|
+
// }
|
|
21
20
|
|
|
22
|
-
const miolo_obj= {
|
|
23
|
-
fetcher
|
|
21
|
+
const miolo_obj = {
|
|
22
|
+
fetcher
|
|
24
23
|
//socket
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
return miolo_obj
|
|
28
27
|
}
|
|
29
|
-
|
|
30
|
-
export {miolo_client}
|
package/src/socket/index.mjs
CHANGED