@twin.org/api-processors 0.0.3-next.20 → 0.0.3-next.21
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.
- package/README.md +1 -1
- package/docs/changelog.md +15 -1
- package/docs/examples.md +189 -1
- package/package.json +3 -3
package/README.md
CHANGED
package/docs/changelog.md
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.3-next.21](https://github.com/twinfoundation/api/compare/api-processors-v0.0.3-next.20...api-processors-v0.0.3-next.21) (2026-03-11)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **api-processors:** Synchronize repo versions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/api-models bumped from 0.0.3-next.20 to 0.0.3-next.21
|
|
2
16
|
|
|
3
17
|
## [0.0.3-next.20](https://github.com/twinfoundation/api/compare/api-processors-v0.0.3-next.19...api-processors-v0.0.3-next.20) (2026-02-09)
|
|
4
18
|
|
package/docs/examples.md
CHANGED
|
@@ -1 +1,189 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Processors Examples
|
|
2
|
+
|
|
3
|
+
These snippets demonstrate request and response processors that can be composed into REST, socket, and MIME handling pipelines.
|
|
4
|
+
|
|
5
|
+
## SocketRouteProcessor
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { SocketRouteProcessor } from '@twin.org/api-processors';
|
|
9
|
+
|
|
10
|
+
const socketProcessor = new SocketRouteProcessor();
|
|
11
|
+
|
|
12
|
+
console.log(socketProcessor.className()); // SocketRouteProcessor
|
|
13
|
+
|
|
14
|
+
const socketRoute = {
|
|
15
|
+
event: 'tenant.updated',
|
|
16
|
+
handler: async () => ({
|
|
17
|
+
body: {
|
|
18
|
+
ok: true
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const socketRequest = {
|
|
24
|
+
socketId: 'conn-1',
|
|
25
|
+
url: '/tenant.updated',
|
|
26
|
+
pathParams: {},
|
|
27
|
+
query: {},
|
|
28
|
+
body: {
|
|
29
|
+
tenantId: '0123456789abcdef0123456789abcdef'
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const socketResponse = {};
|
|
34
|
+
const socketProcessorState = {};
|
|
35
|
+
|
|
36
|
+
await socketProcessor.connected(socketRequest, socketRoute);
|
|
37
|
+
|
|
38
|
+
await socketProcessor.process(
|
|
39
|
+
socketRequest,
|
|
40
|
+
socketResponse,
|
|
41
|
+
socketRoute,
|
|
42
|
+
socketProcessorState,
|
|
43
|
+
async (topic, response) => {
|
|
44
|
+
console.log(topic); // tenant.updated
|
|
45
|
+
console.log(response.statusCode ?? 200); // 200
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
await socketProcessor.disconnected(socketRequest, socketRoute);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## ContextIdProcessor
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { ContextIdProcessor } from '@twin.org/api-processors';
|
|
56
|
+
|
|
57
|
+
const contextProcessor = new ContextIdProcessor({
|
|
58
|
+
config: {
|
|
59
|
+
key: 'node'
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await contextProcessor.start('default');
|
|
64
|
+
console.log(contextProcessor.className()); // ContextIdProcessor
|
|
65
|
+
|
|
66
|
+
const contextIds = {};
|
|
67
|
+
await contextProcessor.pre(
|
|
68
|
+
{ method: 'get', url: '/health', headers: {} },
|
|
69
|
+
{},
|
|
70
|
+
undefined,
|
|
71
|
+
contextIds,
|
|
72
|
+
{}
|
|
73
|
+
);
|
|
74
|
+
console.log(typeof contextIds.node); // string
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## LoggingProcessor
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { LoggingProcessor } from '@twin.org/api-processors';
|
|
81
|
+
|
|
82
|
+
const logging = new LoggingProcessor();
|
|
83
|
+
|
|
84
|
+
console.log(logging.className()); // LoggingProcessor
|
|
85
|
+
|
|
86
|
+
const request = {
|
|
87
|
+
method: 'get',
|
|
88
|
+
url: 'http://localhost:3000/health',
|
|
89
|
+
headers: {}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const response = {
|
|
93
|
+
statusCode: 200,
|
|
94
|
+
headers: {}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const processorState: { [id: string]: unknown } = {};
|
|
98
|
+
|
|
99
|
+
await logging.pre(request, response, undefined, {}, processorState);
|
|
100
|
+
await logging.post(request, response, undefined, {}, processorState);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## JsonLdMimeTypeProcessor
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { JsonLdMimeTypeProcessor } from '@twin.org/api-processors';
|
|
107
|
+
|
|
108
|
+
const jsonLd = new JsonLdMimeTypeProcessor();
|
|
109
|
+
|
|
110
|
+
const mediaTypes = jsonLd.getTypes();
|
|
111
|
+
const body = new TextEncoder().encode('{"@context":"https://schema.org","name":"Tenant"}');
|
|
112
|
+
const handled = await jsonLd.handle(body);
|
|
113
|
+
|
|
114
|
+
console.log(mediaTypes[0]); // application/ld+json
|
|
115
|
+
console.log(typeof handled); // object
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## JwtMimeTypeProcessor
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { JwtMimeTypeProcessor } from '@twin.org/api-processors';
|
|
122
|
+
|
|
123
|
+
const jwt = new JwtMimeTypeProcessor();
|
|
124
|
+
|
|
125
|
+
const mediaTypes = jwt.getTypes();
|
|
126
|
+
const body = new TextEncoder().encode('eyJhbGciOiJIUzI1NiJ9.payload.signature');
|
|
127
|
+
const handled = await jwt.handle(body);
|
|
128
|
+
|
|
129
|
+
console.log(mediaTypes[0]); // application/jwt
|
|
130
|
+
console.log(typeof handled); // string
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## RestRouteProcessor
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { RestRouteProcessor } from '@twin.org/api-processors';
|
|
137
|
+
|
|
138
|
+
const restProcessor = new RestRouteProcessor();
|
|
139
|
+
|
|
140
|
+
console.log(restProcessor.className()); // RestRouteProcessor
|
|
141
|
+
|
|
142
|
+
const request = {
|
|
143
|
+
method: 'get',
|
|
144
|
+
url: '/info',
|
|
145
|
+
headers: {},
|
|
146
|
+
pathParams: {},
|
|
147
|
+
query: {}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const response = {};
|
|
151
|
+
|
|
152
|
+
const route = {
|
|
153
|
+
method: 'get',
|
|
154
|
+
path: '/info',
|
|
155
|
+
handler: async () => ({
|
|
156
|
+
body: {
|
|
157
|
+
name: 'Twin API'
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
await restProcessor.process(request, response, route, {});
|
|
163
|
+
console.log(response.statusCode ?? 200); // 200
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## StaticContextIdProcessor
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { StaticContextIdProcessor } from '@twin.org/api-processors';
|
|
170
|
+
|
|
171
|
+
const staticContext = new StaticContextIdProcessor({
|
|
172
|
+
config: {
|
|
173
|
+
key: 'environment',
|
|
174
|
+
value: 'test'
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
console.log(staticContext.className()); // StaticContextIdProcessor
|
|
179
|
+
|
|
180
|
+
const contextIds = {};
|
|
181
|
+
await staticContext.pre(
|
|
182
|
+
{ method: 'get', url: '/health', headers: {} },
|
|
183
|
+
{},
|
|
184
|
+
undefined,
|
|
185
|
+
contextIds,
|
|
186
|
+
{}
|
|
187
|
+
);
|
|
188
|
+
console.log(contextIds.environment); // test
|
|
189
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/api-processors",
|
|
3
|
-
"version": "0.0.3-next.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3-next.21",
|
|
4
|
+
"description": "Reusable request and route processors for logging, context handling, and content negotiation.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/twinfoundation/api.git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/api-models": "0.0.3-next.
|
|
17
|
+
"@twin.org/api-models": "0.0.3-next.21",
|
|
18
18
|
"@twin.org/context": "next",
|
|
19
19
|
"@twin.org/core": "next",
|
|
20
20
|
"@twin.org/logging-models": "next",
|