@longzai-intelligence-transport/http-preset-browser 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 +69 -0
- package/dist/index.js +1 -0
- package/package.json +47 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CsrfProvider, HttpClient, TokenProvider } from "@longzai-intelligence-transport/http-core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Browser 预设配置
|
|
6
|
+
*/
|
|
7
|
+
type BrowserPresetConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* API 基础 URL
|
|
10
|
+
*/
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
/**
|
|
13
|
+
* Token 提供者
|
|
14
|
+
*/
|
|
15
|
+
tokenProvider?: TokenProvider;
|
|
16
|
+
/**
|
|
17
|
+
* CSRF 提供者
|
|
18
|
+
*/
|
|
19
|
+
csrfProvider?: CsrfProvider;
|
|
20
|
+
/**
|
|
21
|
+
* 是否启用日志,默认开发环境启用
|
|
22
|
+
*/
|
|
23
|
+
enableLogging?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 日志级别
|
|
26
|
+
*/
|
|
27
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error';
|
|
28
|
+
/**
|
|
29
|
+
* 请求超时时间(毫秒)
|
|
30
|
+
*/
|
|
31
|
+
timeout?: number;
|
|
32
|
+
/**
|
|
33
|
+
* 自定义请求头
|
|
34
|
+
*/
|
|
35
|
+
headers?: Record<string, string>;
|
|
36
|
+
/**
|
|
37
|
+
* 401 未授权回调
|
|
38
|
+
*/
|
|
39
|
+
onUnauthorized?: () => void | Promise<void>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* 创建 Browser 预设 HTTP 客户端
|
|
43
|
+
*
|
|
44
|
+
* 预配置了浏览器环境常用的拦截器:
|
|
45
|
+
* - Auth Token 注入
|
|
46
|
+
* - CSRF 保护
|
|
47
|
+
* - 请求/响应日志(开发环境)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { createBrowserHttpClient } from '@longzai-intelligence-transport/http/presets/browser';
|
|
52
|
+
*
|
|
53
|
+
* const client = createBrowserHttpClient({
|
|
54
|
+
* baseUrl: '/api',
|
|
55
|
+
* tokenProvider: {
|
|
56
|
+
* getToken: () => localStorage.getItem('token'),
|
|
57
|
+
* },
|
|
58
|
+
* csrfProvider: {
|
|
59
|
+
* getToken: () => document.querySelector('meta[name="csrf-token"]')?.getAttribute('content'),
|
|
60
|
+
* },
|
|
61
|
+
* });
|
|
62
|
+
* ```;
|
|
63
|
+
*
|
|
64
|
+
* @param config - 预设配置
|
|
65
|
+
* @returns HTTP 客户端实例
|
|
66
|
+
*/
|
|
67
|
+
declare function createBrowserHttpClient(config: BrowserPresetConfig): HttpClient;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { type BrowserPresetConfig, createBrowserHttpClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createAuthTokenInterceptor as e,createCsrfInterceptor as t,createHttpClient as n,createLoggingInterceptor as r}from"@longzai-intelligence-transport/http-core";function i(i){let{baseUrl:a,tokenProvider:o,csrfProvider:s,enableLogging:c=import.meta.env?.DEV??!1,logLevel:l=`debug`,timeout:u,headers:d,onUnauthorized:f}=i,p=[],m=[];if(o&&p.push(e(o)),s&&p.push(t(s)),c){let e=r({level:l});p.push(e.request),m.push(e.response)}return n({baseUrl:a,timeout:u,headers:d,requestInterceptors:p,responseInterceptors:m,onUnauthorized:f})}export{i as createBrowserHttpClient};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-transport/http-preset-browser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"browser",
|
|
6
|
+
"http",
|
|
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
|
+
}
|