@snail-js/api 0.1.0 → 0.1.2

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.
@@ -0,0 +1,4 @@
1
+ import { Strategy } from "../typings";
2
+ export declare class JwtStrategy implements Strategy {
3
+ applyRequest(request: any): Promise<any>;
4
+ }
@@ -1,28 +1,9 @@
1
- import { RequestBody } from "./request.body";
2
- import { Api } from "../core/api";
3
- import { ResponseErrorData, ResponseSuccessData, PaginationData } from "./response.data";
4
- export interface PipeResult {
5
- data: RequestBody | undefined;
6
- headers: Record<string, string>;
7
- }
8
- export type RequestPipe = (data: RequestBody | undefined, headers: Record<string, string>) => PipeResult;
9
- export interface ApiConfig<R = any> {
10
- name?: string;
1
+ import { RequestMethod } from "./request.method";
2
+ export interface ApiConfig {
11
3
  timeout?: number;
12
4
  version?: string;
13
- transform?: (data: R | PaginationData<R>) => R | PaginationData<R>;
14
- headers?: Record<string, string>;
15
- params?: Record<string, string>;
16
- hitSource?: string | Api;
17
- }
18
- export interface SendOptions {
19
- params?: Record<string, string>;
20
- data?: RequestBody;
21
- version?: string;
22
5
  }
23
- export interface ApiResponse<T, E> {
24
- error: ResponseErrorData<E> | null;
25
- data: ResponseSuccessData<T> | null;
26
- hitCache: boolean;
27
- Catch: (error: any) => any;
6
+ export interface MethodOption {
7
+ method: RequestMethod;
8
+ path: string;
28
9
  }
@@ -0,0 +1,11 @@
1
+ export type ApiResponse<T> = Promise<{
2
+ data: T | null;
3
+ error: Error | null;
4
+ }>;
5
+ export type ApiProxy<T, R extends {
6
+ data: any;
7
+ }> = {
8
+ [K in keyof T]: T[K] extends (...args: infer A) => any ? <D = any, E = any>(...args: A) => ApiResponse<R & {
9
+ data: D;
10
+ }> : T[K];
11
+ };
@@ -0,0 +1,19 @@
1
+ export declare enum CacheType {
2
+ Memory = 0,
3
+ IndexDB = 1,
4
+ LocalStorage = 2
5
+ }
6
+ interface MemoryCacheOption {
7
+ type: CacheType.Memory;
8
+ }
9
+ interface IndexDBCacheOption {
10
+ type: CacheType.IndexDB;
11
+ }
12
+ interface LocalStorageCacheOption {
13
+ type: CacheType.LocalStorage;
14
+ }
15
+ interface CacheCommonOption {
16
+ ttl?: number;
17
+ }
18
+ export type CacheManagementOption = CacheCommonOption & (MemoryCacheOption | IndexDBCacheOption | LocalStorageCacheOption);
19
+ export {};
@@ -1,8 +1,10 @@
1
1
  export * from "./request.body";
2
2
  export * from "./request.method";
3
- export * from "./snail.config";
3
+ export * from "./snail.option";
4
4
  export * from "./api.config";
5
- export * from "./versioning.config";
6
- export * from "./cache.management.config";
5
+ export * from "./versioning.option";
6
+ export * from "./cache.management.option";
7
7
  export * from "./cache.type";
8
8
  export * from "./response.data";
9
+ export * from "./strategy";
10
+ export * from "./apiProxy";
@@ -1,4 +1,4 @@
1
- export interface ResponseErrorData<E = any> {
1
+ export interface ResponseErrorData<E> {
2
2
  code: number;
3
3
  message: string;
4
4
  data: E;
@@ -9,9 +9,9 @@ export interface PaginationData<T> {
9
9
  size: number;
10
10
  record: T[];
11
11
  }
12
- export interface ResponseSuccessData<T = any> {
12
+ export interface ResponseSuccessData<T> {
13
13
  code: 0;
14
14
  message: string;
15
15
  data: T | PaginationData<T>;
16
16
  }
17
- export type ResponseData<T, E> = ResponseSuccessData<T> | ResponseErrorData<E>;
17
+ export type ResponseData<T = any, E = any> = ResponseSuccessData<T> | ResponseErrorData<E>;
@@ -0,0 +1,9 @@
1
+ import { CacheManagementOption } from "./cache.management.option";
2
+ import { VersioningOption } from "./versioning.option";
3
+ export interface SnailOption {
4
+ baseURL?: string;
5
+ Versioning?: VersioningOption;
6
+ CacheManage?: CacheManagementOption;
7
+ enableLog?: boolean;
8
+ timeout?: number;
9
+ }
@@ -0,0 +1,5 @@
1
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
2
+ export declare class Strategy {
3
+ applyRequest?(request: AxiosRequestConfig): AxiosRequestConfig | Promise<AxiosRequestConfig>;
4
+ applyResponse?(response: AxiosResponse): AxiosResponse | Promise<AxiosResponse>;
5
+ }
@@ -0,0 +1,30 @@
1
+ export declare enum VersioningType {
2
+ Uri = 0,
3
+ Header = 1,
4
+ Query = 2,
5
+ Custom = 3
6
+ }
7
+ interface VersioningCommonOption {
8
+ defaultVersion: string;
9
+ }
10
+ export interface VersioningUriOption extends VersioningCommonOption {
11
+ type: VersioningType.Uri;
12
+ prefix?: string;
13
+ }
14
+ export interface VersioningHeaderOption extends VersioningCommonOption {
15
+ type: VersioningType.Header;
16
+ header?: string;
17
+ }
18
+ export interface VersioningQueryOption extends VersioningCommonOption {
19
+ type: VersioningType.Query;
20
+ key?: string;
21
+ }
22
+ export interface VersioningCustomOption extends VersioningCommonOption {
23
+ type: VersioningType.Custom;
24
+ extractor: (requestOptions: unknown) => {
25
+ url: string;
26
+ headers: Record<string, any>;
27
+ };
28
+ }
29
+ export type VersioningOption = VersioningUriOption | VersioningHeaderOption | VersioningQueryOption | VersioningCustomOption;
30
+ export {};
@@ -7,5 +7,3 @@ export declare const apiKey: <T extends {
7
7
  headers?: Record<string, any>;
8
8
  }>(apiInstance: T) => string;
9
9
  export declare function recordToString(record: Record<string, any>): string;
10
- export declare function getResponseDataFromCache(cacheKey: string): string;
11
- export declare function deepCopy<T>(obj: T): T;
@@ -1,7 +1,8 @@
1
- import { VersioningConfig } from "../typings";
1
+ import { VersioningOption } from "../typings";
2
2
  interface VersioningResult {
3
3
  url?: string;
4
4
  headers?: Record<string, any>;
5
+ params?: Record<string, any>;
5
6
  }
6
- export declare function applyVersioning(version: string, versioning: VersioningConfig): VersioningResult;
7
+ export declare function applyVersioning(version: string, versioning: VersioningOption): VersioningResult;
7
8
  export {};
package/package.json CHANGED
@@ -1,21 +1,28 @@
1
1
  {
2
2
  "name": "@snail-js/api",
3
- "version": "0.1.0",
4
- "description": "",
5
- "main": "index.ts",
3
+ "version": "0.1.2",
4
+ "description": "Http Request with Decorators Api, build on axios",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
6
7
  "exports": {
7
8
  ".": {
8
- "import": "./dist/snail-api.js",
9
+ "import": "./dist/snail-api.mjs",
9
10
  "require": "./dist/snail-api.umd.cjs",
10
11
  "types": "./dist/index.d.ts"
11
12
  }
12
13
  },
13
14
  "scripts": {
14
15
  "clean:dist": "rimraf ./dist",
15
- "build": "pnpm run clean:dist && vite build",
16
+ "build": "pnpm run clean:dist && vite build && pnpm run build:type",
17
+ "build:type": "vue-tsc -p tsconfig.build.json --composite false --declaration --emitDeclarationOnly",
16
18
  "test": "echo \"Error: no test specified\" && exit 1"
17
19
  },
18
- "keywords": [],
20
+ "keywords": [
21
+ "Decorators",
22
+ "api",
23
+ "Request",
24
+ "axios"
25
+ ],
19
26
  "author": "",
20
27
  "license": "MIT",
21
28
  "peerDependencies": {
@@ -24,5 +31,8 @@
24
31
  "files": [
25
32
  "README.md",
26
33
  "dist"
27
- ]
34
+ ],
35
+ "dependencies": {
36
+ "reflect-metadata": "^0.2.2"
37
+ }
28
38
  }
@@ -1,40 +0,0 @@
1
- import { RequestBody, RequestMethod, ApiConfig, SendOptions, ApiResponse, RequestPipe } from "../typings";
2
- import { Snail } from "./snail";
3
- /**
4
- * 泛型 R response success 时 返回的data类型
5
- * 泛型 E error 时 返回的error类型
6
- * 泛型 D error 时 返回error携带的data类型
7
- */
8
- export declare class Api<R = any, E = any, D = any> {
9
- baseURL: string;
10
- url: string;
11
- data?: RequestBody;
12
- params?: Record<string, any>;
13
- headers?: Record<string, any>;
14
- private key;
15
- method: RequestMethod;
16
- private hitCache;
17
- context: Snail;
18
- version?: string;
19
- private versioning?;
20
- private name?;
21
- private hitSource?;
22
- private timeout;
23
- private transform?;
24
- private pipes;
25
- constructor(method: RequestMethod, url: string, context?: any, config?: ApiConfig<R>);
26
- private handleVersioning;
27
- /**
28
- *
29
- * @param options 发送配置:子项params,data
30
- * 泛型<R = any, E = any>R返回数据类型, E返回错误类型(code!=0)
31
- * @returns
32
- */
33
- send(options?: SendOptions): Promise<ApiResponse<R, E>>;
34
- private checkCache;
35
- delCache(version?: string): Promise<void>;
36
- use(pipe: RequestPipe): void;
37
- private processRequestPipes;
38
- private sendRequest;
39
- private lapsed;
40
- }
@@ -1,19 +0,0 @@
1
- export declare enum CacheType {
2
- Memory = 0,
3
- IndexDB = 1,
4
- LocalStorage = 2
5
- }
6
- interface MemoryCacheConfig {
7
- type: CacheType.Memory;
8
- }
9
- interface IndexDBCacheConfig {
10
- type: CacheType.IndexDB;
11
- }
12
- interface LocalStorageCacheConfig {
13
- type: CacheType.LocalStorage;
14
- }
15
- interface CacheCommonConfig {
16
- ttl?: number;
17
- }
18
- export type CacheManagementConfig = CacheCommonConfig & (MemoryCacheConfig | IndexDBCacheConfig | LocalStorageCacheConfig);
19
- export {};
@@ -1,21 +0,0 @@
1
- import { AxiosInterceptorOptions, AxiosResponse, InternalAxiosRequestConfig } from "axios";
2
- import { CacheManagementConfig } from "./cache.management.config";
3
- import { VersioningConfig } from "./versioning.config";
4
- export type AxiosRequestInterceptor<T = any> = {
5
- onFulfilled?: ((value: InternalAxiosRequestConfig<T>) => InternalAxiosRequestConfig<T> | Promise<InternalAxiosRequestConfig<T>>) | null;
6
- onRejected?: ((error: any) => any) | null;
7
- options?: AxiosInterceptorOptions;
8
- };
9
- export type AxiosResponseInterceptor<T = any> = {
10
- onFulfilled?: ((response: AxiosResponse<T>) => AxiosResponse<T> | Promise<AxiosResponse<T>>) | null;
11
- onRejected?: ((error: any) => any) | null;
12
- };
13
- export interface SnailConfig {
14
- baseURL?: string;
15
- Versioning?: VersioningConfig;
16
- CacheManage?: CacheManagementConfig;
17
- enableLog?: boolean;
18
- timeout?: number;
19
- requestInterceptors?: AxiosRequestInterceptor;
20
- responseInterceptors?: AxiosResponseInterceptor;
21
- }
@@ -1,30 +0,0 @@
1
- export declare enum VersioningType {
2
- Uri = 0,
3
- Header = 1,
4
- Query = 2,
5
- Custom = 3
6
- }
7
- interface VersioningCommonConfig {
8
- defaultVersion: string;
9
- }
10
- export interface VersioningUriConfig extends VersioningCommonConfig {
11
- type: VersioningType.Uri;
12
- prefix?: string;
13
- }
14
- export interface VersioningHeaderConfig extends VersioningCommonConfig {
15
- type: VersioningType.Header;
16
- header: string;
17
- }
18
- export interface VersioningQueryConfig extends VersioningCommonConfig {
19
- type: VersioningType.Query;
20
- key?: string;
21
- }
22
- export interface VersioningCustomConfig extends VersioningCommonConfig {
23
- type: VersioningType.Custom;
24
- extractor: (requestOptions: unknown) => {
25
- url: string;
26
- headers: Record<string, any>;
27
- };
28
- }
29
- export type VersioningConfig = VersioningUriConfig | VersioningHeaderConfig | VersioningQueryConfig | VersioningCustomConfig;
30
- export {};