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

Revert "Allow multiple hooks objects, create types file" #4

Merged
merged 1 commit into from
Mar 9, 2020
Merged
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
33 changes: 16 additions & 17 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'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.
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
},
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
},
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'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.
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module' // Allows for the use of imports
},
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-non-null-assertion': 'off'
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const withHooks = useHooks({
3. useHooks returns a function withHooks. Pass your **async** lambda into the withHooks function to decorate your lambda and then export as normal.

```javascript
const handler = async (event, context) => {...}
const handler = async (event, context) => {...}

exports.handler = withHooks(handler)
```
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/handleScheduledEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HookCreator } from '../types/hooks'
import { HookCreator } from '../index'

export const handleScheduledEvent: HookCreator = () => async state => {
const { event } = state
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/handleUnexpectedError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HookCreator } from '../types/hooks'
import { HookCreator } from '../index'

export const handleUnexpectedError: HookCreator = () => async state => {
const { error } = state
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/logEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HookCreator } from '../types/hooks'
import { HookCreator } from '../index'

export const logEvent: HookCreator = () => async state => {
console.log(`received event: ${state.event}`)
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/parseEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HookCreator } from '../types/hooks'
import { HookCreator } from '../index'

export const parseEvent: HookCreator = () => async state => {
const { event } = state
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/validateEventBody.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectSchema } from 'yup'

import { HookCreator } from '../types/hooks'
import { HookCreator } from '../index'

interface Config {
requestSchema: ObjectSchema
Expand Down
60 changes: 45 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Context } from 'aws-lambda'

import { Event, HooksObject, UseHooks, UseHooksState } from './types/hooks'
import { combineHooks } from './utils'
import { defaultHook } from './utils/defaultHook'

export {
handleScheduledEvent,
handleUnexpectedError,
Expand All @@ -12,28 +8,62 @@ export {
validateEventBody,
} from './hooks'

export * from './types/hooks'
interface Hooks {
before?: HookHandler[]
after?: HookHandler[]
onError?: HookHandler[]
}

type Response = any
type Event = any

interface State {
event: Event
context: Context
exit: boolean
response?: Response
error?: Error
}

/**
* Using the provided hooks create an `withHooks` higher order function
* @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
* @returns WithHooks() function that wraps around your lambda
* @param config optional configuration object for this hook
* @returns HookHandler
*/
export const useHooks: UseHooks = (...hooksArr) => {
if (!hooksArr) {
hooksArr = [defaultHook]
}
export type HookCreator<Config = {}> = (config?: Config) => HookHandler
/**
* @param state a state object that might be manipulated by this function
* @param state.event event passed in from AWS
* @param state.context context passed in from AWS
* @param state.exit defaults to false, if set to true program will exit early after ivocation of this hook
* @param state.response returned when state.exit is set to true
* @param state.error exists only if there's an unhandled exception thrown inside a hook or the lambda
* @returns Promise<state>
*/
type HookHandler = (state: State) => Promise<State>

const hooks: HooksObject = combineHooks(hooksArr)
type UseHooks = (hooks: Hooks) => WithHooks
type WithHooks = (lambda: any) => (event: any, context: Context) => Promise<any>
/**
* Using the provided hooks create an withHooks higher order function
* @param hooks a config object of the hooks to apply to your lambda
* @param hooks.before an array of hooks to run before the provided lambda
* @param hooks.after an array of hooks to run after the provided lambda
* @param hooks.onError an array of hooks to run only if there's an error during the execution
* @returns WithHooks() function that wraps around your lambda
*/
export const useHooks: UseHooks = (hooks: Hooks): WithHooks => {
if (!hooks.before) hooks.before = []
if (!hooks.after) hooks.after = []
if (!hooks.onError) hooks.onError = []

/**
* Higher order function that takes a lambda function
* as input and applies the hooks provided to useHooks()
* @param lambda lambda function
* @returns supercharged lambda 🚀
* @returns supercharged lambda 🚀
*/
const withHooks = (lambda: any) => async (event: Event, context: Context) => {
let state: UseHooksState = { event, context, exit: false }
let state: State = { event, context, exit: false }

try {
for (const hook of hooks.before!) {
Expand Down
47 changes: 0 additions & 47 deletions src/types/hooks.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils/defaultHook.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/utils/index.ts

This file was deleted.