Skip to content

Commit

Permalink
fix: support for fetch in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
daretodave committed Apr 27, 2024
1 parent 430d9c1 commit 46b9b5b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions resources/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion resources/commands/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "./dist",
"noImplicitAny": false,
"module": "esnext",
"target": "es5",
"target": "esnext",
"allowJs": true,
"moduleResolution": "node"
}
Expand Down
1 change: 1 addition & 0 deletions src/main/framework/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class Commands {

setTimeout = global.setTimeout
console = global.console
fetch = global.fetch

has(key: string): boolean {
return !!this.lib[key]
Expand Down
6 changes: 5 additions & 1 deletion src/main/framework/runtime-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ export async function execute(context: ExecuteContext): Promise<void | boolean>

// 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
}
Expand Down

0 comments on commit 46b9b5b

Please sign in to comment.