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:
parent
2cb030f47b
commit
de08807f1a
@ -11,5 +11,8 @@
|
||||
"homepage": "https://getleon.ai",
|
||||
"bugs": {
|
||||
"url": "https://github.com/leon-ai/leon/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "1.4.0"
|
||||
}
|
||||
}
|
||||
|
22
bridges/nodejs/src/sdk/network.ts
Normal file
22
bridges/nodejs/src/sdk/network.ts
Normal 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
|
||||
}
|
||||
}
|
@ -9,6 +9,6 @@
|
||||
"answers": {
|
||||
"default": ["I'm..."],
|
||||
"greet": ["Hey, just a try %name% again %name%", "Another try, hi"],
|
||||
"config": ["%config%"]
|
||||
"answer": ["%answer%"]
|
||||
}
|
||||
}
|
||||
|
@ -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}`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user