mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 04:01:58 +00:00
feat: fetch effect (#1605)
* feat: fetch effect * fix: Make not available in sandbox * chore: Update to use text(), and to use headers * chore: use the postman echo for testing * chore: add json * chore: Testing the json * chore: Make the json lazy
This commit is contained in:
@@ -727,3 +727,36 @@ export async function setConfig(effects) {
|
||||
error: "Not setup"
|
||||
};
|
||||
}
|
||||
|
||||
const assert = (condition, message) => {
|
||||
if (!condition) {
|
||||
throw new Error(message)
|
||||
}
|
||||
}
|
||||
|
||||
export const action = {
|
||||
async fetch(effects, _input) {
|
||||
const example = await effects.fetch("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
|
||||
assert(Number(example.headers['content-length']) > 0 && Number(example.headers['content-length']) <= 1000000,"Should have content length");
|
||||
assert((example.text() instanceof Promise), "example.text() should be a promise");
|
||||
assert((example.body === undefined), "example.body should not be defined");
|
||||
assert((JSON.parse(await example.text()).args.foo1 === 'bar1'), "Body should be parsed");
|
||||
const message = `This worked @ ${new Date().toISOString()}`;
|
||||
const secondResponse = await effects.fetch("https://postman-echo.com/post", {
|
||||
method: "POST",
|
||||
body:JSON.stringify({message }),
|
||||
headers:{
|
||||
test: "1234",
|
||||
}
|
||||
})
|
||||
assert(((await secondResponse.json()).json.message === message), "Body should be parsed from response");
|
||||
return {
|
||||
result: {
|
||||
copyable: false,
|
||||
message: "Done",
|
||||
version: "0",
|
||||
qr: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user