Skip to content

Commit

Permalink
perf: 支持query插值和error插值
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 21, 2024
1 parent bd03b19 commit 79160f2
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/plugins/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
function interpolate(template, data) {
return template.split('{{').reduce((result, part) => {
const [value, rest] = part.split('}}');
if (rest === undefined)
if (rest === undefined) {
return result + part;
}

const keys = value.trim().split('.');
let currentValue = data;
Expand Down Expand Up @@ -64,9 +65,12 @@ function interpolateObject(obj, data) {
// input: {
// type: 'text', // 'json', 'space-separated', 'dot-separated'
// },
// query: {
// 'q': '{{query}}',
// },
// body: {
// type: 'json', // or 'form' or 'text'
// content: {
// content: {
// name: '{{name}}',
// age: '{{age}}',
// },
Expand All @@ -77,6 +81,13 @@ function interpolateObject(obj, data) {
// type: 'text', // or image
// output: 'Hello, {{name}}',
// },
// error: {
// type: 'json', // or 'text'
// content: {
// type: 'text', // or image
// output: 'Error: {{message}}',
// },
// }
// },
// }

Expand All @@ -101,6 +112,7 @@ export const TemplateResponseContentTypeImage = 'image';
* @property {{[key: string]: string}} headers
* @property {object} input
* @property {string} input.type
* @property {{[key: string]: string}} query
* @property {object} body
* @property {string} body.type
* @property {{[key: string]: string} | string} body.content
Expand All @@ -117,13 +129,25 @@ export const TemplateResponseContentTypeImage = 'image';
* @returns {Promise<string>}
*/
export async function executeRequest(template, data) {
const url = interpolate(template.url, data);
const url = new URL(interpolate(template.url, data));

if (template.query !== null) {
for (const [key, value] of Object.entries(template.query)) {
url.searchParams.append(key, interpolate(value, data));
}
}

const method = template.method;
const headers = Object.fromEntries(
Object.entries(template.headers).map(([key, value]) => {
return [key, interpolate(value, data)];
}),
);
for (const key of Object.keys(headers)) {
if (headers[key] === null) {
delete headers[key];
}
}

let body = null;
if (template.body !== null) {
Expand All @@ -145,6 +169,16 @@ export async function executeRequest(template, data) {
body,
});

if (!response.ok) {
if (template.response?.error?.type === 'json') {
return interpolate(template.response.error.content.output, await response.json());
} else if (template.response?.error?.type === 'text') {
return interpolate(template.response.error.content.output, await response.text());
} else {
return await response.text();
}
}

if (template.response?.type === 'json') {
return interpolate(template.response.content.output, await response.json());
} else if (template.response?.type === 'text') {
Expand Down

0 comments on commit 79160f2

Please sign in to comment.