Skip to content

Commit

Permalink
refactor/code: code cleanup to improve type safety and dev experience (
Browse files Browse the repository at this point in the history
…#313)

The following fixes were put in
- make typecasts explicit
- remove dead SMS trigger code
- import apps statically instead of dynamically through the file system
- additional tests to enforce fixes
- remove circular imports for if-then
- remove unused type root for build
---------

Co-authored-by: Kuan Wee Loong <[email protected]>
  • Loading branch information
liangyuanruo and ogp-weeloong authored Nov 8, 2023
1 parent eb81ae6 commit 6606533
Show file tree
Hide file tree
Showing 52 changed files with 278 additions and 279 deletions.
22 changes: 22 additions & 0 deletions packages/backend/src/apps/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs'
import { join } from 'path'
import { describe, expect, it } from 'vitest'

import exportedApps from '@/apps'

describe('index.ts', () => {
it('should export all apps keys that also match their folder names', () => {
const exportedAppNames = Object.keys(exportedApps)

// Dynamic apps
const folderPath = join(__dirname, '..')

const appFolderNames = fs
.readdirSync(folderPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && dirent.name !== '__tests__')
.map((dirent) => dirent.name)
// Set comparison
expect(exportedAppNames.length).toEqual(appFolderNames.length)
expect(exportedAppNames.sort()).toEqual(appFolderNames.sort())
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { URL } from 'url'
import { IRawAction } from '@plumber/types'

import defineAction from '@/helpers/define-action'
import { URL } from 'url'

import { isUrlAllowed } from '../../common/ip-resolver'

type TMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'

export default defineAction({
const action: IRawAction = {
name: 'Make a HTTP Request',
key: 'httpRequest',
description: 'Makes a custom HTTP request by providing raw details.',
Expand Down Expand Up @@ -76,4 +76,6 @@ export default defineAction({

$.setActionItem({ raw: { data: responseData } })
},
})
}

export default action
8 changes: 5 additions & 3 deletions packages/backend/src/apps/custom-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import defineApp from '@/helpers/define-app'
import { IApp } from '@plumber/types'

import addHeaders from './common/add-headers'
import actions from './actions'
import auth from './auth'

export default defineApp({
const app: IApp = {
name: 'Custom API',
key: 'custom-api',
iconUrl: '{BASE_URL}/apps/custom-api/assets/favicon.svg',
Expand All @@ -16,4 +16,6 @@ export default defineApp({
apiBaseUrl: '',
primaryColor: '0059F7',
actions,
})
}

export default app
8 changes: 5 additions & 3 deletions packages/backend/src/apps/delay/actions/delay-for/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import defineAction from '@/helpers/define-action'
import { IRawAction } from '@plumber/types'

export default defineAction({
const action: IRawAction = {
name: 'Delay For',
key: 'delayFor',
description:
Expand Down Expand Up @@ -59,4 +59,6 @@ export default defineAction({

$.setActionItem({ raw: dataItem })
},
})
}

export default action
8 changes: 5 additions & 3 deletions packages/backend/src/apps/delay/actions/delay-until/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import defineAction from '@/helpers/define-action'
import { IRawAction } from '@plumber/types'

import generateTimestamp from '../../helpers/generate-timestamp'

export default defineAction({
const action: IRawAction = {
name: 'Delay Until',
key: 'delayUntil',
description:
Expand Down Expand Up @@ -47,4 +47,6 @@ export default defineAction({

$.setActionItem({ raw: dataItem })
},
})
}

export default action
8 changes: 5 additions & 3 deletions packages/backend/src/apps/delay/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import defineApp from '@/helpers/define-app'
import { IApp } from '@plumber/types'

import actions from './actions'

export default defineApp({
const app: IApp = {
name: 'Delay',
key: 'delay',
iconUrl: '{BASE_URL}/apps/delay/assets/favicon.svg',
Expand All @@ -12,4 +12,6 @@ export default defineApp({
apiBaseUrl: '',
primaryColor: '001F52',
actions,
})
}

export default app
8 changes: 5 additions & 3 deletions packages/backend/src/apps/formsg/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defineApp from '@/helpers/define-app'
import { IApp } from '@plumber/types'

import auth from './auth'
import triggers from './triggers'

export default defineApp({
const app: IApp = {
name: 'FormSG',
key: 'formsg',
iconUrl: '{BASE_URL}/apps/formsg/assets/favicon.svg',
Expand All @@ -16,4 +16,6 @@ export default defineApp({
auth,
triggers,
actions: [],
})
}

export default app
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { IGlobalVariable } from '@plumber/types'
import { IGlobalVariable, IRawTrigger } from '@plumber/types'

import isEmpty from 'lodash/isEmpty'

import defineTrigger from '@/helpers/define-trigger'

import {
registerWebhookUrl,
verifyWebhookUrl,
Expand All @@ -18,7 +16,7 @@ export const NricFilter = {
Hash: 'hash',
}

export default defineTrigger({
const trigger: IRawTrigger = {
name: 'New form submission',
key: 'newSubmission',
type: 'webhook',
Expand Down Expand Up @@ -75,4 +73,6 @@ export default defineTrigger({
})
}
},
})
}

export default trigger
29 changes: 29 additions & 0 deletions packages/backend/src/apps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IApp } from '@plumber/types'

import customApiApp from './custom-api'
import delayApp from './delay'
import formsgApp from './formsg'
import postmanApp from './postman'
import schedulerApp from './scheduler'
import slackApp from './slack'
import telegramBotApp from './telegram-bot'
import toolboxApp from './toolbox'
import twilioApp from './twilio'
import vaultWorkspaceApp from './vault-workspace'
import webhookApp from './webhook'

const apps: Record<string, IApp> = {
[customApiApp.key]: customApiApp,
[delayApp.key]: delayApp,
[formsgApp.key]: formsgApp,
[postmanApp.key]: postmanApp,
[schedulerApp.key]: schedulerApp,
[slackApp.key]: slackApp,
[telegramBotApp.key]: telegramBotApp,
[toolboxApp.key]: toolboxApp,
[vaultWorkspaceApp.key]: vaultWorkspaceApp,
[webhookApp.key]: webhookApp,
[twilioApp.key]: twilioApp,
}

export default apps
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IRawAction } from '@plumber/types'

import { SafeParseError } from 'zod'
import { fromZodError } from 'zod-validation-error'

import defineAction from '@/helpers/define-action'
import { getObjectFromS3Id } from '@/helpers/s3'

import { sendTransactionalEmails } from '../../common/email-helper'
Expand All @@ -10,7 +11,7 @@ import { getRatelimitedRecipientList } from '../../common/rate-limit'

import { fields, schema } from './parameters'

export default defineAction({
const action: IRawAction = {
name: 'Send email with attachments',
key: 'sendEmailWithAttachments',
description: "Sends an email with attachments (via Postman's API).",
Expand Down Expand Up @@ -89,4 +90,6 @@ export default defineAction({
}
}
},
})
}

export default action
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IRawAction } from '@plumber/types'

import { SafeParseError } from 'zod'
import { fromZodError } from 'zod-validation-error'

import defineAction from '@/helpers/define-action'

import { sendTransactionalEmails } from '../../common/email-helper'
import {
transactionalEmailFields,
Expand All @@ -11,7 +11,7 @@ import {
import { getDefaultReplyTo } from '../../common/parameters-helper'
import { getRatelimitedRecipientList } from '../../common/rate-limit'

export default defineAction({
const action: IRawAction = {
name: 'Send email',
key: 'sendTransactionalEmail',
description: "Sends an email using Postman's transactional API.",
Expand Down Expand Up @@ -73,4 +73,6 @@ export default defineAction({
}
}
},
})
}

export default action
8 changes: 5 additions & 3 deletions packages/backend/src/apps/postman/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import defineApp from '@/helpers/define-app'
import { IApp } from '@plumber/types'

import actions from './actions'

export default defineApp({
const app: IApp = {
name: 'Email by Postman',
key: 'postman',
iconUrl: '{BASE_URL}/apps/postman/assets/favicon.svg',
Expand All @@ -12,4 +12,6 @@ export default defineApp({
apiBaseUrl: 'https://api.postman.gov.sg',
primaryColor: '000000',
actions,
})
}

export default app
8 changes: 5 additions & 3 deletions packages/backend/src/apps/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import defineApp from '@/helpers/define-app'
import { IApp } from '@plumber/types'

import triggers from './triggers'

export default defineApp({
const app: IApp = {
name: 'Scheduler',
key: 'scheduler',
iconUrl: '{BASE_URL}/apps/scheduler/assets/favicon.svg',
Expand All @@ -13,4 +13,6 @@ export default defineApp({
primaryColor: '0059F7',
supportsConnections: false,
triggers,
})
}

export default app
10 changes: 5 additions & 5 deletions packages/backend/src/apps/scheduler/triggers/every-day/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IGlobalVariable } from '@plumber/types'
import { IGlobalVariable, IRawTrigger } from '@plumber/types'

import { DateTime } from 'luxon'

import defineTrigger from '@/helpers/define-trigger'

import cronTimes from '../../common/cron-times'
import getDateTimeObjectRepresentation from '../../common/get-date-time-object'
import getNextCronDateTime from '../../common/get-next-cron-date-time'
import getDataOutMetadata from '../get-data-out-metadata'

export default defineTrigger({
const trigger: IRawTrigger = {
name: 'Daily - triggers every day, choose a specific hour timeslot',
key: 'everyDay',
description: 'Triggers every day.',
Expand Down Expand Up @@ -169,4 +167,6 @@ export default defineTrigger({

await $.pushTriggerItem(dataItem)
},
})
}

export default trigger
10 changes: 5 additions & 5 deletions packages/backend/src/apps/scheduler/triggers/every-hour/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IGlobalVariable } from '@plumber/types'
import { IGlobalVariable, IRawTrigger } from '@plumber/types'

import { DateTime } from 'luxon'

import defineTrigger from '@/helpers/define-trigger'

import cronTimes from '../../common/cron-times'
import getDateTimeObjectRepresentation from '../../common/get-date-time-object'
import getNextCronDateTime from '../../common/get-next-cron-date-time'
import getDataOutMetadata from '../get-data-out-metadata'

export default defineTrigger({
const trigger: IRawTrigger = {
name: 'Hourly - triggers every hour',
key: 'everyHour',
description: 'Triggers every hour.',
Expand Down Expand Up @@ -62,4 +60,6 @@ export default defineTrigger({

await $.pushTriggerItem(dataItem)
},
})
}

export default trigger
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IGlobalVariable } from '@plumber/types'
import { IGlobalVariable, IRawTrigger } from '@plumber/types'

import { DateTime } from 'luxon'

import defineTrigger from '@/helpers/define-trigger'

import cronTimes from '../../common/cron-times'
import getDateTimeObjectRepresentation from '../../common/get-date-time-object'
import getNextCronDateTime from '../../common/get-next-cron-date-time'
import getDataOutMetadata from '../get-data-out-metadata'

export default defineTrigger({
const trigger: IRawTrigger = {
name: 'Monthly - triggers every month, choose a specific day of the month',
key: 'everyMonth',
description: 'Triggers every month.',
Expand Down Expand Up @@ -286,4 +284,6 @@ export default defineTrigger({

await $.pushTriggerItem(dataItem)
},
})
}

export default trigger
10 changes: 5 additions & 5 deletions packages/backend/src/apps/scheduler/triggers/every-week/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IGlobalVariable } from '@plumber/types'
import { IGlobalVariable, IRawTrigger } from '@plumber/types'

import { DateTime } from 'luxon'

import defineTrigger from '@/helpers/define-trigger'

import cronTimes from '../../common/cron-times'
import getDateTimeObjectRepresentation from '../../common/get-date-time-object'
import getNextCronDateTime from '../../common/get-next-cron-date-time'
import getDataOutMetadata from '../get-data-out-metadata'

export default defineTrigger({
const trigger: IRawTrigger = {
name: 'Weekly - triggers every week, choose a specific day of the week',
key: 'everyWeek',
description: 'Triggers every week.',
Expand Down Expand Up @@ -190,4 +188,6 @@ export default defineTrigger({

await $.pushTriggerItem(dataItem)
},
})
}

export default trigger
Loading

0 comments on commit 6606533

Please sign in to comment.