Skip to content

Commit 5ef35f9

Browse files
authored
Update README.md
1 parent 7887bc6 commit 5ef35f9

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

README.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</p>
44

55
<h3 align="center">hooks</h3>
6-
<p align="center" style="margin-bottom: 2em;">opinionated decorator suite for easy cross-project observability consistency and self-explanatory codebase</p>
6+
<p align="center" style="margin-bottom: 2em;">configurable decorators for automated observability and self-explanatory codebase</p>
77

88
<p align="center">
99
<a href="https://www.npmjs.com/package/@opbi/hooks">
@@ -76,25 +76,30 @@ class UserProfileAPI
7676
//...
7777
@eventLogger()
7878
@eventTimer()
79-
getSubscription({ userId }):
79+
getSubscription({ userId }) {
8080
//...
81+
}
8182

8283
class SubscriptionAPI
8384
//...
8485
@eventLogger()
8586
@eventTimer()
8687
@errorRetry({ condition: e => e.type === 'TimeoutError' })
87-
cancel({ SubscriptionId }):
88+
cancel({ subscriptionId }) {
8889
//...
90+
}
8991
```
9092
```js
9193
/* handler.js - an illustration of the business logic */
9294
import { UserProfileAPI, SubscriptionAPI } from './api.js';
9395

94-
export const userCancelSubscription = ({ userId }, meta, context)
95-
|> UserProfileAPI.getSubscription
96-
|> SubscriptionAPI.cancel
97-
96+
class Handler
97+
//...
98+
@eventLogger()
99+
@eventTimer()
100+
userCancelSubscription = ({ userId }, meta, context)
101+
|> UserProfileAPI.getSubscription
102+
|> SubscriptionAPI.cancel
98103
```
99104
> Thanks to the [opionated function signature](#opinionated-function-signature), those decorators work out of box with minimum configuration to create a calling stack tree using the exact names of the decorated functions, producing structured log, metrics, tracing.
100105
@@ -105,23 +110,25 @@ The structured log it produced below makes it a breeze to precisely pinpoint the
105110
[error] event: userCancelSubscription.cancelSubscription, type: TimeoutError, Retry: 1, Param: { subscriptionId: '4672c33a-ff0a-4a8c-8632-80aea3a1c1c1' }
106111
```
107112
108-
> We are calling those decorators **hooks(decorators at call-time beside definition-time)** to indicate that they can be used at any point of a business logic function lifecycle to extend highly flexible and precise control.
113+
We are calling those decorators **hooks(decorators at call-time beside definition-time)** to indicate that they can be used at any point of a business logic function lifecycle to extend highly flexible and precise control.
109114
110115
```js
111116
/* handler.js - configure and attach hooks to business logic steps with hookEachPipe */
112-
import { pipeHookEach, eventLogger, eventTimer } from '@opbi/hooks';
117+
import { chain, eventLogger, eventTimer, errorRetry } from '@opbi/hooks';
113118
import { UserProfileAPI, SubscriptionAPI } from './api.js';
114119

115-
export const userCancelSubscription = async pipeHookEach(
116-
// all with default configuration applied to each step below
117-
eventLogger(), eventTimer()
118-
)(
119-
UserProfileAPI.getSubscription,
120-
// precise control with step only hook
121-
chain(
122-
errorRetry({ condition: e => e.type === 'TimeoutError' })
123-
)(SubscriptionAPI.cancel),
124-
);
120+
const monitor = chain(eventLogger(), eventTimer());
121+
122+
const userCancelSubscription = ({ userId }, meta, context)
123+
|> monitor(UserProfileAPI.getSubscription)
124+
|> chain(
125+
monitor,
126+
errorRetry({ condition: e => e.type === 'TimeoutError' }), // step level control
127+
)(SubscriptionAPI.cancel)
128+
129+
export default {
130+
'userCancelSubscription': monitor(userCancelSubscription)
131+
};
125132
```
126133
127134
#### Self-Expanatory Business Logic
@@ -219,9 +226,9 @@ export default adaptorExpress(express, { logger, metrics });
219226
220227
/* router.js - use the handler with automated logger, metrics */
221228
import app from './app.js';
222-
import { userCancelSubscription } from './handler.js';
229+
import Handler from './handler.js';
223230
224-
app.delete('/subscription/:userId', userCancelSubscription);
231+
app.delete('/subscription/:userId', Handler.userCancelSubscription);
225232
```
226233
227234
#### Integrate with Redux

0 commit comments

Comments
 (0)