1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-24 04:31:31 +03:00

feat(bridge/nodejs): add Network module (WIP)

This commit is contained in:
Divlo 2023-05-06 14:21:24 +02:00
parent 2cb030f47b
commit de08807f1a
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 40 additions and 3 deletions

View File

@ -11,5 +11,8 @@
"homepage": "https://getleon.ai",
"bugs": {
"url": "https://github.com/leon-ai/leon/issues"
},
"dependencies": {
"axios": "1.4.0"
}
}

View File

@ -0,0 +1,22 @@
import axios from 'axios'
import type { AxiosInstance } from 'axios'
export interface NetworkOptions {
baseURL?: string
}
export class Network {
private options: NetworkOptions
private axios: AxiosInstance
public constructor(options: NetworkOptions = {}) {
this.options = options
this.axios = axios.create({
baseURL: this.options.baseURL
})
}
public async get<T>(url: string): Promise<T> {
return (await this.axios.get<T>(url)).data as T
}
}

View File

@ -9,6 +9,6 @@
"answers": {
"default": ["I'm..."],
"greet": ["Hey, just a try %name% again %name%", "Another try, hi"],
"config": ["%config%"]
"answer": ["%answer%"]
}
}

View File

@ -1,5 +1,6 @@
import type { ActionFunction } from '@sdk/types'
import { leon } from '@sdk/leon'
import { Network } from '@sdk/network'
import { Button } from '@sdk/aurora/button'
export const run: ActionFunction = async function () {
@ -20,9 +21,20 @@ export const run: ActionFunction = async function () {
const options = leon.getSRCConfig<{ someSampleConfig: string }>('options')
await leon.answer({
key: 'config',
key: 'answer',
data: {
config: options.someSampleConfig + someSampleConfig
answer: options.someSampleConfig + someSampleConfig
}
})
const network = new Network({
baseURL: 'https://jsonplaceholder.typicode.com'
})
const data = await network.get<{ title: string }>('/todos/1')
await leon.answer({
key: 'answer',
data: {
answer: `Todo n°1: ${data.title}`
}
})
}