You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+112-6Lines changed: 112 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ Lambda Hooks help avoid repeated logic in your lambda functions. Use some of the
6
6
7
7
## Principles
8
8
9
-
- Zero dependancies
10
-
- Fast & simple to use
11
-
- First class support for TypeScript & modern JavaScript
9
+
- Zero dependancies 🚫
10
+
- Fast & simple to use 🛤
11
+
- First class support for TypeScript & ES7+ JavaScript 🤓
12
12
13
13
## Example
14
14
@@ -48,15 +48,15 @@ _TypeScript types are included_ 🧰
48
48
49
49
## Usage
50
50
51
-
1.Import/require the package
51
+
1.Require the package
52
52
53
53
```javascript
54
-
constuseHooks=require('lambda-hooks')
54
+
const{ useHooks }=require('lambda-hooks')
55
55
```
56
56
57
57
2. Call useHooks with the hooks that you want to use. There's 3 types of hooks that are executed either before the lambda execution, after or if an error occurs.
58
58
59
-
Note that the order of the hooks matters, they are executed one by one from the first hook in the before array.
59
+
Note that the order of the hooks matters, they are executed one by one starting from the first hook in the before array, then your lambda function is invoked, then through all hooks in the after array. If at any point an error occurs, execution is directed towards the onError hooks array.
60
60
61
61
Also, notice that we are invoking the hooks when they are passed in, this is deliberate and will make more sense when we get to a more complex example later.
62
62
@@ -163,3 +163,109 @@ Woah calm down, actually there are a few rules ☝️
163
163
2. The returned function (HookHandler) must be async or return a promise
164
164
3. The HookHandler accepts the state object as input and must return the state object
165
165
4. Your lambda function must be async
166
+
167
+
## Recommendations
168
+
169
+
> _"with great power, comes great responsibility"_ - someone, somewhere
170
+
171
+
Here's a few recommendations that might make your life easier.
172
+
173
+
-**Export the withHooks function to share across related lambdas.** For example, all your API lambdas might utilise the same hooks, but, your DynamoDB stream lambdas might need to utilise a different set of hooks. Rather than repeating the useHooks call for each lambda, call once and share around the related lambdas...
-**Write your own hooks.** It's really easy to do. And, if you're migrating an existing project over, the logic will barely change. Just remember that to create a hook, you need a function (HookCreator) that returns another function (HookHandler). The HookCreator takes an optional config object. The HookHandler takes the state as input and also returns the state. That is all you need to know!
200
+
201
+
Feel free to share any hooks you make by submitting a PR 😉and, here's a boilerplate hook (that does absolutely nothing) to get you started:
202
+
203
+
```javascript
204
+
exportconstmyNewHook= () =>asyncstate=> {
205
+
const { event, context } = state
206
+
207
+
// your custom hook logic here....
208
+
209
+
return state
210
+
}
211
+
```
212
+
213
+
-**Use TypeScript.** I bet some of you JS folk are sick of hearing about it. But, once you get over the hump, it makes coding a lot more enjoyable, honestly. Speaking of which...
Here's some types for more clarity on the explanations above. Note, you don't need to copy & paste these, this is just for comprehension, any types you need can be imported from the package.
Now let's get to an example of a hook written in TypeScript. Often when building with lambdas you'll want to keep some of your lambdas warm to avoid cold starts, but if you're doing this, remember to check and quit immediately otherwise you're wasting 💰. That's what this hook does...
0 commit comments