miolo-cli 3.0.0-beta.154 → 3.0.0-beta.158

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo-cli",
3
- "version": "3.0.0-beta.154",
3
+ "version": "3.0.0-beta.158",
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
- "dist",
28
- "src"
23
+ "src",
24
+ "package.json"
29
25
  ],
30
26
  "scripts": {
31
- "lint": "npx xeira lint ./src",
32
- "reset": "rm -fr package-lock.json npm-lock.yaml dist/* && npm i",
33
- "clean": "rm -fr ./dist/*",
34
- "bundle": "npx xeira bundle --target=browser --source_index=./src/index.mjs --bundle_folder=./dist --bundle_name=miolo-cli --bundle_extension=umd,mjs",
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
- "qs": "^6.14.1",
33
+ "qs": "^6.15.0",
39
34
  "socket.io-client": "^4.8.3"
40
35
  },
41
- "devDependencies": {
42
- "xeira": "^2.0.0-beta.10"
36
+ "engines": {
37
+ "node": ">=24"
43
38
  }
44
- }
39
+ }
@@ -1,9 +1,9 @@
1
- function init_catcher (catcher_url, fetcher) {
2
- if (typeof window == "undefined") {
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
- 'error': {
33
- msg: event?.message || 'Client error',
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
- 'path' : window.location.pathname,
40
- 'agent': 'UserAgent' + navigator.userAgent
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
- 'warning': {
55
- msg: event?.reason || 'Client Unhandled rejection',
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
- 'path' : window.location.pathname,
62
- 'agent': 'UserAgent' + navigator.userAgent
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 }
@@ -1,120 +1,119 @@
1
- import {omit_nil, trim_left, json_to_query_string, null_to_undefined} from './utils.mjs'
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
9
- this.auth = undefined
8
+ this.config = config
9
+ this.http_auth = undefined
10
10
  }
11
11
 
12
- set_auth(auth) {
12
+ set_http_auth(auth) {
13
13
  if (auth) {
14
- const {username, password}= auth
15
- this.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
- let headers = {}
20
+ const headers = {}
21
21
 
22
- if (this.auth) {
23
- let {username, password}= this.auth
24
- username= username || ''
25
- password= password || ''
22
+ if (this.http_auth) {
23
+ let { username, password } = this.http_auth
24
+ username = username || ""
25
+ password = password || ""
26
26
 
27
27
  let sauth
28
28
  try {
29
- sauth= 'Basic ' + Buffer.from(username + ":" + password).toString('base64')
30
- } catch(_) {
31
- sauth= 'Basic ' + btoa(username + ":" + password)
29
+ sauth = "Basic " + Buffer.from(username + ":" + password).toString("base64")
30
+ } catch (_) {
31
+ sauth = "Basic " + btoa(username + ":" + password)
32
32
  }
33
33
 
34
- headers['Authorization']= sauth
34
+ headers.Authorization = sauth
35
35
  }
36
36
 
37
37
  return headers
38
38
  }
39
39
 
40
- _prepare_url (url) {
41
- const endpoint = '/' + trim_left(url, '/')
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 (method, url, params, auth= undefined) {
52
- this.set_auth(auth)
51
+ async _fetch(method, url, params, http_auth = undefined) {
52
+ this.set_http_auth(http_auth)
53
53
 
54
- let request = {
54
+ const request = {
55
55
  method,
56
- mode: 'cors',
57
- credentials: 'include',
56
+ mode: "cors",
57
+ credentials: "include",
58
58
  headers: {
59
- 'content-type': 'application/json',
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 === 'POST') {
67
- request.body = JSON.stringify(params || {}, (k, v) => v === undefined ? null : v)
68
- } else if (method === 'GET') {
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 == 'object'
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(`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...`)
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('content-type').indexOf('json') >= 0) {
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, 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('GET', url, omit_nil(params), auth)
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, auth= undefined) {
130
+ async post(url, data, http_auth = undefined) {
132
131
  try {
133
- const resp = await this._fetch('POST', url, data, auth)
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)
@@ -144,66 +143,55 @@ class Fetcher {
144
143
  status: -1
145
144
  }
146
145
  }
147
- }
148
-
149
- async login(url, {username, password}) {
150
- const res= await this._fetch('POST', url || '/login', {username, password})
151
- return res
152
146
  }
153
147
 
154
- async logout(url) {
155
- const res= await this._fetch('POST', url || '/logout', {})
156
- return res
148
+ async read(url, params, http_auth = undefined) {
149
+ return await this.get(`${url}/read`, params, http_auth)
157
150
  }
158
151
 
159
- async read(url, params, auth= undefined) {
160
- return await this.get(`${url}/read`, params, auth)
152
+ async key_list(url, params, http_auth = undefined) {
153
+ return await this.get(`${url}/key_list`, params, http_auth)
161
154
  }
162
155
 
163
- async key_list(url, params, auth= undefined) {
164
- return await this.get(`${url}/key_list`, params, auth)
165
- }
166
-
167
- async name_list(url, params, auth= undefined) {
168
- const resp = await this.key_list(url, params, auth)
156
+ async name_list(url, params, http_auth = undefined) {
157
+ const resp = await this.key_list(url, params, http_auth)
169
158
  if (resp?.data) {
170
159
  resp.data = Object.values(resp.data)
171
160
  }
172
161
  return resp
173
162
  }
174
163
 
175
- async find(url, id, auth= undefined) {
176
- return await this.get(`${url}/find`, { id: id }, auth)
164
+ async find(url, id, http_auth = undefined) {
165
+ return await this.get(`${url}/find`, { id: id }, http_auth)
177
166
  }
178
167
 
179
- async distinct(url, field, params, auth= undefined) {
180
- const nparams= {
168
+ async distinct(url, field, params, http_auth = undefined) {
169
+ const nparams = {
181
170
  ...params,
182
171
  distinct_field: field
183
172
  }
184
- return await this.get(`${url}/distinct`, nparams, auth)
173
+ return await this.get(`${url}/distinct`, nparams, http_auth)
185
174
  }
186
175
 
187
- async upsave(url, data, auth= undefined) {
176
+ async upsave(url, data, http_auth = undefined) {
188
177
  let resp
189
- if (data.id == undefined) {
178
+ if (data.id === undefined) {
190
179
  delete data.id
191
- resp= await this.post(`${url}/save`, data, auth)
180
+ resp = await this.post(`${url}/save`, data, http_auth)
192
181
  } else {
193
- resp= await this.post(`${url}/update`, data, auth)
182
+ resp = await this.post(`${url}/update`, data, http_auth)
194
183
  }
195
-
184
+
196
185
  return resp
197
- }
186
+ }
198
187
 
199
- async remove(url, id, auth= undefined) {
188
+ async remove(url, id, http_auth = undefined) {
200
189
  const data = { id: id }
201
- return await this.post(`${url}/delete`, data, auth)
190
+ return await this.post(`${url}/delete`, data, http_auth)
202
191
  }
203
192
  }
204
193
 
205
- Fetcher.keyList= Fetcher.key_list
206
- Fetcher.nameList= Fetcher.name_list
194
+ Fetcher.keyList = Fetcher.key_list
195
+ Fetcher.nameList = Fetcher.name_list
207
196
 
208
197
  export { Fetcher }
209
-
@@ -1,7 +1,6 @@
1
- import {Fetcher} from './fetcher.mjs'
2
-
1
+ import { Fetcher } from "./fetcher.mjs"
3
2
 
4
3
  export function init_fetcher(config) {
5
- const fetcher= new Fetcher(config)
4
+ const fetcher = new Fetcher(config)
6
5
  return fetcher
7
6
  }
@@ -1,4 +1,4 @@
1
- import qs from 'qs'
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: 'repeat' })}`;
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 || '\\s'}+`), '')
38
+ return str.replace(new RegExp(`^${what || "\\s"}+`), "")
39
39
  }
40
40
 
41
-
42
41
  export function omit_nil(obj) {
43
- if (typeof obj !== 'object') return 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 === 'object') {
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 './catcher/index.mjs'
2
- import {init_fetcher} from './fetcher/index.mjs'
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}
@@ -1,6 +1,6 @@
1
- import { io } from "socket.io-client";
1
+ import { io } from "socket.io-client"
2
2
 
3
3
  export function init_socket(domain, options) {
4
4
  const socket = io(domain, options)
5
5
  return socket
6
- }
6
+ }