@@ -35,7 +35,7 @@ app.listen(port);
3535
3636```
3737
38- This will expose the request-level context to all future points that will handle this request making it exposable by accessing the context
38+ This will expose any point of your code form this point that handles that request.
3939
4040``` js
4141// ./controller/user/index.js
@@ -47,6 +47,8 @@ const logger = require('../service/logger');
4747export class UserController {
4848 async create (req , res , next ) {
4949 const { user } = req .body ;
50+
51+ // This will return the reference number set by out ContextMiddleware
5052 const { reference } = Context .get ();
5153
5254 logger .info (' Created user for reference: ' , reference);
@@ -55,3 +57,61 @@ export class UserController {
5557 }
5658}
5759```
60+
61+ ## API
62+
63+ ### create(initialContext?: object)
64+
65+ Creates for the current async resource an execution context entry identified with his asyncId.
66+ Any future processes that will be added to the async execution chain will be exposed to this context.
67+
68+ ### update(update: object)
69+ Updates the current execution context with a given update obect.
70+
71+ ### get()
72+
73+ Returns the current execution context identified with the current asyncId.
74+
75+ ### API Usage
76+
77+
78+ ``` js
79+ const Context = require (' node-execution-context' );
80+
81+ Context .create ({
82+ value: true
83+ });
84+
85+ Promise .resolve ().then (() => {
86+ console .log (Context .get ()) // outputs: {"value": true}
87+
88+ Context .update ({
89+ value: false
90+ });
91+
92+ return new Promise ((resolve ) => {
93+ setTimeout (() => {
94+ console .log (Context .get ()) // outputs: {"value": false}
95+
96+ Context .update ({
97+ butter: ' fly'
98+ });
99+
100+ process .nextTick (() => {
101+ console .log (Context .get ()) // outputs: {"value": false, "butter": 'fly'}
102+ });
103+
104+ }, 1000 );
105+
106+ console .log (Context .get ()) // outputs: {"value": true}
107+ });
108+ });
109+ ```
110+
111+ The following errors can be thrown while accessing to the context API :
112+
113+ | Code | when |
114+ |-|-
115+ | CONTEXT_ALREADY_DECLARED | When trying to ` create ` execution context, but current async resource already exists.
116+ | CONTEXT_DOES_NOT_EXISTS | When try to ` get ` / ` update ` the context, but it yet been created.
117+
0 commit comments