@nickyzj2023/utils 1.0.11 → 1.0.13
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/dist/hoc.d.ts +10 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/network.d.ts +21 -6
- package/dist/object.d.ts +1 -0
- package/dist/time.d.ts +7 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/fetcher.html +8 -6
- package/docs/functions/isObject.html +1 -1
- package/docs/functions/sleep.html +6 -0
- package/docs/functions/timeLog.html +1 -1
- package/docs/functions/to.html +1 -1
- package/docs/functions/withCache.html +5 -5
- package/docs/modules.html +1 -1
- package/package.json +1 -1
- package/src/hoc.ts +114 -109
- package/src/index.ts +5 -4
- package/src/network.ts +110 -101
- package/src/object.ts +32 -0
- package/src/time.ts +11 -0
package/dist/hoc.d.ts
CHANGED
|
@@ -12,28 +12,30 @@ type SetTtl = (seconds: number) => void;
|
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* // 异步函数示例
|
|
15
|
-
* const fetchData = withCache(async (
|
|
15
|
+
* const fetchData = withCache(async function (url: string) {
|
|
16
16
|
* const data = await fetch(url).then((res) => res.json());
|
|
17
|
-
* setTtl(data.expiresIn);
|
|
17
|
+
* this.setTtl(data.expiresIn); // 根据实际情况调整过期时间
|
|
18
18
|
* return data;
|
|
19
19
|
* });
|
|
20
20
|
*
|
|
21
21
|
* await fetchData(urlA);
|
|
22
22
|
* await fetchData(urlA); // 使用缓存结果
|
|
23
|
-
|
|
24
23
|
* await fetchData(urlB);
|
|
25
24
|
* await fetchData(urlB); // 使用缓存结果
|
|
26
25
|
*
|
|
27
|
-
* fetchData.clear(); //
|
|
28
|
-
* await fetchData(urlA); //
|
|
29
|
-
* await fetchData(urlB); //
|
|
26
|
+
* fetchData.clear(); // 清除缓存
|
|
27
|
+
* await fetchData(urlA); // 重新请求
|
|
28
|
+
* await fetchData(urlB); // 重新请求
|
|
30
29
|
*
|
|
31
30
|
* // 缓存过期前
|
|
32
|
-
*
|
|
31
|
+
* await sleep();
|
|
32
|
+
* fetchData.updateTtl(180); // 更新 ttl 并为所有未过期的缓存续期
|
|
33
33
|
* await fetchData(urlA); // 使用缓存结果
|
|
34
34
|
* await fetchData(urlB); // 使用缓存结果
|
|
35
35
|
*/
|
|
36
|
-
export declare const withCache: <Args extends any[], Result>(fn: (
|
|
36
|
+
export declare const withCache: <Args extends any[], Result>(fn: (this: {
|
|
37
|
+
setTtl: SetTtl;
|
|
38
|
+
}, ...args: Args) => Result, ttlSeconds?: number) => {
|
|
37
39
|
(...args: Args): Result;
|
|
38
40
|
clear(): void;
|
|
39
41
|
updateTtl(seconds: number): void;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var M=(...P)=>{console.log(`${new Date().toLocaleTimeString()}`,...P)};var Q=(P,W=-1)=>{let D=new Map,m=(...x)=>{let H=JSON.stringify(x),$=Date.now(),f=D.get(H);if(f&&$<f.expiresAt)return f.value;let z={setTtl:(F)=>W=F},I=W===-1?1/0:$+W*1000,C=P.apply(z,x);if(C instanceof Promise){let F=C.then((K)=>{return D.set(H,{value:K,expiresAt:I}),K});return D.set(H,{value:F,expiresAt:I}),F}return D.set(H,{value:C,expiresAt:I}),C};return m.clear=()=>D.clear(),m.updateTtl=(x)=>{W=x;let H=Date.now(),$=H+x*1000;for(let[f,z]of D.entries())if(z.expiresAt>H)z.expiresAt=$,D.set(f,z)},m};var B=(P)=>{return P?.constructor===Object};var J=(...P)=>{return P.reduce((W,D)=>{if(!B(D))return W;return Object.keys(D).forEach((m)=>{let x=W[m],H=D[m];if(Array.isArray(x)&&Array.isArray(H))W[m]=[...x,...H];else if(B(H)&&B(x))W[m]=J(x,H);else W[m]=H}),W},{})};var E=(P="",W={})=>{let D=async(m,x={})=>{let H=P?`${P}${m}`:m,$=J(W,x);if(B($.body))$.body=JSON.stringify($.body),$.headers={...$.headers,"Content-Type":"application/json"};let f=await fetch(H,$);if(!f.ok)throw Error(f.statusText);return await f.json()};return{get:(m,x={})=>D(m,{...x,method:"GET"}),post:(m,x,H={})=>D(m,{...H,method:"POST",body:x}),put:(m,x,H={})=>D(m,{...H,method:"PUT",body:x}),delete:(m,x={})=>D(m,{...x,method:"DELETE"})}},T=async(P)=>{try{return[null,await P]}catch(W){return[W,void 0]}};var g=async(P=150)=>{return new Promise((W)=>{setTimeout(W,P)})};export{Q as withCache,T as to,M as timeLog,g as sleep,B as isObject,E as fetcher};
|
package/dist/network.d.ts
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
7
|
* 特性:
|
|
8
|
+
* - 合并客户端级别、方法级别的请求选项
|
|
8
9
|
* - 在 body 里直接传递对象
|
|
9
|
-
* -
|
|
10
|
+
* - 可选择使用 to() 处理返回结果为 [Error, Response]
|
|
11
|
+
* - 可选择使用 withCache() 缓存请求结果
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* // 用法1:创建客户端
|
|
@@ -15,12 +17,25 @@
|
|
|
15
17
|
*
|
|
16
18
|
* // 用法2:直接发送请求
|
|
17
19
|
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
|
|
20
|
+
*
|
|
21
|
+
* // 安全处理返回结果
|
|
22
|
+
* const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
|
|
23
|
+
* if (error) {
|
|
24
|
+
* console.error(error);
|
|
25
|
+
* return;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // 缓存请求结果
|
|
29
|
+
* const getBlogs = withCache(api.get);
|
|
30
|
+
* await getBlogs("/blogs");
|
|
31
|
+
* await sleep(1000);
|
|
32
|
+
* await getBlogs("/blogs"); // 不请求,使用缓存结果
|
|
18
33
|
*/
|
|
19
|
-
export declare const fetcher: (baseURL?: string, defaultOptions?:
|
|
20
|
-
get: <T>(url: string, options?: Omit<
|
|
21
|
-
post: <T>(url: string, body
|
|
22
|
-
put: <T>(url: string, body
|
|
23
|
-
delete: <T>(url: string, options?: Omit<
|
|
34
|
+
export declare const fetcher: (baseURL?: string, defaultOptions?: BunFetchRequestInit) => {
|
|
35
|
+
get: <T>(url: string, options?: Omit<BunFetchRequestInit, "method">) => Promise<T>;
|
|
36
|
+
post: <T>(url: string, body: any, options?: Omit<BunFetchRequestInit, "method" | "body">) => Promise<T>;
|
|
37
|
+
put: <T>(url: string, body: any, options?: Omit<BunFetchRequestInit, "method" | "body">) => Promise<T>;
|
|
38
|
+
delete: <T>(url: string, options?: Omit<BunFetchRequestInit, "method" | "body">) => Promise<T>;
|
|
24
39
|
};
|
|
25
40
|
/**
|
|
26
41
|
* Go 语言风格的异步处理方式
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const mergeObjects: (...objects: Record<string, any>[]) => Record<string, any>;
|
package/dist/time.d.ts
ADDED
|
@@ -1 +1 @@
|
|
|
1
|
-
window.navigationData = "eJyLrlYqSa0oUbJSSkstSc5ILVLSUSpILMkACZTmJZdk5ucV60Ol9DJKcnOUdJSyM/NSlKzMTGp14Jozi/
|
|
1
|
+
window.navigationData = "eJyLrlYqSa0oUbJSSkstSc5ILVLSUSpILMkACZTmJZdk5ucV60Ol9DJKcnOUdJSyM/NSlKzMTGp14Jozi/2TslKTS7Dphsnh0V6ck5pagE0vWAKPxpLM3FSf/HRsWqFS+DTnY9WXj0dLeWZJhnNickYqNp1wSQwDYgFf6Hjx"
|
package/docs/assets/search.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
window.searchData = "
|
|
1
|
+
window.searchData = "eJyNk0trhDAUhf/LWYcZH7HQbLsqFLrrJkiZxjudtGoG48wUxP9e4qMZMVB3em++L+DxdGjMzULIDt+6LiAeOEN9qAgCR2rViRowXJrSvV9q1WpT2/202Z3aqgSDKg/WkoUAehYQafv68UWqDZjm1VaVLYnOAc8w3yppdUUv5jOgmTabRSbkMFvxm25PTwd1ooDlbxeS5Qy6LugHosOVGqtNDYFkl+4ewXDUVBYu0/EeBmWqimr3+QujLsNjPh17I9Waxh0eT+8jMBmxONvxiOc5kzM8LIbB7PCTAYzBZBwC4xUYL8AETCYhMFmByQJMwWQaAtMVmC5ADiZ5COQrkC/ADExmITBbgeNkCOpKTUvF8xiYlL5XHd6nGKP5n+gQQXR970MTXX+Xm9u5e7Q1U6W8JPaSeJNkKpM3JN6QbDK4vpSuSd6Reke6zWHuce5xvgl3RVFjibwl85bsf0vOcNZnKnVNEDLv+1/caKN7";
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>fetcher | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">fetcher</a></li></ul><h1>Function fetcher</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="fetcher"><span class="tsd-kind-call-signature">fetcher</span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">baseURL</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">defaultOptions</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">
|
|
1
|
+
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>fetcher | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">fetcher</a></li></ul><h1>Function fetcher</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="fetcher"><span class="tsd-kind-call-signature">fetcher</span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">baseURL</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">defaultOptions</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span><br/><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span><br/> <span class="tsd-kind-property">delete</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">get</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">post</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">body</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">put</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">body</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/><span class="tsd-signature-symbol">}</span><a href="#fetcher" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>基于 Fetch API 的请求客户端</p>
|
|
2
2
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">baseURL</span>: <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> = ""</span></span><div class="tsd-comment tsd-typography"><p>接口前缀,如 <a href="https://nickyzj.run:3030">https://nickyzj.run:3030</a>,也可以不填</p>
|
|
3
|
-
</div></li><li><span><span class="tsd-kind-parameter">defaultOptions</span>: <span class="tsd-signature-type">
|
|
4
|
-
</div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-symbol">{</span><br/> <span class="tsd-kind-property">delete</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">
|
|
3
|
+
</div></li><li><span><span class="tsd-kind-parameter">defaultOptions</span>: <span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol"> = {}</span></span><div class="tsd-comment tsd-typography"><p>客户端级别的请求选项,方法级别的选项会覆盖这里的相同值</p>
|
|
4
|
+
</div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-symbol">{</span><br/> <span class="tsd-kind-property">delete</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">get</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">post</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">body</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-property">put</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">url</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">body</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">Omit</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">BunFetchRequestInit</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-type">"method"</span> <span class="tsd-signature-symbol">|</span> <span class="tsd-signature-type">"body"</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type tsd-kind-type-parameter">T</span><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">;</span><br/><span class="tsd-signature-symbol">}</span></h4><div class="tsd-comment tsd-typography"><div class="tsd-tag-remarks"><h4 class="tsd-anchor-link" id="remarks">Remarks<a href="#remarks" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><p>特性:</p>
|
|
5
5
|
<ul>
|
|
6
|
+
<li>合并客户端级别、方法级别的请求选项</li>
|
|
6
7
|
<li>在 body 里直接传递对象</li>
|
|
7
|
-
<li
|
|
8
|
+
<li>可选择使用 to() 处理返回结果为 [Error, Response]</li>
|
|
9
|
+
<li>可选择使用 withCache() 缓存请求结果</li>
|
|
8
10
|
</ul>
|
|
9
|
-
</div><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-0">// 用法1:创建客户端</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">api</span><span class="hl-2"> = </span><span class="hl-1">fetcher</span><span class="hl-2">(</span><span class="hl-3">"https://nickyzj.run:3030"</span><span class="hl-2">, { </span><span class="hl-5">headers:</span><span class="hl-2"> { </span><span class="hl-5">Authorization:</span><span class="hl-2"> </span><span class="hl-3">"Bearer token"</span><span class="hl-2"> } });</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">res</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"/blogs/hello-world"</span><span class="hl-2">);</span><br/><br/><span class="hl-0">// 用法2:直接发送请求</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">res</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetcher</span><span class="hl-2">().</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"https://nickyzj.run:3030/blogs/hello-world"</span><span class="hl-2">);</span>
|
|
11
|
+
</div><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-0">// 用法1:创建客户端</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">api</span><span class="hl-2"> = </span><span class="hl-1">fetcher</span><span class="hl-2">(</span><span class="hl-3">"https://nickyzj.run:3030"</span><span class="hl-2">, { </span><span class="hl-5">headers:</span><span class="hl-2"> { </span><span class="hl-5">Authorization:</span><span class="hl-2"> </span><span class="hl-3">"Bearer token"</span><span class="hl-2"> } });</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">res</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"/blogs/hello-world"</span><span class="hl-2">);</span><br/><br/><span class="hl-0">// 用法2:直接发送请求</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">res</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetcher</span><span class="hl-2">().</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"https://nickyzj.run:3030/blogs/hello-world"</span><span class="hl-2">);</span><br/><br/><span class="hl-0">// 安全处理返回结果</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">error</span><span class="hl-2">, </span><span class="hl-7">data</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">to</span><span class="hl-2">(</span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"/blogs/hello-world"</span><span class="hl-2">));</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">error</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-5">console</span><span class="hl-2">.</span><span class="hl-1">error</span><span class="hl-2">(</span><span class="hl-5">error</span><span class="hl-2">);</span><br/><span class="hl-2"> </span><span class="hl-4">return</span><span class="hl-2">;</span><br/><span class="hl-2">}</span><br/><br/><span class="hl-0">// 缓存请求结果</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">getBlogs</span><span class="hl-2"> = </span><span class="hl-1">withCache</span><span class="hl-2">(</span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-5">get</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">getBlogs</span><span class="hl-2">(</span><span class="hl-3">"/blogs"</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">sleep</span><span class="hl-2">(</span><span class="hl-9">1000</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">getBlogs</span><span class="hl-2">(</span><span class="hl-3">"/blogs"</span><span class="hl-2">); </span><span class="hl-0">// 不请求,使用缓存结果</span>
|
|
10
12
|
</code><button type="button">Copy</button></pre>
|
|
11
13
|
|
|
12
|
-
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
14
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/network.ts#L37">network.ts:37</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>isObject | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">isObject</a></li></ul><h1>Function isObject</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="isobject"><span class="tsd-kind-call-signature">isObject</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">value</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-kind-parameter">value</span> <span class="tsd-signature-keyword">is</span> <span class="tsd-signature-type">object</span><a href="#isobject" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>检测传入的值是否为<strong>普通对象</strong></p>
|
|
2
2
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">value</span>: <span class="tsd-signature-type">any</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-kind-parameter">value</span> <span class="tsd-signature-keyword">is</span> <span class="tsd-signature-type">object</span></h4><p>如果是普通对象,返回 true,否则返回 false</p>
|
|
3
|
-
<aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
3
|
+
<aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/is.ts#L6">is.ts:6</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>sleep | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">sleep</a></li></ul><h1>Function sleep</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="sleep"><span class="tsd-kind-call-signature">sleep</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">time</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">unknown</span><span class="tsd-signature-symbol">></span><a href="#sleep" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>延迟一段时间再执行后续代码</p>
|
|
2
|
+
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">time</span>: <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol"> = 150</span></span><div class="tsd-comment tsd-typography"><p>延迟时间,默认 150ms</p>
|
|
3
|
+
</div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol"><</span><span class="tsd-signature-type">unknown</span><span class="tsd-signature-symbol">></span></h4><div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">sleep</span><span class="hl-2">(</span><span class="hl-9">1000</span><span class="hl-2">); </span><span class="hl-0">// 等待 1 秒执行后续代码</span>
|
|
4
|
+
</code><button type="button">Copy</button></pre>
|
|
5
|
+
|
|
6
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/time.ts#L7">time.ts:7</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span>: <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">[]</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-1">timeLog</span><span class="hl-2">(</span><span class="hl-3">"Hello"</span><span class="hl-2">, </span><span class="hl-3">"World"</span><span class="hl-2">); </span><span class="hl-0">// 14:30:00 Hello World</span>
|
|
3
3
|
</code><button type="button">Copy</button></pre>
|
|
4
4
|
|
|
5
|
-
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
5
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/dom.ts#L8">dom.ts:8</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
package/docs/functions/to.html
CHANGED
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
<div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">error</span><span class="hl-2">, </span><span class="hl-7">response</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">to</span><span class="hl-2">(</span><span class="hl-1">fetcher</span><span class="hl-2">().</span><span class="hl-1">get</span><span class="hl-2"><</span><span class="hl-8">Blog</span><span class="hl-2">>(</span><span class="hl-3">"/blogs/hello-world"</span><span class="hl-2">));</span>
|
|
5
5
|
</code><button type="button">Copy</button></pre>
|
|
6
6
|
|
|
7
|
-
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
7
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/network.ts#L101">network.ts:101</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>withCache | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">withCache</a></li></ul><h1>Function withCache</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="withcache"><span class="tsd-kind-call-signature">withCache</span><span class="tsd-signature-symbol"><</span><a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a> <span class="tsd-signature-keyword">extends</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">[]</span><span class="tsd-signature-symbol">,</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">fn</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">setTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">SetTtl</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">ttlSeconds</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">,</span><br/><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span><br/> <span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-call-signature">updateTtl</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">;</span><br/><span class="tsd-signature-symbol">}</span><a href="#withcache" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>创建一个带缓存的高阶函数</p>
|
|
1
|
+
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>withCache | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">withCache</a></li></ul><h1>Function withCache</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="withcache"><span class="tsd-kind-call-signature">withCache</span><span class="tsd-signature-symbol"><</span><a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a> <span class="tsd-signature-keyword">extends</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">[]</span><span class="tsd-signature-symbol">,</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">></span><span class="tsd-signature-symbol">(</span><br/> <span class="tsd-kind-parameter">fn</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">this</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span> <span class="tsd-kind-property">setTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">SetTtl</span> <span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">,</span><br/> <span class="tsd-kind-parameter">ttlSeconds</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">,</span><br/><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span><br/> <span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-kind-call-signature">updateTtl</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span><br/> <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a><span class="tsd-signature-symbol">;</span><br/><span class="tsd-signature-symbol">}</span><a href="#withcache" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>创建一个带缓存的高阶函数</p>
|
|
2
2
|
</div><section class="tsd-panel"><h4>Type Parameters</h4><ul class="tsd-type-parameter-list"><li><span id="withcacheargs"><span class="tsd-kind-type-parameter">Args</span> <span class="tsd-signature-keyword">extends</span> <span class="tsd-signature-type">any</span><span class="tsd-signature-symbol">[]</span></span><div class="tsd-comment tsd-typography"><p>被包装函数的参数类型数组</p>
|
|
3
3
|
</div></li><li><span id="withcacheresult"><span class="tsd-kind-type-parameter">Result</span></span><div class="tsd-comment tsd-typography"><p>被包装函数的返回类型</p>
|
|
4
|
-
</div></li></ul></section><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">fn</span>: <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">setTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">SetTtl</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></span><div class="tsd-comment tsd-typography"><p>需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间</p>
|
|
4
|
+
</div></li></ul></section><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">fn</span>: <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">this</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{</span> <span class="tsd-kind-property">setTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">SetTtl</span> <span class="tsd-signature-symbol">}</span><span class="tsd-signature-symbol">,</span> <span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=></span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></span><div class="tsd-comment tsd-typography"><p>需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间</p>
|
|
5
5
|
</div></li><li><span><span class="tsd-kind-parameter">ttlSeconds</span>: <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol"> = -1</span></span><div class="tsd-comment tsd-typography"><p>以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖</p>
|
|
6
6
|
</div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-symbol">{</span> <span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span> <span class="tsd-kind-call-signature">updateTtl</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span> <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a> <span class="tsd-signature-symbol">}</span></h4><p>返回包装后的函数,以及缓存相关的额外方法</p>
|
|
7
7
|
<ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature"><span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span>: <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a></span></li></ul></div><h4 class="tsd-returns-title">Returns <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></h4></li></ul></li><li class="tsd-parameter"><h5><span class="tsd-kind-method">clear</span><span class="tsd-signature-symbol">:</span> function</h5><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link"><span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>手动清除缓存</p>
|
|
8
|
-
</div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
9
|
-
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">seconds</span>: <span class="tsd-signature-type">number</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
8
|
+
</div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/hoc.ts#L95">hoc.ts:95</a></li></ul></aside></div></li></ul></li><li class="tsd-parameter"><h5><span class="tsd-kind-method">updateTtl</span><span class="tsd-signature-symbol">:</span> function</h5><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link"><span class="tsd-kind-call-signature">updateTtl</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>更新 TTL,同时刷新所有未过期缓存的时间</p>
|
|
9
|
+
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">seconds</span>: <span class="tsd-signature-type">number</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/hoc.ts#L98">hoc.ts:98</a></li></ul></aside></div></li></ul></li></ul><div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-0">// 异步函数示例</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">fetchData</span><span class="hl-2"> = </span><span class="hl-1">withCache</span><span class="hl-2">(</span><span class="hl-6">async</span><span class="hl-2"> </span><span class="hl-6">function</span><span class="hl-2"> (</span><span class="hl-5">url</span><span class="hl-2">: </span><span class="hl-8">string</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">data</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetch</span><span class="hl-2">(</span><span class="hl-5">url</span><span class="hl-2">).</span><span class="hl-1">then</span><span class="hl-2">((</span><span class="hl-5">res</span><span class="hl-2">) </span><span class="hl-6">=></span><span class="hl-2"> </span><span class="hl-5">res</span><span class="hl-2">.</span><span class="hl-1">json</span><span class="hl-2">());</span><br/><span class="hl-2"> </span><span class="hl-6">this</span><span class="hl-2">.</span><span class="hl-1">setTtl</span><span class="hl-2">(</span><span class="hl-5">data</span><span class="hl-2">.</span><span class="hl-5">expiresIn</span><span class="hl-2">); </span><span class="hl-0">// 根据实际情况调整过期时间</span><br/><span class="hl-2"> </span><span class="hl-4">return</span><span class="hl-2"> </span><span class="hl-5">data</span><span class="hl-2">;</span><br/><span class="hl-2">});</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">clear</span><span class="hl-2">(); </span><span class="hl-0">// 清除缓存</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 重新请求</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 重新请求</span><br/><br/><span class="hl-0">// 缓存过期前</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">sleep</span><span class="hl-2">();</span><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">updateTtl</span><span class="hl-2">(</span><span class="hl-9">180</span><span class="hl-2">); </span><span class="hl-0">// 更新 ttl 并为所有未过期的缓存续期</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span>
|
|
10
10
|
</code><button type="button">Copy</button></pre>
|
|
11
11
|
|
|
12
|
-
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/
|
|
12
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/97e47cdfe2f0da3c22e9fa246971865eb08656e4/src/hoc.ts#L42">hoc.ts:42</a></li></ul></aside></div></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
package/docs/modules.html
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"></ul><h1>@nickyzj2023/utils</h1></div><details class="tsd-panel-group tsd-member-group tsd-accordion" open><summary class="tsd-accordion-summary" data-key="section-Functions"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h2>Functions</h2></summary><dl class="tsd-member-summaries"><dt class="tsd-member-summary" id="fetcher"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/fetcher.html">fetcher</a><a href="#fetcher" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="isobject"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/isObject.html">isObject</a><a href="#isobject" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="timelog"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/timeLog.html">timeLog</a><a href="#timelog" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="to"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/to.html">to</a><a href="#to" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="withcache"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/withCache.html">withCache</a><a href="#withcache" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd></dl></details></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><details open class="tsd-accordion tsd-page-navigation-section"><summary class="tsd-accordion-summary" data-key="section-Functions"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>Functions</summary><div><a href="#fetcher"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>fetcher</span></a><a href="#isobject"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>is<wbr/>Object</span></a><a href="#timelog"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>time<wbr/>Log</span></a><a href="#to"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>to</span></a><a href="#withcache"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>with<wbr/>Cache</span></a></div></details></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html" class="current">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
|
1
|
+
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"></ul><h1>@nickyzj2023/utils</h1></div><details class="tsd-panel-group tsd-member-group tsd-accordion" open><summary class="tsd-accordion-summary" data-key="section-Functions"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h2>Functions</h2></summary><dl class="tsd-member-summaries"><dt class="tsd-member-summary" id="fetcher"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/fetcher.html">fetcher</a><a href="#fetcher" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="isobject"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/isObject.html">isObject</a><a href="#isobject" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="sleep"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/sleep.html">sleep</a><a href="#sleep" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="timelog"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/timeLog.html">timeLog</a><a href="#timelog" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="to"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/to.html">to</a><a href="#to" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd><dt class="tsd-member-summary" id="withcache"><span class="tsd-member-summary-name"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><a href="functions/withCache.html">withCache</a><a href="#withcache" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></span></dt><dd class="tsd-member-summary"></dd></dl></details></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><details open class="tsd-accordion tsd-page-navigation-section"><summary class="tsd-accordion-summary" data-key="section-Functions"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg>Functions</summary><div><a href="#fetcher"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>fetcher</span></a><a href="#isobject"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>is<wbr/>Object</span></a><a href="#sleep"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>sleep</span></a><a href="#timelog"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>time<wbr/>Log</span></a><a href="#to"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>to</span></a><a href="#withcache"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Function"><use href="assets/icons.svg#icon-64"></use></svg><span>with<wbr/>Cache</span></a></div></details></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html" class="current">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
package/package.json
CHANGED
package/src/hoc.ts
CHANGED
|
@@ -1,109 +1,114 @@
|
|
|
1
|
-
type SetTtl = (seconds: number) => void;
|
|
2
|
-
|
|
3
|
-
type CacheEntry = {
|
|
4
|
-
value: any;
|
|
5
|
-
expiresAt: number; // 时间戳(毫秒)
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 创建一个带缓存的高阶函数
|
|
10
|
-
*
|
|
11
|
-
* @template Args 被包装函数的参数类型数组
|
|
12
|
-
* @template Result 被包装函数的返回类型
|
|
13
|
-
*
|
|
14
|
-
* @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
|
|
15
|
-
* @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
|
|
16
|
-
*
|
|
17
|
-
* @returns 返回包装后的函数,以及缓存相关的额外方法
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // 异步函数示例
|
|
21
|
-
* const fetchData = withCache(async (
|
|
22
|
-
* const data = await fetch(url).then((res) => res.json());
|
|
23
|
-
* setTtl(data.expiresIn);
|
|
24
|
-
* return data;
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* await fetchData(urlA);
|
|
28
|
-
* await fetchData(urlA); // 使用缓存结果
|
|
29
|
-
|
|
30
|
-
* await fetchData(urlB);
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* fetchData
|
|
34
|
-
* await fetchData(
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* fetchData.updateTtl(180); // 更新 ttl
|
|
39
|
-
* await fetchData(urlA); // 使用缓存结果
|
|
40
|
-
* await fetchData(urlB); // 使用缓存结果
|
|
41
|
-
*/
|
|
42
|
-
export const withCache = <Args extends any[], Result>(
|
|
43
|
-
fn: (setTtl: SetTtl, ...args: Args) => Result,
|
|
44
|
-
ttlSeconds = -1,
|
|
45
|
-
) => {
|
|
46
|
-
const cache = new Map<string, CacheEntry>();
|
|
47
|
-
|
|
48
|
-
const wrapped = (...args: Args): Result => {
|
|
49
|
-
const key = JSON.stringify(args);
|
|
50
|
-
const now = Date.now();
|
|
51
|
-
const entry = cache.get(key);
|
|
52
|
-
|
|
53
|
-
// 命中缓存且未过期
|
|
54
|
-
if (entry && now < entry.expiresAt) {
|
|
55
|
-
return entry.value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
1
|
+
type SetTtl = (seconds: number) => void;
|
|
2
|
+
|
|
3
|
+
type CacheEntry = {
|
|
4
|
+
value: any;
|
|
5
|
+
expiresAt: number; // 时间戳(毫秒)
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 创建一个带缓存的高阶函数
|
|
10
|
+
*
|
|
11
|
+
* @template Args 被包装函数的参数类型数组
|
|
12
|
+
* @template Result 被包装函数的返回类型
|
|
13
|
+
*
|
|
14
|
+
* @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
|
|
15
|
+
* @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
|
|
16
|
+
*
|
|
17
|
+
* @returns 返回包装后的函数,以及缓存相关的额外方法
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // 异步函数示例
|
|
21
|
+
* const fetchData = withCache(async function (url: string) {
|
|
22
|
+
* const data = await fetch(url).then((res) => res.json());
|
|
23
|
+
* this.setTtl(data.expiresIn); // 根据实际情况调整过期时间
|
|
24
|
+
* return data;
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* await fetchData(urlA);
|
|
28
|
+
* await fetchData(urlA); // 使用缓存结果
|
|
29
|
+
* await fetchData(urlB);
|
|
30
|
+
* await fetchData(urlB); // 使用缓存结果
|
|
31
|
+
*
|
|
32
|
+
* fetchData.clear(); // 清除缓存
|
|
33
|
+
* await fetchData(urlA); // 重新请求
|
|
34
|
+
* await fetchData(urlB); // 重新请求
|
|
35
|
+
*
|
|
36
|
+
* // 缓存过期前
|
|
37
|
+
* await sleep();
|
|
38
|
+
* fetchData.updateTtl(180); // 更新 ttl 并为所有未过期的缓存续期
|
|
39
|
+
* await fetchData(urlA); // 使用缓存结果
|
|
40
|
+
* await fetchData(urlB); // 使用缓存结果
|
|
41
|
+
*/
|
|
42
|
+
export const withCache = <Args extends any[], Result>(
|
|
43
|
+
fn: (this: { setTtl: SetTtl }, ...args: Args) => Result,
|
|
44
|
+
ttlSeconds = -1,
|
|
45
|
+
) => {
|
|
46
|
+
const cache = new Map<string, CacheEntry>();
|
|
47
|
+
|
|
48
|
+
const wrapped = (...args: Args): Result => {
|
|
49
|
+
const key = JSON.stringify(args);
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
const entry = cache.get(key);
|
|
52
|
+
|
|
53
|
+
// 命中缓存且未过期
|
|
54
|
+
if (entry && now < entry.expiresAt) {
|
|
55
|
+
return entry.value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 创建上下文,包含 setTtl 方法
|
|
59
|
+
const thisArg = {
|
|
60
|
+
setTtl: (seconds: number) => (ttlSeconds = seconds),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const expiresAt = ttlSeconds === -1 ? Infinity : now + ttlSeconds * 1000;
|
|
64
|
+
const result = fn.apply(thisArg, args);
|
|
65
|
+
|
|
66
|
+
// 异步函数:缓存 Promise 的 resolved 值
|
|
67
|
+
if (result instanceof Promise) {
|
|
68
|
+
const promise = result.then((resolved) => {
|
|
69
|
+
cache.set(key, {
|
|
70
|
+
value: resolved,
|
|
71
|
+
expiresAt,
|
|
72
|
+
});
|
|
73
|
+
return resolved;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// 将 promise 先塞进去避免重复请求
|
|
77
|
+
cache.set(key, {
|
|
78
|
+
value: promise,
|
|
79
|
+
expiresAt,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return promise as Result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 同步函数缓存
|
|
86
|
+
cache.set(key, {
|
|
87
|
+
value: result,
|
|
88
|
+
expiresAt,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** 手动清除缓存 */
|
|
95
|
+
wrapped.clear = () => cache.clear();
|
|
96
|
+
|
|
97
|
+
/** 更新 TTL,同时刷新所有未过期缓存的时间 */
|
|
98
|
+
wrapped.updateTtl = (seconds: number) => {
|
|
99
|
+
// 更新默认 TTL
|
|
100
|
+
ttlSeconds = seconds;
|
|
101
|
+
|
|
102
|
+
// 给未过期缓存续期
|
|
103
|
+
const now = Date.now();
|
|
104
|
+
const newExpiresAt = now + seconds * 1000;
|
|
105
|
+
for (const [key, entry] of cache.entries()) {
|
|
106
|
+
if (entry.expiresAt > now) {
|
|
107
|
+
entry.expiresAt = newExpiresAt;
|
|
108
|
+
cache.set(key, entry);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return wrapped;
|
|
114
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export * from "./dom";
|
|
2
|
-
export * from "./hoc";
|
|
3
|
-
export * from "./is";
|
|
4
|
-
export * from "./network";
|
|
1
|
+
export * from "./dom";
|
|
2
|
+
export * from "./hoc";
|
|
3
|
+
export * from "./is";
|
|
4
|
+
export * from "./network";
|
|
5
|
+
export * from "./time";
|
package/src/network.ts
CHANGED
|
@@ -1,101 +1,110 @@
|
|
|
1
|
-
import { isObject } from "./is";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* -
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // 用法1:创建客户端
|
|
18
|
-
* const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
|
|
19
|
-
* const res = await api.get<Blog>("/blogs/hello-world");
|
|
20
|
-
*
|
|
21
|
-
* // 用法2:直接发送请求
|
|
22
|
-
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
1
|
+
import { isObject } from "./is";
|
|
2
|
+
import { mergeObjects } from "./object";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 基于 Fetch API 的请求客户端
|
|
6
|
+
* @param baseURL 接口前缀,如 https://nickyzj.run:3030,也可以不填
|
|
7
|
+
* @param defaultOptions 客户端级别的请求选项,方法级别的选项会覆盖这里的相同值
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* 特性:
|
|
11
|
+
* - 合并客户端级别、方法级别的请求选项
|
|
12
|
+
* - 在 body 里直接传递对象
|
|
13
|
+
* - 可选择使用 to() 处理返回结果为 [Error, Response]
|
|
14
|
+
* - 可选择使用 withCache() 缓存请求结果
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // 用法1:创建客户端
|
|
18
|
+
* const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
|
|
19
|
+
* const res = await api.get<Blog>("/blogs/hello-world");
|
|
20
|
+
*
|
|
21
|
+
* // 用法2:直接发送请求
|
|
22
|
+
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
|
|
23
|
+
*
|
|
24
|
+
* // 安全处理返回结果
|
|
25
|
+
* const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
|
|
26
|
+
* if (error) {
|
|
27
|
+
* console.error(error);
|
|
28
|
+
* return;
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* // 缓存请求结果
|
|
32
|
+
* const getBlogs = withCache(api.get);
|
|
33
|
+
* await getBlogs("/blogs");
|
|
34
|
+
* await sleep(1000);
|
|
35
|
+
* await getBlogs("/blogs"); // 不请求,使用缓存结果
|
|
36
|
+
*/
|
|
37
|
+
export const fetcher = (
|
|
38
|
+
baseURL = "",
|
|
39
|
+
defaultOptions: BunFetchRequestInit = {},
|
|
40
|
+
) => {
|
|
41
|
+
const createRequest = async <T>(
|
|
42
|
+
path: string,
|
|
43
|
+
requestOptions: BunFetchRequestInit = {},
|
|
44
|
+
) => {
|
|
45
|
+
// 构建完整 URL
|
|
46
|
+
const url = baseURL ? `${baseURL}${path}` : path;
|
|
47
|
+
|
|
48
|
+
// 合并 options
|
|
49
|
+
const options = mergeObjects(defaultOptions, requestOptions);
|
|
50
|
+
|
|
51
|
+
// 转换 body 为字符串
|
|
52
|
+
if (isObject(options.body)) {
|
|
53
|
+
options.body = JSON.stringify(options.body);
|
|
54
|
+
options.headers = {
|
|
55
|
+
...options.headers,
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 发送请求
|
|
61
|
+
const response = await fetch(url, options);
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(response.statusText);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const data = await response.json();
|
|
67
|
+
return data as T;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
get: <T>(url: string, options: Omit<BunFetchRequestInit, "method"> = {}) =>
|
|
72
|
+
createRequest<T>(url, { ...options, method: "GET" }),
|
|
73
|
+
|
|
74
|
+
post: <T>(
|
|
75
|
+
url: string,
|
|
76
|
+
body: any,
|
|
77
|
+
options: Omit<BunFetchRequestInit, "method" | "body"> = {},
|
|
78
|
+
) => createRequest<T>(url, { ...options, method: "POST", body }),
|
|
79
|
+
|
|
80
|
+
put: <T>(
|
|
81
|
+
url: string,
|
|
82
|
+
body: any,
|
|
83
|
+
options: Omit<BunFetchRequestInit, "method" | "body"> = {},
|
|
84
|
+
) => createRequest<T>(url, { ...options, method: "PUT", body }),
|
|
85
|
+
|
|
86
|
+
delete: <T>(
|
|
87
|
+
url: string,
|
|
88
|
+
options: Omit<BunFetchRequestInit, "method" | "body"> = {},
|
|
89
|
+
) => createRequest<T>(url, { ...options, method: "DELETE" }),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Go 语言风格的异步处理方式
|
|
95
|
+
* @param promise 一个能被 await 的异步函数
|
|
96
|
+
* @returns 如果成功,返回 [null, 异步函数结果],否则返回 [Error, undefined]
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* const [error, response] = await to(fetcher().get<Blog>("/blogs/hello-world"));
|
|
100
|
+
*/
|
|
101
|
+
export const to = async <T, U = Error>(
|
|
102
|
+
promise: Promise<T>,
|
|
103
|
+
): Promise<[null, T] | [U, undefined]> => {
|
|
104
|
+
try {
|
|
105
|
+
const response = await promise;
|
|
106
|
+
return [null, response];
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return [error as U, undefined];
|
|
109
|
+
}
|
|
110
|
+
};
|
package/src/object.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isObject } from "./is";
|
|
2
|
+
|
|
3
|
+
export const mergeObjects = (...objects: Record<string, any>[]) => {
|
|
4
|
+
return objects.reduce(
|
|
5
|
+
(acc: Record<string, any>, obj: Record<string, any>) => {
|
|
6
|
+
if (!isObject(obj)) {
|
|
7
|
+
return acc;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Object.keys(obj).forEach((key) => {
|
|
11
|
+
const accValue = acc[key];
|
|
12
|
+
const objValue = obj[key];
|
|
13
|
+
|
|
14
|
+
// 如果两个值都是数组,则合并数组
|
|
15
|
+
if (Array.isArray(accValue) && Array.isArray(objValue)) {
|
|
16
|
+
acc[key] = [...accValue, ...objValue];
|
|
17
|
+
}
|
|
18
|
+
// 如果两个值都是对象,则递归合并
|
|
19
|
+
else if (isObject(objValue) && isObject(accValue)) {
|
|
20
|
+
acc[key] = mergeObjects(accValue, objValue);
|
|
21
|
+
}
|
|
22
|
+
// 其他情况直接覆盖
|
|
23
|
+
else {
|
|
24
|
+
acc[key] = objValue;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return acc;
|
|
29
|
+
},
|
|
30
|
+
{},
|
|
31
|
+
);
|
|
32
|
+
};
|
package/src/time.ts
ADDED