|
1 | 1 | import { Stream, Readable } from 'stream';
|
2 |
| -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; |
3 | 2 | import * as FormData from 'form-data';
|
4 | 3 | import {
|
5 |
| - FireFlyOptions, |
6 |
| - FireFlyOptionsInput, |
7 | 4 | FireFlyStatusResponse,
|
8 | 5 | FireFlyPrivateSendOptions,
|
9 | 6 | FireFlyCreateOptions,
|
@@ -51,106 +48,10 @@ import {
|
51 | 48 | FireFlyContractAPIFilter,
|
52 | 49 | } from './interfaces';
|
53 | 50 | import { FireFlyWebSocket, FireFlyWebSocketCallback } from './websocket';
|
| 51 | +import HttpBase, { mapConfig } from './http'; |
54 | 52 |
|
55 |
| -function isSuccess(status: number) { |
56 |
| - return status >= 200 && status < 300; |
57 |
| -} |
58 |
| - |
59 |
| -function mapConfig( |
60 |
| - options: FireFlyGetOptions | FireFlyCreateOptions | undefined, |
61 |
| - params?: any, |
62 |
| -): AxiosRequestConfig { |
63 |
| - return { |
64 |
| - ...options?.requestConfig, |
65 |
| - params: { |
66 |
| - ...params, |
67 |
| - confirm: options?.confirm, |
68 |
| - }, |
69 |
| - }; |
70 |
| -} |
71 |
| - |
72 |
| -export class FireFlyError extends Error {} |
73 |
| - |
74 |
| -export default class FireFly { |
75 |
| - private options: FireFlyOptions; |
76 |
| - private rootHttp: AxiosInstance; |
77 |
| - private http: AxiosInstance; |
| 53 | +export default class FireFly extends HttpBase { |
78 | 54 | private queue = Promise.resolve();
|
79 |
| - private errorHandler?: (err: FireFlyError) => void; |
80 |
| - |
81 |
| - constructor(options: FireFlyOptionsInput) { |
82 |
| - this.options = this.setDefaults(options); |
83 |
| - this.rootHttp = axios.create({ |
84 |
| - ...options.requestConfig, |
85 |
| - baseURL: `${options.host}/api/v1`, |
86 |
| - }); |
87 |
| - this.http = axios.create({ |
88 |
| - ...options.requestConfig, |
89 |
| - baseURL: `${options.host}/api/v1/namespaces/${this.options.namespace}`, |
90 |
| - }); |
91 |
| - } |
92 |
| - |
93 |
| - private setDefaults(options: FireFlyOptionsInput): FireFlyOptions { |
94 |
| - return { |
95 |
| - ...options, |
96 |
| - namespace: options.namespace ?? 'default', |
97 |
| - websocket: { |
98 |
| - ...options.websocket, |
99 |
| - host: options.websocket?.host ?? options.host.replace('http', 'ws'), |
100 |
| - reconnectDelay: options.websocket?.reconnectDelay ?? 5000, |
101 |
| - heartbeatInterval: options.websocket?.heartbeatInterval ?? 30000, |
102 |
| - }, |
103 |
| - }; |
104 |
| - } |
105 |
| - |
106 |
| - private async wrapError<T>(response: Promise<AxiosResponse<T>>) { |
107 |
| - return response.catch((err) => { |
108 |
| - if (axios.isAxiosError(err)) { |
109 |
| - const errorMessage = err.response?.data?.error; |
110 |
| - const ffError = new FireFlyError(errorMessage ?? err.message); |
111 |
| - if (this.errorHandler !== undefined) { |
112 |
| - this.errorHandler(ffError); |
113 |
| - } |
114 |
| - throw ffError; |
115 |
| - } |
116 |
| - throw err; |
117 |
| - }); |
118 |
| - } |
119 |
| - |
120 |
| - private async getMany<T>(url: string, params?: any, options?: FireFlyGetOptions, root = false) { |
121 |
| - const http = root ? this.rootHttp : this.http; |
122 |
| - const response = await this.wrapError(http.get<T>(url, mapConfig(options, params))); |
123 |
| - return response.data; |
124 |
| - } |
125 |
| - |
126 |
| - private async getOne<T>(url: string, options?: FireFlyGetOptions, params?: any, root = false) { |
127 |
| - const http = root ? this.rootHttp : this.http; |
128 |
| - const response = await this.wrapError( |
129 |
| - http.get<T>(url, { |
130 |
| - ...mapConfig(options, params), |
131 |
| - validateStatus: (status) => status === 404 || isSuccess(status), |
132 |
| - }), |
133 |
| - ); |
134 |
| - return response.status === 404 ? undefined : response.data; |
135 |
| - } |
136 |
| - |
137 |
| - private async createOne<T>(url: string, data: any, options?: FireFlyCreateOptions) { |
138 |
| - const response = await this.wrapError(this.http.post<T>(url, data, mapConfig(options))); |
139 |
| - return response.data; |
140 |
| - } |
141 |
| - |
142 |
| - private async replaceOne<T>(url: string, data: any) { |
143 |
| - const response = await this.wrapError(this.http.put<T>(url, data)); |
144 |
| - return response.data; |
145 |
| - } |
146 |
| - |
147 |
| - private async deleteOne<T>(url: string) { |
148 |
| - await this.wrapError(this.http.delete<T>(url)); |
149 |
| - } |
150 |
| - |
151 |
| - onError(handler: (err: FireFlyError) => void) { |
152 |
| - this.errorHandler = handler; |
153 |
| - } |
154 | 55 |
|
155 | 56 | async getStatus(options?: FireFlyGetOptions): Promise<FireFlyStatusResponse> {
|
156 | 57 | const response = await this.rootHttp.get<FireFlyStatusResponse>('/status', mapConfig(options));
|
|
0 commit comments