@yh-ui/request 0.1.21

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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/dist/adapters/fetch.cjs +157 -0
  4. package/dist/adapters/fetch.d.ts +25 -0
  5. package/dist/adapters/fetch.mjs +148 -0
  6. package/dist/adapters/index.cjs +27 -0
  7. package/dist/adapters/index.d.ts +5 -0
  8. package/dist/adapters/index.mjs +2 -0
  9. package/dist/adapters/platform.cjs +394 -0
  10. package/dist/adapters/platform.d.ts +72 -0
  11. package/dist/adapters/platform.mjs +369 -0
  12. package/dist/cache/index.cjs +56 -0
  13. package/dist/cache/index.d.ts +21 -0
  14. package/dist/cache/index.mjs +14 -0
  15. package/dist/cache/indexedDB.cjs +188 -0
  16. package/dist/cache/indexedDB.d.ts +58 -0
  17. package/dist/cache/indexedDB.mjs +176 -0
  18. package/dist/cache/localStorage.cjs +158 -0
  19. package/dist/cache/localStorage.d.ts +58 -0
  20. package/dist/cache/localStorage.mjs +153 -0
  21. package/dist/cache/memory.cjs +112 -0
  22. package/dist/cache/memory.d.ts +71 -0
  23. package/dist/cache/memory.mjs +103 -0
  24. package/dist/graphql.cjs +255 -0
  25. package/dist/graphql.d.ts +192 -0
  26. package/dist/graphql.mjs +235 -0
  27. package/dist/http-cache.cjs +248 -0
  28. package/dist/http-cache.d.ts +156 -0
  29. package/dist/http-cache.mjs +233 -0
  30. package/dist/index.cjs +181 -0
  31. package/dist/index.d.ts +23 -0
  32. package/dist/index.mjs +16 -0
  33. package/dist/interceptors/debug.cjs +139 -0
  34. package/dist/interceptors/debug.d.ts +92 -0
  35. package/dist/interceptors/debug.mjs +130 -0
  36. package/dist/interceptors/index.cjs +38 -0
  37. package/dist/interceptors/index.d.ts +6 -0
  38. package/dist/interceptors/index.mjs +3 -0
  39. package/dist/interceptors/progress.cjs +185 -0
  40. package/dist/interceptors/progress.d.ts +97 -0
  41. package/dist/interceptors/progress.mjs +177 -0
  42. package/dist/interceptors/security.cjs +154 -0
  43. package/dist/interceptors/security.d.ts +83 -0
  44. package/dist/interceptors/security.mjs +134 -0
  45. package/dist/plugin.cjs +166 -0
  46. package/dist/plugin.d.ts +106 -0
  47. package/dist/plugin.mjs +163 -0
  48. package/dist/request.cjs +396 -0
  49. package/dist/request.d.ts +111 -0
  50. package/dist/request.mjs +339 -0
  51. package/dist/types.cjs +13 -0
  52. package/dist/types.d.ts +157 -0
  53. package/dist/types.mjs +7 -0
  54. package/dist/useAIStream.cjs +125 -0
  55. package/dist/useAIStream.d.ts +89 -0
  56. package/dist/useAIStream.mjs +108 -0
  57. package/dist/useLoadMore.cjs +136 -0
  58. package/dist/useLoadMore.d.ts +84 -0
  59. package/dist/useLoadMore.mjs +134 -0
  60. package/dist/usePagination.cjs +141 -0
  61. package/dist/usePagination.d.ts +89 -0
  62. package/dist/usePagination.mjs +132 -0
  63. package/dist/useQueue.cjs +243 -0
  64. package/dist/useQueue.d.ts +118 -0
  65. package/dist/useQueue.mjs +239 -0
  66. package/dist/useRequest.cjs +325 -0
  67. package/dist/useRequest.d.ts +126 -0
  68. package/dist/useRequest.mjs +329 -0
  69. package/dist/useRequestQueue.cjs +36 -0
  70. package/dist/useRequestQueue.d.ts +52 -0
  71. package/dist/useRequestQueue.mjs +27 -0
  72. package/dist/useSSE.cjs +241 -0
  73. package/dist/useSSE.d.ts +74 -0
  74. package/dist/useSSE.mjs +226 -0
  75. package/dist/websocket.cjs +325 -0
  76. package/dist/websocket.d.ts +163 -0
  77. package/dist/websocket.mjs +316 -0
  78. package/package.json +61 -0
