@mptool/all 0.6.0-beta.8 → 0.6.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/lib/index.d.mts CHANGED
@@ -1,6 +1,103 @@
1
1
  declare const encode: (arraybuffer: ArrayBuffer) => string;
2
2
  declare const decode: (base64: string) => ArrayBuffer;
3
3
 
4
+ declare const isQQ: boolean;
5
+ declare const isWx: boolean;
6
+ declare const isMp: boolean;
7
+
8
+ type EventType = string | symbol;
9
+ type Handler<T = unknown> = (event: T) => void | Promise<void>;
10
+ type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void | Promise<void>;
11
+ type EventHandlerList<T = unknown> = Array<Handler<T>>;
12
+ type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
13
+ type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
14
+ interface EmitterInstance<Events extends Record<EventType, unknown>> {
15
+ all: EventHandlerMap<Events>;
16
+ on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
17
+ on(type: "*", handler: WildcardHandler<Events>): void;
18
+ off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
19
+ off(type: "*", handler: WildcardHandler<Events>): void;
20
+ emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
21
+ emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
22
+ emitAsync<Key extends keyof Events>(type: Key, event: Events[Key]): Promise<void>;
23
+ emitAsync<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): Promise<void>;
24
+ }
25
+ /**
26
+ * Tiny (~300b) functional event emitter / pubsub.
27
+ * @name emitter
28
+ * @returns Emitter
29
+ */
30
+ declare function Emitter<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): EmitterInstance<Events>;
31
+
32
+ /** 写入普通日志 */
33
+ declare const debug: (...args: any[]) => void;
34
+ /** 写入信息日志 */
35
+ declare const info: (...args: any[]) => void;
36
+ /** 写入警告日志 */
37
+ declare const warn: (...args: any[]) => void;
38
+ /** 写入错误日志 */
39
+ declare const error: (...args: any[]) => void;
40
+ /**
41
+ * 写入过滤信息
42
+ *
43
+ * @param filterMsg 过滤信息
44
+ */
45
+ declare const filter: (filterMsg: string) => void;
46
+
47
+ declare const logger_debug: typeof debug;
48
+ declare const logger_error: typeof error;
49
+ declare const logger_filter: typeof filter;
50
+ declare const logger_info: typeof info;
51
+ declare const logger_warn: typeof warn;
52
+ declare namespace logger {
53
+ export { logger_debug as debug, logger_error as error, logger_filter as filter, logger_info as info, logger_warn as warn };
54
+ }
55
+
56
+ /**
57
+ * 字符串参数解析
58
+ *
59
+ * @param queryString 需要解析的字符串
60
+ * @param [splitter='&'] 分隔符
61
+ *
62
+ * @returns 参数对象
63
+ */
64
+ declare const parse: (queryString?: string, splitter?: string) => Record<string, string>;
65
+ /**
66
+ * query 对象转换字符串
67
+ *
68
+ * @param params query 对象
69
+ * @param [splitter='&] 分隔符
70
+ * @param [unencoded=false] 是否已经解码
71
+ *
72
+ * @returns 解析的字符串
73
+ */
74
+ declare const stringify: (params?: Record<string, string>, splitter?: string, unencoded?: boolean) => string;
75
+ /**
76
+ * URL 添加 query
77
+ *
78
+ * @param path 前部分路径
79
+ * @param queries query对象
80
+ * @param unencoded 是否已经解码,默认为否
81
+ *
82
+ * @returns 处理过的 url
83
+ */
84
+ declare const join: (path: string, queries: Record<string, string>, unencoded?: boolean) => string;
85
+
86
+ declare const query_join: typeof join;
87
+ declare const query_parse: typeof parse;
88
+ declare const query_stringify: typeof stringify;
89
+ declare namespace query {
90
+ export { query_join as join, query_parse as parse, query_stringify as stringify };
91
+ }
92
+
93
+ /**
94
+ * 获取变量类型
95
+ *
96
+ * @param obj 需要辨别的变量
97
+ * @returns 对象的类型
98
+ */
99
+ declare const type: (obj: unknown) => string;
100
+
4
101
  /**
5
102
  * 包装函数,先执行 wrapper ,然后执行原函数
6
103
  *
@@ -111,99 +208,6 @@ declare class Queue {
111
208
  */
112
209
  declare const funcQueue: <A extends unknown[], T = unknown>(fn: (next: () => void, ...args: A) => void, capacity?: number) => (this: T, ...args: A) => void;
113
210
 
