import { isNullOrUndefined } from 'util'; import { ApiClient } from './api.client'; import { ClientOptions } from './api.client.factory'; export class InterfaceError extends Error { code: string | number; errorMessage: string; stack: string; constructor({ errorString = '', returnCode = 0 }) { super(); this.errorMessage = errorString; this.message = errorString; this.code = returnCode; this.stack = new Error().stack; } } export class GssApiClient extends ApiClient { constructor(protected options: ClientOptions) { super( options.baseUrl, options.headerProcessor, (resp) => { if (resp.status === 204) { return null; } const { returnCode, errorString, result } = resp.data; if (returnCode === 0) { if (!isNullOrUndefined(result)) { return result; } else { throw new InterfaceError({ returnCode: 0, errorString: '数据为空' }); } } throw new InterfaceError({ returnCode, errorString }); }, options.errorHandler ); } } export interface GssApiResponse { returnCode: number; errorString: string; result: any; }