Skip to content

Commit

Permalink
validate app on registration (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
efstajas authored Apr 26, 2020
1 parent 64e3625 commit a07ece5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@efstajas/tela",
"version": "0.3.0",
"version": "0.3.2",
"description": "A framework for building Intercom Canvas Kit applications.",
"main": "lib/index.js",
"scripts": {
"build": "tsc"
},
"keywords": ["intercom", "canvas kit", "custom apps", "express"],
"keywords": ["intercom", "canvas kit", "custom apps", "express", "typescript"],
"repository": {
"type" : "git",
"url" : "https://github.com/efstajas/tela.git"
Expand Down
2 changes: 2 additions & 0 deletions src/methods/appValidator/errorTemplates/handlerNotFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (appName: string, methodName: string) =>
`Expected handler ${methodName} of app ${appName} to be function that returns Promise<Component[]> or Component[]`
2 changes: 2 additions & 0 deletions src/methods/appValidator/errorTemplates/hookNotFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (appName: string, hookName: string) =>
`Expected hook ${hookName} of app ${appName} to be function like (req, res, next, context) => void`
2 changes: 2 additions & 0 deletions src/methods/appValidator/errorTemplates/hooksNotObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (appName: string) =>
`Expected 'hooks' member of app ${appName} to be object of shape { [key: string]: (req, res, next, context) => void }`
2 changes: 2 additions & 0 deletions src/methods/appValidator/errorTemplates/missingMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (appName: string, methodName: string) =>
`App ${appName} is missing required method ${methodName}`
50 changes: 50 additions & 0 deletions src/methods/appValidator/validateApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { App } from '../../app.types'
import missingMethodTemplate from './errorTemplates/missingMethod'
import hooksNotObjectTemplate from './errorTemplates/hooksNotObject'
import handlerNotFunctionTemplate from './errorTemplates/handlerNotFunction'
import hookNotFunctionTemplate from './errorTemplates/hookNotFunction'

export default (appName: string, app: App) => {
const requiredMembers = ['initialize']
const optionalMembers = ['submit', 'configure', 'hooks']
const possibleMembers = requiredMembers.concat(optionalMembers)

const appMethods = Object.keys(app)

// check if all required methods are present

requiredMembers.forEach((key: string) => {
if (!appMethods.includes(key)) {
throw new Error(missingMethodTemplate(appName, key))
}
})

// Check if members are of expected type

const appAsAny = (app as any)

possibleMembers.forEach((key: string) => {
const memberType = typeof appAsAny[key]

if (appAsAny[key]) {
switch (key) {
case 'hooks':
if (memberType !== 'object') {
throw new Error(hooksNotObjectTemplate(appName))
}

Object.keys(appAsAny.hooks).forEach((h: string) => {
if (typeof appAsAny.hooks[h] !== 'function') {
throw new Error(hookNotFunctionTemplate(appName, h))
}
})
break
default:
if (memberType !== 'function') {
throw new Error(handlerNotFunctionTemplate(appName, key))
}
break
}
}
})
}
3 changes: 3 additions & 0 deletions src/tela.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import * as bodyParser from 'body-parser'
import { App } from './app.types'
import constructHandlerContext from './methods/construct-handler-context'
import constructRouter from './methods/construct-router'
import validateApp from './methods/appValidator/validateApp'

export default class Tela {
private server = express().use(bodyParser.json());

public registerApp = async (appName: string, app: App) => {
validateApp(appName, app)

const router = constructRouter(appName, app)

const path = `/${appName.toLowerCase()}`
Expand Down

0 comments on commit a07ece5

Please sign in to comment.