114
- type EventType = string | symbol;
115
- type Handler<T = unknown> = (event: T) => void | Promise<void>;
116
- type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void | Promise<void>;
117
- type EventHandlerList<T = unknown> = Array<Handler<T>>;
118
- type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
119
- type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
120
- interface EmitterInstance<Events extends Record<EventType, unknown>> {
121
- all: EventHandlerMap<Events>;
122
- on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
123
- on(type: "*", handler: WildcardHandler<Events>): void;
124
- off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
125
- off(type: "*", handler: WildcardHandler<Events>): void;
126
- emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
127
- emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
128
- emitAsync<Key extends keyof Events>(type: Key, event: Events[Key]): Promise<void>;
129
- emitAsync<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): Promise<void>;
130
- }
131
- /**
132
- * Tiny (~300b) functional event emitter / pubsub.
133
- * @name emitter
134
- * @returns Emitter
135
- */
136
- declare function Emitter<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): EmitterInstance<Events>;
137
-
138
- /** 写入普通日志 */
139
- declare const debug: (...args: any[]) => void;
140
- /** 写入信息日志 */
141
- declare const info: (...args: any[]) => void;
142
- /** 写入警告日志 */
143
- declare const warn: (...args: any[]) => void;
144
- /** 写入错误日志 */
145
- declare const error: (...args: any[]) => void;
146
- /**
147
- * 写入过滤信息
148
- *
149
- * @param filterMsg 过滤信息
150
- */
151
- declare const filter: (filterMsg: string) => void;
152
-
153
- declare const logger_debug: typeof debug;
154
- declare const logger_error: typeof error;
155
- declare const logger_filter: typeof filter;
156
- declare const logger_info: typeof info;
157
- declare const logger_warn: typeof warn;
158
- declare namespace logger {
159
- export { logger_debug as debug, logger_error as error, logger_filter as filter, logger_info as info, logger_warn as warn };
160
- }
161
-
162
- /**
163
- * 字符串参数解析
164
- *
165
- * @param queryString 需要解析的字符串
166
- * @param [splitter='&'] 分隔符
167
- *
168
- * @returns 参数对象
169
- */
170
- declare const parse: (queryString?: string, splitter?: string) => Record<string, string>;
171
- /**
172
- * query 对象转换字符串
173
- *
174
- * @param params query 对象
175
- * @param [splitter='&] 分隔符
176
- * @param [unencoded=false] 是否已经解码
177
- *
178
- * @returns 解析的字符串
179
- */
180
- declare const stringify: (params?: Record<string, string>, splitter?: string, unencoded?: boolean) => string;
181
- /**
182
- * URL 添加 query
183
- *
184
- * @param path 前部分路径
185
- * @param queries query对象
186
- * @param unencoded 是否已经解码,默认为否
187
- *
188
- * @returns 处理过的 url
189
- */
190
- declare const join: (path: string, queries: Record<string, string>, unencoded?: boolean) => string;
191
-
192
- declare const query_join: typeof join;
193
- declare const query_parse: typeof parse;
194
- declare const query_stringify: typeof stringify;
195
- declare namespace query {
196
- export { query_join as join, query_parse as parse, query_stringify as stringify };
197
- }
198
-
199
- /**
200
- * 获取变量类型
201
- *
202
- * @param obj 需要辨别的变量
203
- * @returns 对象的类型
204
- */
205
- declare const type: (obj: unknown) => string;
206
-
207
211
  type Props = Record<string, unknown>;
