stable-ts-type
Version:
Obtain the most stable type code of 'typescript' through multiple network requests
57 lines (56 loc) • 2.73 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mockjs_1 = __importDefault(require("mockjs"));
const axios_1 = __importDefault(require("axios"));
const curl_to_json_1 = __importDefault(require("@juln/curl-to-json"));
const utils_1 = require("./utils");
const curl2RequestOpts = (curl) => {
try {
const opts = (0, curl_to_json_1.default)(curl);
return {
url: opts.url,
method: opts.method,
headers: opts.header,
data: opts.data,
params: opts.params,
};
}
catch (_a) {
throw (0, utils_1.simpleError)('failed to parse "curl"');
}
};
function fetch(request, requestConfig = {}, requestMock) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const requestOpts = typeof request === 'string'
? curl2RequestOpts(request)
: request;
Object.assign(requestOpts, requestConfig);
if (requestMock === null || requestMock === void 0 ? void 0 : requestMock.params)
requestOpts.params = mockjs_1.default.mock(requestMock === null || requestMock === void 0 ? void 0 : requestMock.params);
if (requestMock === null || requestMock === void 0 ? void 0 : requestMock.data)
requestOpts.data = mockjs_1.default.mock(requestMock === null || requestMock === void 0 ? void 0 : requestMock.data);
try {
const axiosResp = yield (0, axios_1.default)(requestOpts);
const respData = axiosResp.data;
return respData;
}
catch (error) {
throw (0, utils_1.simpleError)(`fetch error: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error.toString()}\nAxiosRequestConfig of error: \n${JSON.stringify(requestOpts, null, 2)}`);
}
});
}
;
exports.default = fetch;