mirror of
https://github.com/morten-olsen/http.md.git
synced 2026-02-08 00:46:28 +01:00
94 lines
2.3 KiB
TypeScript
94 lines
2.3 KiB
TypeScript
import Handlebars from "handlebars";
|
|
import YAML from "yaml";
|
|
import { ExecutionHandler } from "../execution.js";
|
|
|
|
const httpHandler: ExecutionHandler = ({
|
|
node,
|
|
addStep,
|
|
}) => {
|
|
if (node.type !== 'code' || node.lang !== 'http') {
|
|
return;
|
|
}
|
|
const optionParts = node.meta?.split(',') || [];
|
|
const options = Object.fromEntries(
|
|
optionParts.filter(Boolean).map((option) => {
|
|
const [key, value] = option.split('=');
|
|
return [key.trim(), value?.trim() || true];
|
|
})
|
|
);
|
|
let id = options.id?.toString();
|
|
const idPart = optionParts.find((option) => option.startsWith('#'));
|
|
if (idPart) {
|
|
id = idPart.slice(1);
|
|
}
|
|
|
|
addStep({
|
|
type: 'http',
|
|
node,
|
|
action: async ({ context }) => {
|
|
if (options.disable === true) {
|
|
return;
|
|
}
|
|
const template = Handlebars.compile(node.value);
|
|
const content = template(context);
|
|
const [head, body] = content.split('\n\n');
|
|
const [top, ...headerItems] = head.split('\n');
|
|
const [method, ...urlParts] = top.split(' ');
|
|
const url = urlParts.join(' ').trim();
|
|
|
|
const headers = Object.fromEntries(
|
|
headerItems.map((header) => {
|
|
const [key, value] = header.split(':');
|
|
return [key.trim(), value?.trim() || ''];
|
|
})
|
|
);
|
|
|
|
let parsedBody = body;
|
|
if (options.format === 'yaml') {
|
|
try {
|
|
const parsed = YAML.parse(body);
|
|
parsedBody = JSON.stringify(parsed);
|
|
} catch (error) {
|
|
parsedBody = `Error parsing YAML: ${error}`;
|
|
}
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method,
|
|
headers,
|
|
body
|
|
});
|
|
|
|
let responseText = await response.text();
|
|
if (options.json) {
|
|
try {
|
|
responseText = JSON.parse(responseText);
|
|
} catch (e) {
|
|
responseText = `Error parsing JSON: ${e}`;
|
|
}
|
|
}
|
|
|
|
node.value = content;
|
|
node.meta = undefined;
|
|
|
|
context.addRequest({
|
|
id,
|
|
request: {
|
|
method,
|
|
url,
|
|
headers,
|
|
body,
|
|
},
|
|
response: {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers: Object.fromEntries(response.headers.entries()),
|
|
body: responseText,
|
|
},
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export { httpHandler };
|