@longzai-intelligence-transport/http-adapter-taro 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 +166 -0
- package/dist/index.js +1 -0
- package/package.json +50 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { ApiError, ApiResponse, ApiResponse as ApiResponse$1, AuthStrategy, CsrfProvider, HttpAdapter, HttpAdapter as HttpAdapter$1, HttpClient, HttpClient as HttpClient$1, HttpClientConfig, HttpClientConfig as HttpClientConfig$1, RequestConfig, RequestConfig as RequestConfig$1, RequestInterceptor, RequestInterceptorContext, ResponseInterceptor, ResponseInterceptorContext, TaroAdapterConfig, TaroAdapterConfig as TaroAdapterConfig$1, TokenProvider, TokenProvider as TokenProvider$1, createAuthStrategyInterceptor, createAuthTokenInterceptor, createCsrfInterceptor, createLoggingRequestInterceptor, createLoggingResponseInterceptor, createResponseTransformerInterceptor, createRetryInterceptor, createValidationInterceptor } from "@longzai-intelligence-transport/http-core";
|
|
2
|
+
import Taro from "@tarojs/taro";
|
|
3
|
+
|
|
4
|
+
//#region src/taro.adapter.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* 创建 Taro 适配器
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import Taro from '@tarojs/taro';
|
|
11
|
+
* import { createTaroAdapter } from '@longzai-intelligence-transport/http-adapter-taro';
|
|
12
|
+
*
|
|
13
|
+
* const adapter = createTaroAdapter({ timeout: 30000 }, Taro);
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @param config - 适配器配置
|
|
17
|
+
* @param taro - Taro 实例(用于依赖注入,方便测试)
|
|
18
|
+
* @returns HTTP 适配器实例
|
|
19
|
+
*/
|
|
20
|
+
declare function createTaroAdapter(config?: TaroAdapterConfig$1, taro?: typeof Taro): HttpAdapter$1;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/token.provider.d.ts
|
|
23
|
+
/**
|
|
24
|
+
* Taro Token 存储配置
|
|
25
|
+
*/
|
|
26
|
+
type TaroTokenStorageConfig = {
|
|
27
|
+
/**
|
|
28
|
+
* Token 存储键名
|
|
29
|
+
*/
|
|
30
|
+
tokenKey?: string;
|
|
31
|
+
/**
|
|
32
|
+
* 存储方式,默认 'storage'(持久化)
|
|
33
|
+
*/
|
|
34
|
+
storageType?: 'storage' | 'asyncStorage';
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* 创建 Taro Token 提供者
|
|
38
|
+
*
|
|
39
|
+
* 使用 Taro.getStorageSync 或 Taro.getStorage 实现 Token 获取
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import { createTaroTokenProvider } from '@longzai-intelligence-transport/http-adapter-taro';
|
|
44
|
+
*
|
|
45
|
+
* const tokenProvider = createTaroTokenProvider({
|
|
46
|
+
* tokenKey: 'auth_token',
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // 在 createHttpClient 中使用
|
|
50
|
+
* const client = createHttpClient({
|
|
51
|
+
* baseUrl: 'https://api.example.com',
|
|
52
|
+
* tokenProvider,
|
|
53
|
+
* });
|
|
54
|
+
* ```;
|
|
55
|
+
*
|
|
56
|
+
* @param config - 配置选项
|
|
57
|
+
* @returns Token 提供者实例
|
|
58
|
+
*/
|
|
59
|
+
declare function createTaroTokenProvider(config?: TaroTokenStorageConfig): TokenProvider$1;
|
|
60
|
+
/**
|
|
61
|
+
* 创建 Taro Token 设置器
|
|
62
|
+
*
|
|
63
|
+
* 提供设置和删除 Token 的方法
|
|
64
|
+
*
|
|
65
|
+
* @param config - 配置选项
|
|
66
|
+
* @returns Token 设置器实例
|
|
67
|
+
*/
|
|
68
|
+
declare function createTaroTokenSetter(config?: TaroTokenStorageConfig): {
|
|
69
|
+
setToken: (token: string | null) => Promise<void>;
|
|
70
|
+
removeToken: () => Promise<void>;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/create-taro-http-client.client.d.ts
|
|
74
|
+
/**
|
|
75
|
+
* Taro HTTP 客户端类型
|
|
76
|
+
*
|
|
77
|
+
* 扩展基础 HttpClient,增加 REST 便捷方法
|
|
78
|
+
*/
|
|
79
|
+
type TaroHttpClient = HttpClient$1 & {
|
|
80
|
+
/**
|
|
81
|
+
* 发送 GET 请求
|
|
82
|
+
*
|
|
83
|
+
* @typeParam T - 响应数据类型
|
|
84
|
+
* @param path - 请求路径
|
|
85
|
+
* @param params - 查询参数
|
|
86
|
+
* @param config - 请求配置
|
|
87
|
+
* @returns API 响应
|
|
88
|
+
*/
|
|
89
|
+
get<T = unknown>(path: string, params?: QueryParams, config?: RequestConfig$1): Promise<ApiResponse$1<T>>;
|
|
90
|
+
/**
|
|
91
|
+
* 发送 POST 请求
|
|
92
|
+
*
|
|
93
|
+
* @typeParam T - 响应数据类型
|
|
94
|
+
* @param path - 请求路径
|
|
95
|
+
* @param data - 请求体数据
|
|
96
|
+
* @param config - 请求配置
|
|
97
|
+
* @returns API 响应
|
|
98
|
+
*/
|
|
99
|
+
post<T = unknown>(path: string, data?: unknown, config?: RequestConfig$1): Promise<ApiResponse$1<T>>;
|
|
100
|
+
/**
|
|
101
|
+
* 发送 PUT 请求
|
|
102
|
+
*
|
|
103
|
+
* @typeParam T - 响应数据类型
|
|
104
|
+
* @param path - 请求路径
|
|
105
|
+
* @param data - 请求体数据
|
|
106
|
+
* @param config - 请求配置
|
|
107
|
+
* @returns API 响应
|
|
108
|
+
*/
|
|
109
|
+
put<T = unknown>(path: string, data?: unknown, config?: RequestConfig$1): Promise<ApiResponse$1<T>>;
|
|
110
|
+
/**
|
|
111
|
+
* 发送 PATCH 请求
|
|
112
|
+
*
|
|
113
|
+
* @typeParam T - 响应数据类型
|
|
114
|
+
* @param path - 请求路径
|
|
115
|
+
* @param data - 请求体数据
|
|
116
|
+
* @param config - 请求配置
|
|
117
|
+
* @returns API 响应
|
|
118
|
+
*/
|
|
119
|
+
patch<T = unknown>(path: string, data?: unknown, config?: RequestConfig$1): Promise<ApiResponse$1<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* 发送 DELETE 请求
|
|
122
|
+
*
|
|
123
|
+
* @typeParam T - 响应数据类型
|
|
124
|
+
* @param path - 请求路径
|
|
125
|
+
* @param config - 请求配置
|
|
126
|
+
* @returns API 响应
|
|
127
|
+
*/
|
|
128
|
+
delete<T = unknown>(path: string, config?: RequestConfig$1): Promise<ApiResponse$1<T>>;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* 查询参数类型
|
|
132
|
+
*/
|
|
133
|
+
type QueryParams = Record<string, string | number | boolean | undefined>;
|
|
134
|
+
/**
|
|
135
|
+
* 创建 Taro HTTP 客户端
|
|
136
|
+
*
|
|
137
|
+
* 创建基于 Taro.request 的 HTTP 客户端实例
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* import {
|
|
142
|
+
* createTaroHttpClient,
|
|
143
|
+
* createTaroTokenProvider,
|
|
144
|
+
* } from '@longzai-intelligence-transport/http-adapter-taro';
|
|
145
|
+
* import { createAuthTokenInterceptor } from '@longzai-intelligence-transport/http-core';
|
|
146
|
+
*
|
|
147
|
+
* const tokenProvider = createTaroTokenProvider({ tokenKey: 'auth_token' });
|
|
148
|
+
*
|
|
149
|
+
* const client = createTaroHttpClient({
|
|
150
|
+
* baseUrl: 'https://api.example.com',
|
|
151
|
+
* requestInterceptors: [createAuthTokenInterceptor(tokenProvider)],
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* // 使用类型安全的 request 方法
|
|
155
|
+
* const response = await client.request({ route: userRoutes.list });
|
|
156
|
+
*
|
|
157
|
+
* // 使用便捷方法
|
|
158
|
+
* const data = await client.get('/users');
|
|
159
|
+
* ```;
|
|
160
|
+
*
|
|
161
|
+
* @param config - 客户端配置
|
|
162
|
+
* @returns HTTP 客户端实例
|
|
163
|
+
*/
|
|
164
|
+
declare function createTaroHttpClient(config: HttpClientConfig$1): TaroHttpClient;
|
|
165
|
+
//#endregion
|
|
166
|
+
export { type ApiError, type ApiResponse, type AuthStrategy, type CsrfProvider, type HttpAdapter, type HttpClient, type HttpClientConfig, type RequestConfig, type RequestInterceptor, type RequestInterceptorContext, type ResponseInterceptor, type ResponseInterceptorContext, type TaroAdapterConfig, type TaroHttpClient, type TaroTokenStorageConfig, type TokenProvider, createAuthStrategyInterceptor, createAuthTokenInterceptor, createCsrfInterceptor, createLoggingRequestInterceptor, createLoggingResponseInterceptor, createResponseTransformerInterceptor, createRetryInterceptor, createTaroAdapter, createTaroHttpClient, createTaroTokenProvider, createTaroTokenSetter, createValidationInterceptor };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createAuthStrategyInterceptor as e,createAuthTokenInterceptor as t,createCsrfInterceptor as n,createInterceptorExecutor as r,createLoggingRequestInterceptor as i,createLoggingResponseInterceptor as a,createRequestInterceptorsFromConfig as o,createResponseInterceptorsFromConfig as s,createResponseTransformerInterceptor as c,createRetryInterceptor as l,createValidationInterceptor as u,isLegacyConfig as d,warnDeprecatedConfig as f}from"@longzai-intelligence-transport/http-core";function p(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){return e!=null}const h={GET:`GET`,POST:`POST`,PUT:`PUT`,PATCH:`PATCH`,DELETE:`DELETE`,HEAD:`HEAD`,OPTIONS:`OPTIONS`};function g(e){return e in h}function _(e){if(e!=null)return typeof e==`string`||e instanceof ArrayBuffer||p(e)?e:JSON.stringify(e)}function v(e){if(e)return e;let t=globalThis;if(p(t)&&`Taro`in t){let e=t.Taro;if(m(e))return e}throw Error(`Taro 实例未找到,请确保已安装 @tarojs/taro 或通过参数传入 Taro 实例`)}function y(e,t){let n=e?.timeout??3e4,r=v(t);return{async execute(e){let t=e.method.toUpperCase(),i=g(t)?h[t]:`GET`,a={url:e.url,method:i,header:e.headers,timeout:n,data:_(e.body)};if(e.params){let t=Object.entries(e.params).map(([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(String(t))}`).join(`&`);a.url=`${a.url}?${t}`}let o=await r.request(a),s={};if(o.header)for(let[e,t]of Object.entries(o.header))s[e.toLowerCase()]=String(t);return{status:o.statusCode,headers:s,data:o.data}}}}function b(){let e=globalThis;if(p(e)&&`Taro`in e){let t=e.Taro;if(m(t))return t}throw Error(`Taro 未找到,请确保已安装 @tarojs/taro 或通过全局注入 Taro 实例`)}function x(e){let t=e?.tokenKey??`access_token`,n=e?.storageType??`storage`;return{getToken:async()=>{try{let e=b();return n===`asyncStorage`?(await e.getStorage({key:t})).data??null:e.getStorageSync(t)??null}catch{return null}}}}function S(e){let t=e?.tokenKey??`access_token`,n=e?.storageType??`storage`,r=async e=>{let r=b();if(e===null){n===`asyncStorage`?await r.removeStorage({key:t}):r.removeStorageSync(t);return}n===`asyncStorage`?await r.setStorage({key:t,data:e}):r.setStorageSync(t,e)};return{setToken:r,removeToken:async()=>{await r(null)}}}function C(e,t){if(!t)return e;let n=new URLSearchParams;Object.entries(t).forEach(([e,t])=>{t!==void 0&&n.append(e,String(t))});let r=n.toString();return r?`${e}?${r}`:e}function w(e,t,n,r,i){return{url:`${e}${t}`,method:n,headers:i?.headers??{},body:r,config:i,metadata:{}}}function T(e,t,n){let r=e.path;if(t&&Object.entries(t).forEach(([e,t])=>{r=r.replace(`:${e}`,String(t))}),n){let e=new URLSearchParams;Object.entries(n).forEach(([t,n])=>{n!==void 0&&e.append(t,String(n))});let t=e.toString();t&&(r=`${r}?${t}`)}return r}async function E(e,t,n,r,i,a,o){let s=w(t,i,r,a,{...o,headers:{...n,...o?.headers}});return e.execute(s)}function D(e){if(m(e))return e;throw Error(`响应类型不匹配`)}function O(e,t,n){return async(r,i,a)=>D(await E(e,t,n,`GET`,C(r,i),void 0,a))}function k(e,t,n,r){return async(i,a,o)=>D(await E(e,t,n,r,i,a,o))}function A(e,t,n){return async(r,i)=>D(await E(e,t,n,`DELETE`,r,void 0,i))}function j(e,t,n){return{get:O(e,t,n),post:k(e,t,n,`POST`),put:k(e,t,n,`PUT`),patch:k(e,t,n,`PATCH`),delete:A(e,t,n)}}function M(e){let{baseUrl:t,timeout:n=3e4,headers:i,onUnauthorized:a}=e;d(e)&&f(e);let c=o(e),l=s(e),u=y({timeout:n}),p=r({requestInterceptors:c,responseInterceptors:l,onUnauthorized:a},u);return{request:async e=>{let{route:n,params:r,query:a,body:o,config:s}=e,c=T(n,r,a);return D(await E(p,t,i,n.method,c,o,s))},...j(p,t,i)}}export{e as createAuthStrategyInterceptor,t as createAuthTokenInterceptor,n as createCsrfInterceptor,i as createLoggingRequestInterceptor,a as createLoggingResponseInterceptor,c as createResponseTransformerInterceptor,l as createRetryInterceptor,y as createTaroAdapter,M as createTaroHttpClient,x as createTaroTokenProvider,S as createTaroTokenSetter,u as createValidationInterceptor};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-transport/http-adapter-taro",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"adapter",
|
|
6
|
+
"http",
|
|
7
|
+
"miniprogram",
|
|
8
|
+
"taro",
|
|
9
|
+
"utils"
|
|
10
|
+
],
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsgo --build tsconfig/build.json && resolve-aliases -p tsconfig/build.json",
|
|
26
|
+
"build:prod": "NODE_ENV=production tsdown",
|
|
27
|
+
"prepublishOnly": "bun run build:prod",
|
|
28
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
29
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
30
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
31
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
32
|
+
"lint": "oxlint && oxfmt --check",
|
|
33
|
+
"lint:fix": "oxlint --fix && oxfmt",
|
|
34
|
+
"test": "bun test",
|
|
35
|
+
"test:watch": "bun test --watch",
|
|
36
|
+
"test:coverage": "bun test --coverage",
|
|
37
|
+
"clean": "rimraf dist out .cache"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@longzai-intelligence-transport/http-core": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@tarojs/taro": "^4.2.0",
|
|
44
|
+
"zod": "^4.4.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@tarojs/taro": ">=3.6.0",
|
|
48
|
+
"zod": "^4.4.3"
|
|
49
|
+
}
|
|
50
|
+
}
|