@@ -0,0 +1,71 @@
1
+ /**
2
+ * 内存缓存实现
3
+ * 默认缓存策略
4
+ */
5
+ export interface CacheOptions {
6
+ /** 缓存过期时间 (ms),默认 5 分钟 */
7
+ staleTime?: number;
8
+ /** 缓存最大存活时间 (ms),默认 10 分钟 */
9
+ cacheTime?: number;
10
+ /** 是否启用缓存 */
11
+ enabled?: boolean;
12
+ }
13
+ export interface CacheItem<T> {
14
+ data: T;
15
+ expireTime: number;
16
+ createTime: number;
17
+ /** 缓存标签 */
18
+ tags?: string[];
19
+ /** ETag */
20
+ etag?: string;
21
+ /** Last-Modified */
22
+ lastModified?: string;
23
+ }
24
+ /**
25
+ * 内存缓存类
26
+ */
27
+ export declare class MemoryCache {
28
+ private cache;
29
+ private cleanupTimer;
30
+ /**
31
+ * 获取缓存
32
+ */
33
+ get<T>(key: string, _staleTime?: number): T | undefined;
34
+ /**
35
+ * 设置缓存
36
+ */
37
+ set<T>(key: string, data: T, options?: CacheOptions): void;
38
+ /**
39
+ * 删除缓存
40
+ */
41
+ delete(key: string): boolean;
42
+ /**
43
+ * 清空缓存
44
+ */
45
+ clear(): void;
46
+ /**
47
+ * 检查缓存是否存在
48
+ */
49
+ has(key: string): boolean;
50
+ /**
51
+ * 通过标签清除缓存
52
+ */
53
+ deleteByTags(_tags: string[]): void;
54
+ /**
55
+ * 获取缓存大小
56
+ */
57
+ size(): number;
58
+ /**
59
+ * 批量获取缓存 keys
60
+ */
61
+ keys(): string[];
62
+ /**
63
+ * 启动定时清理
64
+ */
65
+ startCleanup(interval?: number): void;
66
+ /**
67
+ * 停止定时清理
68
+ */
69
+ stopCleanup(): void;
70
+ }
71
+ export declare const memoryCache: MemoryCache;
@@ -0,0 +1,103 @@
1
+ export class MemoryCache {
2
+ cache = /* @__PURE__ */ new Map();
3
+ cleanupTimer = null;
4
+ /**
5
+ * 获取缓存
6
+ */
7
+ get(key, _staleTime) {
8
+ const item = this.cache.get(key);
9
+ if (!item) return void 0;
10
+ const now = Date.now();
11
+ if (item.expireTime && now > item.expireTime) {
12
+ this.cache.delete(key);
13
+ return void 0;
14
+ }
15
+ return item.data;
16
+ }
17
+ /**
18
+ * 设置缓存
19
+ */
20
+ set(key, data, options = {}) {
21
+ const { staleTime = 5 * 60 * 1e3, cacheTime = 10 * 60 * 1e3 } = options;
22
+ const now = Date.now();
23
+ this.cache.set(key, {
24
+ data,
25
+ expireTime: now + staleTime,
26
+ createTime: now
27
+ });
28
+ setTimeout(() => {
29
+ const item = this.cache.get(key);
30
+ if (item && item.expireTime <= Date.now()) {
31
+ this.cache.delete(key);
32
+ }
33
+ }, cacheTime);
34
+ }
35
+ /**
36
+ * 删除缓存
37
+ */
38
+ delete(key) {
39
+ return this.cache.delete(key);
40
+ }
41
+ /**
42
+ * 清空缓存
43
+ */
44
+ clear() {
45
+ this.cache.clear();
46
+ }
47
+ /**
48
+ * 检查缓存是否存在
49
+ */
50
+ has(key) {
51
+ const item = this.cache.get(key);
52
+ if (!item) return false;
53
+ if (item.expireTime && Date.now() > item.expireTime) {
54
+ this.cache.delete(key);
55
+ return false;
56
+ }
57
+ return true;
58
+ }
59
+ /**
60
+ * 通过标签清除缓存
61
+ */
62
+ deleteByTags(_tags) {
63
+ }
64
+ /**
65
+ * 获取缓存大小
66
+ */
67
+ size() {
68
+ return this.cache.size;
69
+ }
70
+ /**
71
+ * 批量获取缓存 keys
72
+ */
73
+ keys() {
74
+ return Array.from(this.cache.keys());
75
+ }
76
+ /**
77
+ * 启动定时清理
78
+ */
79
+ startCleanup(interval = 60 * 1e3) {
80
+ if (this.cleanupTimer) return;
81
+ this.cleanupTimer = setInterval(() => {
82
+ const now = Date.now();
83
+ for (const [key, item] of this.cache.entries()) {
84
+ if (item.expireTime && now > item.expireTime) {
85
+ this.cache.delete(key);
86
+ }
87
+ }
88
+ }, interval);
89
+ }
90
+ /**
91
+ * 停止定时清理
92
+ */
93
+ stopCleanup() {
94
+ if (this.cleanupTimer) {
95
+ clearInterval(this.cleanupTimer);
96
+ this.cleanupTimer = null;
97
+ }
98
+ }
99
+ }
100
+ export const memoryCache = new MemoryCache();
101
+ if (typeof window !== "undefined") {
102
+ memoryCache.startCleanup();
103
+ }
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.GraphQLClient = exports.GraphQLBuilder = void 0;
7
+ exports.createGraphQLBuilder = createGraphQLBuilder;
8
+ exports.createGraphQLClient = createGraphQLClient;
9
+ exports.createPaginatedQuery = createPaginatedQuery;
10
+ exports.gql = gql;
11
+ exports.parseGraphQLResponse = parseGraphQLResponse;
12
+ class GraphQLBuilder {
13
+ operationType = "query";
14
+ operationName = "";
15
+ variables = {};
16
+ fields = [];
17
+ fragments = [];
18
+ variablesDefinitions = [];
19
+ /**
20
+ * 设置操作类型
21
+ */
22
+ operation(type) {
23
+ this.operationType = type;
24
+ return this;
25
+ }
26
+ /**
27
+ * 设置操作名称
28
+ */
29
+ name(name) {
30
+ this.operationName = name;
31
+ return this;
32
+ }
33
+ /**
34
+ * 添加变量定义
35
+ */
36
+ variable(name, type, defaultValue) {
37
+ const defaultStr = defaultValue !== void 0 ? ` = ${JSON.stringify(defaultValue)}` : "";
38
+ this.variablesDefinitions.push(`$${name}: ${type}${defaultStr}`);
39
+ return this;
40
+ }
41
+ /**
42
+ * 设置变量
43
+ */
44
+ variables_(vars) {
45
+ this.variables = vars;
46
+ return this;
47
+ }
48
+ /**
49
+ * 添加字段
50
+ */
51
+ field(field) {
52
+ this.fields.push(field);
53
+ return this;
54
+ }
55
+ /**
56
+ * 添加多个字段
57
+ */
58
+ addFields(fieldsInput) {
59
+ this.fields.push(...fieldsInput);
60
+ return this;
61
+ }
62
+ /**
63
+ * 添加内联片段
64
+ */
65
+ inlineFragment(type, fieldsInput) {
66
+ this.fields.push(`... on ${type} { ${fieldsInput.join(" ")} }`);
67
+ return this;
68
+ }
69
+ /**
70
+ * 添加片段
71
+ */
72
+ fragment(name) {
73
+ this.fragments.push(`...${name}`);
74
+ return this;
75
+ }
76
+ /**
77
+ * 构建 GraphQL 查询字符串
78
+ */
79
+ build() {
80
+ const parts = [];
81
+ const operationHeader = this.operationName ? `${this.operationType} ${this.operationName}` : this.operationType;
82
+ const variablesStr = this.variablesDefinitions.length > 0 ? `(${this.variablesDefinitions.join(", ")})` : "";
83
+ const fieldsStr = this.fields.join("\n");
84
+ parts.push(`${operationHeader}${variablesStr} {
85
+ ${fieldsStr}
86
+ }`);
87
+ return parts.join("\n");
88
+ }
89
+ /**
90
+ * 转换为请求配置
91
+ */
92
+ toRequestOptions(options = {}) {
93
+ return {
94
+ ...options,
95
+ method: "POST",
96
+ headers: {
97
+ "Content-Type": "application/json",
98
+ ...options.headers
99
+ },
100
+ data: {
101
+ query: this.build(),
102
+ variables: Object.keys(this.variables).length > 0 ? this.variables : void 0,
103
+ operationName: this.operationName || void 0
104
+ }
105
+ };
106
+ }
107
+ /**
108
+ * 重置构建器
109
+ */
110
+ reset() {
111
+ this.operationType = "query";
112
+ this.operationName = "";
113
+ this.variables = {};
114
+ this.fields = [];
115
+ this.fragments = [];
116
+ this.variablesDefinitions = [];
117
+ }
118
+ }
119
+ exports.GraphQLBuilder = GraphQLBuilder;
120
+ class GraphQLClient {
121
+ endpoint;
122
+ defaultHeaders = {};
123
+ defaultOptions = {};
124
+ /**
125
+ * 创建 GraphQL 客户端
126
+ */
127
+ constructor(endpoint, options = {}) {
128
+ this.endpoint = endpoint;
129
+ this.defaultHeaders = options.headers || {};
130
+ this.defaultOptions = {
131
+ credentials: options.credentials || "same-origin"
132
+ };
133
+ }
134
+ /**
135
+ * 发送查询
136
+ */
137
+ async query(query, variables, options = {}) {
138
+ const queryStr = typeof query === "string" ? query : query.build();
139
+ return this.request({
140
+ ...this.defaultOptions,
141
+ ...options,
142
+ method: "POST",
143
+ url: this.endpoint,
144
+ headers: {
145
+ ...this.defaultHeaders,
146
+ "Content-Type": "application/json",
147
+ ...options.headers
148
+ },
149
+ data: {
150
+ query: queryStr,
151
+ variables,
152
+ operationName: typeof query !== "string" ? query["operationName"] : void 0
153
+ }
154
+ });
155
+ }
156
+ /**
157
+ * 发送 mutation
158
+ */
159
+ async mutate(mutation, variables, options = {}) {
160
+ return this.query(mutation, variables, options);
161
+ }
162
+ /**
163
+ * 发送原始请求
164
+ */
165
+ async request(options) {
166
+ const {
167
+ request
168
+ } = await Promise.resolve().then(() => require("./request.cjs"));
169
+ return request.request({
170
+ ...options,
171
+ url: options.url || this.endpoint
172
+ });
173
+ }
174
+ /**
175
+ * 创建查询构建器
176
+ */
177
+ createQuery() {
178
+ return new GraphQLBuilder().operation("query");
179
+ }
180
+ /**
181
+ * 创建 Mutation 构建器
182
+ */
183
+ createMutation() {
184
+ return new GraphQLBuilder().operation("mutation");
185
+ }
186
+ /**
187
+ * 设置默认请求头
188
+ */
189
+ setHeader(key, value) {
190
+ this.defaultHeaders[key] = value;
191
+ }
192
+ /**
193
+ * 设置认证 Token
194
+ */
195
+ setAuthToken(token) {
196
+ this.defaultHeaders["Authorization"] = `Bearer ${token}`;
197
+ }
198
+ }
199
+ exports.GraphQLClient = GraphQLClient;
200
+ function createGraphQLBuilder() {
201
+ return new GraphQLBuilder();
202
+ }
203
+ function createGraphQLClient(endpoint, options) {
204
+ return new GraphQLClient(endpoint, options);
205
+ }
206
+ function gql(strings, ...values) {
207
+ let result = "";
208
+ strings.forEach((str, i) => {
209
+ result += str;
210
+ if (i < values.length) {
211
+ const value = values[i];
212
+ if (typeof value === "string") {
213
+ result += value;
214
+ } else if (value !== void 0) {
215
+ result += JSON.stringify(value);
216
+ }
217
+ }
218
+ });
219
+ return result;
220
+ }
221
+ function createPaginatedQuery(queryName, itemType, pageSize = 10) {
222
+ const query = `
223
+ query ${queryName}($first: Int!, $after: String) {
224
+ ${queryName}(first: $first, after: $after) {
225
+ pageInfo {
226
+ hasNextPage
227
+ endCursor
228
+ }
229
+ edges {
230
+ node {
231
+ ${itemType}
232
+ }
233
+ }
234
+ }
235
+ }
236
+ `;
237
+ const getVariables = cursor => ({
238
+ first: pageSize,
239
+ ...(cursor ? {
240
+ after: cursor
241
+ } : {})
242
+ });
243
+ return {
244
+ query,
245
+ getVariables
246
+ };
247
+ }
248
+ function parseGraphQLResponse(response) {
249
+ if (response.errors && response.errors.length > 0) {
250
+ const error = new Error(response.errors.map(e => e.message).join(", "));
251
+ error.code = "GRAPHQL_ERROR";
252
+ throw error;
253
+ }
254
+ return response.data;
255
+ }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * GraphQL 支持
3
+ * 提供 GraphQL 查询构建器和请求处理
4
+ */
5
+ import type { RequestOptions, RequestResponse } from './types';
6
+ /** GraphQL 操作类型 */
7
+ export type GraphQLOperationType = 'query' | 'mutation' | 'subscription';
8
+ /** GraphQL 变量 */
9
+ export type GraphQLVariable = string | number | boolean | null | GraphQLVariable[] | {
10
+ [key: string]: GraphQLVariable;
11
+ };
12
+ /** GraphQL 请求配置 */
13
+ export interface GraphQLRequestOptions extends RequestOptions {
14
+ /** GraphQL 操作名称 */
15
+ operationName?: string;
16
+ /** 是否返回原始响应 */
17
+ rawResponse?: boolean;
18
+ }
19
+ /** GraphQL 错误 */
20
+ export interface GraphQLError {
21
+ message: string;
22
+ locations?: Array<{
23
+ line: number;
24
+ column: number;
25
+ }>;
26
+ path?: Array<string | number>;
27
+ extensions?: Record<string, unknown>;
28
+ }
29
+ /** GraphQL 响应 */
30
+ export interface GraphQLResponse<T = unknown> {
31
+ data?: T;
32
+ errors?: GraphQLError[];
33
+ extensions?: Record<string, unknown>;
34
+ }
35
+ /**
36
+ * GraphQL 查询构建器
37
+ *
38
+ * @example
39
+ * const query = gql`
40
+ * query GetUser($id: ID!) {
41
+ * user(id: $id) {
42
+ * id
43
+ * name
44
+ * email
45
+ * }
46
+ * }
47
+ * `
48
+ */
49
+ export declare class GraphQLBuilder {
50
+ private operationType;
51
+ private operationName;
52
+ private variables;
53
+ private fields;
54
+ private fragments;
55
+ private variablesDefinitions;
56
+ /**
57
+ * 设置操作类型
58
+ */
59
+ operation(type: GraphQLOperationType): this;
60
+ /**
61
+ * 设置操作名称
62
+ */
63
+ name(name: string): this;
64
+ /**
65
+ * 添加变量定义
66
+ */
67
+ variable(name: string, type: string, defaultValue?: GraphQLVariable): this;
68
+ /**
69
+ * 设置变量
70
+ */
71
+ variables_(vars: Record<string, GraphQLVariable>): this;
72
+ /**
73
+ * 添加字段
74
+ */
75
+ field(field: string): this;
76
+ /**
77
+ * 添加多个字段
78
+ */
79
+ addFields(fieldsInput: string[]): this;
80
+ /**
81
+ * 添加内联片段
82
+ */
83
+ inlineFragment(type: string, fieldsInput: string[]): this;
84
+ /**
85
+ * 添加片段
86
+ */
87
+ fragment(name: string): this;
88
+ /**
89
+ * 构建 GraphQL 查询字符串
90
+ */
91
+ build(): string;
92
+ /**
93
+ * 转换为请求配置
94
+ */
95
+ toRequestOptions(options?: GraphQLRequestOptions): GraphQLRequestOptions & {
96
+ data: {
97
+ query: string;
98
+ variables?: Record<string, GraphQLVariable>;
99
+ operationName?: string;
100
+ };
101
+ };
102
+ /**
103
+ * 重置构建器
104
+ */
105
+ reset(): void;
106
+ }
107
+ /**
108
+ * GraphQL 客户端
109
+ * 简化 GraphQL 请求的发送
110
+ */
111
+ export declare class GraphQLClient {
112
+ private endpoint;
113
+ private defaultHeaders;
114
+ private defaultOptions;
115
+ /**
116
+ * 创建 GraphQL 客户端
117
+ */
118
+ constructor(endpoint: string, options?: {
119
+ headers?: Record<string, string>;
120
+ credentials?: RequestCredentials;
121
+ });
122
+ /**
123
+ * 发送查询
124
+ */
125
+ query<T = unknown>(query: string | GraphQLBuilder, variables?: Record<string, GraphQLVariable>, options?: GraphQLRequestOptions): Promise<RequestResponse<GraphQLResponse<T>>>;
126
+ /**
127
+ * 发送 mutation
128
+ */
129
+ mutate<T = unknown>(mutation: string | GraphQLBuilder, variables?: Record<string, GraphQLVariable>, options?: GraphQLRequestOptions): Promise<RequestResponse<GraphQLResponse<T>>>;
130
+ /**
131
+ * 发送原始请求
132
+ */
133
+ request<T>(options: GraphQLRequestOptions & {
134
+ url?: string;
135
+ data?: unknown;
136
+ }): Promise<RequestResponse<T>>;
137
+ /**
138
+ * 创建查询构建器
139
+ */
140
+ createQuery(): GraphQLBuilder;
141
+ /**
142
+ * 创建 Mutation 构建器
143
+ */
144
+ createMutation(): GraphQLBuilder;
145
+ /**
146
+ * 设置默认请求头
147
+ */
148
+ setHeader(key: string, value: string): void;
149
+ /**
150
+ * 设置认证 Token
151
+ */
152
+ setAuthToken(token: string): void;
153
+ }
154
+ /**
155
+ * 创建 GraphQL 查询构建器
156
+ */
157
+ export declare function createGraphQLBuilder(): GraphQLBuilder;
158
+ /**
159
+ * 创建 GraphQL 客户端
160
+ */
161
+ export declare function createGraphQLClient(endpoint: string, options?: {
162
+ headers?: Record<string, string>;
163
+ credentials?: RequestCredentials;
164
+ }): GraphQLClient;
165
+ /**
166
+ * GraphQL 模板标签函数
167
+ * 用于更方便地编写 GraphQL 查询
168
+ *
169
+ * @example
170
+ * const query = gql`
171
+ * query GetUser($id: ID!) {
172
+ * user(id: $id) {
173
+ * id
174
+ * name
175
+ * }
176
+ * }
177
+ * `
178
+ */
179
+ export declare function gql(strings: TemplateStringsArray, ...values: unknown[]): string;
180
+ /**
181
+ * 创建分页 GraphQL 查询
182
+ * @returns 包含 query 字符串和 getVariables 函数的分页查询配置
183
+ */
184
+ export declare function createPaginatedQuery(queryName: string, itemType: string, pageSize?: number): {
185
+ query: string;
186
+ getVariables: (cursor?: string) => Record<string, GraphQLVariable>;
187
+ };
188
+ /**
189
+ * 解析 GraphQL 响应
190
+ * @returns 返回 data 字段,类型与 GraphQLResponse 的泛型参数一致
191
+ */
192
+ export declare function parseGraphQLResponse<T>(response: GraphQLResponse<T>): typeof response.data;