@fullstackunicorn/initialize 1.0.8 → 1.0.10

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 (2) hide show
  1. package/index.js +23 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -46,6 +46,7 @@ globalThis.F3.obj.isGood = obj => !!obj && typeof obj === 'object' && Object.key
46
46
  globalThis.F3.obj.safe = obj => (typeof obj === 'object' && obj !== null) ? obj : {}
47
47
  globalThis.F3.obj.isPlain = obj => obj !== null && typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype
48
48
  globalThis.F3.obj.inKb = obj => new TextEncoder().encode(JSON.stringify(obj)).length / 1000
49
+ globalThis.F3.obj.get = (o, p) => p ? p.split('.').reduce((a, k) => a?.[k], o) ?? null : o
49
50
  globalThis.F3.obj.toStr = obj => {
50
51
  if (typeof obj === 'string') return obj
51
52
  try { return JSON.stringify(obj, null, 2) } catch { return String(obj) }
@@ -113,10 +114,9 @@ globalThis.F3.throw = (...args) => { throw new F3Error(F3.service, ...args) }
113
114
  globalThis.F3.err = {}
114
115
  globalThis.F3.err.is500 = status => !!status && status >= 500
115
116
  globalThis.F3.err.is400 = status => !!status && status >= 400 && status < 500
116
-
117
- globalThis.F3.err.toObj = err => {
118
- const base = { name:err.name, message:err.message, stack:err.stack }
119
- try { return Object.assign(base, Object.fromEntries(Object.entries(err)))}
117
+ globalThis.F3.err.toObj = err => {
118
+ const base = { name: err.name, message: err.message, stack: err.stack }
119
+ try { return Object.assign(base, Object.fromEntries(Object.entries(err))) }
120
120
  catch { return base }
121
121
  }
122
122
  globalThis.F3.err.toStr = err => {
@@ -127,7 +127,25 @@ globalThis.F3.err.toStr = err => {
127
127
  }
128
128
  globalThis.F3.err.logs = err => {
129
129
  if (err instanceof F3Error) console.error(F3.obj.toStr(err))
130
- else console.error('non-F3-error',F3.obj.toStr(err))
130
+ else console.error('non-F3-error', F3.obj.toStr(err))
131
+ }
132
+ globalThis.F3.err.isOffline = (error) => {
133
+ if (!F3.obj.isGood(error)) return false
134
+ return (
135
+ error.code === 'ECONNABORTED' ||
136
+ error.code === 'ERR_NETWORK' ||
137
+ error.message.includes('Network Error') ||
138
+ error.message.includes('timeout')
139
+ )
140
+ }
141
+ globalThis.F3.err.cache = async (error, cached) => {
142
+ if (!F3.err.isOffline(error)) return { isError: true }
143
+ if (cached) return cached
144
+ return { isOffline: true }
145
+ }
146
+ globalThis.F3.err.handle = (error) => {
147
+ if (!F3.err.isOffline(error)) return { isError: true }
148
+ return { isOffline: true }
131
149
  }
132
150
 
133
151
  globalThis.F3.catch = {}
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.8",
2
+ "version": "1.0.10",
3
3
  "name": "@fullstackunicorn/initialize",
4
4
  "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
5
  "description": "A collection of global utility functions for JavaScript/Node.js projects (type checks, string helpers, logging, etc.). Side-effect import extends globals.",