|
| 1 | +#!/usr/bin/env -S npx ts-node --transpileOnly |
| 2 | + |
| 3 | +import { Substrate, Box, Module, sb } from "substrate"; |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"]; |
| 7 | + |
| 8 | + const substrate = new Substrate({ apiKey: SUBSTRATE_API_KEY }); |
| 9 | + |
| 10 | + const x = sb.input({ type: "string", default: "hello" }); |
| 11 | + const y = sb.input({ type: "string" }); |
| 12 | + const z = sb.input({ type: "object", properties: {} }); |
| 13 | + |
| 14 | + const a = new Box({ value: { a: x, z: z, array: [x, x, x] } }, { id: "A" }); |
| 15 | + const b = new Box( |
| 16 | + { value: { b: sb.interpolate`x=${a.future.value.get("a")}, y=${y}` } }, |
| 17 | + { id: "B" }, |
| 18 | + ); |
| 19 | + |
| 20 | + // publish the module on substrate.run |
| 21 | + // const publication = await substrate.module.publish({ |
| 22 | + // name: "my reusable graph", |
| 23 | + // nodes: [a, b], |
| 24 | + // inputs: { x, y, z }, |
| 25 | + // }); |
| 26 | + |
| 27 | + // update the module on substrate.run |
| 28 | + // const updated = await substrate.module.publish({ |
| 29 | + // id: publication.id, |
| 30 | + // name: "my reusable graph (edited)", |
| 31 | + // nodes: [a, b], |
| 32 | + // inputs: { x, y, z }, |
| 33 | + // }); |
| 34 | + |
| 35 | + // using the module from JSON |
| 36 | + const mod = new Module({ |
| 37 | + module_json: substrate.module.serialize({ |
| 38 | + nodes: [a, b], |
| 39 | + inputs: { x, y, z }, |
| 40 | + }), |
| 41 | + inputs: { |
| 42 | + // when commented will use "hello" because it is defined as the default above |
| 43 | + // x: 123, |
| 44 | + y: "yyy", |
| 45 | + z: { |
| 46 | + arr: ["123"], |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + // using the module from publication/module id |
| 52 | + // const mod = new Module({ |
| 53 | + // module_id: publication.id, |
| 54 | + // inputs: { y: "yyy", z: { arr: ["123"] } }, |
| 55 | + // }); |
| 56 | + |
| 57 | + // using the module from publication/module uri |
| 58 | + // const mod = new Module({ |
| 59 | + // module_uri: publication.uri, |
| 60 | + // inputs: { y: "yyy", z: { arr: ["123"] } }, |
| 61 | + // }); |
| 62 | + |
| 63 | + const c = new Box( |
| 64 | + { |
| 65 | + value: { |
| 66 | + "1": mod.future.get("A.value.z.arr[0]"), |
| 67 | + "2": mod.future.get("B.value.b"), |
| 68 | + }, |
| 69 | + }, |
| 70 | + { id: "C" }, |
| 71 | + ); |
| 72 | + |
| 73 | + const res = await substrate.run(mod, c); |
| 74 | + console.log(JSON.stringify(res.json, null, 2)); |
| 75 | +} |
| 76 | +main(); |
0 commit comments