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

Convert from onEvent to composeWebhook and convert to Typescript #4

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 120

[*.md]
trim_trailing_whitespace = false
20 changes: 11 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +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: {},
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
63 changes: 60 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,73 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Editors
# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# environment variables file
.env
.environment

# next.js build output
.next

# editors
.vscode
.idea

# Yalc
# yalc
.yalc
yalc*

# macOS
.DS_Store
.DS_Store

# output
dist/
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
32 changes: 0 additions & 32 deletions __tests__/index.js

This file was deleted.

75 changes: 0 additions & 75 deletions index.js

This file was deleted.

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

export interface TwilioMetaInput {
config: {
accountSID: string
authToken: string
triggeringEventsAndNumber: string // Format: eventName:PhoneNumber,eventName:PhoneNumber
senderPhoneNumber: string
timeout: string // This should get deprecated since it won't be used going forward
}
}

const plugin: Plugin<TwilioMetaInput> = {
composeWebhook: (event, { config }) => {
const eventAndNumberMap: Record<string, string> = {}

const eventToNumberPairs = (config.triggeringEventsAndNumber || '').split(',').map((value) => value.trim())

if (eventToNumberPairs.length === 0) {
return null
}

for (const pair of eventToNumberPairs) {
const [event, num] = (pair || '').split(':').map((value) => value.trim())
eventAndNumberMap[event] = num
}

if (!(event.event in eventAndNumberMap)) {
return null
}

const number = eventAndNumberMap[event.event]

return {
url: `https://api.twilio.com/2010-04-01/Accounts/${config.accountSID}/Messages.json`,
headers: {
Authorization: `Basic ${Buffer.from(`${config.accountSID}:${config.authToken}`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `Body=Hi ${event.event} occurred - PostHog&From=${config.senderPhoneNumber}&To=${number}`,
method: 'POST',
} as Webhook
},
}

export default plugin
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils')
const { compilerOptions } = require('./tsconfig')

const moduleNameMapper = undefined
if (compilerOptions.paths) {
moduleNameMapper = pathsToModuleNameMapper(compilerOptions.paths, { prefix: 'src/' })
}

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper,
}
Loading