fix(dianziqian): 外部请求不带token (#1529)

Co-authored-by: sealday <zhanglin@daoyoucloud.com>
Co-authored-by: TomyJan <tomyjan6@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1529
Co-authored-by: wanggang <1366376228@qq.com>
Co-committed-by: wanggang <1366376228@qq.com>
This commit is contained in:
wanggang 2024-09-11 15:13:14 +08:00 committed by TomyJan
parent fcab167c7b
commit 0aca5ef9a3
3 changed files with 25 additions and 17 deletions

View File

@ -95,20 +95,26 @@ async function request(config, context) {
const formData = new FormData(); const formData = new FormData();
for (const [key, value] of Object.entries(data)) { for (const [key, value] of Object.entries(data)) {
if (key === 'file') { if (key === 'file') {
const { resourceUrl, params: Params, headers: Header, body: Body } = data[key]; let { url: resourceUrl, params: resourceParams, headers: resourceHeader, body: resourceBody } = data[key];
resourceUrl = resourceUrl?.trim() || '';
if (!resourceUrl.startsWith('http')) {
//如果开头不是http默认该url是内部url,需要加上token
resourceUrl = `${origin}${resourceUrl}`;
resourceHeader.Authorization = 'Bearer ' + token
}
if (Header['Content-Type'] === 'multipart/form-data' && Body) { if (resourceHeader['Content-Type'] === 'multipart/form-data' && resourceBody) {
//resource contentType类型 //resource contentType类型
const formData = new FormData(); const formData = new FormData();
Object.entries(Body).forEach(([key, value]) => { Object.entries(resourceBody).forEach(([key, value]) => {
formData.append(key, value); formData.append(key, value);
}); });
config.data = formData; config.data = formData;
} else { } else {
config.data = Body; config.data = resourceBody;
} }
const stream = await downloadToStream(resourceUrl, Header, Params, Body); const stream = await downloadToStream(resourceUrl, resourceHeader, resourceParams, resourceBody);
formData.append('file', stream); formData.append('file', stream);
headers = { ...formData.getHeaders() }; headers = { ...formData.getHeaders() };

View File

@ -66,18 +66,18 @@ export class CreateInstruction extends Instruction {
throw new Error('Invalid data URL format'); throw new Error('Invalid data URL format');
} }
} else if (parseRes) { } else if (parseRes) {
const { resourceUrl, params, headers, body } = parseRes; const { url: resourceUrl, params: resourceParams, headers: resourceHeaders, body: resourceBody } = parseRes;
config.url = resourceUrl; config.url = resourceUrl;
config.params = params; config.params = resourceParams;
config.headers = headers; config.headers = resourceHeaders;
if (headers['content-type'] === 'multipart/form-data') { if (resourceHeaders['content-type'] === 'multipart/form-data') {
const formData = new FormData(); const formData = new FormData();
Object.entries(body).forEach(([key, value]) => { Object.entries(resourceBody).forEach(([key, value]) => {
formData.append(key, value); formData.append(key, value);
}); });
config.data = formData; config.data = formData;
} else { } else {
config.data = body; config.data = resourceBody;
} }
const response = await axios(config); const response = await axios(config);
const contentType = response.headers['content-type']; const contentType = response.headers['content-type'];

View File

@ -68,18 +68,20 @@ export class UpdateInstruction extends Instruction {
throw new Error('Invalid data URL format'); throw new Error('Invalid data URL format');
} }
} else if (parseRes) { } else if (parseRes) {
const { resourceUrl, params, headers, body } = parseRes; const { url: resourceUrl, params: resourceParams, headers: resourceHeaders, body: resourceBody } = parseRes;
config.url = resourceUrl; config.url = resourceUrl;
config.params = params; config.params = resourceParams;
config.headers = headers; config.headers = resourceHeaders;
if (headers['content-type'] === 'multipart/form-data') { if (resourceHeaders['content-type'] === 'multipart/form-data') {
const formData = new FormData(); const formData = new FormData();
Object.entries(body).forEach(([key, value]) => { Object.entries(resourceBody).forEach(([key, value]) => {
formData.append(key, value); formData.append(key, value);
}); });
config.data = formData; config.data = formData;
} else { } else {
config.data = body; config.data = resourceBody;
} }
const response = await axios(config); const response = await axios(config);
const contentType = response.headers['content-type']; const contentType = response.headers['content-type'];