miolo-cli 3.0.0-beta.20 → 3.0.0-beta.201

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.20",
3
+ "version": "3.0.0-beta.201",
4
4
  "description": "cli utils for miolo",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -17,32 +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
- "reset": "rm -fr package-lock.json npm-lock.yaml dist/* && npm i",
32
- "clean": "rm -fr ./dist/*",
33
- "bundle": "npx xeira bundle --target=browser --source_index=./src/index.mjs --bundle_folder=./dist --bundle_name=miolo-cli --bundle_extension=umd,mjs",
34
- "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"
35
31
  },
36
32
  "dependencies": {
37
- "socket.io-client": "^4.8.1"
33
+ "qs": "^6.15.1",
34
+ "socket.io-client": "^4.8.3"
38
35
  },
39
- "devDependencies": {
40
- "node-fetch": "^3.3.2",
41
- "xeira": "^1.2.3"
42
- },
43
- "eslintConfig": {
44
- "extends": [
45
- "../../node_modules/xeira/configs/eslint.cjs"
46
- ]
36
+ "engines": {
37
+ "node": ">=24"
47
38
  }
48
- }
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,214 +1,206 @@
1
- import {omit_nil, trim_left, json_to_query_string, parse_login_cookie} 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
10
- this.cookie = undefined
8
+ this.config = config
9
+ this.http_auth = undefined
11
10
  }
12
11
 
