Skip to content

Commit 65c0f0b

Browse files
committed
Update index.ts
1 parent 3c8a8a2 commit 65c0f0b

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/index.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
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>
45

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+
},
921
}
1022

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

Comments
 (0)