From 5b8eed331feb98a42ad9fa4d3e87fb0648e432ff Mon Sep 17 00:00:00 2001 From: Gorka Date: Thu, 16 May 2024 08:29:50 -0300 Subject: [PATCH] [wip] feat: add instructions to readme --- README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/README.md b/README.md index d641b4b..59d7d4b 100644 --- a/README.md +++ b/README.md @@ -1 +1,75 @@ # JS Lilypad cli wrapper + + +## Usage + +`node src/run.js` or `node src/stream.js` + +```js +// run.js +const { run } = require("./") + +run( + "private-key", + "module name" + 'payload (key=value)' +).then((res) => { + console.log(res) +}) + +// stream.js +const { stream } = require("./") + +stream( + "private-key", + "module name" + 'payload (key=value)' + { stream: true }, +).then(() => { + console.log("Result in ./output/result") +}) + + +// index.js +const fetch = require("node-fetch") +const fs = require("fs") + +const URL = "https://js-cli-wrapper.lilypad.tech" +const METHOD = "POST" +const HEADERS = { + Accept: "application/json", + "Content-Type": "application/json", +} +const OUTPUT = "./output" + +function stream(pk, module, inputs, opts) { + const body = JSON.stringify({ pk, module, inputs, opts }) + + return fetch(URL, { + headers: HEADERS, + method: METHOD, + body, + }).then(function (res) { + const fileStream = fs.createWriteStream(`./${OUTPUT}/result`) + res.body.pipe(fileStream) + res.body.on("error", (error) => { + return { error } + }) + fileStream.on("finish", () => { + return { status: "done" } + }) + }) +} + +function run(pk, module, inputs) { + const body = JSON.stringify({ pk, module, inputs }) + + return fetch(URL, { + headers: HEADERS, + method: METHOD, + body, + }).then((raw) => raw.json()) +} + +module.exports = { run, stream } +```