import { RequestMethod, RequestOptions, UploadFileOptions, DownloadFileOptions, Certificate, ConfigMTLSOptions, } from './interface.uts' // request export const API_REQUEST = 'request'; export const RequestApiProtocol = new Map([ [ 'url', { type: 'string', required: true } ], [ 'data', { type: 'object', required: false } ], [ 'header', { type: 'object', required: false } ], [ 'method', { type: 'string', required: false } ], [ 'dataType', { type: 'string', required: false } ], [ 'responseType', { type: 'string', required: false } ], [ 'timeout', { type: 'number', required: false } ], [ 'sslVerify', { type: 'boolean', required: false } ], [ 'withCredentials', { type: 'boolean', required: false } ], [ 'firstIpv4', { type: 'boolean', required: false } ], ]) export const RequestApiOptions: ApiOptions> = { formatArgs: new Map([ [ 'url', function (url: string, params: RequestOptions) { if (url == null) { throw new Error('url is required') } } ], [ 'method', function (method: string, params: RequestOptions) { params.method = (method || 'GET').toUpperCase() as RequestMethod } ], [ 'dataType', function (dataType: string, params: RequestOptions) { if (dataType == null) { params.dataType = 'json' } } ], [ 'responseType', function (responseType: string, params: RequestOptions) { if (responseType == null) { params.responseType = 'text' } } ], [ 'timeout', function (timeout: number, params: RequestOptions) { if (timeout == null) { params.timeout = 60000 } } ], [ 'sslVerify', function (sslVerify: boolean, params: RequestOptions) { if (sslVerify == null) { params.sslVerify = true } } ], [ 'withCredentials', function (withCredentials: boolean, params: RequestOptions) { if (withCredentials == null) { params.withCredentials = false } } ], [ 'firstIpv4', function (firstIpv4: boolean, params: RequestOptions) { if (firstIpv4 == null) { params.firstIpv4 = false } } ], ]), } // downloadFile export const API_DOWNLOAD_FILE = 'downloadFile'; export const DownloadFileApiProtocol = new Map([ [ 'url', { type: 'string', required: true } ], [ 'header', { type: 'object', required: false } ], [ 'timeout', { type: 'number', required: false } ], ]) export const DownloadFileApiOptions: ApiOptions = { formatArgs: new Map([ [ 'url', function (url: string, params: DownloadFileOptions) { if (url == null) { throw new Error('url is required') } } ], ]), } // uploadFile export const API_UPLOAD_FILE = 'uploadFile'; export const UploadFileApiProtocol = new Map([ [ 'url', { type: 'string', required: true } ], [ 'filePath', { type: 'string', required: false } ], [ 'name', { type: 'string', required: false } ], [ 'header', { type: 'object', required: false } ], [ 'formData', { type: 'object', required: false } ], [ 'timeout', { type: 'number', required: false } ], ]) export const UploadFileApiOptions: ApiOptions = { formatArgs: new Map([ [ 'url', function (url: string, params: UploadFileOptions) { if (url == null) { throw new Error('url is required') } } ], [ 'name', function (name: string, params: UploadFileOptions) { if (name == null) { params.name = 'file' } } ], ]), } // #region configMTLS export const API_CONFIG_MTLS = 'configMTLS' export const ConfigMTLSApiProtocol = new Map([ [ 'certificates', { type: 'array', required: true, } ] ]) export const ConfigMTLSApiOptions: ApiOptions = { formatArgs: new Map( [ [ 'certificates', function (certificates?: Certificate[]) { if (!certificates || certificates.some((item) => typeof item.host !== 'string')) { return '参数 certificates 配置错误,请确认后重试' } return undefined } ] ] ) } // #endregion