-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/methods/appValidator/errorTemplates/handlerNotFunction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters