Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Finalise plugin #7

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'simple-import-sort'],
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
ignorePatterns: ['bin', 'dist', 'node_modules'],
rules: {
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
curly: 'error',
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
36 changes: 18 additions & 18 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
pull_request:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up node
uses: actions/setup-node@v1
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Set up node
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Install dependencies
run: yarn install
- name: Typecheck
run: yarn typecheck
- name: Run tests
run: yarn test
35 changes: 5 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,14 @@

[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg?style=flat-square)](https://opensource.org/licenses/MIT)

This is a basic exemplary PostHog plugin. It adds property `"greeting"` to every event, with a configurable value (default: `"Hello world!"`).

Feel free to use it as a base for your own plugins!
This is a basic webhook app.

## How to develop

All of the plugin's code is located in the `index.js` file, which is JavaScript ran inside of PostHog.
To get yourself up to speed with this environment, we sincerely recommend checking out our [Plugins overview in PostHog Docs]([the Plugins Overview](https://posthog.com/docs/plugins/build/overview).
For a crash course, read our [plugin building tutorial in PostHog Docs](https://posthog.com/docs/plugins/build/tutorial).

## How to test

To test the plugin, you'll need to install a few `npm` dependencies already specified in `package.json`:
```bash
npm install
```

This will get you the testing library Jest and some our test helpers.
Then to run tests it's just:

```bash
npm test
```

## How to install

1. Open PostHog.
1. Open the Plugins page from the sidebar.
1. Head to the Advanced tab.
1. "Install from GitHub, GitLab or npm" using this repository's URL.

## Questions?

### [Join our Slack community.](https://join.slack.com/t/posthogusers/shared_invite/enQtOTY0MzU5NjAwMDY3LTc2MWQ0OTZlNjhkODk3ZDI3NDVjMDE1YjgxY2I4ZjI4MzJhZmVmNjJkN2NmMGJmMzc2N2U3Yjc3ZjI5NGFlZDQ)

We're here to help you with anything PostHog!
yarn install
yarn typecheck
yarn test
```
26 changes: 0 additions & 26 deletions index.js

This file was deleted.

61 changes: 0 additions & 61 deletions index.test.js

This file was deleted.

25 changes: 25 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PostHogEvent, Webhook } from '@posthog/plugin-scaffold'

const { composeWebhook } = require('./index')

test('creates the correct webhook body', async () => {
const event: PostHogEvent = {
uuid: '123',
team_id: 1,
distinct_id: 'alice',
event: 'pageview',
timestamp: new Date('2023-01-01T00:00:00.000Z'),
properties: {
prop: 'value',
},
}
const webhook: Webhook = composeWebhook(event, { config: { url: 'https://example.com' } })
expect(webhook).toEqual({
url: 'https://example.com',
body: '{"uuid":"123","team_id":1,"distinct_id":"alice","event":"pageview","timestamp":"2023-01-01T00:00:00.000Z","properties":{"prop":"value"}}',
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
})
})
13 changes: 13 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PostHogEvent, Webhook } from '@posthog/plugin-scaffold'

export function composeWebhook(event: PostHogEvent, { config }: any): Webhook {
return {
url: config.url,
body: JSON.stringify(config.payload || event),
headers: {
'Content-Type': 'application/json',
...config.headers,
},
method: config.method || 'POST',
}
}
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
preset: 'ts-jest',
moduleDirectories: ['node_modules', 'src'],
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
},
transformIgnorePatterns: [],
globals: {
'ts-jest': {
// Without isolatedModules, tests run realy slow, so we enable them
isolatedModules: true,
},
},
}
Loading
Loading