forked from PostHog/posthog-plugin-replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
35 lines (31 loc) · 1.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Plugin } from '@posthog/plugin-scaffold'
import fetch from 'node-fetch'
export interface ReplicatorMetaInput {
config: {
host: string
project_api_key: string
replication: string
}
}
const plugin: Plugin<ReplicatorMetaInput> = {
exportEvents: async (events, { config }) => {
const batch = []
for (const event of events) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { team_id, now, offset, sent_at, ...sendableEvent } = { ...event, token: config.project_api_key }
const replication = parseInt(config.replication) || 1
for (let i = 0; i < replication; i++) {
batch.push(sendableEvent)
}
}
if (batch.length > 0) {
await fetch(`https://${config.host}/e`, {
method: 'POST',
body: JSON.stringify(batch),
headers: { 'Content-Type': 'application/json' },
})
console.log(`Flushing ${batch.length} event${batch.length > 1 ? 's' : ''} to ${config.host}`)
}
},
}
module.exports = plugin