Skip to content

runInput

Given an Input, runs its produce() with any provided args.

runInput takes in up to two arguments:

  1. input (required): an Input
  2. context (optional): any properties from an Input Context

runInput returns the result of the Input’s produce().

For example, this Input production reads data from an existing data.json file on disk:

import { createInput, runInput } from 'bingo';
const inputDataJson = createInput({
async produce({ fs }) {
return await JSON.parse(await fs.readFile('data.json'));
},
});
// Type: string
await runInput(inputDataJson);

Any args to provide to produce(), as defined by the input’s args schema.

For example, this Input production reads data from a JSON file on disk by path:

import { Template } from 'bingo';
import { z } from 'zod';
const inputJsonFile = createInput({
args: {
path: z.string(),
},
async produce({ args, fs }) {
return await JSON.parse(await fs.readFile(args.path));
},
});
await runInput(inputJsonFile, {
args: 'data.json',
});

Whether to hint to the input not to make network requests.

This is equivalent to the --offline CLI flag.

For example, this input returns undefined instead of fetching a cat fact when offline is enabled:

import { createInput, runInput } from 'bingo';
import { z } from 'zod';
export const inputCatFact = createInput({
async produce({ fetchers, offline }) {
if (offline) {
return undefined;
}
const response = await fetchers.fetch('https://catfact.ninja/fact');
const data = (await response.json()) as { fact: string };
return data.fact;
},
});
// "Cats can jump up to 7 times their tail length."
await runInput(inputCatFact);
// undefined
await runInput(inputCatFact, { offline: true });
Made with 💝 in Boston byJosh Goldberg.