@andrew_l/service-actor 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +30 -8
  2. package/package.json +10 -3
package/README.md CHANGED
@@ -1,14 +1,25 @@
1
- # Description
1
+ # Service Actor Toolkit <!-- omit in toc -->
2
2
 
3
- This package aims to avoid passing data like trace IDs between functions.
3
+ ![license](https://img.shields.io/npm/l/%40andrew_l%2Fservice-actor) <!-- omit in toc -->
4
+ ![npm version](https://img.shields.io/npm/v/%40andrew_l%2Fservice-actor) <!-- omit in toc -->
5
+ ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40andrew_l%2Fservice-actor) <!-- omit in toc -->
4
6
 
5
- Actor object is structured like a simple, plain JavaScript object. This means it only contains properties (key-value pairs) without any special behaviors, so it can be used as a value in database queries without causing issues.
6
-
7
- It also means you can add functions (methods) to Actor object. These custom methods won’t interfere with the data stored in the object or show up when you loop through (iterate over) the object's properties.
7
+ The package is designed to help manage contextual data, such as trace IDs, across functions without explicitly passing them around. This allows you to easily track and manage contexts (like user actions or requests) while keeping your code clean and decoupled from context-passing logic.
8
8
 
9
9
  [Documentation](https://men232.github.io/toolkit/reference/@andrew_l/service-actor/)
10
10
 
11
- # Example
11
+ <!-- install placeholder -->
12
+
13
+ ## ✨ Features
14
+
15
+ - **Contextual Data Management**: Allows you to manage context (such as trace IDs or user info) in a central "actor" object.
16
+ - **No Context Passing**: Eliminates the need to manually pass context data between functions.
17
+ - **Custom Methods**: Actor objects can contain custom methods without affecting the data or iteration logic.
18
+ - **Compatible with Databases**: Actor objects are simple JavaScript objects and can be safely used in database queries.
19
+
20
+ ## 🚀 Example Usage
21
+
22
+ ### Setting up Actor Context
12
23
 
13
24
  ```js
14
25
  import { serviceActor } from '@andrew_l/service-actor';
@@ -22,7 +33,7 @@ const { with: withServiceActor, inject: injectServiceActor } = serviceActor(
22
33
  }),
23
34
  );
24
35
 
25
- // Bind actor context to the request
36
+ // Bind actor context to the request in your middleware
26
37
  app.use((ctx, next) => {
27
38
  return withServiceActor(
28
39
  {
@@ -43,7 +54,9 @@ app.patch('/users/:id', async ctx => {
43
54
  });
44
55
  ```
45
56
 
46
- # Usage
57
+ In the above example, the `traceId` and `ipAddress` are automatically associated with the current context (request), eliminating the need to manually pass them through function calls.
58
+
59
+ ### Using Service Actor in a Service
47
60
 
48
61
  ```js
49
62
  class UserService {
@@ -57,3 +70,12 @@ class UserService {
57
70
  }
58
71
  }
59
72
  ```
73
+
74
+ In this example, the `UserService.updateById` method retrieves the `actor` object, which contains contextual information, such as the trace ID, without explicitly passing it as an argument.
75
+
76
+ ## 🤔 Why Use This Package?
77
+
78
+ 1. **No Context Passing:** Automatically manages contextual information (e.g., trace IDs, user info) across different parts of your application.
79
+ 2. **Cleaner Code:** Avoids cluttering your function signatures with unnecessary context arguments.
80
+ 3. **Flexible and Extendable:** Actor objects can be extended with custom methods to suit your application's needs without interfering with the data.
81
+ 4. **Database Safe:** Since the actor is just a plain object, it can be safely stored and used in database queries.
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "@andrew_l/service-actor",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "keywords": [
7
+ "trace",
8
+ "id",
9
+ "provide",
10
+ "inject",
11
+ "hooks"
12
+ ],
6
13
  "repository": {
7
14
  "type": "git",
8
15
  "url": "git+https://github.com/men232/toolkit.git",
@@ -26,8 +33,8 @@
26
33
  "vitest": "^2.1.3"
27
34
  },
28
35
  "dependencies": {
29
- "@andrew_l/toolkit": "0.0.1",
30
- "@andrew_l/context": "0.0.1"
36
+ "@andrew_l/toolkit": "0.0.2",
37
+ "@andrew_l/context": "0.0.2"
31
38
  },
32
39
  "scripts": {
33
40
  "build": "unbuild",