nitro-web 0.3.1 → 0.3.3

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/util.js CHANGED
@@ -12,20 +12,11 @@ export { TZDate } from '@date-fns/tz'
12
12
  /** @typedef {import('react').Dispatch<import('react').SetStateAction<any>>} SetState */
13
13
 
14
14
  /**
15
- * Create an `axios` instance type that contains the `axios-retry` global declarations.
15
+ * Axios types, `axios-retry` augments AxiosRequestConfig with the 'axios-retry' key itself
16
16
  * @typedef {import('axios').AxiosInstance} AxiosInstance
17
17
  * @typedef {import('axios').AxiosRequestConfig} AxiosRequestConfig
18
- * @typedef {import('axios').AxiosResponse} AxiosResponse
19
- * @typedef {import('axios-retry').IAxiosRetryConfigExtended} IAxiosRetryConfigExtended
20
- *
21
- * Extend the config to be used below
22
- * @typedef {AxiosRequestConfig & { 'axios-retry'?: IAxiosRetryConfigExtended }} AxiosRequestConfigWithRetry
23
- *
24
- * We only need to fix the `get` method, the rest of the methods inherit the new extended config...
25
- * @typedef {Omit<AxiosInstance, 'get'> & {
26
- * get<T = any, R = AxiosResponse, D = any>(url: string, config?: AxiosRequestConfigWithRetry): Promise<R>
27
- * }} AxiosInstanceWithRetry
28
- */
18
+ * @typedef {AxiosInstance} AxiosInstanceWithRetry
19
+ */
29
20
 
30
21
  /** @typedef {object} ObjectId */
31
22
  /** @typedef {(value: string) => ObjectId} parseId */
@@ -1417,6 +1408,16 @@ export function pad (num=0, padLeft=0, fixedRight) {
1417
1408
  }
1418
1409
  }
1419
1410
 
1411
+ /**
1412
+ * Escape user input before it is interpolated into html, e.g. email templates
1413
+ * @param {any} str
1414
+ * @returns {string}
1415
+ */
1416
+ export function escapeHtml (str) {
1417
+ const map = /** @type {{ [key: string]: string }} */ ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#39;' })
1418
+ return String(str ?? '').replace(/[&<>"']/g, c => map[c])
1419
+ }
1420
+
1420
1421
  /**
1421
1422
  * Escapes a string for use in a regex
1422
1423
  * @param {string} str
@@ -1850,10 +1851,10 @@ export async function request (route, data, event, setIsLoading, setState, optio
1850
1851
  const formData2 = hasFiles ? formData({ ...data }, { allowEmptyArrays: true, indices: true }) : undefined
1851
1852
 
1852
1853
  // send the request
1853
- const axiosFn = axios()[method]
1854
- const axiosPromise = (method === 'get' || method === 'delete')
1855
- ? axiosFn(uri, axiosConfig)
1856
- : axiosFn(uri, formData2 || data, axiosConfig)
1854
+ const client = axios()
1855
+ const axiosPromise = (method === 'get' || method === 'delete')
1856
+ ? client[method](uri, axiosConfig)
1857
+ : client[method](uri, formData2 || data, axiosConfig)
1857
1858
 
1858
1859
  const [res] = await Promise.allSettled([
1859
1860
  axiosPromise,
@@ -2237,3 +2238,31 @@ export function ucFirst (string) {
2237
2238
  if (!string) return ''
2238
2239
  return string.charAt(0).toUpperCase() + string.slice(1)
2239
2240
  }
2241
+
2242
+ /**
2243
+ * Ensures the saved document is in the select options, adding a "(Saved)" sentinel option when it's missing.
2244
+ * Handy when the options list is filtered/paginated, but the form state still references a document.
2245
+ * @template {{ value: any, label?: any }} T
2246
+ * @param {T[]} [options]
2247
+ * @param {{ [key: string]: any } | null} [optionToEnsure] - document to ensure is in the options list
2248
+ * @returns {T[]}
2249
+ */
2250
+ export function addSentinel (options=[], optionToEnsure) {
2251
+ if (!optionToEnsure || typeof optionToEnsure !== 'object') return options
2252
+ const value = optionToEnsure._id
2253
+ const label = optionToEnsure.name ?? optionToEnsure.business?.name
2254
+ if (options.some(o => o.value === value)) return options // option already exists
2255
+
2256
+ const sentinelOption = {
2257
+ value: value,
2258
+ label: label ? `${label} (Saved)` : 'Saved value',
2259
+ IconLeft: optionToEnsure.IconLeft,
2260
+ data: { ...optionToEnsure, IconLeft: undefined }, // original option/document data kept here
2261
+ }
2262
+
2263
+ // Add sentinel after the "none"/"new" option if it exists, otherwise prepend it
2264
+ const _options = [...options]
2265
+ const afterIndex = _options.findIndex(o => o.value === '' || (typeof o.value === 'string' && o.value.includes('__new__')))
2266
+ _options.splice(afterIndex >= 0 ? afterIndex + 1 : 0, 0, /** @type {any} */(sentinelOption))
2267
+ return _options
2268
+ }