fix: 解决 reqeust 生成文件流的问题 (#239)

This commit is contained in:
qlin 2024-03-28 18:27:40 +08:00 committed by GitHub
parent c67e7ea12a
commit 3bc59652a0
3 changed files with 49 additions and 47 deletions

View File

@ -1,39 +1,39 @@
{ {
"name": "@fesjs/plugin-request", "name": "@fesjs/plugin-request",
"version": "4.0.0-rc.1", "version": "4.0.0-rc.1",
"description": "@fesjs/plugin-request", "description": "@fesjs/plugin-request",
"main": "lib/index.js", "author": "qlin",
"files": [ "license": "MIT",
"lib", "homepage": "https://github.com/WeBankFinTech/fes.js#readme",
"types.d.ts" "repository": {
], "type": "git",
"scripts": { "url": "git+https://github.com/WeBankFinTech/fes.js.git",
"test": "echo \"Error: no test specified\" && exit 1" "directory": "packages/fes-plugin-request"
}, },
"repository": { "bugs": {
"type": "git", "url": "https://github.com/WeBankFinTech/fes.js/issues"
"url": "git+https://github.com/WeBankFinTech/fes.js.git", },
"directory": "packages/fes-plugin-request" "keywords": [
}, "fes"
"keywords": [ ],
"fes" "main": "lib/index.js",
], "files": [
"author": "qlin", "lib",
"license": "MIT", "types.d.ts"
"bugs": { ],
"url": "https://github.com/WeBankFinTech/fes.js/issues" "scripts": {
}, "test": "echo \"Error: no test specified\" && exit 1"
"homepage": "https://github.com/WeBankFinTech/fes.js#readme", },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
}, },
"peerDependencies": { "peerDependencies": {
"@fesjs/fes": "^3.1.4", "@fesjs/fes": "^3.1.4",
"vue": "^3.2.37" "vue": "^3.2.37"
}, },
"dependencies": { "dependencies": {
"@fesjs/utils": "^3.0.1", "@fesjs/utils": "^3.0.1",
"@qlin/request": "^0.1.2" "@qlin/request": "^0.2.0"
}, },
"typings": "./types.d.ts" "typings": "./types.d.ts"
} }

View File

@ -1,4 +1,4 @@
import { join } from 'path'; import { join } from 'node:path';
import { name } from '../package.json'; import { name } from '../package.json';
export default (api) => { export default (api) => {
@ -9,7 +9,9 @@ export default (api) => {
let generatedOnce = false; let generatedOnce = false;
api.onGenerateFiles(() => { api.onGenerateFiles(() => {
if (generatedOnce) return; if (generatedOnce) {
return;
}
generatedOnce = true; generatedOnce = true;
api.copyTmpFiles({ api.copyTmpFiles({
namespace, namespace,

View File

@ -9,7 +9,6 @@ function getRequestInstance() {
type: ApplyPluginsType.modify, type: ApplyPluginsType.modify,
initialValue: { initialValue: {
timeout: 10000, timeout: 10000,
responseType: 'json',
}, },
}); });
@ -18,7 +17,7 @@ function getRequestInstance() {
let currentRequest; let currentRequest;
export const rawRequest = (url, data, options = {}) => { export function rawRequest(url, data, options = {}) {
if (typeof options === 'string') { if (typeof options === 'string') {
options = { options = {
method: options, method: options,
@ -28,12 +27,12 @@ export const rawRequest = (url, data, options = {}) => {
currentRequest = getRequestInstance(); currentRequest = getRequestInstance();
} }
return currentRequest(url, data, options); return currentRequest(url, data, options);
}; }
export const request = async (url, data, options = {}) => { export async function request(url, data, options = {}) {
const response = await rawRequest(url, data, options); const response = await rawRequest(url, data, options);
return response.data; return response.data;
}; }
request.version = '4.0.0'; request.version = '4.0.0';
@ -41,14 +40,15 @@ function isPromiseLike(obj) {
return !!obj && typeof obj === 'object' && typeof obj.then === 'function'; return !!obj && typeof obj === 'object' && typeof obj.then === 'function';
} }
export const useRequest = (url, data, options = {}) => { export function useRequest(url, data, options = {}) {
const loadingRef = ref(true); const loadingRef = ref(true);
const errorRef = ref(null); const errorRef = ref(null);
const dataRef = shallowRef(null); const dataRef = shallowRef(null);
let promise; let promise;
if (isPromiseLike(url)) { if (isPromiseLike(url)) {
promise = url; promise = url;
} else { }
else {
promise = request(url, data, options); promise = request(url, data, options);
} }
promise promise
@ -66,4 +66,4 @@ export const useRequest = (url, data, options = {}) => {
error: errorRef, error: errorRef,
data: dataRef, data: dataRef,
}; };
}; }