@alwatr/fetch 3.1.7 → 4.0.0
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/CHANGELOG.md +22 -0
- package/dist/main.cjs +2 -2
- package/dist/main.cjs.map +3 -3
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +2 -2
- package/dist/main.mjs.map +3 -3
- package/dist/type.d.ts +7 -7
- package/dist/type.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/main.test.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.0.0](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.8...@alwatr/fetch@4.0.0) (2024-09-15)
|
|
7
|
+
|
|
8
|
+
### ⚠ BREAKING CHANGES
|
|
9
|
+
|
|
10
|
+
* **fetch:** queryParametters renamed to queryParams
|
|
11
|
+
|
|
12
|
+
### Code Refactoring
|
|
13
|
+
|
|
14
|
+
* **fetch:** handle fetchJson error responses properly ([ae8fe24](https://github.com/Alwatr/nanolib/commit/ae8fe244aca17f235c4347ff1fd10070a410340c)) by @AliMD
|
|
15
|
+
* **fetch:** update query parameters handling ([939b3d5](https://github.com/Alwatr/nanolib/commit/939b3d52998ec7b3f5c32ff5438b649148109ede)) by @AliMD
|
|
16
|
+
|
|
17
|
+
### Dependencies update
|
|
18
|
+
|
|
19
|
+
* bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @dependabot[bot]
|
|
20
|
+
* update ([c36ed50](https://github.com/Alwatr/nanolib/commit/c36ed50f68da2f5608ccd96119963a16cfacb4ce)) by @AliMD
|
|
21
|
+
|
|
22
|
+
## [3.1.8](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.7...@alwatr/fetch@3.1.8) (2024-08-31)
|
|
23
|
+
|
|
24
|
+
### Miscellaneous Chores
|
|
25
|
+
|
|
26
|
+
* Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
|
|
27
|
+
|
|
6
28
|
## [3.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/fetch@3.1.6...@alwatr/fetch@3.1.7) (2024-08-31)
|
|
7
29
|
|
|
8
30
|
**Note:** Version bump only for package @alwatr/fetch
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/fetch
|
|
2
|
-
"use strict";var m=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var x=(e,r)=>{for(var
|
|
1
|
+
/* @alwatr/fetch v4.0.0 */
|
|
2
|
+
"use strict";var m=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var x=(e,r)=>{for(var a in r)m(e,a,{get:r[a],enumerable:!0})},v=(e,r,a,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of O(r))!k.call(e,c)&&c!==a&&m(e,c,{get:()=>r[c],enumerable:!(t=b(r,c))||t.enumerable});return e};var P=e=>v(m({},"__esModule",{value:!0}),e);var T={};x(T,{cacheSupported:()=>s,fetch:()=>S,fetchJson:()=>C});module.exports=P(T);var f=require("@alwatr/global-scope"),_=require("@alwatr/logger"),g=require("@alwatr/wait"),n=(0,_.definePackage)("@alwatr/fetch","4.0.0"),o,s=Object.hasOwn(f.globalScope,"caches"),u={};function w(e){if(e.method??(e.method="GET"),e.window??(e.window=null),e.timeout??(e.timeout=8e3),e.retry??(e.retry=3),e.retryDelay??(e.retryDelay=1e3),e.cacheStrategy??(e.cacheStrategy="network_only"),e.removeDuplicate??(e.removeDuplicate="never"),e.headers??(e.headers={}),e.cacheStrategy!=="network_only"&&s!==!0&&(n.incident?.("fetch","fetch_cache_strategy_unsupported",{cacheSupported:s}),e.cacheStrategy="network_only"),e.removeDuplicate==="auto"&&(e.removeDuplicate=s?"until_load":"always"),e.url.lastIndexOf("?")===-1&&e.queryParams!=null){let r=e.queryParams,a=Object.keys(r).map(t=>`${t}=${String(r[t])}`);a.length>0&&(e.url+="?"+a.join("&"))}return e.bodyJson!==void 0&&(e.body=JSON.stringify(e.bodyJson),e.headers["Content-Type"]="application/json"),e.bearerToken!==void 0?e.headers.Authorization=`Bearer ${e.bearerToken}`:e.alwatrAuth!==void 0&&(e.headers.Authorization=`Alwatr ${e.alwatrAuth.userId}:${e.alwatrAuth.userToken}`),e}async function R(e){if(e.cacheStrategy==="network_only")return h(e);n.logMethod?.("_handleCacheStrategy"),o==null&&e.cacheStorageName==null&&(o=await caches.open("fetch_cache"));let r=e.cacheStorageName!=null?await caches.open(e.cacheStorageName):o,a=new Request(e.url,e);switch(e.cacheStrategy){case"cache_first":{let t=await r.match(a);if(t!=null)return t;let c=await h(e);return c.ok&&r.put(a,c.clone()),c}case"cache_only":{let t=await r.match(a);if(t==null)throw n.accident("_handleCacheStrategy","fetch_cache_not_found",{url:a.url}),new Error("fetch_cache_not_found");return t}case"network_first":try{let t=await h(e);return t.ok&&r.put(a,t.clone()),t}catch(t){let c=await r.match(a);if(c!=null)return c;throw t}case"update_cache":{let t=await h(e);return t.ok&&r.put(a,t.clone()),t}case"stale_while_revalidate":{let t=await r.match(a),c=h(e).then(l=>(l.ok&&(r.put(a,l.clone()),typeof e.revalidateCallback=="function"&&setTimeout(e.revalidateCallback,0,l.clone())),l));return t??c}default:return h(e)}}async function h(e){if(e.removeDuplicate==="never")return i(e);n.logMethod?.("handleRemoveDuplicate_");let r=e.method+" "+e.url;u[r]??(u[r]=i(e));try{let a=await u[r];return u[r]!=null&&(a.ok!==!0||e.removeDuplicate==="until_load")&&delete u[r],a.clone()}catch(a){throw delete u[r],a}}async function i(e){if(!(e.retry>1))return y(e);n.logMethod?.("_handleRetryPattern"),e.retry--;let r=e.signal;try{let a=await y(e);if(a.status<500)return a;throw new Error("fetch_server_error")}catch(a){if(n.accident("fetch","fetch_failed_retry",a),f.globalScope.navigator?.onLine===!1)throw n.accident("handleRetryPattern_","offline","Skip retry because offline"),a;return await(0,g.waitForTimeout)(e.retryDelay),e.signal=r,i(e)}}function y(e){return e.timeout===0?f.globalScope.fetch(e.url,e):(n.logMethod?.("handleTimeout_"),new Promise((r,a)=>{let t=typeof AbortController=="function"?new AbortController:null,c=e.signal;e.signal=t?.signal,t!==null&&c!=null&&c.addEventListener("abort",()=>t.abort(),{once:!0});let l=setTimeout(()=>{a(new Error("fetch_timeout")),t?.abort("fetch_timeout")},e.timeout);f.globalScope.fetch(e.url,e).then(d=>r(d)).catch(d=>a(d)).finally(()=>{delete e.signal,clearTimeout(l)})}))}async function C(e){let r,a,t;try{return r=await S(e),a=await r.text(),t=JSON.parse(a),t.ok=!0,t.statusCode=r.status,t}catch(c){let l={ok:!1,statusCode:r?.status??500,errorCode:t?.errorCode??c.message,errorMessage:t?.errorMessage??c.message,responseText:a,meta:t?.meta};return n.accident("fetchJson","fetch_json_failed",{responseError:l,error:c}),l}}function S(e){return e=w(e),n.logMethodArgs?.("fetch",{options:e}),R(e)}0&&(module.exports={cacheSupported,fetch,fetchJson});
|
|
3
3
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/core.ts"],
|
|
4
|
-
"sourcesContent": ["import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\nimport type {JsonObject} from '@alwatr/type-helper';\n\nexport {cacheSupported};\nexport type * from './type.js';\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status,\n statusText: response?.statusText,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_failed', {responseError, error});\n return responseError;\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport function fetch(options: FetchOptions): Promise<Response> {\n options = processOptions_(options);\n logger_.logMethodArgs?.('fetch', {options});\n return handleCacheStrategy_(options as Required<FetchOptions>);\n}\n", "import {globalScope} from '@alwatr/global-scope';\nimport {definePackage} from '@alwatr/logger';\nimport {waitForTimeout} from '@alwatr/wait';\n\nimport type {FetchOptions} from './type.js';\nimport type {} from '@alwatr/nano-build';\n\nexport const logger_ = definePackage('@alwatr/fetch', __package_version__);\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = Object.hasOwn(globalScope, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_000;\n options.retry ??= 3;\n options.retryDelay ??= 1_000;\n options.cacheStrategy ??= 'network_only';\n options.removeDuplicate ??= 'never';\n options.headers ??= {};\n\n if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {\n logger_.incident?.('fetch', 'fetch_cache_strategy_unsupported', {\n cacheSupported,\n });\n options.cacheStrategy = 'network_only';\n }\n\n if (options.removeDuplicate === 'auto') {\n options.removeDuplicate = cacheSupported ? 'until_load' : 'always';\n }\n\n if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {\n const queryParameters = options.queryParameters;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParameters)\n .map((key) => `${key}=${String(queryParameters[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return handleRemoveDuplicate_(options);\n }\n // else handle cache strategies!\n logger_.logMethod?.('_handleCacheStrategy');\n\n if (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\n\n const request = new Request(options.url, options);\n\n switch (options.cacheStrategy) {\n case 'cache_first': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n const response = await handleRemoveDuplicate_(options);\n if (response.ok) {\n cacheStorage.put(request, response.clone());\n }\n return response;\n }\n\n case 'cache_only': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse == null) {\n logger_.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});\n throw new Error('fetch_cache_not_found');\n }\n // else\n return cachedResponse;\n }\n\n case 'network_first': {\n try {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n catch (err) {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n throw err;\n }\n }\n\n case 'update_cache': {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n\n case 'stale_while_revalidate': {\n const cachedResponse = await cacheStorage.match(request);\n const fetchedResponsePromise = handleRemoveDuplicate_(options).then((networkResponse) => {\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n if (typeof options.revalidateCallback === 'function') {\n setTimeout(options.revalidateCallback, 0, networkResponse.clone());\n }\n }\n return networkResponse;\n });\n\n return cachedResponse ?? fetchedResponsePromise;\n }\n\n default: {\n return handleRemoveDuplicate_(options);\n }\n }\n}\n\n/**\n * Handle Remove Duplicates over `_handleRetryPattern`.\n */\nexport async function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return handleRetryPattern_(options);\n\n logger_.logMethod?.('handleRemoveDuplicate_');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage_[cacheKey] ??= handleRetryPattern_(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage_[cacheKey];\n\n if (duplicateRequestStorage_[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage_[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage_[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `handleTimeout_`.\n */\nexport async function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return handleTimeout_(options);\n\n logger_.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await handleTimeout_(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger_.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await waitForTimeout(options.retryDelay);\n\n options.signal = externalAbortSignal;\n return handleRetryPattern_(options);\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` with timeout.\n */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof AbortController === 'function' ? new AbortController() : null;\n const externalAbortSignal = options.signal;\n options.signal = abortController?.signal;\n\n if (abortController !== null && externalAbortSignal != null) {\n // Respect external abort signal\n externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});\n }\n\n const timeoutId = setTimeout(() => {\n reject(new Error('fetch_timeout'));\n abortController?.abort('fetch_timeout');\n }, options.timeout);\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalScope\n .fetch(options.url, options)\n .then((response) => resolved(response))\n .catch((reason) => reject(reason))\n .finally(() => {\n delete options.signal; // try to avoid memory leak in nodejs!\n clearTimeout(timeoutId);\n });\n });\n}\n"],
|
|
5
|
-
"mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,UAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAL,GCAA,IAAAM,EAA0B,gCAC1BC,EAA4B,0BAC5BC,EAA6B,wBAKhBC,KAAU,iBAAc,gBAAiB,OAAmB,EAErEC,EACSC,EAAiB,OAAO,OAAO,cAAa,QAAQ,EAE3DC,EAA8D,CAAC,EAS9D,SAASC,EAAgBC,EAA+C,CAsB7E,GArBAA,EAAQ,SAARA,EAAQ,OAAW,OACnBA,EAAQ,SAARA,EAAQ,OAAW,MAEnBA,EAAQ,UAARA,EAAQ,QAAY,KACpBA,EAAQ,QAARA,EAAQ,MAAU,GAClBA,EAAQ,aAARA,EAAQ,WAAe,KACvBA,EAAQ,gBAARA,EAAQ,cAAkB,gBAC1BA,EAAQ,kBAARA,EAAQ,gBAAoB,SAC5BA,EAAQ,UAARA,EAAQ,QAAY,CAAC,GAEjBA,EAAQ,gBAAkB,gBAAkBH,IAAmB,KACjEF,EAAQ,WAAW,QAAS,mCAAoC,CAC9D,eAAAE,CACF,CAAC,EACDG,EAAQ,cAAgB,gBAGtBA,EAAQ,kBAAoB,SAC9BA,EAAQ,gBAAkBH,EAAiB,aAAe,UAGxDG,EAAQ,IAAI,YAAY,GAAG,IAAM,IAAMA,EAAQ,
|
|
6
|
-
"names": ["main_exports", "__export", "cacheSupported", "fetch", "fetchJson", "__toCommonJS", "import_global_scope", "import_logger", "import_wait", "logger_", "cacheStorage_", "cacheSupported", "duplicateRequestStorage_", "processOptions_", "options", "
|
|
4
|
+
"sourcesContent": ["import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\nimport type {JsonObject} from '@alwatr/type-helper';\n\nexport {cacheSupported};\nexport type * from './type.js';\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status ?? 500,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport function fetch(options: FetchOptions): Promise<Response> {\n options = processOptions_(options);\n logger_.logMethodArgs?.('fetch', {options});\n return handleCacheStrategy_(options as Required<FetchOptions>);\n}\n", "import {globalScope} from '@alwatr/global-scope';\nimport {definePackage} from '@alwatr/logger';\nimport {waitForTimeout} from '@alwatr/wait';\n\nimport type {FetchOptions} from './type.js';\nimport type {} from '@alwatr/nano-build';\n\nexport const logger_ = definePackage('@alwatr/fetch', __package_version__);\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = Object.hasOwn(globalScope, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_000;\n options.retry ??= 3;\n options.retryDelay ??= 1_000;\n options.cacheStrategy ??= 'network_only';\n options.removeDuplicate ??= 'never';\n options.headers ??= {};\n\n if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {\n logger_.incident?.('fetch', 'fetch_cache_strategy_unsupported', {\n cacheSupported,\n });\n options.cacheStrategy = 'network_only';\n }\n\n if (options.removeDuplicate === 'auto') {\n options.removeDuplicate = cacheSupported ? 'until_load' : 'always';\n }\n\n if (options.url.lastIndexOf('?') === -1 && options.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return handleRemoveDuplicate_(options);\n }\n // else handle cache strategies!\n logger_.logMethod?.('_handleCacheStrategy');\n\n if (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\n\n const request = new Request(options.url, options);\n\n switch (options.cacheStrategy) {\n case 'cache_first': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n const response = await handleRemoveDuplicate_(options);\n if (response.ok) {\n cacheStorage.put(request, response.clone());\n }\n return response;\n }\n\n case 'cache_only': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse == null) {\n logger_.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});\n throw new Error('fetch_cache_not_found');\n }\n // else\n return cachedResponse;\n }\n\n case 'network_first': {\n try {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n catch (err) {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n throw err;\n }\n }\n\n case 'update_cache': {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n\n case 'stale_while_revalidate': {\n const cachedResponse = await cacheStorage.match(request);\n const fetchedResponsePromise = handleRemoveDuplicate_(options).then((networkResponse) => {\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n if (typeof options.revalidateCallback === 'function') {\n setTimeout(options.revalidateCallback, 0, networkResponse.clone());\n }\n }\n return networkResponse;\n });\n\n return cachedResponse ?? fetchedResponsePromise;\n }\n\n default: {\n return handleRemoveDuplicate_(options);\n }\n }\n}\n\n/**\n * Handle Remove Duplicates over `_handleRetryPattern`.\n */\nexport async function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return handleRetryPattern_(options);\n\n logger_.logMethod?.('handleRemoveDuplicate_');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage_[cacheKey] ??= handleRetryPattern_(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage_[cacheKey];\n\n if (duplicateRequestStorage_[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage_[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage_[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `handleTimeout_`.\n */\nexport async function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return handleTimeout_(options);\n\n logger_.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await handleTimeout_(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger_.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await waitForTimeout(options.retryDelay);\n\n options.signal = externalAbortSignal;\n return handleRetryPattern_(options);\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` with timeout.\n */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof AbortController === 'function' ? new AbortController() : null;\n const externalAbortSignal = options.signal;\n options.signal = abortController?.signal;\n\n if (abortController !== null && externalAbortSignal != null) {\n // Respect external abort signal\n externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});\n }\n\n const timeoutId = setTimeout(() => {\n reject(new Error('fetch_timeout'));\n abortController?.abort('fetch_timeout');\n }, options.timeout);\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalScope\n .fetch(options.url, options)\n .then((response) => resolved(response))\n .catch((reason) => reject(reason))\n .finally(() => {\n delete options.signal; // try to avoid memory leak in nodejs!\n clearTimeout(timeoutId);\n });\n });\n}\n"],
|
|
5
|
+
"mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,UAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAL,GCAA,IAAAM,EAA0B,gCAC1BC,EAA4B,0BAC5BC,EAA6B,wBAKhBC,KAAU,iBAAc,gBAAiB,OAAmB,EAErEC,EACSC,EAAiB,OAAO,OAAO,cAAa,QAAQ,EAE3DC,EAA8D,CAAC,EAS9D,SAASC,EAAgBC,EAA+C,CAsB7E,GArBAA,EAAQ,SAARA,EAAQ,OAAW,OACnBA,EAAQ,SAARA,EAAQ,OAAW,MAEnBA,EAAQ,UAARA,EAAQ,QAAY,KACpBA,EAAQ,QAARA,EAAQ,MAAU,GAClBA,EAAQ,aAARA,EAAQ,WAAe,KACvBA,EAAQ,gBAARA,EAAQ,cAAkB,gBAC1BA,EAAQ,kBAARA,EAAQ,gBAAoB,SAC5BA,EAAQ,UAARA,EAAQ,QAAY,CAAC,GAEjBA,EAAQ,gBAAkB,gBAAkBH,IAAmB,KACjEF,EAAQ,WAAW,QAAS,mCAAoC,CAC9D,eAAAE,CACF,CAAC,EACDG,EAAQ,cAAgB,gBAGtBA,EAAQ,kBAAoB,SAC9BA,EAAQ,gBAAkBH,EAAiB,aAAe,UAGxDG,EAAQ,IAAI,YAAY,GAAG,IAAM,IAAMA,EAAQ,aAAe,KAAM,CACtE,IAAM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,OACf,aAAaoB,CAAS,CACxB,CAAC,CACL,CAAC,EACH,CDzOA,eAAsBE,EAAgCC,EAAoE,CACxH,IAAIC,EACAC,EACAC,EAEJ,GAAI,CACF,OAAAF,EAAW,MAAMG,EAAMJ,CAAO,EAC9BE,EAAe,MAAMD,EAAS,KAAK,EACnCE,EAAe,KAAK,MAAMD,CAAY,EACtCC,EAAa,GAAK,GAClBA,EAAa,WAAaF,EAAS,OAC5BE,CACT,OACOE,EAAO,CACZ,IAAMC,EAA+B,CACnC,GAAI,GACJ,WAAYL,GAAU,QAAU,IAChC,UAAYE,GAAc,WAAyBE,EAAgB,QACnE,aAAeF,GAAc,cAA4BE,EAAgB,QACzE,aAAAH,EACA,KAAMC,GAAc,IACtB,EAEA,OAAAI,EAAQ,SAAS,YAAa,oBAAqB,CAAC,cAAAD,EAAe,MAAAD,CAAK,CAAC,EAClEC,CACT,CACF,CAwBO,SAASF,EAAMJ,EAA0C,CAC9D,OAAAA,EAAUQ,EAAgBR,CAAO,EACjCO,EAAQ,gBAAgB,QAAS,CAAC,QAAAP,CAAO,CAAC,EACnCS,EAAqBT,CAAiC,CAC/D",
|
|
6
|
+
"names": ["main_exports", "__export", "cacheSupported", "fetch", "fetchJson", "__toCommonJS", "import_global_scope", "import_logger", "import_wait", "logger_", "cacheStorage_", "cacheSupported", "duplicateRequestStorage_", "processOptions_", "options", "queryParams", "queryArray", "key", "handleCacheStrategy_", "handleRemoveDuplicate_", "cacheStorage", "request", "cachedResponse", "response", "networkResponse", "err", "fetchedResponsePromise", "handleRetryPattern_", "cacheKey", "handleTimeout_", "externalAbortSignal", "resolved", "reject", "abortController", "timeoutId", "reason", "fetchJson", "options", "response", "responseText", "responseJson", "fetch", "error", "responseError", "logger_", "processOptions_", "handleCacheStrategy_"]
|
|
7
7
|
}
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiD,cAAc,EAAC,MAAM,WAAW,CAAC;AAEzF,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,WAAW,CAAC;AAC5E,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAC,cAAc,EAAC,CAAC;AACxB,mBAAmB,WAAW,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiD,cAAc,EAAC,MAAM,WAAW,CAAC;AAEzF,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,WAAW,CAAC;AAC5E,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAC,cAAc,EAAC,CAAC;AACxB,mBAAmB,WAAW,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CA0BxH;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAI9D"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/fetch
|
|
2
|
-
import{globalScope as f}from"@alwatr/global-scope";import{definePackage as
|
|
1
|
+
/* @alwatr/fetch v4.0.0 */
|
|
2
|
+
import{globalScope as f}from"@alwatr/global-scope";import{definePackage as g}from"@alwatr/logger";import{waitForTimeout as w}from"@alwatr/wait";var n=g("@alwatr/fetch","4.0.0"),m,s=Object.hasOwn(f,"caches"),u={};function y(e){if(e.method??(e.method="GET"),e.window??(e.window=null),e.timeout??(e.timeout=8e3),e.retry??(e.retry=3),e.retryDelay??(e.retryDelay=1e3),e.cacheStrategy??(e.cacheStrategy="network_only"),e.removeDuplicate??(e.removeDuplicate="never"),e.headers??(e.headers={}),e.cacheStrategy!=="network_only"&&s!==!0&&(n.incident?.("fetch","fetch_cache_strategy_unsupported",{cacheSupported:s}),e.cacheStrategy="network_only"),e.removeDuplicate==="auto"&&(e.removeDuplicate=s?"until_load":"always"),e.url.lastIndexOf("?")===-1&&e.queryParams!=null){let a=e.queryParams,t=Object.keys(a).map(r=>`${r}=${String(a[r])}`);t.length>0&&(e.url+="?"+t.join("&"))}return e.bodyJson!==void 0&&(e.body=JSON.stringify(e.bodyJson),e.headers["Content-Type"]="application/json"),e.bearerToken!==void 0?e.headers.Authorization=`Bearer ${e.bearerToken}`:e.alwatrAuth!==void 0&&(e.headers.Authorization=`Alwatr ${e.alwatrAuth.userId}:${e.alwatrAuth.userToken}`),e}async function _(e){if(e.cacheStrategy==="network_only")return h(e);n.logMethod?.("_handleCacheStrategy"),m==null&&e.cacheStorageName==null&&(m=await caches.open("fetch_cache"));let a=e.cacheStorageName!=null?await caches.open(e.cacheStorageName):m,t=new Request(e.url,e);switch(e.cacheStrategy){case"cache_first":{let r=await a.match(t);if(r!=null)return r;let c=await h(e);return c.ok&&a.put(t,c.clone()),c}case"cache_only":{let r=await a.match(t);if(r==null)throw n.accident("_handleCacheStrategy","fetch_cache_not_found",{url:t.url}),new Error("fetch_cache_not_found");return r}case"network_first":try{let r=await h(e);return r.ok&&a.put(t,r.clone()),r}catch(r){let c=await a.match(t);if(c!=null)return c;throw r}case"update_cache":{let r=await h(e);return r.ok&&a.put(t,r.clone()),r}case"stale_while_revalidate":{let r=await a.match(t),c=h(e).then(l=>(l.ok&&(a.put(t,l.clone()),typeof e.revalidateCallback=="function"&&setTimeout(e.revalidateCallback,0,l.clone())),l));return r??c}default:return h(e)}}async function h(e){if(e.removeDuplicate==="never")return o(e);n.logMethod?.("handleRemoveDuplicate_");let a=e.method+" "+e.url;u[a]??(u[a]=o(e));try{let t=await u[a];return u[a]!=null&&(t.ok!==!0||e.removeDuplicate==="until_load")&&delete u[a],t.clone()}catch(t){throw delete u[a],t}}async function o(e){if(!(e.retry>1))return i(e);n.logMethod?.("_handleRetryPattern"),e.retry--;let a=e.signal;try{let t=await i(e);if(t.status<500)return t;throw new Error("fetch_server_error")}catch(t){if(n.accident("fetch","fetch_failed_retry",t),f.navigator?.onLine===!1)throw n.accident("handleRetryPattern_","offline","Skip retry because offline"),t;return await w(e.retryDelay),e.signal=a,o(e)}}function i(e){return e.timeout===0?f.fetch(e.url,e):(n.logMethod?.("handleTimeout_"),new Promise((a,t)=>{let r=typeof AbortController=="function"?new AbortController:null,c=e.signal;e.signal=r?.signal,r!==null&&c!=null&&c.addEventListener("abort",()=>r.abort(),{once:!0});let l=setTimeout(()=>{t(new Error("fetch_timeout")),r?.abort("fetch_timeout")},e.timeout);f.fetch(e.url,e).then(d=>a(d)).catch(d=>t(d)).finally(()=>{delete e.signal,clearTimeout(l)})}))}async function v(e){let a,t,r;try{return a=await R(e),t=await a.text(),r=JSON.parse(t),r.ok=!0,r.statusCode=a.status,r}catch(c){let l={ok:!1,statusCode:a?.status??500,errorCode:r?.errorCode??c.message,errorMessage:r?.errorMessage??c.message,responseText:t,meta:r?.meta};return n.accident("fetchJson","fetch_json_failed",{responseError:l,error:c}),l}}function R(e){return e=y(e),n.logMethodArgs?.("fetch",{options:e}),_(e)}export{s as cacheSupported,R as fetch,v as fetchJson};
|
|
3
3
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/core.ts", "../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {globalScope} from '@alwatr/global-scope';\nimport {definePackage} from '@alwatr/logger';\nimport {waitForTimeout} from '@alwatr/wait';\n\nimport type {FetchOptions} from './type.js';\nimport type {} from '@alwatr/nano-build';\n\nexport const logger_ = definePackage('@alwatr/fetch', __package_version__);\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = Object.hasOwn(globalScope, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_000;\n options.retry ??= 3;\n options.retryDelay ??= 1_000;\n options.cacheStrategy ??= 'network_only';\n options.removeDuplicate ??= 'never';\n options.headers ??= {};\n\n if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {\n logger_.incident?.('fetch', 'fetch_cache_strategy_unsupported', {\n cacheSupported,\n });\n options.cacheStrategy = 'network_only';\n }\n\n if (options.removeDuplicate === 'auto') {\n options.removeDuplicate = cacheSupported ? 'until_load' : 'always';\n }\n\n if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {\n const queryParameters = options.queryParameters;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParameters)\n .map((key) => `${key}=${String(queryParameters[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return handleRemoveDuplicate_(options);\n }\n // else handle cache strategies!\n logger_.logMethod?.('_handleCacheStrategy');\n\n if (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\n\n const request = new Request(options.url, options);\n\n switch (options.cacheStrategy) {\n case 'cache_first': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n const response = await handleRemoveDuplicate_(options);\n if (response.ok) {\n cacheStorage.put(request, response.clone());\n }\n return response;\n }\n\n case 'cache_only': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse == null) {\n logger_.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});\n throw new Error('fetch_cache_not_found');\n }\n // else\n return cachedResponse;\n }\n\n case 'network_first': {\n try {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n catch (err) {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n throw err;\n }\n }\n\n case 'update_cache': {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n\n case 'stale_while_revalidate': {\n const cachedResponse = await cacheStorage.match(request);\n const fetchedResponsePromise = handleRemoveDuplicate_(options).then((networkResponse) => {\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n if (typeof options.revalidateCallback === 'function') {\n setTimeout(options.revalidateCallback, 0, networkResponse.clone());\n }\n }\n return networkResponse;\n });\n\n return cachedResponse ?? fetchedResponsePromise;\n }\n\n default: {\n return handleRemoveDuplicate_(options);\n }\n }\n}\n\n/**\n * Handle Remove Duplicates over `_handleRetryPattern`.\n */\nexport async function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return handleRetryPattern_(options);\n\n logger_.logMethod?.('handleRemoveDuplicate_');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage_[cacheKey] ??= handleRetryPattern_(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage_[cacheKey];\n\n if (duplicateRequestStorage_[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage_[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage_[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `handleTimeout_`.\n */\nexport async function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return handleTimeout_(options);\n\n logger_.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await handleTimeout_(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger_.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await waitForTimeout(options.retryDelay);\n\n options.signal = externalAbortSignal;\n return handleRetryPattern_(options);\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` with timeout.\n */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof AbortController === 'function' ? new AbortController() : null;\n const externalAbortSignal = options.signal;\n options.signal = abortController?.signal;\n\n if (abortController !== null && externalAbortSignal != null) {\n // Respect external abort signal\n externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});\n }\n\n const timeoutId = setTimeout(() => {\n reject(new Error('fetch_timeout'));\n abortController?.abort('fetch_timeout');\n }, options.timeout);\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalScope\n .fetch(options.url, options)\n .then((response) => resolved(response))\n .catch((reason) => reject(reason))\n .finally(() => {\n delete options.signal; // try to avoid memory leak in nodejs!\n clearTimeout(timeoutId);\n });\n });\n}\n", "import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\nimport type {JsonObject} from '@alwatr/type-helper';\n\nexport {cacheSupported};\nexport type * from './type.js';\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status,\n statusText: response?.statusText,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_failed', {responseError, error});\n return responseError;\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport function fetch(options: FetchOptions): Promise<Response> {\n options = processOptions_(options);\n logger_.logMethodArgs?.('fetch', {options});\n return handleCacheStrategy_(options as Required<FetchOptions>);\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,OAAQ,eAAAA,MAAkB,uBAC1B,OAAQ,iBAAAC,MAAoB,iBAC5B,OAAQ,kBAAAC,MAAqB,eAKtB,IAAMC,EAAUF,EAAc,gBAAiB,OAAmB,EAErEG,EACSC,EAAiB,OAAO,OAAOL,EAAa,QAAQ,EAE3DM,EAA8D,CAAC,EAS9D,SAASC,EAAgBC,EAA+C,CAsB7E,GArBAA,EAAQ,SAARA,EAAQ,OAAW,OACnBA,EAAQ,SAARA,EAAQ,OAAW,MAEnBA,EAAQ,UAARA,EAAQ,QAAY,KACpBA,EAAQ,QAARA,EAAQ,MAAU,GAClBA,EAAQ,aAARA,EAAQ,WAAe,KACvBA,EAAQ,gBAARA,EAAQ,cAAkB,gBAC1BA,EAAQ,kBAARA,EAAQ,gBAAoB,SAC5BA,EAAQ,UAARA,EAAQ,QAAY,CAAC,GAEjBA,EAAQ,gBAAkB,gBAAkBH,IAAmB,KACjEF,EAAQ,WAAW,QAAS,mCAAoC,CAC9D,eAAAE,CACF,CAAC,EACDG,EAAQ,cAAgB,gBAGtBA,EAAQ,kBAAoB,SAC9BA,EAAQ,gBAAkBH,EAAiB,aAAe,UAGxDG,EAAQ,IAAI,YAAY,GAAG,IAAM,IAAMA,EAAQ,
|
|
6
|
-
"names": ["globalScope", "definePackage", "waitForTimeout", "logger_", "cacheStorage_", "cacheSupported", "duplicateRequestStorage_", "processOptions_", "options", "
|
|
4
|
+
"sourcesContent": ["import {globalScope} from '@alwatr/global-scope';\nimport {definePackage} from '@alwatr/logger';\nimport {waitForTimeout} from '@alwatr/wait';\n\nimport type {FetchOptions} from './type.js';\nimport type {} from '@alwatr/nano-build';\n\nexport const logger_ = definePackage('@alwatr/fetch', __package_version__);\n\nlet cacheStorage_: Cache;\nexport const cacheSupported = Object.hasOwn(globalScope, 'caches');\n\nconst duplicateRequestStorage_: Record<string, Promise<Response>> = {};\n\n/**\n * Process fetch options and set defaults, etc.\n *\n * @param options Fetch options.\n *\n * @returns Required fetch options.\n */\nexport function processOptions_(options: FetchOptions): Required<FetchOptions> {\n options.method ??= 'GET';\n options.window ??= null;\n\n options.timeout ??= 8_000;\n options.retry ??= 3;\n options.retryDelay ??= 1_000;\n options.cacheStrategy ??= 'network_only';\n options.removeDuplicate ??= 'never';\n options.headers ??= {};\n\n if (options.cacheStrategy !== 'network_only' && cacheSupported !== true) {\n logger_.incident?.('fetch', 'fetch_cache_strategy_unsupported', {\n cacheSupported,\n });\n options.cacheStrategy = 'network_only';\n }\n\n if (options.removeDuplicate === 'auto') {\n options.removeDuplicate = cacheSupported ? 'until_load' : 'always';\n }\n\n if (options.url.lastIndexOf('?') === -1 && options.queryParams != null) {\n const queryParams = options.queryParams;\n // prettier-ignore\n const queryArray = Object\n .keys(queryParams)\n .map((key) => `${key}=${String(queryParams[key])}`);\n\n if (queryArray.length > 0) {\n options.url += '?' + queryArray.join('&');\n }\n }\n\n if (options.bodyJson !== undefined) {\n options.body = JSON.stringify(options.bodyJson);\n options.headers['Content-Type'] = 'application/json';\n }\n\n if (options.bearerToken !== undefined) {\n options.headers.Authorization = `Bearer ${options.bearerToken}`;\n }\n else if (options.alwatrAuth !== undefined) {\n options.headers.Authorization = `Alwatr ${options.alwatrAuth.userId}:${options.alwatrAuth.userToken}`;\n }\n\n return options as Required<FetchOptions>;\n}\n\n/**\n * Handle Cache Strategy over `handleRemoveDuplicate_`.\n */\nexport async function handleCacheStrategy_(options: Required<FetchOptions>): Promise<Response> {\n if (options.cacheStrategy === 'network_only') {\n return handleRemoveDuplicate_(options);\n }\n // else handle cache strategies!\n logger_.logMethod?.('_handleCacheStrategy');\n\n if (cacheStorage_ == null && options.cacheStorageName == null) {\n cacheStorage_ = await caches.open('fetch_cache');\n }\n\n const cacheStorage = options.cacheStorageName != null ? await caches.open(options.cacheStorageName) : cacheStorage_;\n\n const request = new Request(options.url, options);\n\n switch (options.cacheStrategy) {\n case 'cache_first': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n const response = await handleRemoveDuplicate_(options);\n if (response.ok) {\n cacheStorage.put(request, response.clone());\n }\n return response;\n }\n\n case 'cache_only': {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse == null) {\n logger_.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});\n throw new Error('fetch_cache_not_found');\n }\n // else\n return cachedResponse;\n }\n\n case 'network_first': {\n try {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n catch (err) {\n const cachedResponse = await cacheStorage.match(request);\n if (cachedResponse != null) {\n return cachedResponse;\n }\n // else\n throw err;\n }\n }\n\n case 'update_cache': {\n const networkResponse = await handleRemoveDuplicate_(options);\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n }\n return networkResponse;\n }\n\n case 'stale_while_revalidate': {\n const cachedResponse = await cacheStorage.match(request);\n const fetchedResponsePromise = handleRemoveDuplicate_(options).then((networkResponse) => {\n if (networkResponse.ok) {\n cacheStorage.put(request, networkResponse.clone());\n if (typeof options.revalidateCallback === 'function') {\n setTimeout(options.revalidateCallback, 0, networkResponse.clone());\n }\n }\n return networkResponse;\n });\n\n return cachedResponse ?? fetchedResponsePromise;\n }\n\n default: {\n return handleRemoveDuplicate_(options);\n }\n }\n}\n\n/**\n * Handle Remove Duplicates over `_handleRetryPattern`.\n */\nexport async function handleRemoveDuplicate_(options: Required<FetchOptions>): Promise<Response> {\n if (options.removeDuplicate === 'never') return handleRetryPattern_(options);\n\n logger_.logMethod?.('handleRemoveDuplicate_');\n\n const cacheKey = options.method + ' ' + options.url;\n\n // We must cache fetch promise without await for handle other parallel requests.\n duplicateRequestStorage_[cacheKey] ??= handleRetryPattern_(options);\n\n try {\n // For all requests need to await for clone responses.\n const response = await duplicateRequestStorage_[cacheKey];\n\n if (duplicateRequestStorage_[cacheKey] != null) {\n if (response.ok !== true || options.removeDuplicate === 'until_load') {\n delete duplicateRequestStorage_[cacheKey];\n }\n }\n\n return response.clone();\n }\n catch (err) {\n // clean cache on any error.\n delete duplicateRequestStorage_[cacheKey];\n throw err;\n }\n}\n\n/**\n * Handle retry pattern over `handleTimeout_`.\n */\nexport async function handleRetryPattern_(options: Required<FetchOptions>): Promise<Response> {\n if (!(options.retry > 1)) return handleTimeout_(options);\n\n logger_.logMethod?.('_handleRetryPattern');\n options.retry--;\n\n const externalAbortSignal = options.signal;\n\n try {\n const response = await handleTimeout_(options);\n\n if (response.status < 500) {\n return response;\n }\n // else\n throw new Error('fetch_server_error');\n }\n catch (err) {\n logger_.accident('fetch', 'fetch_failed_retry', err);\n\n if (globalScope.navigator?.onLine === false) {\n logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');\n throw err;\n }\n\n await waitForTimeout(options.retryDelay);\n\n options.signal = externalAbortSignal;\n return handleRetryPattern_(options);\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` with timeout.\n */\nexport function handleTimeout_(options: FetchOptions): Promise<Response> {\n if (options.timeout === 0) {\n return globalScope.fetch(options.url, options);\n }\n // else\n logger_.logMethod?.('handleTimeout_');\n return new Promise((resolved, reject) => {\n const abortController = typeof AbortController === 'function' ? new AbortController() : null;\n const externalAbortSignal = options.signal;\n options.signal = abortController?.signal;\n\n if (abortController !== null && externalAbortSignal != null) {\n // Respect external abort signal\n externalAbortSignal.addEventListener('abort', () => abortController.abort(), {once: true});\n }\n\n const timeoutId = setTimeout(() => {\n reject(new Error('fetch_timeout'));\n abortController?.abort('fetch_timeout');\n }, options.timeout);\n\n // abortController.signal.addEventListener('abort', () => {\n // logger.incident('fetch', 'fetch_abort_signal', {\n // reason: abortController.signal.reason,\n // });\n // });\n\n globalScope\n .fetch(options.url, options)\n .then((response) => resolved(response))\n .catch((reason) => reject(reason))\n .finally(() => {\n delete options.signal; // try to avoid memory leak in nodejs!\n clearTimeout(timeoutId);\n });\n });\n}\n", "import {handleCacheStrategy_, logger_, processOptions_, cacheSupported} from './core.js';\n\nimport type {FetchOptions, ResponseError, ResponseSuccess} from './type.js';\nimport type {JsonObject} from '@alwatr/type-helper';\n\nexport {cacheSupported};\nexport type * from './type.js';\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n * @see {@link ResponseSuccess}\n * @see {@link ResponseError}\n *\n * @param options Fetch options.\n *\n * @returns A success or error response.\n *\n * @example\n * ```typescript\n * const responseJson = await fetchJson({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport async function fetchJson<T extends JsonObject>(options: FetchOptions): Promise<ResponseSuccess<T> | ResponseError> {\n let response;\n let responseText;\n let responseJson;\n\n try {\n response = await fetch(options);\n responseText = await response.text();\n responseJson = JSON.parse(responseText) as ResponseSuccess<T>;\n responseJson.ok = true;\n responseJson.statusCode = response.status;\n return responseJson;\n }\n catch (error) {\n const responseError: ResponseError = {\n ok: false,\n statusCode: response?.status ?? 500,\n errorCode: (responseJson?.errorCode as string) ?? (error as Error).message,\n errorMessage: (responseJson?.errorMessage as string) ?? (error as Error).message,\n responseText,\n meta: responseJson?.meta as JsonObject,\n };\n\n logger_.accident('fetchJson', 'fetch_json_failed', {responseError, error});\n return responseError;\n }\n}\n\n/**\n * It's a wrapper around the browser's `fetch` function that adds retry pattern, timeout, cacheStrategy,\n * remove duplicates, etc.\n *\n * @see {@link FetchOptions}\n *\n * @param options Fetch options.\n *\n * @returns A promise that resolves to the Response to that request, whether it is successful or not.\n *\n * @example\n * ```typescript\n * const response = await fetch({\n * url: '/api/products',\n * queryParameters: {limit: 10},\n * timeout: 8_000,\n * retry: 3,\n * cacheStrategy: 'stale_while_revalidate',\n * cacheDuplicate: 'auto',\n * });\n * ```\n */\nexport function fetch(options: FetchOptions): Promise<Response> {\n options = processOptions_(options);\n logger_.logMethodArgs?.('fetch', {options});\n return handleCacheStrategy_(options as Required<FetchOptions>);\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAQ,eAAAA,MAAkB,uBAC1B,OAAQ,iBAAAC,MAAoB,iBAC5B,OAAQ,kBAAAC,MAAqB,eAKtB,IAAMC,EAAUF,EAAc,gBAAiB,OAAmB,EAErEG,EACSC,EAAiB,OAAO,OAAOL,EAAa,QAAQ,EAE3DM,EAA8D,CAAC,EAS9D,SAASC,EAAgBC,EAA+C,CAsB7E,GArBAA,EAAQ,SAARA,EAAQ,OAAW,OACnBA,EAAQ,SAARA,EAAQ,OAAW,MAEnBA,EAAQ,UAARA,EAAQ,QAAY,KACpBA,EAAQ,QAARA,EAAQ,MAAU,GAClBA,EAAQ,aAARA,EAAQ,WAAe,KACvBA,EAAQ,gBAARA,EAAQ,cAAkB,gBAC1BA,EAAQ,kBAARA,EAAQ,gBAAoB,SAC5BA,EAAQ,UAARA,EAAQ,QAAY,CAAC,GAEjBA,EAAQ,gBAAkB,gBAAkBH,IAAmB,KACjEF,EAAQ,WAAW,QAAS,mCAAoC,CAC9D,eAAAE,CACF,CAAC,EACDG,EAAQ,cAAgB,gBAGtBA,EAAQ,kBAAoB,SAC9BA,EAAQ,gBAAkBH,EAAiB,aAAe,UAGxDG,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ACrC,MAAOY,GAAWH,EAAOG,CAAM,CAAC,EAChC,QAAQ,IAAM,CACb,OAAOrB,EAAQ,OACf,aAAaoB,CAAS,CACxB,CAAC,CACL,CAAC,EACH,CCzOA,eAAsBE,EAAgCC,EAAoE,CACxH,IAAIC,EACAC,EACAC,EAEJ,GAAI,CACF,OAAAF,EAAW,MAAMG,EAAMJ,CAAO,EAC9BE,EAAe,MAAMD,EAAS,KAAK,EACnCE,EAAe,KAAK,MAAMD,CAAY,EACtCC,EAAa,GAAK,GAClBA,EAAa,WAAaF,EAAS,OAC5BE,CACT,OACOE,EAAO,CACZ,IAAMC,EAA+B,CACnC,GAAI,GACJ,WAAYL,GAAU,QAAU,IAChC,UAAYE,GAAc,WAAyBE,EAAgB,QACnE,aAAeF,GAAc,cAA4BE,EAAgB,QACzE,aAAAH,EACA,KAAMC,GAAc,IACtB,EAEA,OAAAI,EAAQ,SAAS,YAAa,oBAAqB,CAAC,cAAAD,EAAe,MAAAD,CAAK,CAAC,EAClEC,CACT,CACF,CAwBO,SAASF,EAAMJ,EAA0C,CAC9D,OAAAA,EAAUQ,EAAgBR,CAAO,EACjCO,EAAQ,gBAAgB,QAAS,CAAC,QAAAP,CAAO,CAAC,EACnCS,EAAqBT,CAAiC,CAC/D",
|
|
6
|
+
"names": ["globalScope", "definePackage", "waitForTimeout", "logger_", "cacheStorage_", "cacheSupported", "duplicateRequestStorage_", "processOptions_", "options", "queryParams", "queryArray", "key", "handleCacheStrategy_", "handleRemoveDuplicate_", "cacheStorage", "request", "cachedResponse", "response", "networkResponse", "err", "fetchedResponsePromise", "handleRetryPattern_", "cacheKey", "handleTimeout_", "externalAbortSignal", "resolved", "reject", "abortController", "timeoutId", "reason", "fetchJson", "options", "response", "responseText", "responseJson", "fetch", "error", "responseError", "logger_", "processOptions_", "handleCacheStrategy_"]
|
|
7
7
|
}
|
package/dist/type.d.ts
CHANGED
|
@@ -2,12 +2,12 @@ import type { Dictionary, Json, JsonObject } from '@alwatr/type-helper';
|
|
|
2
2
|
/**
|
|
3
3
|
* Represents the available HTTP methods.
|
|
4
4
|
*/
|
|
5
|
-
export type
|
|
5
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE';
|
|
6
6
|
/**
|
|
7
7
|
* Represents a dictionary of query parameters.
|
|
8
8
|
* The keys are strings and the values can be strings, numbers, or booleans.
|
|
9
9
|
*/
|
|
10
|
-
export type
|
|
10
|
+
export type QueryParams = Dictionary<string | number | boolean>;
|
|
11
11
|
/**
|
|
12
12
|
* Represents the cache strategy for fetching data.
|
|
13
13
|
*
|
|
@@ -40,7 +40,7 @@ export interface FetchOptions extends RequestInit {
|
|
|
40
40
|
*
|
|
41
41
|
* @default 'GET'
|
|
42
42
|
*/
|
|
43
|
-
method?:
|
|
43
|
+
method?: HttpMethod;
|
|
44
44
|
/**
|
|
45
45
|
* A Headers object to set the request's headers.
|
|
46
46
|
*/
|
|
@@ -105,7 +105,7 @@ export interface FetchOptions extends RequestInit {
|
|
|
105
105
|
/**
|
|
106
106
|
* URL query parameters as a JavaScript object.
|
|
107
107
|
*/
|
|
108
|
-
|
|
108
|
+
queryParams?: QueryParams;
|
|
109
109
|
/**
|
|
110
110
|
* Add token to the Authentication bearer header.
|
|
111
111
|
*/
|
|
@@ -124,10 +124,10 @@ export type ResponseSuccess<T extends JsonObject> = T & {
|
|
|
124
124
|
};
|
|
125
125
|
export type ResponseError = {
|
|
126
126
|
ok: false;
|
|
127
|
-
statusCode
|
|
128
|
-
statusText?: string;
|
|
127
|
+
statusCode: number;
|
|
129
128
|
errorCode: string;
|
|
129
|
+
errorMessage: string;
|
|
130
130
|
responseText?: string;
|
|
131
|
-
meta?:
|
|
131
|
+
meta?: Json;
|
|
132
132
|
};
|
|
133
133
|
//# sourceMappingURL=type.d.ts.map
|
package/dist/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.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WG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;IAE9B;;OAEG;IACH,kBAAkB,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAElE;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,EAAE,IAAI,CAAC;IAEhB;;OAEG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,UAAU,CAAC,EAAE;QACX,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAED,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,UAAU,IAAI,CAAC,GAAG;IACtD,EAAE,EAAE,IAAI,CAAC;IACT,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,KAAK,CAAC;IACV,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,IAAI,CAAC;CACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/fetch",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Enhanced fetch API with cache strategy, retry pattern, timeout, helper methods and enhanced types.",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"keywords": [
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"types": "./dist/main.d.ts",
|
|
32
32
|
"exports": {
|
|
33
33
|
".": {
|
|
34
|
+
"types": "./dist/main.d.ts",
|
|
34
35
|
"import": "./dist/main.mjs",
|
|
35
|
-
"require": "./dist/main.cjs"
|
|
36
|
-
"types": "./dist/main.d.ts"
|
|
36
|
+
"require": "./dist/main.cjs"
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"license": "MIT",
|
|
@@ -71,18 +71,18 @@
|
|
|
71
71
|
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@alwatr/global-scope": "^1.1.
|
|
75
|
-
"@alwatr/logger": "^3.2.
|
|
76
|
-
"@alwatr/wait": "^1.1.
|
|
74
|
+
"@alwatr/global-scope": "^1.1.20",
|
|
75
|
+
"@alwatr/logger": "^3.2.13",
|
|
76
|
+
"@alwatr/wait": "^1.1.15"
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
|
-
"@alwatr/nano-build": "^1.3.
|
|
79
|
+
"@alwatr/nano-build": "^1.3.9",
|
|
80
80
|
"@alwatr/prettier-config": "^1.0.4",
|
|
81
81
|
"@alwatr/tsconfig-base": "^1.2.0",
|
|
82
|
-
"@alwatr/type-helper": "^1.2.
|
|
83
|
-
"@types/node": "^22.5.
|
|
82
|
+
"@alwatr/type-helper": "^1.2.6",
|
|
83
|
+
"@types/node": "^22.5.5",
|
|
84
84
|
"jest": "^29.7.0",
|
|
85
|
-
"typescript": "^5.
|
|
85
|
+
"typescript": "^5.6.2"
|
|
86
86
|
},
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "30b89ecac526d2549d7361843ec47f8dde056d45"
|
|
88
88
|
}
|