@snail-js/api 0.1.5 → 0.1.7

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/README.md CHANGED
@@ -378,23 +378,116 @@ export const Sse = Service.createSse(ServerSend);
378
378
  ```
379
379
 
380
380
  ### 服务端推送装饰器`@Sse`
381
+
381
382
  - `@Sse(path:string,options:{withCredentials: boolean})`
382
383
  - 创建一个服务端推送连接,当被装饰的方法调用时打开连接
383
384
  - 被装饰的方法调用后会返回`{eventSource:EventSource,close:function}`
384
- - eventSource: sse连接实例
385
- - close: 关闭此sse连接的方法
385
+ - eventSource: sse 连接实例
386
+ - close: 关闭此 sse 连接的方法
386
387
 
387
388
  ### 注册`onopen`装饰器`@OnSseOpen`
389
+
388
390
  - 当`@Sse`装饰的方法被调用时,将`@OnSseOpen`装饰的方法注册为`@Sse`装饰的方法返回的`EventSource`实例`onopen`处理函数
389
391
 
390
392
  ### 注册`onerror`装饰器`@OnSseError`
393
+
391
394
  - 当`@Sse`装饰的方法被调用时,将`@OnSseError`装饰的方法注册为`@Sse`装饰的方法返回的`EventSource`实例`onerror`处理函数
392
395
 
393
396
  ### 事件处理装饰器`@SseEvent`
397
+
394
398
  - `@SseEvent(eventName?:string)`
395
399
  - 未传入`eventName`,默认注册为`message`事件处理器
396
400
  - 传入`eventName`,注册为对应名称的事件处理器
397
401
 
402
+ ---
403
+
404
+ ## TypeScript 支持
405
+
406
+ ### 默认返回类型
407
+
408
+ ```typescript
409
+ export interface ResponseData<T = any> {
410
+ code: 0;
411
+ message: string;
412
+ data: T;
413
+ }
414
+ ```
415
+
416
+ ### 定义返回类型
417
+
418
+ 1. 定义后端标准数据格式
419
+
420
+ ```typescript
421
+ export class CustomResponse {
422
+ status_code: number;
423
+ msg: string;
424
+ }
425
+ ```
426
+
427
+ 2. 创建时应用格式
428
+
429
+ ```typescript
430
+ // service.ts
431
+ import { Snail, Server } from "@snail-js/api";
432
+
433
+ @Server({
434
+ baseURL: "/api",
435
+ timeout: 5000,
436
+ })
437
+ class BackEnd extends Snail<CustomResponse> {}
438
+
439
+ export const Service = new BackEnd();
440
+ ```
441
+
442
+ 3. 调用 API 时携带数据格式
443
+
444
+ ```typescript
445
+ import { userApi } from "./user";
446
+
447
+ class User {
448
+ id: number;
449
+ name: string;
450
+ tel: string;
451
+ age: number;
452
+ }
453
+
454
+ const res = await userApi.get<User>("1");
455
+ // 默认情况,以data为key存储数据
456
+ // res.data => CustomResponse & { data : User}
457
+ ```
458
+
459
+ > API 被调用的返回格式
460
+
461
+ ```typescript
462
+ const res = await userApi.get<User>("1");
463
+
464
+ // res type
465
+ {
466
+ data: T;
467
+ error: null | Error
468
+ hitCache?: boolean
469
+ }
470
+ ```
471
+
472
+ > `data: T`,后端响应数据;默认为`ResponseData<T = any>`类型;可由用户自定义修改
473
+
474
+ > `error: null | Error` ; 请求过程中的错误包含在此,可先判断 error 是否为 null 再进行数据处理
475
+
476
+ > `hitCache?: boolean`; 标识请求是否击中缓存;若从缓存获得数据,则不会发送请求。
477
+
478
+ 4. 若后端返回数据不是以 data 为 key 包含数据
479
+
480
+ ```typescript
481
+ @Server({
482
+ baseURL: "/api",
483
+ timeout: 5000,
484
+ })
485
+ class BackEnd extends Snail<CustomResponse, "record"> {}
486
+
487
+ const res = await userApi.get<User>("1");
488
+ // 自定义数据key
489
+ // res.data => CustomResponse & { record : User}
490
+ ```
398
491
 
399
492
  ### 代码仓库
400
493
 
@@ -1,8 +1,6 @@
1
1
  import "reflect-metadata";
2
2
  import { Strategy, ApiProxy, SseProxy, ResponseData } from "../typings";
3
- export declare class Snail<R extends {
4
- data: any;
5
- } = ResponseData> {
3
+ export declare class Snail<R extends {} = ResponseData, DK extends string = "data"> {
6
4
  private axiosInstance;
7
5
  private strategies;
8
6
  private cacheStorage?;
@@ -10,7 +8,7 @@ export declare class Snail<R extends {
10
8
  private sourceMap;
11
9
  private eventSource;
12
10
  registerStrategy(strategy: Strategy): void;
13
- createApi<T extends object>(constructor: new () => T): ApiProxy<T, R>;
11
+ createApi<T extends object>(constructor: new () => T): ApiProxy<T, R, DK>;
14
12
  private buildRequestArgs;
15
13
  private getStrategies;
16
14
  private applyStrategies;
package/dist/snail-api.js CHANGED
@@ -3944,9 +3944,17 @@ class Snail {
3944
3944
  onUploadProgress,
3945
3945
  onDownloadProgress
3946
3946
  });
3947
+ const responseStrategies = strategies.filter(
3948
+ (strategy) => strategy.applyResponse
3949
+ );
3950
+ const strategyResponse = await this.applyStrategies(
3951
+ response,
3952
+ responseStrategies,
3953
+ "response"
3954
+ );
3947
3955
  hitSource !== null && this.setCache(request, response);
3948
3956
  this.expireCache(propertyKey);
3949
- return this.handleResponse(response, false);
3957
+ return this.handleResponse(strategyResponse, false);
3950
3958
  } catch (error) {
3951
3959
  return this.handleError(error);
3952
3960
  }
@@ -4024,16 +4032,9 @@ class Snail {
4024
4032
  statusText: "get response data from cache",
4025
4033
  config: { headers: new AxiosHeaders2() }
4026
4034
  };
4027
- const responseStrategies = strategies.filter(
4028
- (strategy) => strategy.applyResponse
4029
- );
4030
- const strategyResponse = await this.applyStrategies(
4031
- cacheResponse,
4032
- responseStrategies,
4033
- "response"
4034
- );
4035
- return strategyResponse;
4035
+ return cacheResponse;
4036
4036
  }
4037
+ return void 0;
4037
4038
  }
4038
4039
  initCacheManage(options) {
4039
4040
  if (!this.cacheStorage && options !== void 0) {
@@ -3948,9 +3948,17 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3948
3948
  onUploadProgress,
3949
3949
  onDownloadProgress
3950
3950
  });
3951
+ const responseStrategies = strategies.filter(
3952
+ (strategy) => strategy.applyResponse
3953
+ );
3954
+ const strategyResponse = await this.applyStrategies(
3955
+ response,
3956
+ responseStrategies,
3957
+ "response"
3958
+ );
3951
3959
  hitSource !== null && this.setCache(request, response);
3952
3960
  this.expireCache(propertyKey);
3953
- return this.handleResponse(response, false);
3961
+ return this.handleResponse(strategyResponse, false);
3954
3962
  } catch (error) {
3955
3963
  return this.handleError(error);
3956
3964
  }
@@ -4028,16 +4036,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4028
4036
  statusText: "get response data from cache",
4029
4037
  config: { headers: new AxiosHeaders() }
4030
4038
  };
4031
- const responseStrategies = strategies.filter(
4032
- (strategy) => strategy.applyResponse
4033
- );
4034
- const strategyResponse = await this.applyStrategies(
4035
- cacheResponse,
4036
- responseStrategies,
4037
- "response"
4038
- );
4039
- return strategyResponse;
4039
+ return cacheResponse;
4040
4040
  }
4041
+ return void 0;
4041
4042
  }
4042
4043
  initCacheManage(options) {
4043
4044
  if (!this.cacheStorage && options !== void 0) {
@@ -1,11 +1,11 @@
1
+ import { ResponseData } from "./response.data";
1
2
  export type ApiResponse<T> = Promise<{
2
3
  data: T | null;
3
4
  error: Error | null;
5
+ hitCache?: boolean;
4
6
  }>;
5
- export type ApiProxy<T, R extends {
6
- data: any;
7
- }> = {
7
+ export type ApiProxy<T, R extends {} = ResponseData, DK extends string = "data"> = {
8
8
  [K in keyof T]: T[K] extends (...args: infer A) => any ? <D = any>(...args: A) => ApiResponse<R & {
9
- data: D;
9
+ [KK in DK]: D;
10
10
  }> : T[K];
11
11
  };
@@ -1,17 +1,11 @@
1
- export interface ResponseErrorData<E> {
2
- code: number;
3
- message: string;
4
- data: E;
5
- }
6
1
  export interface PaginationData<T> {
7
2
  total: number;
8
3
  page: number;
9
4
  size: number;
10
5
  record: T[];
11
6
  }
12
- export interface ResponseSuccessData<T> {
7
+ export interface ResponseData<T = any> {
13
8
  code: 0;
14
9
  message: string;
15
10
  data: T | PaginationData<T>;
16
11
  }
17
- export type ResponseData<T = any, E = any> = ResponseSuccessData<T> | ResponseErrorData<E>;
@@ -1,6 +1,6 @@
1
1
  export type SseProxy<T extends object> = {
2
2
  [K in keyof T]: T[K] extends (...args: infer A) => any ? () => {
3
- sourceEvent: EventSource;
3
+ eventSource: EventSource;
4
4
  close: Function;
5
5
  } : T[K];
6
6
  };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@snail-js/api",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Http Request with Decorators Api, build on axios",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "exports": {
8
8
  ".": {
9
- "import": "./dist/snail-api.mjs",
9
+ "import": "./dist/snail-api.js",
10
10
  "require": "./dist/snail-api.umd.cjs",
11
11
  "types": "./dist/index.d.ts"
12
12
  }