UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

75 lines (64 loc) 2.27 kB
import Service from 'request-pre'; import axios from 'axios'; import { stringify } from 'qs'; import addConfigs from './add.configs'; import aslConfig from '../../types/config'; const formatContentType = function (contentType, data) { const map = { 'application/x-www-form-urlencoded'(data) { return 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'; if (headers.operationDesc) headers.operationDesc = encodeURIComponent(headers.operationDesc); if (typeof window === 'undefined') { baseURL = config.baseURL || aslConfig.baseURL || ''; if (aslConfig.cookie) headers.Cookie = aslConfig.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 (aslConfig.debugRequest && requestOptions.method !== 'GET') { console.debug('[Request Debug]', requestOptions); } const req = axios(requestOptions); return req; }; const service = new Service(requester); addConfigs(service); export 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); };