From 46b9b5b87cf036bd3ddcaeb5f8abc163dffd5d6f Mon Sep 17 00:00:00 2001 From: DR Date: Sat, 27 Apr 2024 16:09:39 -0400 Subject: [PATCH] fix: support for fetch in commands --- README.md | 14 ++++++++++++++ resources/commands/commands.ts | 11 +++++++++++ resources/commands/tsconfig.json | 2 +- src/main/framework/commands.ts | 1 + src/main/framework/runtime-executor.ts | 6 +++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b3b2ac..66cb303 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,20 @@ Now run `hello X` from mterm - > In this case, no argument was provided so the `os.userInfo().username` was the fallback. `Hello, DR` is the result! +Try fetching data! + +```typescript +export async function query(): Promise<{ + userId: number + id: number + title: string + completed: boolean +}> { + const response = await fetch('https://jsonplaceholder.typicode.com/todos/1') + + return await response.json() +} +``` ### Project Setup diff --git a/resources/commands/commands.ts b/resources/commands/commands.ts index 84f3316..f4da3b8 100644 --- a/resources/commands/commands.ts +++ b/resources/commands/commands.ts @@ -2,3 +2,14 @@ import * as os from 'node:os' export function hello(name: string = os.userInfo().username): string { return `Hi, ${name}` } + +export async function query(): Promise<{ + userId: number + id: number + title: string + completed: boolean +}> { + const response = await fetch('https://jsonplaceholder.typicode.com/todos/1') + + return await response.json() +} diff --git a/resources/commands/tsconfig.json b/resources/commands/tsconfig.json index 2cf7462..7677228 100644 --- a/resources/commands/tsconfig.json +++ b/resources/commands/tsconfig.json @@ -4,7 +4,7 @@ "outDir": "./dist", "noImplicitAny": false, "module": "esnext", - "target": "es5", + "target": "esnext", "allowJs": true, "moduleResolution": "node" } diff --git a/src/main/framework/commands.ts b/src/main/framework/commands.ts index 18af518..8f6e0c0 100644 --- a/src/main/framework/commands.ts +++ b/src/main/framework/commands.ts @@ -22,6 +22,7 @@ export class Commands { setTimeout = global.setTimeout console = global.console + fetch = global.fetch has(key: string): boolean { return !!this.lib[key] diff --git a/src/main/framework/runtime-executor.ts b/src/main/framework/runtime-executor.ts index b5bfe77..cdd5fe6 100644 --- a/src/main/framework/runtime-executor.ts +++ b/src/main/framework/runtime-executor.ts @@ -30,13 +30,17 @@ export async function execute(context: ExecuteContext): Promise // check for user commands if (workspace.commands.has(cmd)) { - const result = await Promise.resolve(workspace.commands.run(context, cmd, ...args)) + let result = await Promise.resolve(workspace.commands.run(context, cmd, ...args)) if (!result) { // nothing was replied with, assume this is a run that will happen in time return false } + if (typeof result === 'object') { + result = JSON.stringify(result, null, 2) + } + out(`${result}`) return }