UNPKG

@morjs/runtime-web

Version:
88 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("whatwg-fetch"); const index_1 = require("./utils/index"); exports.default = { closeSocket() { return new Promise((resolve) => { resolve(); }); }, connectSocket() { return new Promise((resolve) => { resolve(); }); }, downloadFile() { return new Promise((resolve) => { const res = { apFilePath: '' }; resolve(res); }); }, // eslint-disable-next-line @typescript-eslint/no-empty-function offSocketClose() { }, // eslint-disable-next-line @typescript-eslint/no-empty-function offSocketMessage() { }, request(options) { const METHOD_GET = 'GET'; const { method = METHOD_GET, headers = {}, data = {}, timeout = 30000, dataType = 'json' } = options; let url = options.url; if (METHOD_GET === method && Object.keys(data).length > 0) { url = (0, index_1.appendQueryToUrl)(url, data); } const params = { method, headers }; // 允许透传 credentials 和 mode if (options.credentials) params.credentials = options.credentials; if (options.mode) params.mode = options.mode; if (METHOD_GET !== method) params.body = JSON.stringify(data); return new Promise((resolve, reject) => { return fetch(url, params) .then((res) => { const getData = () => { const TYPE = { json: 'json', text: 'text', arraybuffer: 'arrayBuffer', base64: 'blob' }; const type = TYPE[(dataType || '').toLowerCase()]; if (dataType === 'base64') return res[type]().then(index_1.convertBlobToBase64); return res[type](); }; const getHeaders = () => { const headers = {}; const resHeaders = res.headers; let headerKeys = resHeaders && resHeaders.keys(); if (!headerKeys) return headers; headerKeys = Array.from(headerKeys); headerKeys.map((key) => { headers[key] = resHeaders.get(key); }); return headers; }; if (res && res.status >= 200 && res.status < 400) { return getData() .then((data) => { resolve({ data, status: res.status, headers: getHeaders() }); }) .catch(reject); } else { return reject(new Error(`HTTP 错误`)); } }) .catch(reject); }); } }; //# sourceMappingURL=network.js.map