Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@ export type UserFunction<Input extends {}, Output extends {}> = (
input: Input
) => Output;

interface Javy {
JSON: {
fromStdin(): any;
toStdout(val: any);
}
}

interface ShopifyFunction {
readInput(): any;
writeOutput(val: any);
}

declare global {
const Javy: Javy;
const ShopifyFunction: ShopifyFunction;
}

export default function <I extends {}, O extends {}>(
userfunction: UserFunction<I, O>
) {
try {
ShopifyFunction;
} catch (e) {
if (Javy.JSON) {
const input_obj = Javy.JSON.fromStdin();
const output_obj = userfunction(input_obj);
Javy.JSON.toStdout(output_obj);
} else if (ShopifyFunction) {
const input_obj = ShopifyFunction.readInput();
const output_obj = userfunction(input_obj);
ShopifyFunction.writeOutput(output_obj);
} else {
throw new Error(
"ShopifyFunction is not defined. Please rebuild your function using the latest version of Shopify CLI."
);
}

const input_obj = ShopifyFunction.readInput();
const output_obj = userfunction(input_obj);
ShopifyFunction.writeOutput(output_obj);
}