UNPKG

easy-api-client

Version:

开源项目服务端api框架easy-spring-boot-api的typescript客户端api对接工具包

192 lines (191 loc) 8.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClientInstance = exports.newApiClientInstance = void 0; var ApiClient_1 = require("../ApiClient"); var axios_1 = require("axios"); var ApiPreHandlerInstances_1 = require("./ApiPreHandlerInstances"); var ApiAfterHandlerInstances_1 = require("./ApiAfterHandlerInstances"); var ApiResponseInterceptor_1 = require("./ApiResponseInterceptor"); function newApiClientInstance(options) { var client = ApiClientInstance.newInstance(options); client.addAfterHandler(ApiAfterHandlerInstances_1.ApiCheckSignAfterHandler.newInstance()); client.addAfterHandler(ApiAfterHandlerInstances_1.ApiDecryptAfterHandler.newInstance()); client.addPreHandler(ApiPreHandlerInstances_1.ApiSignPreHandler.newInstance()); client.addPreHandler(ApiPreHandlerInstances_1.ApiEncryptPreHandler.newInstance()); client.addPreHandler(ApiPreHandlerInstances_1.ApiSysParamSetPreHandler.newInstance()); client.addResponseInterceptor(new ApiResponseInterceptor_1.DefaultApiResponseInterceptor()); return client; } exports.newApiClientInstance = newApiClientInstance; var ApiClientInstance = /** @class */ (function () { function ApiClientInstance() { this.preHandlers = []; this.afterHandlers = []; this.interceptors = []; this.options = { baseUrl: "http://localhost:8080", appId: "", secret: "" }; } ApiClientInstance.newInstance = function (options) { var instance = new ApiClientInstance(); instance.init(options); return instance; }; ApiClientInstance.prototype.init = function (options) { var axiosConfig = options.config; if (!axiosConfig) { axiosConfig = {}; } axiosConfig.baseURL = options.baseUrl || axiosConfig.baseURL; axiosConfig.timeout = options.timeout || axiosConfig.timeout; if (!axiosConfig.headers) { axiosConfig.headers = {}; } axiosConfig.headers['Content-Type'] = options.contentType || axiosConfig.headers['Content-Type'] || 'application/json'; this.api = axios_1.default.create(axiosConfig); options.config = axiosConfig; this.options = options; this.initMethodMapping(); this.initResponseInterceptors(); }; ApiClientInstance.prototype.initMethodMapping = function () { var _a; if (!this.api) { return; } var api = this.api; this.apiMethodMappings = (_a = {}, _a[ApiClient_1.RequestType.get] = function (url, data, config) { config = config || {}; config.params = data; return api.get(url, config); }, _a[ApiClient_1.RequestType.post] = function (url, data, config) { if (data.parameters) { config = config || {}; config.params = data.parameters; } return api.post(url, data, config); }, _a[ApiClient_1.RequestType.delete] = function (url, data, config) { config = config || {}; config.params = data; return api.delete(url, config); }, _a[ApiClient_1.RequestType.head] = function (url, data, config) { config = config || {}; config.params = data; return api.head(url, config); }, _a[ApiClient_1.RequestType.put] = function (url, data, config) { return api.put(url, data, config); }, _a[ApiClient_1.RequestType.patch] = function (url, data, config) { return api.patch(url, data, config); }, _a); }; ApiClientInstance.prototype.errorHandle = function (status, other) { this.interceptors.forEach(function (intercetor) { if (intercetor.support(status)) { intercetor.failedHandle(status, other && JSON.stringify(other)); } }); }; ApiClientInstance.prototype.initResponseInterceptors = function () { var _this = this; // 添加响应拦截器 this.api && this.api.interceptors.response.use(function (response) { // 对响应数据做点什么 if (!response) { return response; } if (response.headers["Content-Type"] == "application/octet-stream" || (response.headers["content-disposition"] && (response.headers["Content-Type"] != "application/json"))) { return Promise.resolve(response); } if (response.data.code !== 0) { _this.interceptors.forEach(function (intercetor) { if (intercetor.support(response.status)) { intercetor.failedHandle(response.status, response.data.msg, response.data.code); } }); return Promise.reject(response); } else { //成功 _this.interceptors.forEach(function (intercetor) { if (intercetor.support(200, 0)) { intercetor.successHandle(response.data); } }); } return Promise.resolve(response); }, function (error) { // 对响应错误做点什么 var response = error.response; if (response) { // 请求已发出,但是不在2xx的范围 _this.errorHandle(response.status, response.data.message || error.message); return Promise.reject(response); } return Promise.reject(error); }); }; ApiClientInstance.prototype.request = function (api, data, contentType, config) { var _this = this; return new Promise(function (resolve, reject) { try { if (!data) { data = {}; } for (var _i = 0, _a = _this.preHandlers; _i < _a.length; _i++) { var h = _a[_i]; data = h.handle(api, _this.options, data); } var url = _this.options.baseUrl + api.apiName; if (api.apiSuffix !== undefined) { url += api.apiSuffix; } else { url += (_this.options.apiSuffix || ''); } _this.apiMethodMappings && _this.apiMethodMappings[api.type](url, data, config).then(function (response) { //执行拦截器方法 for (var _i = 0, _a = _this.afterHandlers; _i < _a.length; _i++) { var h = _a[_i]; response = h.handle(api, _this.options, response); } resolve(response); }, function (reason) { reject(reason); }).catch(function (reason) { console.warn(reason); reject(reason); }); } catch (e) { console.warn(e.message); console.warn(e); reject(e.message); } }); }; ApiClientInstance.prototype.addAfterHandler = function (handler) { this.afterHandlers.push(handler); //排序 this.afterHandlers.sort(function (h1, h2) { return h1.order() - h2.order(); }); }; ApiClientInstance.prototype.addPreHandler = function (handler) { this.preHandlers.push(handler); //排序 this.preHandlers.sort(function (h1, h2) { return h1.order() - h2.order(); }); }; ApiClientInstance.prototype.addResponseInterceptor = function (interceptor) { this.interceptors.push(interceptor); }; return ApiClientInstance; }()); exports.ApiClientInstance = ApiClientInstance;