kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: refactor signature and add tests
rodzic
32e971f4a2
commit
3e69080fdf
|
@ -34,24 +34,27 @@ export class NovuClient {
|
||||||
this.baseUrl = baseUrl
|
this.baseUrl = baseUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
async triggerEvent(
|
async triggerEvent({
|
||||||
name: string,
|
name,
|
||||||
payload: Record<string, unknown>,
|
payload,
|
||||||
|
to
|
||||||
|
}: {
|
||||||
|
name: string
|
||||||
|
payload: Record<string, unknown>
|
||||||
to: NovuSubscriber[]
|
to: NovuSubscriber[]
|
||||||
) {
|
}) {
|
||||||
const url = `${this.baseUrl}/events/trigger`
|
const url = `${this.baseUrl}/events/trigger`
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `ApiKey ${this.apiKey}`,
|
Authorization: `ApiKey ${this.apiKey}`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
const body = JSON.stringify({
|
|
||||||
name,
|
|
||||||
payload,
|
|
||||||
to
|
|
||||||
})
|
|
||||||
const response = await ky.post(url, {
|
const response = await ky.post(url, {
|
||||||
headers,
|
headers,
|
||||||
body
|
json: {
|
||||||
|
name,
|
||||||
|
payload,
|
||||||
|
to
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return response.json<NovuTriggerEventResponse>()
|
return response.json<NovuTriggerEventResponse>()
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,11 +69,7 @@ export class NovuNotificationTool extends BaseTask<
|
||||||
// TODO: handle errors gracefully
|
// TODO: handle errors gracefully
|
||||||
input = this.inputSchema.parse(input)
|
input = this.inputSchema.parse(input)
|
||||||
|
|
||||||
const result = await this._novuClient.triggerEvent(
|
const result = await this._novuClient.triggerEvent(input)
|
||||||
input.name,
|
|
||||||
input.payload,
|
|
||||||
input.to
|
|
||||||
)
|
|
||||||
return {
|
return {
|
||||||
result,
|
result,
|
||||||
metadata: {}
|
metadata: {}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
import test from 'ava'
|
||||||
|
|
||||||
|
import { NovuClient } from '../src/services/novu'
|
||||||
|
import './_utils'
|
||||||
|
|
||||||
|
test('NovuClient.triggerEvent', async (t) => {
|
||||||
|
if (!process.env.NOVU_API_KEY) {
|
||||||
|
return t.pass()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.timeout(2 * 60 * 1000)
|
||||||
|
const client = new NovuClient()
|
||||||
|
|
||||||
|
const result = await client.triggerEvent({
|
||||||
|
name: 'send-email',
|
||||||
|
payload: {
|
||||||
|
content: 'Hello World!'
|
||||||
|
},
|
||||||
|
to: [
|
||||||
|
{
|
||||||
|
subscriberId: '123456',
|
||||||
|
email: 'pburckhardt@outlook.com'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
t.truthy(result)
|
||||||
|
})
|
Ładowanie…
Reference in New Issue