@lcap/nasl
Version:
NetEase Application Specific Language
87 lines • 3.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createService = void 0;
const request_pre_1 = __importDefault(require("request-pre"));
const axios_1 = __importDefault(require("axios"));
const qs_1 = require("qs");
const add_configs_1 = __importDefault(require("./add.configs"));
const config_1 = require("../../config");
const formatContentType = function (contentType, data) {
const map = {
'application/x-www-form-urlencoded'(data) {
return (0, qs_1.stringify)(data);
},
};
return map[contentType] ? map[contentType](data) : data;
};
const requester = function (requestInfo) {
const { url, config = {} } = requestInfo;
const { path, method, body = {}, headers = {}, query = {} } = url;
let baseURL = config.baseURL || '';
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
try {
const refreshToken = localStorage.getItem('refreshtoken');
headers['refreshtoken'] = refreshToken;
const authorizationExpiredRefreshTime = localStorage.getItem('authorizationExpiredRefreshTime');
if (refreshToken && authorizationExpiredRefreshTime) {
if (Date.parse(new Date()) / 1000 >= Number(authorizationExpiredRefreshTime)) {
headers['refreshtoken'] = refreshToken;
}
}
}
catch (err) {
console.warn(err);
}
if (headers.operationDesc)
headers.operationDesc = encodeURIComponent(headers.operationDesc);
if (typeof window === 'undefined') {
baseURL = config.baseURL || config_1.config.baseURL || '';
if (config_1.config.cookie)
headers.Cookie = config_1.config.cookie;
}
let data;
const method2 = method.toUpperCase();
if (Object.keys(body).length || ['PUT', 'POST', 'PATCH'].includes(method2)) {
data = formatContentType(headers['Content-Type'], body);
}
Object.keys(headers).forEach((key) => {
if (headers[key] === undefined) {
delete headers[key];
}
});
const requestOptions = {
params: query,
baseURL,
method: method2,
url: path,
data,
headers,
withCredentials: !baseURL,
};
if (config.timeout)
requestOptions.timeout = config.timeout;
if (config_1.config.debugRequestAll || (config_1.config.debugRequest && requestOptions.method !== 'GET')) {
console.debug('[Request Debug]', requestOptions);
}
const req = (0, axios_1.default)(requestOptions);
return req;
};
const service = new request_pre_1.default(requester);
(0, add_configs_1.default)(service);
const createService = function createService(apiSchemaList, serviceConfig, dynamicServices) {
const fixServiceConfig = serviceConfig || {};
fixServiceConfig.config = fixServiceConfig.config || {};
Object.assign(fixServiceConfig.config, {
httpCode: true,
httpError: true,
shortResponse: true,
devProxy: true,
});
serviceConfig = fixServiceConfig;
return service.generator(apiSchemaList, dynamicServices, serviceConfig);
};
exports.createService = createService;
//# sourceMappingURL=index.js.map