@longzai-intelligence-transport/http-preset-node 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +85 -0
- package/dist/index.js +1 -0
- package/package.json +47 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { HttpClient, TokenProvider } from "@longzai-intelligence-transport/http-core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Node 预设配置
|
|
6
|
+
*/
|
|
7
|
+
type NodePresetConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* API 基础 URL
|
|
10
|
+
*/
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
/**
|
|
13
|
+
* Token 提供者
|
|
14
|
+
*/
|
|
15
|
+
tokenProvider?: TokenProvider;
|
|
16
|
+
/**
|
|
17
|
+
* 是否启用重试,默认 true
|
|
18
|
+
*/
|
|
19
|
+
enableRetry?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 最大重试次数
|
|
22
|
+
*/
|
|
23
|
+
maxRetries?: number;
|
|
24
|
+
/**
|
|
25
|
+
* 重试基础延迟(毫秒)
|
|
26
|
+
*/
|
|
27
|
+
retryBaseDelay?: number;
|
|
28
|
+
/**
|
|
29
|
+
* 是否启用日志,默认 true
|
|
30
|
+
*/
|
|
31
|
+
enableLogging?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* 日志级别
|
|
34
|
+
*/
|
|
35
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
|
36
|
+
/**
|
|
37
|
+
* 请求超时时间(毫秒)
|
|
38
|
+
*/
|
|
39
|
+
timeout?: number;
|
|
40
|
+
/**
|
|
41
|
+
* 自定义请求头
|
|
42
|
+
*/
|
|
43
|
+
headers?: Record<string, string>;
|
|
44
|
+
/**
|
|
45
|
+
* 401 未授权回调
|
|
46
|
+
*/
|
|
47
|
+
onUnauthorized?: () => void | Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* 服务名称(用于 User-Agent)
|
|
50
|
+
*/
|
|
51
|
+
serviceName?: string;
|
|
52
|
+
/**
|
|
53
|
+
* 服务版本(用于 User-Agent)
|
|
54
|
+
*/
|
|
55
|
+
serviceVersion?: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* 创建 Node 预设 HTTP 客户端
|
|
59
|
+
*
|
|
60
|
+
* 预配置了 Node.js 环境常用的拦截器:
|
|
61
|
+
* - Auth Token 注入
|
|
62
|
+
* - 自动重试(服务器错误)
|
|
63
|
+
* - 请求/响应日志
|
|
64
|
+
* - 自定义 User-Agent
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* import { createNodeHttpClient } from '@longzai-intelligence-transport/http/presets/node';
|
|
69
|
+
*
|
|
70
|
+
* const client = createNodeHttpClient({
|
|
71
|
+
* baseUrl: 'https://api.example.com',
|
|
72
|
+
* tokenProvider: {
|
|
73
|
+
* getToken: () => process.env.API_TOKEN,
|
|
74
|
+
* },
|
|
75
|
+
* serviceName: 'my-service',
|
|
76
|
+
* serviceVersion: '1.0.0',
|
|
77
|
+
* });
|
|
78
|
+
* ```;
|
|
79
|
+
*
|
|
80
|
+
* @param config - 预设配置
|
|
81
|
+
* @returns HTTP 客户端实例
|
|
82
|
+
*/
|
|
83
|
+
declare function createNodeHttpClient(config: NodePresetConfig): HttpClient;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { type NodePresetConfig, createNodeHttpClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createAuthTokenInterceptor as e,createHttpClient as t,createLoggingInterceptor as n,createRetryInterceptor as r}from"@longzai-intelligence-transport/http-core";function i(i){let{baseUrl:a,tokenProvider:o,enableRetry:s=!0,maxRetries:c=3,retryBaseDelay:l=1e3,enableLogging:u=!0,logLevel:d=`info`,timeout:f,headers:p,onUnauthorized:m,serviceName:h,serviceVersion:g}=i,_=[],v=[];if(o&&_.push(e(o)),s&&v.push(r({maxRetries:c,baseDelay:l})),u){let e=n({level:d});_.push(e.request),v.push(e.response)}let y={...p};return h&&(y[`User-Agent`]=g?`${h}/${g}`:h),t({baseUrl:a,timeout:f,headers:y,requestInterceptors:_,responseInterceptors:v,onUnauthorized:m})}export{i as createNodeHttpClient};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-transport/http-preset-node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"http",
|
|
6
|
+
"node",
|
|
7
|
+
"preset",
|
|
8
|
+
"utils"
|
|
9
|
+
],
|
|
10
|
+
"license": "UNLICENSED",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsgo --build tsconfig/build.json && resolve-aliases -p tsconfig/build.json",
|
|
25
|
+
"build:prod": "NODE_ENV=production tsdown",
|
|
26
|
+
"prepublishOnly": "bun run build:prod",
|
|
27
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
28
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
29
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
30
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
31
|
+
"lint": "oxlint && oxfmt --check",
|
|
32
|
+
"lint:fix": "oxlint --fix && oxfmt",
|
|
33
|
+
"test": "bun test",
|
|
34
|
+
"test:watch": "bun test --watch",
|
|
35
|
+
"test:coverage": "bun test --coverage",
|
|
36
|
+
"clean": "rimraf dist out .cache"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@longzai-intelligence-transport/http-core": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"zod": "^4.4.3"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"zod": "^4.4.3"
|
|
46
|
+
}
|
|
47
|
+
}
|