Skip to content

Commit cb97da7

Browse files
authored
Merge pull request #4 from sweeetland/revert-3-allowMultipleHooksObjects
Revert "Allow multiple hooks objects, create types file"
2 parents 430e11a + 32d98a5 commit cb97da7

File tree

11 files changed

+67
-112
lines changed

11 files changed

+67
-112
lines changed

.eslintrc.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
module.exports = {
2-
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3-
extends: [
4-
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
5-
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
6-
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
7-
],
8-
parserOptions: {
9-
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
10-
sourceType: 'module', // Allows for the use of imports
11-
},
12-
rules: {
13-
'@typescript-eslint/explicit-function-return-type': 'off',
14-
'@typescript-eslint/no-explicit-any': 'off',
15-
'@typescript-eslint/no-use-before-define': 'off',
16-
'@typescript-eslint/no-non-null-assertion': 'off',
17-
'@typescript-eslint/no-var-requires': 'off',
18-
},
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
extends: [
4+
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
5+
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
6+
'plugin:prettier/recommended' // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
7+
],
8+
parserOptions: {
9+
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
10+
sourceType: 'module' // Allows for the use of imports
11+
},
12+
rules: {
13+
'@typescript-eslint/explicit-function-return-type': 'off',
14+
'@typescript-eslint/no-explicit-any': 'off',
15+
'@typescript-eslint/no-use-before-define': 'off',
16+
'@typescript-eslint/no-non-null-assertion': 'off'
17+
}
1918
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const withHooks = useHooks({
7171
3. useHooks returns a function withHooks. Pass your **async** lambda into the withHooks function to decorate your lambda and then export as normal.
7272

7373
```javascript
74-
const handler = async (event, context) => {...}
74+
const handler = async (event, context) => {...}
7575

7676
exports.handler = withHooks(handler)
7777
```

src/hooks/handleScheduledEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HookCreator } from '../types/hooks'
1+
import { HookCreator } from '../index'
22

33
export const handleScheduledEvent: HookCreator = () => async state => {
44
const { event } = state

src/hooks/handleUnexpectedError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HookCreator } from '../types/hooks'
1+
import { HookCreator } from '../index'
22

33
export const handleUnexpectedError: HookCreator = () => async state => {
44
const { error } = state

src/hooks/logEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HookCreator } from '../types/hooks'
1+
import { HookCreator } from '../index'
22

33
export const logEvent: HookCreator = () => async state => {
44
console.log(`received event: ${state.event}`)

src/hooks/parseEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HookCreator } from '../types/hooks'
1+
import { HookCreator } from '../index'
22

33
export const parseEvent: HookCreator = () => async state => {
44
const { event } = state

src/hooks/validateEventBody.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ObjectSchema } from 'yup'
22

3-
import { HookCreator } from '../types/hooks'
3+
import { HookCreator } from '../index'
44

55
interface Config {
66
requestSchema: ObjectSchema

src/index.ts

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { Context } from 'aws-lambda'
22

3-
import { Event, HooksObject, UseHooks, UseHooksState } from './types/hooks'
4-
import { combineHooks } from './utils'
5-
import { defaultHook } from './utils/defaultHook'
6-
73
export {
84
handleScheduledEvent,
95
handleUnexpectedError,
@@ -12,28 +8,62 @@ export {
128
validateEventBody,
139
} from './hooks'
1410

15-
export * from './types/hooks'
11+
interface Hooks {
12+
before?: HookHandler[]
13+
after?: HookHandler[]
14+
onError?: HookHandler[]
15+
}
16+
17+
type Response = any
18+
type Event = any
19+
20+
interface State {
21+
event: Event
22+
context: Context
23+
exit: boolean
24+
response?: Response
25+
error?: Error
26+
}
1627

1728
/**
18-
* Using the provided hooks create an `withHooks` higher order function
19-
* @param hooks a variadic array of config objects containing the hooks to apply to your lambda. Each config object can contain `before`, `after` and `onError` arrays
20-
* @returns WithHooks() function that wraps around your lambda
29+
* @param config optional configuration object for this hook
30+
* @returns HookHandler
2131
*/
22-
export const useHooks: UseHooks = (...hooksArr) => {
23-
if (!hooksArr) {
24-
hooksArr = [defaultHook]
25-
}
32+
export type HookCreator<Config = {}> = (config?: Config) => HookHandler
33+
/**
34+
* @param state a state object that might be manipulated by this function
35+
* @param state.event event passed in from AWS
36+
* @param state.context context passed in from AWS
37+
* @param state.exit defaults to false, if set to true program will exit early after ivocation of this hook
38+
* @param state.response returned when state.exit is set to true
39+
* @param state.error exists only if there's an unhandled exception thrown inside a hook or the lambda
40+
* @returns Promise<state>
41+
*/
42+
type HookHandler = (state: State) => Promise<State>
2643

27-
const hooks: HooksObject = combineHooks(hooksArr)
44+
type UseHooks = (hooks: Hooks) => WithHooks
45+
type WithHooks = (lambda: any) => (event: any, context: Context) => Promise<any>
46+
/**
47+
* Using the provided hooks create an withHooks higher order function
48+
* @param hooks a config object of the hooks to apply to your lambda
49+
* @param hooks.before an array of hooks to run before the provided lambda
50+
* @param hooks.after an array of hooks to run after the provided lambda
51+
* @param hooks.onError an array of hooks to run only if there's an error during the execution
52+
* @returns WithHooks() function that wraps around your lambda
53+
*/
54+
export const useHooks: UseHooks = (hooks: Hooks): WithHooks => {
55+
if (!hooks.before) hooks.before = []
56+
if (!hooks.after) hooks.after = []
57+
if (!hooks.onError) hooks.onError = []
2858

2959
/**
3060
* Higher order function that takes a lambda function
3161
* as input and applies the hooks provided to useHooks()
3262
* @param lambda lambda function
33-
* @returns supercharged lambda 🚀
63+
* @returns supercharged lambda 🚀
3464
*/
3565
const withHooks = (lambda: any) => async (event: Event, context: Context) => {
36-
let state: UseHooksState = { event, context, exit: false }
66+
let state: State = { event, context, exit: false }
3767

3868
try {
3969
for (const hook of hooks.before!) {

src/types/hooks.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/utils/defaultHook.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)