|
1 |
| -export interface HelloOptions { |
2 |
| - log?: boolean |
3 |
| -} |
| 1 | +import { Plugin } from '@posthog/plugin-scaffold' |
| 2 | +import type { RequestInfo, RequestInit, Response } from 'node-fetch' |
| 3 | + |
| 4 | +declare function fetch(url: RequestInfo, init?: RequestInit): Promise<Response> |
4 | 5 |
|
5 |
| -export function hello(options?: HelloOptions): string { |
6 |
| - const messageForYou = 'This is where it all starts' |
7 |
| - if (options?.log) console.log(messageForYou) |
8 |
| - return messageForYou |
| 6 | +/** The famed Hello World plugin itself. */ |
| 7 | +const helloWorld: Plugin = { |
| 8 | + setupPlugin: async ({ config }) => { |
| 9 | + console.log('Setting up the plugin!') |
| 10 | + console.log(config) |
| 11 | + }, |
| 12 | + processEvent: async (event, { config, cache }) => { |
| 13 | + const counterValue = (await cache.get('greeting_counter', 0)) as number |
| 14 | + cache.set('greeting_counter', counterValue + 1) |
| 15 | + if (!event.properties) event.properties = {} |
| 16 | + event.properties['greeting'] = config.greeting |
| 17 | + event.properties['greeting_counter'] = counterValue |
| 18 | + event.properties['random'] = await fetchTrulyRandomInteger() |
| 19 | + return event |
| 20 | + }, |
9 | 21 | }
|
10 | 22 |
|
11 |
| -hello() |
| 23 | +export default helloWorld |
| 24 | + |
| 25 | +/** Internal library function. */ |
| 26 | +async function fetchTrulyRandomInteger(): Promise<number> { |
| 27 | + const response = await fetch( |
| 28 | + 'https://www.random.org/integers/?num=1&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new' |
| 29 | + ) |
| 30 | + const integer = parseInt(await response.text()) |
| 31 | + return integer |
| 32 | +} |
0 commit comments