@nickyzj2023/utils 1.0.0 → 1.0.2
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/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/is.d.ts +1 -0
- package/dist/lru-cache.d.ts +18 -0
- package/dist/network.d.ts +22 -0
- package/package.json +6 -2
- package/tsconfig.json +5 -2
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var z=(c)=>{return c?.constructor===Object};class V{cache;maxSize;constructor(c=10){this.cache=new Map,this.maxSize=c}get(c){let a=this.cache.get(c);if(!a)return;return this.cache.delete(c),this.cache.set(c,a),a}set(c,a){if(this.cache.has(c))this.cache.delete(c);else if(this.cache.size>=this.maxSize){let f=[...this.cache.keys()][0];if(f)this.cache.delete(f)}this.cache.set(c,a)}has(c){return this.cache.has(c)}}var g=V;var S=new g,B=(c="")=>{let a=async(f,h={})=>{let e=c?`${c}${f}`:f;if(z(h.body))h.body=JSON.stringify(h.body),h.headers={...h.headers,"Content-Type":"application/json"};let v=()=>fetch(e,h),K;if(!(h.method==="GET"||!h.method))K=v();else{let x=S.get(e);if(!x)x=v(),S.set(e,x);K=x}let d=(await K).clone();if(!d.ok)throw Error(d.statusText);return await d.json()};return{get:(f,h={})=>a(f,{...h,method:"GET"}),post:(f,h,e={})=>a(f,{...e,method:"POST",body:h}),put:(f,h,e={})=>a(f,{...e,method:"PUT",body:h}),delete:(f,h={})=>a(f,{...h,method:"DELETE"})}},C=async(c)=>{try{return[null,await c]}catch(a){return[a,void 0]}};export{C as to,z as isObject,B as fetcher};
|
package/dist/is.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isObject: (value: any) => value is object;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 简易 LRU 缓存
|
|
3
|
+
* @example
|
|
4
|
+
* const cache = new LRUCache<string, number>(2);
|
|
5
|
+
* cache.set("a", 1);
|
|
6
|
+
* cache.set("b", 2);
|
|
7
|
+
* cache.set("c", 3); // 缓存已满,a 被淘汰
|
|
8
|
+
* cache.get("a"); // undefined
|
|
9
|
+
*/
|
|
10
|
+
declare class LRUCache<K, V> {
|
|
11
|
+
private cache;
|
|
12
|
+
private maxSize;
|
|
13
|
+
constructor(maxSize?: number);
|
|
14
|
+
get(key: K): V | undefined;
|
|
15
|
+
set(key: K, value: V): void;
|
|
16
|
+
has(key: K): boolean;
|
|
17
|
+
}
|
|
18
|
+
export default LRUCache;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基于 Fetch API 的请求客户端
|
|
3
|
+
* @example
|
|
4
|
+
* // 用法1:创建客户端
|
|
5
|
+
* const api = fetcher("https://nickyzj.run:3030");
|
|
6
|
+
* const res = await api.get<Blog>("/blogs/2025/猩猩也能懂的Node.js部署教程");
|
|
7
|
+
*
|
|
8
|
+
* // 用法2:直接发送请求
|
|
9
|
+
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/2025/猩猩也能懂的Node.js部署教程");
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetcher: (baseURL?: string) => {
|
|
12
|
+
get: <T>(url: string, options?: Omit<RequestInit, "method">) => Promise<T>;
|
|
13
|
+
post: <T>(url: string, body?: any, options?: Omit<RequestInit, "method" | "body">) => Promise<T>;
|
|
14
|
+
put: <T>(url: string, body?: any, options?: Omit<RequestInit, "method" | "body">) => Promise<T>;
|
|
15
|
+
delete: <T>(url: string, options?: Omit<RequestInit, "method">) => Promise<T>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Go 语言风格的异步处理方式
|
|
19
|
+
* @example
|
|
20
|
+
* const [error, response] = await to(request<Resp>("/blogs/2025/猩猩也能懂的Node.js部署教程"));
|
|
21
|
+
*/
|
|
22
|
+
export declare const to: <T, U = Error>(promise: Promise<T>) => Promise<[null, T] | [U, undefined]>;
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nickyzj2023/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"build": "bun build
|
|
8
|
+
"build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc"
|
|
8
9
|
},
|
|
9
10
|
"devDependencies": {
|
|
10
11
|
"@types/bun": "latest"
|
|
11
12
|
},
|
|
12
13
|
"peerDependencies": {
|
|
13
14
|
"typescript": "^5"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"url": "https://github.com/Nickyzj628/utils"
|
|
14
18
|
}
|
|
15
19
|
}
|
package/tsconfig.json
CHANGED
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
"moduleResolution": "bundler",
|
|
13
13
|
"allowImportingTsExtensions": true,
|
|
14
14
|
"verbatimModuleSyntax": true,
|
|
15
|
-
"
|
|
15
|
+
"outDir": "dist",
|
|
16
|
+
"declaration": true,
|
|
17
|
+
"emitDeclarationOnly": true,
|
|
16
18
|
|
|
17
19
|
// Best practices
|
|
18
20
|
"strict": true,
|
|
@@ -25,5 +27,6 @@
|
|
|
25
27
|
"noUnusedLocals": false,
|
|
26
28
|
"noUnusedParameters": false,
|
|
27
29
|
"noPropertyAccessFromIndexSignature": false
|
|
28
|
-
}
|
|
30
|
+
},
|
|
31
|
+
"include": ["src"]
|
|
29
32
|
}
|