@yl546/core-api 1.0.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/Api.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { App, InjectionKey } from 'vue';
3
+ import { ApiEndpointConstructor, ApiEvent, ApiOptions, ApiRequestConfig, AuthAdapter, LoginParams, LoginResponse } from './types';
4
+ import { WebSocketClient, WebSocketEventRawListeners } from './WebSocketClient';
5
+ export type ApiWithEndpoints<Instance extends Api, Endpoints extends ApiEndpoints = any> = Instance & Endpoints;
6
+ export declare interface ApiEndpoints {
7
+ }
8
+ export type InitWebSocketOptions = {
9
+ override?: boolean;
10
+ protocols?: string | string[];
11
+ listeners?: WebSocketEventRawListeners;
12
+ };
13
+ export type ApiInstance = ApiWithEndpoints<Api, ApiEndpoints>;
14
+ declare module 'vue' {
15
+ interface ComponentCustomProperties {
16
+ $api: ApiInstance;
17
+ }
18
+ }
19
+ export declare const API_CONTEXT_KEY: InjectionKey<ApiInstance>;
20
+ export declare const useApi: () => ApiInstance;
21
+ export declare class Api extends EventEmitter<ApiEvent> {
22
+ static VERSION: string;
23
+ readonly version: string;
24
+ readonly options: Readonly<ApiOptions>;
25
+ readonly auth: AuthAdapter;
26
+ readonly sockets: Record<string, WebSocketClient>;
27
+ private _axios;
28
+ static install: (app: App, options: ApiOptions) => void;
29
+ private _responseFields;
30
+ constructor(options: ApiOptions);
31
+ get host(): string;
32
+ use(EndPoint: ApiEndpointConstructor | Array<ApiEndpointConstructor>): ThisType<Api>;
33
+ addEndPoint(name: string, endPoint: any, enumerable?: boolean): ThisType<Api>;
34
+ /** 登录 */
35
+ login(params: LoginParams): Promise<LoginResponse>;
36
+ /** 登出 */
37
+ logout(): Promise<void>;
38
+ /** 取消所有请求 */
39
+ cancelAllRequest(): void;
40
+ $request<D = any>(config: ApiRequestConfig): Promise<D>;
41
+ $post<D = any>(url: string, data?: any, config?: Partial<ApiRequestConfig>): Promise<D>;
42
+ $get<D = any, Q = any>(url: string, query?: Q, config?: Partial<ApiRequestConfig>): Promise<D>;
43
+ $put<D = any>(url: string, data?: any, config?: Partial<ApiRequestConfig>): Promise<D>;
44
+ $delete<D = any, Q = any>(url: string, query?: Q, config?: Partial<ApiRequestConfig>): Promise<D>;
45
+ private _requestInterceptor;
46
+ private _responseInterceptor;
47
+ setWebSocket: (name: string, endpoint: string, options_?: InitWebSocketOptions) => Promise<WebSocketClient>;
48
+ }
@@ -0,0 +1,14 @@
1
+ import { AxiosResponse } from "axios";
2
+ import { ApiRequestConfig } from "./types";
3
+ export type ApiErrorOptions<D> = Partial<{
4
+ data: D;
5
+ requestConfig: ApiRequestConfig;
6
+ response: AxiosResponse;
7
+ }>;
8
+ export declare class ApiError<D> extends Error {
9
+ readonly code: number;
10
+ readonly requestConfig?: ApiRequestConfig;
11
+ readonly response?: AxiosResponse;
12
+ readonly data?: D;
13
+ constructor(message: string, code: number, options?: ApiErrorOptions<D>);
14
+ }
@@ -0,0 +1,41 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ export declare enum EventSourceEventType {
3
+ Connected = "connected",
4
+ Message = "message",
5
+ RawMessage = "raw-message",
6
+ Error = "error",
7
+ Disconnected = "disconnected"
8
+ }
9
+ export type EventSourceEventListener = (...args: any[]) => any;
10
+ export type EventSourceEventValue = `${EventSourceEventType}`;
11
+ export type EventSourceEventRawListeners = Partial<Record<EventSourceEventValue, Array<EventSourceEventListener>>>;
12
+ export type EventSourceClientOptions = {
13
+ withCredentials?: boolean;
14
+ reconnectInterval?: number;
15
+ /** 是否在实例化时自动连接,默认 true */
16
+ autoConnect?: boolean;
17
+ };
18
+ export declare class EventSourceClient extends EventEmitter<EventSourceEventType> {
19
+ private _endPoint;
20
+ private _options;
21
+ private _rawListeners?;
22
+ private _source?;
23
+ private _active;
24
+ private _reconnectTimer?;
25
+ constructor(endPoint: string, options?: EventSourceClientOptions, rawListeners?: EventSourceEventRawListeners);
26
+ /** 手动建立连接(已连接或连接中时跳过) */
27
+ connect(): void;
28
+ /** 重新连接 */
29
+ reconnect(): void;
30
+ /** 禁用连接 */
31
+ deactivate(): void;
32
+ private _connect;
33
+ private _destroySource;
34
+ private _clearReconnectTimer;
35
+ private _scheduleReconnect;
36
+ private _handleOpen;
37
+ private _handleMessage;
38
+ private _handleError;
39
+ private _bindRawListeners;
40
+ private _parseMessage;
41
+ }
@@ -0,0 +1,31 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ export declare enum WebSocketEventType {
3
+ Connected = "connected",// 连接成功
4
+ Message = "message",// 接受信息,处理之后的信息
5
+ RawMessage = "raw-message",// 原始信息
6
+ Error = "error",//错误的时候
7
+ Disconnected = "disconnected"
8
+ }
9
+ export type WebSocketEventListener = (...args: any[]) => any;
10
+ export type WebSocketEventValue = `${WebSocketEventType}`;
11
+ export type WebSocketEventRawListeners = Partial<Record<WebSocketEventValue, Array<WebSocketEventListener>>>;
12
+ export declare class WebSocketClient extends EventEmitter<WebSocketEventType> {
13
+ private _endPoint;
14
+ private _protocols?;
15
+ private _rawListeners?;
16
+ private _socket;
17
+ private _loopAnimationRequestId?;
18
+ private _loopSocketHeartBeatTimestamp;
19
+ private _socketHeartBeatInterval;
20
+ constructor(endPoint: string, protocols?: string | Array<string>, rawListeners?: WebSocketEventRawListeners);
21
+ reconnect(): void;
22
+ deactivate(): void;
23
+ send(val: string): void;
24
+ private _handleOpen;
25
+ private _handleMessage;
26
+ private _handleError;
27
+ private _handleClose;
28
+ private _initListeners;
29
+ private _resolveMessageEvent;
30
+ private _loop;
31
+ }
@@ -0,0 +1,14 @@
1
+ /** 认证成功业务码 */
2
+ export declare const AUTHORIZED_CODE = 200;
3
+ /** 认证失败业务码 */
4
+ export declare const UNAUTHORIZED_CODE = 401;
5
+ /** 响应字段默认取值 key */
6
+ export declare const DEFAULT_RESPONSE_FIELDS: {
7
+ readonly code: "code";
8
+ readonly msg: "msg";
9
+ readonly data: "data";
10
+ readonly items: "items";
11
+ readonly total: "total";
12
+ readonly pageNum: "pageNum";
13
+ readonly pageSize: "pageSize";
14
+ };
@@ -0,0 +1,8 @@
1
+ export * from './Api';
2
+ export * from './ApiError';
3
+ export * from './constants';
4
+ export * from './types';
5
+ export * from './utils/formatResponseData';
6
+ export * from './utils/resolveResponseFields';
7
+ export * from './WebSocketClient';
8
+ export * from './EventSourceClient';