208
212
  type PropsOptions<Property = Props> = {
209
213
  [K in keyof Property]: PropItem<Property[K]> | null;
@@ -1103,12 +1107,10 @@ declare class Cookie {
1103
1107
  toJSON(): CookieType;
1104
1108
  }
1105
1109
 
1106
- declare const NORMALIZED_HEADERS: unique symbol;
1107
- declare const RAW_HEADER_NAMES: unique symbol;
1108
1110
  type HeadersInit = [string, string][] | Record<string, string> | Headers;
1109
1111
  declare class Headers {
1110
- private [NORMALIZED_HEADERS];
1111
- private [RAW_HEADER_NAMES];
1112
+ private headers;
1113
+ private headerNames;
1112
1114
  constructor(init?: HeadersInit);
1113
1115
  /**
1114
1116
  * Appends a new value onto an existing header inside a `Headers` object, or adds the header if it does not already exist.
@@ -1144,6 +1146,7 @@ declare class Headers {
1144
1146
  keys(): IterableIterator<string>;
1145
1147
  values(): IterableIterator<string>;
1146
1148
  entries(): IterableIterator<[string, string]>;
1149
+ toObject(): Record<string, string>;
1147
1150
  [Symbol.iterator](): IterableIterator<[string, string]>;
1148
1151
  }
1149
1152
 
@@ -1158,11 +1161,11 @@ interface UrlInfo {
1158
1161
  }
1159
1162
  declare const getDomain: (domainOrURL: string) => string;
1160
1163
  declare const parseUrl: (url: string) => UrlInfo;
1161
- type CookieStoreOptions = string | {
1164
+ type CookieOptions = string | {
1162
1165
  domain?: string;
1163
1166
  path?: string;
1164
1167
  };
1165
- declare const getCookieOptions: (options?: CookieStoreOptions) => UrlInfo;
1168
+ declare const getCookieOptions: (options: CookieOptions) => UrlInfo;
1166
1169
  declare const parseCookieHeader: (setCookieHeader: string, domain: string) => Cookie[];
1167
1170
 
1168
1171
  type CookieMap = Map<string, Cookie>;
@@ -1193,7 +1196,7 @@ declare class CookieStore {
1193
1196
  * @param options Cookie 选项
1194
1197
  * @return cookie 对象
1195
1198
  */
1196
- get(name: string, options: CookieStoreOptions): Cookie | null;
1199
+ get(name: string, options: CookieOptions): Cookie | null;
1197
1200
  /**
1198
1201
  * 获取 Cookie 值
1199
1202
  *
@@ -1201,7 +1204,7 @@ declare class CookieStore {
1201
1204
  * @param options Cookie 选项
1202
1205
  * @return Cookie 值
1203
1206
  */
1204
- getValue(name: string, options: CookieStoreOptions): string | undefined;
1207
+ getValue(name: string, options: CookieOptions): string | undefined;
1205
1208
  /**
1206
1209
  * 是否有特定的 Cookie
1207
1210
  *
@@ -1209,7 +1212,7 @@ declare class CookieStore {
1209
1212
  * @param options Cookie 选项
1210
1213
  * @return 是否存在
1211
1214
  */
1212
- has(name: string, options: CookieStoreOptions): boolean;
1215
+ has(name: string, options: CookieOptions): boolean;
1213
1216
  /**
1214
1217
  * 设置 cookie
1215
1218
  */
@@ -1231,7 +1234,7 @@ declare class CookieStore {
1231
1234
  * @param options Cookie 选项
1232
1235
  * @return Cookie 对象数组
1233
1236
  */
1234
- getCookies(options?: CookieStoreOptions): Cookie[];
1237
+ getCookies(options: CookieOptions): Cookie[];
1235
1238
  /**
1236
1239
  * 获取所有 cookies 对象
1237
1240
  *
@@ -1243,7 +1246,7 @@ declare class CookieStore {
1243
1246
  *
1244
1247
  * @return 键值 Map
1245
1248
  */
1246
- getCookiesMap(options: CookieStoreOptions): Record<string, string>;
1249
+ getCookiesMap(options: CookieOptions): Record<string, string>;
1247
1250
  /**
1248
1251
  * 应用 Cookies
1249
1252
  *
@@ -1262,21 +1265,21 @@ declare class CookieStore {
1262
1265
  * @param header 小程序 response header
1263
1266
  * @param domainOrURL Url 或域名
1264
1267
  */
1265
- applyHeader(header: WechatMiniprogram.IAnyObject, domainOrURL: string): void;
1268
+ applyHeader(header: unknown, domainOrURL: string): void;
1266
1269
  /**
1267
1270
  * 应用响应 cookies
1268
1271
  *
1269
1272
  * @param response 小程序 response
1270
1273
  * @param domainOrURL Url 或域名
1271
1274
  */
1272
- applyResponse(response: WechatMiniprogram.RequestSuccessCallbackResult, domainOrURL: string): void;
1275
+ applyResponse(response: unknown, domainOrURL: string): void;
1273
1276
  /**
1274
1277
  * 获取 request cookie header
1275
1278
  *
1276
1279
  * @param options Cookie 选项
1277
1280
  * @return request cookie header
1278
1281
  */
1279
- getHeader(options: CookieStoreOptions): string;
1282
+ getHeader(options: CookieOptions): string;
1280
1283
  /**
1281
1284
  * 从 Storage 读取 cookies
1282
1285
  */
@@ -1407,9 +1410,9 @@ declare class URLSearchParams {
1407
1410
  [Symbol.iterator](): IterableIterator<[string, string]>;
1408
1411
  }
1409
1412
 
1410
- declare const fetchCookieStore: CookieStore;
1411
- type FetchBody = WechatMiniprogram.IAnyObject | ArrayBuffer | URLSearchParams | string | null;
1412
- interface FetchOptions<T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>> extends Omit<WechatMiniprogram.RequestOption<T>, "url" | "method" | "header" | "data"> {
1413
+ declare const requestCookieStore: CookieStore;
1414
+ type RequestBody = WechatMiniprogram.IAnyObject | ArrayBuffer | URLSearchParams | string | null;
1415
+ interface RequestOptions<T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>> extends Omit<WechatMiniprogram.RequestOption<T>, "url" | "method" | "header" | "data"> {
1413
1416
  /**
1414
1417
  * 请求方法
1415
1418
  */
@@ -1421,7 +1424,7 @@ interface FetchOptions<T extends Record<never, never> | unknown[] | string | Arr
1421
1424
  /**
1422
1425
  * 请求主体
1423
1426
  */
1424
- body?: FetchBody;
1427
+ body?: RequestBody;
1425
1428
  /**
1426
1429
  * Cookie 作用域
1427
1430
  */
@@ -1431,7 +1434,7 @@ interface FetchOptions<T extends Record<never, never> | unknown[] | string | Arr
1431
1434
  */
1432
1435
  cookieStore?: CookieStore;
1433
1436
  }
1434
- interface FetchResponse<T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>> {
1437
+ interface RequestResponse<T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>> {
1435
1438
  /** Status Code */
1436
1439
  status: number;
1437
1440
  /** Response headers */
@@ -1439,15 +1442,13 @@ interface FetchResponse<T extends Record<never, never> | unknown[] | string | Ar
1439
1442
  /** Response data */
1440
1443
  data: T;
1441
1444
  }
1442
- type FetchType = <T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>>(url: string, options?: FetchOptions<T>) => Promise<FetchResponse<T>>;
1443
- interface FetchErrorInfo {
1444
- /** 错误信息 */
1445
- errMsg: string;
1445
+ interface RequestError extends Error {
1446
1446
  /** 错误码 */
1447
- errno: number;
1447
+ errno?: number;
1448
1448
  }
1449
- declare const mpFetch: <T extends string | ArrayBuffer | Record<never, never> | unknown[] = Record<string, any>>(url: string, { method, headers, body, cookieScope, cookieStore, ...options }?: FetchOptions<T>) => Promise<FetchResponse<T>>;
1450
- interface FetchInitOptions extends Pick<WechatMiniprogram.RequestOption, "redirect" | "enableCache" | "enableChunked" | "enableHttp2" | "enableHttpDNS" | "enableQuic" | "httpDNSServiceId" | "forceCellularNetwork" | "timeout"> {
1449
+ type RequestType = <T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>>(url: string, options?: RequestOptions<T>) => Promise<RequestResponse<T>>;
1450
+ declare const request: <T extends string | ArrayBuffer | Record<never, never> | unknown[] = Record<string, any>>(url: string, { method, headers, body, cookieScope, cookieStore, ...options }?: RequestOptions<T>) => Promise<RequestResponse<T>>;
1451
+ interface RequestInitOptions extends Pick<WechatMiniprogram.RequestOption, "redirect" | "enableCache" | "enableChunked" | "enableHttp2" | "enableHttpDNS" | "enableQuic" | "httpDNSServiceId" | "forceCellularNetwork" | "timeout"> {
1451
1452
  /**
1452
1453
  * 访问的默认域名
1453
1454
  */
@@ -1459,34 +1460,41 @@ interface FetchInitOptions extends Pick<WechatMiniprogram.RequestOption, "redire
1459
1460
  /**
1460
1461
  * 响应处理器
1461
1462
  *
1462
- * @param response 响应
1463
- * @returns 数据
1464
- * @throws {Error} 自定义的错误数据
1463
+ * @throws {RequestError} 自定义的错误数据
1465
1464
  */
1466
1465
  responseHandler?: <T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>>(
1467
1466
  /** 响应数据 */
1468
- response: FetchResponse<T>,
1467
+ response: RequestResponse<T>,
1469
1468
  /** 请求地址 */
1470
1469
  url: string,
1471
1470
  /** 请求配置 */
1472
- options: FetchOptions<T>) => FetchResponse<T>;
1471
+ options: RequestOptions<T>) => RequestResponse<T>;
1472
+ /**
1473
+ * 错误处理器
1474
+ *
1475
+ * @throws {RequestError} 自定义的错误数据
1476
+ */
1473
1477
  errorHandler?: <T extends Record<never, never> | unknown[] | string | ArrayBuffer = Record<string, any>>(
1474
1478
  /** 错误信息 */
1475
- errInfo: FetchErrorInfo,
1479
+ error: RequestError,
1476
1480
  /** 请求地址 */
1477
1481
  url: string,
1478
1482
  /** 请求配置 */
1479
- options: FetchOptions<T>) => FetchResponse<T> | never;
1483
+ options: RequestOptions<T>) => RequestResponse<T> | never;
1480
1484
  }
1481
- interface FetchFactory {
1485
+ interface RequestFactory {
1486
+ /**
1487
+ * Cookie 存储
1488
+ */
1482
1489
  cookieStore: CookieStore;
1483
- fetch: FetchType;
1490
+ /**
1491
+ * 请求方法
1492
+ */
1493
+ request: RequestType;
1484
1494
  }
1485
1495
  /**
1486
- *
1487
- * @param options fetch 配置选项
1488
- * @returns fetch 函数
1496
+ * @param options request 配置选项
1489
1497
  */
1490
- declare const createMpFetch: ({ cookieStore, server, responseHandler, errorHandler, ...defaultOptions }?: FetchInitOptions) => FetchFactory;
1498
+ declare const createRequest: ({ cookieStore, server, responseHandler, errorHandler, ...defaultOptions }?: RequestInitOptions) => RequestFactory;
1491
1499
 
1492
- export { $App, $Component, $Config, $Page, type AppConfig, type AppConstructor, type AppInstance, type AppOptions, type ComponentConstructor, type ComponentInstance, type ComponentLifetimes, type ComponentOptions, Cookie, type CookieMap, CookieStore, type CookieStoreOptions, type CookieStoreType, type CookieType, Emitter, type EmitterInstance, type EventHandlerList, type EventHandlerMap, type EventType, type ExtendedAppMethods, type ExtendedComponentMethods, type ExtendedComponentProperty, type ExtendsAppOptions, type FetchBody, type FetchErrorInfo, type FetchFactory, type FetchInitOptions, type FetchOptions, type FetchResponse, type FetchType, type Handler, Headers, type HeadersInit, type PageConstructor, type PageInstance, type PageOptions, type PageQuery, type PageState, type PropType, Queue, type RefMap, type SetCookieOptions, type StorageData, type Task, type TrivialComponentInstance, type TrivialComponentOptions, type TrivialPageInstance, type TrivialPageOptions, URLSearchParams, type UrlInfo, type WildCardEventHandlerList, type WildcardHandler, check, checkAsync, createMpFetch, decode as decodeBase64, dirname, userEmitter as emitter, encode as encodeBase64, exists, fetchCookieStore, funcQueue, get, getAsync, getCookieOptions, getCookieScopeDomain, getDomain, isDir, isFile, lock, logger, ls, mkdir, mpFetch, normalizeDomain, once, parseCookieHeader, parseUrl, put, query, readFile, readJSON, remove, removeAsync, rm, saveFile, saveOnlineFile, set, setAsync, storage, take, type, unzip, wrapFunction, writeFile, writeJSON };
1500
+ export { $App, $Component, $Config, $Page, type AppConfig, type AppConstructor, type AppInstance, type AppOptions, type ComponentConstructor, type ComponentInstance, type ComponentLifetimes, type ComponentOptions, Cookie, type CookieMap, type CookieOptions, CookieStore, type CookieStoreType, type CookieType, Emitter, type EmitterInstance, type EventHandlerList, type EventHandlerMap, type EventType, type ExtendedAppMethods, type ExtendedComponentMethods, type ExtendedComponentProperty, type ExtendsAppOptions, type Handler, Headers, type HeadersInit, type PageConstructor, type PageInstance, type PageOptions, type PageQuery, type PageState, type PropType, Queue, type RefMap, type RequestBody, type RequestError, type RequestFactory, type RequestInitOptions, type RequestOptions, type RequestResponse, type RequestType, type SetCookieOptions, type StorageData, type Task, type TrivialComponentInstance, type TrivialComponentOptions, type TrivialPageInstance, type TrivialPageOptions, URLSearchParams, type UrlInfo, type WildCardEventHandlerList, type WildcardHandler, check, checkAsync, createRequest, decode as decodeBase64, dirname, userEmitter as emitter, encode as encodeBase64, exists, funcQueue, get, getAsync, getCookieOptions, getCookieScopeDomain, getDomain, isDir, isFile, isMp, isQQ, isWx, lock, logger, ls, mkdir, normalizeDomain, once, parseCookieHeader, parseUrl, put, query, readFile, readJSON, remove, removeAsync, request, requestCookieStore, rm, saveFile, saveOnlineFile, set, setAsync, storage, take, type, unzip, wrapFunction, writeFile, writeJSON };