13
- set_auth(auth) {
12
+ set_http_auth(auth) {
14
13
  if (auth) {
15
- const {username, password}= auth
16
- this.auth= {username, password}
14
+ const { username, password } = auth
15
+ this.http_auth = { username, password }
17
16
  }
18
17
  }
19
18
 
20
19
  get_headers() {
21
- let headers = {}
20
+ const headers = {}
22
21
 
23
- if (this.auth) {
24
- let {username, password}= this.auth
25
- username= username || ''
26
- password= password || ''
22
+ if (this.http_auth) {
23
+ let { username, password } = this.http_auth
24
+ username = username || ""
25
+ password = password || ""
27
26
 
28
27
  let sauth
29
28
  try {
30
- sauth= 'Basic ' + Buffer.from(username + ":" + password).toString('base64')
31
- } catch(_) {
32
- sauth= 'Basic ' + btoa(username + ":" + password)
29
+ sauth = "Basic " + Buffer.from(username + ":" + password).toString("base64")
30
+ } catch (_) {
31
+ sauth = "Basic " + btoa(username + ":" + password)
33
32
  }
34
33
 
35
- headers['Authorization']= sauth
36
- }
37
-
38
- if (this.cookie) {
39
- headers['Cookie']= this.cookie
34
+ headers.Authorization = sauth
40
35
  }
41
36
 
42
37
  return headers
43
38
  }
44
39
 
45
- _prepare_url (url) {
46
- const endpoint = '/' + trim_left(url, '/')
47
-
48
- 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 || {}
49
44
  if (hostname && force_hostname) {
50
45
  return `http://${hostname}:${port}${endpoint}`
51
46
  }
52
-
47
+
53
48
  return endpoint
54
49
  }
55
50
 
56
- async _fetch (method, url, params, auth= undefined) {
57
- this.set_auth(auth)
51
+ async _fetch(method, url, params, http_auth = undefined) {
52
+ this.set_http_auth(http_auth)
58
53
 
59
- let request = {
54
+ const request = {
60
55
  method,
61
- mode: 'cors',
62
- credentials: 'include',
56
+ mode: "cors",
57
+ credentials: "include",
63
58
  headers: {
64
- 'content-type': 'application/json',
65
- ...this.get_headers() || {}
59
+ "content-type": "application/json",
60
+ ...(this.get_headers() || {})
66
61
  }
67
62
  }
68
63
 
69
64
  let wurl = this._prepare_url(url)
70
-
71
- if (method === 'POST') {
72
- request.body = JSON.stringify(params || {}, (k, v) => v === undefined ? null : v)
73
- } 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") {
74
69
  if (params) {
75
- wurl+= json_to_query_string(params)
70
+ wurl += json_to_query_string(params)
76
71
  }
77
72
  }
78
-
73
+
79
74
  const response = await fetch(wurl, request)
80
-
75
+
76
+ if (response.ok === false) {
77
+ return {
78
+ ok: false,
79
+ status: response.status,
80
+ response,
81
+ error: response.statusText
82
+ }
83
+ }
84
+
81
85
  if (response.redirected) {
82
- const isBrowser = typeof window == 'object'
86
+ const isBrowser = typeof window === "object"
83
87
  if (isBrowser) {
84
88
  // JSDOM does not support navigation, so lets skip it for tests
85
- const isTest = typeof navigator !== "undefined" &&
86
- navigator.userAgent.includes("Node.js");
89
+ const isTest = typeof navigator !== "undefined" && navigator.userAgent.includes("Node.js")
87
90
  if (!isTest) {
88
91
  window.location.replace(response.url)
89
92
  return Promise.resolve(response)
90
93
  } else {
91
- 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...`)
94
+ console.error(
95
+ `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...`
96
+ )
92
97
  }
93
98
  }
94
99
  }
95
-
96
- if (response.headers.get('content-type').indexOf('json') >= 0) {
97
- const data= await response.json()
98
-
100
+
101
+ if (response.headers.get("content-type").indexOf("json") >= 0) {
102
+ let resp = await response.json()
103
+ resp = null_to_undefined(resp)
104
+
99
105
  return {
100
- data,
106
+ ...resp,
101
107
  status: response.status,
102
108
  response
103
109
  }
104
110
  }
105
-
106
- const data= await response.text()
111
+
112
+ const data = await response.text()
107
113
  return {
114
+ ok: true,
108
115
  data,
109
116
  status: response.status,
110
117
  response
111
118
  }
112
-
113
119
  }
114
-
115
- async get(url, params, auth= undefined) {
116
- /* eslint no-unused-vars:0 */
120
+
121
+ async get(url, params, http_auth = undefined) {
117
122
  try {
118
- const resp = await this._fetch('GET', url, omit_nil(params), auth)
123
+ const resp = await this._fetch("GET", url, omit_nil(params), http_auth)
119
124
  return resp
120
- } catch(e) {
125
+ } catch (e) {
121
126
  if (this.config?.silent_fail !== true) {
122
127
  console.error(`Error on GET ${url}`)
123
128
  console.error(e)
124
129
  }
125
130
 
126
131
  return {
127
- data: e,
128
- status: -1,
132
+ ok: false,
133
+ error: e,
134
+ status: -1
129
135
  }
130
136
  }
131
137
  }
132
138
 
133
- async post(url, data, auth= undefined) {
139
+ async post(url, data, http_auth = undefined) {
134
140
  try {
135
- const resp = await this._fetch('POST', url, data, auth)
141
+ const resp = await this._fetch("POST", url, data, http_auth)
136
142
  return resp
137
- } catch(e) {
143
+ } catch (e) {
138
144
  if (this.config?.silent_fail !== true) {
139
145
  console.error(`Error on POST ${url}`)
140
146
  console.error(e)
141
147
  }
142
148
 
143
149
  return {
144
- data: e,
150
+ ok: false,
151
+ error: e,
145
152
  status: -1
146
153
  }
147
154
  }
148
- }
149
-
150
- async login(url, {username, password}) {
151
- const res= await this._fetch('POST', url || '/login', {username, password})
152
- this.cookie= parse_login_cookie(res.response)
153
- return res
154
155
  }
155
156
 
156
- async logout(url) {
157
- this.cookie= undefined
158
- const res= await this._fetch('POST', url || '/logout', {})
159
- return res
157
+ async read(url, params, http_auth = undefined) {
158
+ return await this.get(`${url}/read`, params, http_auth)
160
159
  }
161
160
 
162
- async read(url, params, auth= undefined) {
163
- const result = await this.get(`${url}/read`, params, auth)
164
- return result.data
161
+ async key_list(url, params, http_auth = undefined) {
162
+ return await this.get(`${url}/key_list`, params, http_auth)
165
163
  }
166
164
 
167
- async key_list(url, params, auth= undefined) {
168
- const result = await this.get(`${url}/key_list`, params, auth)
169
- return result.data
170
- }
171
-
172
- async name_list(url, params, auth= undefined) {
173
- const result = await this.key_list(url, params, auth)
174
- return Object.values(result)
165
+ async name_list(url, params, http_auth = undefined) {
166
+ const resp = await this.key_list(url, params, http_auth)
167
+ if (resp?.data) {
168
+ resp.data = Object.values(resp.data)
169
+ }
170
+ return resp
175
171
  }
176
172
 
177
- async find(url, id, auth= undefined) {
178
- const result = await this.get(`${url}/find`, { id: id }, auth)
179
- return result.data
173
+ async find(url, id, http_auth = undefined) {
174
+ return await this.get(`${url}/find`, { id: id }, http_auth)
180
175
  }
181
176
 
182
- async distinct(url, field, params, auth= undefined) {
183
- const nparams= {
177
+ async distinct(url, field, params, http_auth = undefined) {
178
+ const nparams = {
184
179
  ...params,
185
180
  distinct_field: field
186
181
  }
187
- const result = await this.get(`${url}/distinct`, nparams, auth)
188
- return result.data
182
+ return await this.get(`${url}/distinct`, nparams, http_auth)
189
183
  }
190
184
 
191
- async upsave(url, data, auth= undefined) {
192
- let result
193
- if (data.id == undefined) {
185
+ async upsave(url, data, http_auth = undefined) {
186
+ let resp
187
+ if (data.id === undefined) {
194
188
  delete data.id
195
- result= await this.post(`${url}/save`, data, auth)
189
+ resp = await this.post(`${url}/save`, data, http_auth)
196
190
  } else {
197
- result= await this.post(`${url}/update`, data, auth)
191
+ resp = await this.post(`${url}/update`, data, http_auth)
198
192
  }
199
-
200
- return result.data
201
- }
202
193
 
203
- async remove(url, id, auth= undefined) {
194
+ return resp
195
+ }
196
+
197
+ async remove(url, id, http_auth = undefined) {
204
198
  const data = { id: id }
205
- const result = await this.post(`${url}/delete`, data, auth)
206
- return result.data
199
+ return await this.post(`${url}/delete`, data, http_auth)
207
200
  }
208
201
  }
209
202
 
210
- Fetcher.keyList= Fetcher.key_list
211
- Fetcher.nameList= Fetcher.name_list
203
+ Fetcher.keyList = Fetcher.key_list
204
+ Fetcher.nameList = Fetcher.name_list
212
205
 
213
206
  export { Fetcher }
214
-
@@ -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,59 +1,60 @@
1
+ import qs from "qs"
2
+
1
3
  /**
2
4
  * Transform an JSON object to a query string
3
5
  */
4
- const _parse_value= (value) => {
5
- try {
6
- return value.replace(/\+/g, '%2B')
7
- } catch(e) {
8
- return value
9
- }
10
- }
6
+ // const _parse_value= (value) => {
7
+ // try {
8
+ // return value.replace(/\+/g, '%2B')
9
+ // } catch(e) {
10
+ // return value
11
+ // }
12
+ // }
13
+ //
14
+ // export function json_to_query_string(obj) {
15
+ // if (obj && (Object.keys(obj).length>0)) {
16
+ // const uparams = new URLSearchParams()
17
+ // for (const key in obj) {
18
+ // if (Object.hasOwn(obj, key)) {
19
+ // const value = obj[key];
20
+ // if (Array.isArray(value)) {
21
+ // value.forEach(item => uparams.append(key, _parse_value(item)))
22
+ // } else if (value !== undefined && value !== null) {
23
+ // uparams.append(key, _parse_value(value))
24
+ // }
25
+ // }
26
+ // }
27
+ // return `?${uparams.toString()}`
28
+ // }
29
+ // return ''
30
+ // }
11
31
 
12
32
  export function json_to_query_string(obj) {
13
- if (obj && (Object.keys(obj).length>0)) {
14
- const uparams = new URLSearchParams()
15
- for (const key in obj) {
16
- if (Object.hasOwn(obj, key)) {
17
- const value = obj[key];
18
- if (Array.isArray(value)) {
19
- value.forEach(item => uparams.append(key, _parse_value(item)))
20
- } else if (value !== undefined && value !== null) {
21
- uparams.append(key, _parse_value(value))
22
- }
23
- }
24
- }
25
- return `?${uparams.toString()}`
26
- }
27
- return ''
33
+ if (!obj || Object.keys(obj).length === 0) return ""
34
+ return `?${qs.stringify(obj, { arrayFormat: "repeat" })}`
28
35
  }
29
36
 
30
37
  export function trim_left(str, what) {
31
- return str.replace(new RegExp(`^${what || '\\s'}+`), '')
38
+ return str.replace(new RegExp(`^${what || "\\s"}+`), "")
32
39
  }
33
40
 
34
-
35
41
  export function omit_nil(obj) {
36
- if (typeof obj !== 'object') return obj
42
+ if (typeof obj !== "object") return obj
37
43
  return Object.keys(obj).reduce((acc, v) => {
38
- if (obj[v] !== undefined) acc[v] = obj[v]
44
+ if (obj[v] !== undefined) acc[v] = omit_nil(obj[v])
39
45
  return acc
40
46
  }, {})
41
47
  }
42
48
 
43
-
44
- export function parse_login_cookie(response) {
45
- if (typeof window !== 'object') {
46
- return undefined
47
- }
48
- try {
49
- const raw = response.headers.raw()['set-cookie'];
50
- return raw.map((entry) => {
51
- const parts = entry.split(';');
52
- const cookiePart = parts[0];
53
- return cookiePart;
54
- }).join(';');
55
- } catch(e) {
56
- console.log('[miolo-cli] Could not get the set-cookie after login')
57
- return undefined
49
+ export function null_to_undefined(value) {
50
+ if (Array.isArray(value)) {
51
+ return value.map(null_to_undefined)
52
+ } else if (value && typeof value === "object") {
53
+ const data = {}
54
+ for (const [key, val] of Object.entries(value)) {
55
+ data[key] = val === null ? undefined : null_to_undefined(val)
56
+ }
57
+ return data
58
58
  }
59
- }
59
+ return value
60
+ }
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
+ }