@benshi.ai/js-sdk
Version:
Benshi SDK
54 lines (41 loc) • 1.29 kB
text/typescript
import fetch from 'node-fetch';
import { NetworkProxy } from './typings'
export default class BsNetwork implements NetworkProxy{
private config
constructor(key: string) {
this.config = {
headers: {
'Authorization': `Bearer ${key}`
}
}
}
public async get(url:string):Promise<Object> {
try {
const config = {
...this.config,
method: 'GET'
}
const response = (await fetch(`${url}`, config))
const fetchedData = await response.json()
console.log('Data Fetched: ', fetchedData)
return fetchedData
} catch (e) {
console.error('Error retrieving data: ', e)
throw new Error('error-fetching-data')
}
}
public async send(url:string, payload: any):Promise<Object> {
try {
const config = {
...this.config,
body: JSON.stringify(payload),
method: 'POST'
}
const postData = (await fetch(url, config))
return postData
} catch(e) {
console.error('Error sending data: ', e)
throw new Error('error-sending-data')
}
}
}