@twin.org/api-processors 0.0.1-next.10
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/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/cjs/index.cjs +338 -0
- package/dist/esm/index.mjs +333 -0
- package/dist/types/data/routeProcessor.d.ts +31 -0
- package/dist/types/identity/nodeIdentityProcessor.d.ts +28 -0
- package/dist/types/identity/staticUserIdentityProcessor.d.ts +31 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/logging/loggingProcessor.d.ts +44 -0
- package/dist/types/models/IRequestLoggingProcessorConfig.d.ts +17 -0
- package/dist/types/models/IResponseLoggingProcessorConfig.d.ts +9 -0
- package/dist/types/models/IRouteProcessorConfig.d.ts +9 -0
- package/dist/types/models/IStaticUserIdentityProcessorConfig.d.ts +9 -0
- package/docs/changelog.md +5 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/LoggingProcessor.md +123 -0
- package/docs/reference/classes/NodeIdentityProcessor.md +95 -0
- package/docs/reference/classes/RouteProcessor.md +81 -0
- package/docs/reference/classes/StaticUserIdentityProcessor.md +81 -0
- package/docs/reference/index.md +15 -0
- package/docs/reference/interfaces/IRequestLoggingProcessorConfig.md +27 -0
- package/docs/reference/interfaces/IResponseLoggingProcessorConfig.md +11 -0
- package/docs/reference/interfaces/IRouteProcessorConfig.md +11 -0
- package/docs/reference/interfaces/IStaticUserIdentityProcessorConfig.md +11 -0
- package/locales/en.json +12 -0
- package/package.json +42 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { HttpErrorHelper } from '@twin.org/api-models';
|
|
2
|
+
import { Is, NotFoundError, Guards, ObjectHelper, Coerce } from '@twin.org/core';
|
|
3
|
+
import { HttpStatusCode, HeaderTypes, MimeTypes } from '@twin.org/web';
|
|
4
|
+
import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
5
|
+
|
|
6
|
+
// Copyright 2024 IOTA Stiftung.
|
|
7
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
8
|
+
/**
|
|
9
|
+
* Process the REST request and hands it on to the route handler.
|
|
10
|
+
*/
|
|
11
|
+
class RouteProcessor {
|
|
12
|
+
/**
|
|
13
|
+
* Runtime name for the class.
|
|
14
|
+
*/
|
|
15
|
+
CLASS_NAME = "RouteProcessor";
|
|
16
|
+
/**
|
|
17
|
+
* Include the stack with errors.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
_includeErrorStack;
|
|
21
|
+
/**
|
|
22
|
+
* Create a new instance of RouteProcessor.
|
|
23
|
+
* @param options Options for the processor.
|
|
24
|
+
* @param options.config The configuration for the processor.
|
|
25
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
26
|
+
*/
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Process the REST request for the specified route.
|
|
32
|
+
* @param request The incoming request.
|
|
33
|
+
* @param response The outgoing response.
|
|
34
|
+
* @param route The route to process.
|
|
35
|
+
* @param requestIdentity The identity context for the request.
|
|
36
|
+
* @param processorState The state handed through the processors.
|
|
37
|
+
*/
|
|
38
|
+
async process(request, response, route, requestIdentity, processorState) {
|
|
39
|
+
// Don't handle the route if another processor has already set the response
|
|
40
|
+
// status code e.g. from an auth processor
|
|
41
|
+
if (Is.empty(response.statusCode)) {
|
|
42
|
+
if (Is.empty(route)) {
|
|
43
|
+
HttpErrorHelper.buildResponse(response, {
|
|
44
|
+
name: NotFoundError.CLASS_NAME,
|
|
45
|
+
message: `${this.CLASS_NAME}.routeNotFound`,
|
|
46
|
+
properties: {
|
|
47
|
+
notFoundId: request.url
|
|
48
|
+
}
|
|
49
|
+
}, HttpStatusCode.notFound);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
try {
|
|
53
|
+
const req = {
|
|
54
|
+
pathParams: request.pathParams,
|
|
55
|
+
query: request.query,
|
|
56
|
+
body: request.body
|
|
57
|
+
};
|
|
58
|
+
const restRouteResponse = await route.handler({
|
|
59
|
+
...requestIdentity,
|
|
60
|
+
serverRequest: request,
|
|
61
|
+
processorState
|
|
62
|
+
}, req);
|
|
63
|
+
let statusCode = restRouteResponse.statusCode ?? response.statusCode ?? HttpStatusCode.ok;
|
|
64
|
+
const headers = restRouteResponse?.headers ?? {};
|
|
65
|
+
if (Is.empty(restRouteResponse?.body)) {
|
|
66
|
+
// If there is no custom status code and the body is empty
|
|
67
|
+
// use the no content response and set the length to 0
|
|
68
|
+
headers[HeaderTypes.ContentLength] = "0";
|
|
69
|
+
// Only change to no content if the status code is ok
|
|
70
|
+
// This could be something like a created status code
|
|
71
|
+
// which is successful but has no content
|
|
72
|
+
if (statusCode === HttpStatusCode.ok) {
|
|
73
|
+
statusCode = HttpStatusCode.noContent;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// Only set the content type if there is a body
|
|
78
|
+
// If there are custom response types for the route then use them
|
|
79
|
+
// instead of the default application/json
|
|
80
|
+
headers[HeaderTypes.ContentType] =
|
|
81
|
+
restRouteResponse?.attachment?.mimeType ??
|
|
82
|
+
response.headers?.[HeaderTypes.ContentType] ??
|
|
83
|
+
`${MimeTypes.Json}; charset=utf-8`;
|
|
84
|
+
// If there are filename or inline options set then add the content disposition
|
|
85
|
+
if (Is.stringValue(restRouteResponse?.attachment?.filename) ||
|
|
86
|
+
Is.boolean(restRouteResponse?.attachment?.inline)) {
|
|
87
|
+
let filename = "";
|
|
88
|
+
if (Is.stringValue(restRouteResponse?.attachment?.filename)) {
|
|
89
|
+
filename = `; filename="${restRouteResponse?.attachment?.filename}"`;
|
|
90
|
+
}
|
|
91
|
+
headers[HeaderTypes.ContentDisposition] =
|
|
92
|
+
`${restRouteResponse?.attachment?.inline ? "inline" : "attachment"}${filename}`;
|
|
93
|
+
}
|
|
94
|
+
// If this is a binary response then set the content length
|
|
95
|
+
if (Is.uint8Array(restRouteResponse?.body)) {
|
|
96
|
+
const contentLength = restRouteResponse.body.length;
|
|
97
|
+
headers[HeaderTypes.ContentLength] = contentLength.toString();
|
|
98
|
+
}
|
|
99
|
+
response.body = restRouteResponse?.body;
|
|
100
|
+
}
|
|
101
|
+
response.headers = headers;
|
|
102
|
+
response.statusCode = statusCode;
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
|
|
106
|
+
HttpErrorHelper.buildResponse(response, error, httpStatusCode);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Adds a static user identity to the request context.
|
|
115
|
+
*/
|
|
116
|
+
class StaticUserIdentityProcessor {
|
|
117
|
+
/**
|
|
118
|
+
* Runtime name for the class.
|
|
119
|
+
*/
|
|
120
|
+
CLASS_NAME = "StaticUserIdentityProcessor";
|
|
121
|
+
/**
|
|
122
|
+
* The fixed identity for request context.
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
_userIdentity;
|
|
126
|
+
/**
|
|
127
|
+
* Create a new instance of StaticIdentityProcessor.
|
|
128
|
+
* @param options Options for the processor.
|
|
129
|
+
* @param options.config The configuration for the processor.
|
|
130
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
131
|
+
*/
|
|
132
|
+
constructor(options) {
|
|
133
|
+
Guards.object(this.CLASS_NAME, "options", options);
|
|
134
|
+
Guards.object(this.CLASS_NAME, "options.config", options.config);
|
|
135
|
+
Guards.stringValue(this.CLASS_NAME, "options.config.userIdentity", options.config.userIdentity);
|
|
136
|
+
this._userIdentity = options.config.userIdentity;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Pre process the REST request for the specified route.
|
|
140
|
+
* @param request The incoming request.
|
|
141
|
+
* @param response The outgoing response.
|
|
142
|
+
* @param route The route to process.
|
|
143
|
+
* @param requestIdentity The identity context for the request.
|
|
144
|
+
* @param processorState The state handed through the processors.
|
|
145
|
+
*/
|
|
146
|
+
async pre(request, response, route, requestIdentity, processorState) {
|
|
147
|
+
if (!Is.empty(route) && !(route.skipAuth ?? false)) {
|
|
148
|
+
requestIdentity.userIdentity = this._userIdentity;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Adds a node identity to the request identity.
|
|
155
|
+
*/
|
|
156
|
+
class NodeIdentityProcessor {
|
|
157
|
+
/**
|
|
158
|
+
* Runtime name for the class.
|
|
159
|
+
*/
|
|
160
|
+
CLASS_NAME = "NodeIdentityProcessor";
|
|
161
|
+
/**
|
|
162
|
+
* The node identity for request context.
|
|
163
|
+
* @internal
|
|
164
|
+
*/
|
|
165
|
+
_nodeIdentity;
|
|
166
|
+
/**
|
|
167
|
+
* The service needs to be started when the application is initialized.
|
|
168
|
+
* @param nodeIdentity The identity of the node.
|
|
169
|
+
* @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
|
|
170
|
+
* @returns Nothing.
|
|
171
|
+
*/
|
|
172
|
+
async start(nodeIdentity, nodeLoggingConnectorType) {
|
|
173
|
+
Guards.string(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
|
174
|
+
this._nodeIdentity = nodeIdentity;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Pre process the REST request for the specified route.
|
|
178
|
+
* @param request The incoming request.
|
|
179
|
+
* @param response The outgoing response.
|
|
180
|
+
* @param route The route to process.
|
|
181
|
+
* @param requestIdentity The identity context for the request.
|
|
182
|
+
* @param processorState The state handed through the processors.
|
|
183
|
+
*/
|
|
184
|
+
async pre(request, response, route, requestIdentity, processorState) {
|
|
185
|
+
requestIdentity.nodeIdentity = this._nodeIdentity;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Process the REST request and log its information.
|
|
191
|
+
*/
|
|
192
|
+
class LoggingProcessor {
|
|
193
|
+
/**
|
|
194
|
+
* Runtime name for the class.
|
|
195
|
+
*/
|
|
196
|
+
CLASS_NAME = "LoggingProcessor";
|
|
197
|
+
/**
|
|
198
|
+
* The connector for logging the information.
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
_loggingConnector;
|
|
202
|
+
/**
|
|
203
|
+
* Include the body objects when logging the information.
|
|
204
|
+
* @internal
|
|
205
|
+
*/
|
|
206
|
+
_includeBody;
|
|
207
|
+
/**
|
|
208
|
+
* Show the full base64 content for data, default to abbreviate.
|
|
209
|
+
* @internal
|
|
210
|
+
*/
|
|
211
|
+
_fullBase64;
|
|
212
|
+
/**
|
|
213
|
+
* List of property names to obfuscate, defaults to "password".
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
_obfuscateProperties;
|
|
217
|
+
/**
|
|
218
|
+
* Create a new instance of RequestLoggingProcessor.
|
|
219
|
+
* @param options Options for the processor.
|
|
220
|
+
* @param options.loggingConnectorType The type for the logging connector, defaults to "logging".
|
|
221
|
+
* @param options.config The configuration for the processor.
|
|
222
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
223
|
+
*/
|
|
224
|
+
constructor(options) {
|
|
225
|
+
this._loggingConnector = LoggingConnectorFactory.get(options?.loggingConnectorType ?? "logging");
|
|
226
|
+
this._includeBody = options?.config?.includeBody ?? false;
|
|
227
|
+
this._fullBase64 = options?.config?.fullBase64 ?? false;
|
|
228
|
+
this._obfuscateProperties = options?.config?.obfuscateProperties ?? ["password"];
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Pre process the REST request for the specified route.
|
|
232
|
+
* @param request The incoming request.
|
|
233
|
+
* @param response The outgoing response.
|
|
234
|
+
* @param route The route to process.
|
|
235
|
+
* @param requestIdentity The identity context for the request.
|
|
236
|
+
* @param processorState The state handed through the processors.
|
|
237
|
+
*/
|
|
238
|
+
async pre(request, response, route, requestIdentity, processorState) {
|
|
239
|
+
const now = process.hrtime.bigint();
|
|
240
|
+
processorState.requestStart = now;
|
|
241
|
+
const contentType = request.headers?.[HeaderTypes.ContentType];
|
|
242
|
+
const isJson = Is.stringValue(contentType)
|
|
243
|
+
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
244
|
+
: false;
|
|
245
|
+
await this._loggingConnector.log({
|
|
246
|
+
level: "info",
|
|
247
|
+
source: this.CLASS_NAME,
|
|
248
|
+
ts: Date.now(),
|
|
249
|
+
message: `===> ${request.method} ${request.url ? new URL(request.url).pathname : ""}`,
|
|
250
|
+
data: this._includeBody && isJson
|
|
251
|
+
? this.processJson("body", ObjectHelper.clone(request?.body))
|
|
252
|
+
: undefined
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Post process the REST request for the specified route.
|
|
257
|
+
* @param request The incoming request.
|
|
258
|
+
* @param response The outgoing response.
|
|
259
|
+
* @param route The route to process.
|
|
260
|
+
* @param requestIdentity The identity context for the request.
|
|
261
|
+
* @param processorState The state handed through the processors.
|
|
262
|
+
*/
|
|
263
|
+
async post(request, response, route, requestIdentity, processorState) {
|
|
264
|
+
let data;
|
|
265
|
+
if (this._includeBody) {
|
|
266
|
+
const contentType = response.headers?.[HeaderTypes.ContentType];
|
|
267
|
+
const isJson = Is.stringValue(contentType)
|
|
268
|
+
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
269
|
+
: false;
|
|
270
|
+
const contentLength = response.headers?.[HeaderTypes.ContentLength];
|
|
271
|
+
if (isJson) {
|
|
272
|
+
data = {
|
|
273
|
+
body: this.processJson("body", ObjectHelper.clone(response.body))
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
const dataParts = {};
|
|
278
|
+
if (Is.stringValue(contentType)) {
|
|
279
|
+
dataParts["Content Type"] = contentType;
|
|
280
|
+
}
|
|
281
|
+
if (Is.stringValue(contentLength)) {
|
|
282
|
+
dataParts["Content Length"] = contentLength;
|
|
283
|
+
}
|
|
284
|
+
if (Object.keys(dataParts).length > 0) {
|
|
285
|
+
data = {
|
|
286
|
+
headers: dataParts
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const now = process.hrtime.bigint();
|
|
292
|
+
const start = Coerce.bigint(processorState.requestStart) ?? now;
|
|
293
|
+
const elapsed = now - start;
|
|
294
|
+
const elapsedMicroSeconds = Math.floor(Number(elapsed) / 1000);
|
|
295
|
+
await this._loggingConnector.log({
|
|
296
|
+
level: Is.number(response.statusCode) && response.statusCode >= HttpStatusCode.badRequest
|
|
297
|
+
? "error"
|
|
298
|
+
: "info",
|
|
299
|
+
source: this.CLASS_NAME,
|
|
300
|
+
ts: Date.now(),
|
|
301
|
+
message: `<=== ${response.statusCode ?? ""} ${request.method} ${request.url ? new URL(request.url).pathname : ""} duration: ${elapsedMicroSeconds}µs`,
|
|
302
|
+
data
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Process the JSON.
|
|
307
|
+
* @param propValue The property to process.
|
|
308
|
+
* @returns The processed property.
|
|
309
|
+
* @internal
|
|
310
|
+
*/
|
|
311
|
+
processJson(propName, propValue) {
|
|
312
|
+
if (Is.array(propValue)) {
|
|
313
|
+
for (let i = 0; i < propValue.length; i++) {
|
|
314
|
+
propValue[i] = this.processJson(`${i}`, propValue[i]);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else if (Is.object(propValue)) {
|
|
318
|
+
for (const key of Object.keys(propValue)) {
|
|
319
|
+
propValue[key] = this.processJson(key, propValue[key]);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else if (Is.stringBase64(propValue) && !this._fullBase64) {
|
|
323
|
+
propValue = "<base64>";
|
|
324
|
+
}
|
|
325
|
+
else if (Is.string(propValue) &&
|
|
326
|
+
this._obfuscateProperties.some(op => new RegExp(op).test(propName))) {
|
|
327
|
+
propValue = "**************";
|
|
328
|
+
}
|
|
329
|
+
return propValue;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export { LoggingProcessor, NodeIdentityProcessor, RouteProcessor, StaticUserIdentityProcessor };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type IHttpRequestIdentity, type IHttpResponse, type IHttpRestRouteProcessor, type IHttpServerRequest, type IRestRoute } from "@twin.org/api-models";
|
|
2
|
+
import type { IRouteProcessorConfig } from "../models/IRouteProcessorConfig";
|
|
3
|
+
/**
|
|
4
|
+
* Process the REST request and hands it on to the route handler.
|
|
5
|
+
*/
|
|
6
|
+
export declare class RouteProcessor implements IHttpRestRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* Runtime name for the class.
|
|
9
|
+
*/
|
|
10
|
+
readonly CLASS_NAME: string;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new instance of RouteProcessor.
|
|
13
|
+
* @param options Options for the processor.
|
|
14
|
+
* @param options.config The configuration for the processor.
|
|
15
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options?: {
|
|
18
|
+
config?: IRouteProcessorConfig;
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Process the REST request for the specified route.
|
|
22
|
+
* @param request The incoming request.
|
|
23
|
+
* @param response The outgoing response.
|
|
24
|
+
* @param route The route to process.
|
|
25
|
+
* @param requestIdentity The identity context for the request.
|
|
26
|
+
* @param processorState The state handed through the processors.
|
|
27
|
+
*/
|
|
28
|
+
process(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
29
|
+
[id: string]: unknown;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { IHttpRequestIdentity, IHttpResponse, IHttpRestRouteProcessor, IHttpServerRequest, IRestRoute } from "@twin.org/api-models";
|
|
2
|
+
/**
|
|
3
|
+
* Adds a node identity to the request identity.
|
|
4
|
+
*/
|
|
5
|
+
export declare class NodeIdentityProcessor implements IHttpRestRouteProcessor {
|
|
6
|
+
/**
|
|
7
|
+
* Runtime name for the class.
|
|
8
|
+
*/
|
|
9
|
+
readonly CLASS_NAME: string;
|
|
10
|
+
/**
|
|
11
|
+
* The service needs to be started when the application is initialized.
|
|
12
|
+
* @param nodeIdentity The identity of the node.
|
|
13
|
+
* @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
|
|
14
|
+
* @returns Nothing.
|
|
15
|
+
*/
|
|
16
|
+
start(nodeIdentity: string, nodeLoggingConnectorType?: string): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Pre process the REST request for the specified route.
|
|
19
|
+
* @param request The incoming request.
|
|
20
|
+
* @param response The outgoing response.
|
|
21
|
+
* @param route The route to process.
|
|
22
|
+
* @param requestIdentity The identity context for the request.
|
|
23
|
+
* @param processorState The state handed through the processors.
|
|
24
|
+
*/
|
|
25
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
26
|
+
[id: string]: unknown;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { IHttpRequestIdentity, IHttpResponse, IHttpRestRouteProcessor, IHttpServerRequest, IRestRoute } from "@twin.org/api-models";
|
|
2
|
+
import type { IStaticUserIdentityProcessorConfig } from "../models/IStaticUserIdentityProcessorConfig";
|
|
3
|
+
/**
|
|
4
|
+
* Adds a static user identity to the request context.
|
|
5
|
+
*/
|
|
6
|
+
export declare class StaticUserIdentityProcessor implements IHttpRestRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* Runtime name for the class.
|
|
9
|
+
*/
|
|
10
|
+
readonly CLASS_NAME: string;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new instance of StaticIdentityProcessor.
|
|
13
|
+
* @param options Options for the processor.
|
|
14
|
+
* @param options.config The configuration for the processor.
|
|
15
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options: {
|
|
18
|
+
config: IStaticUserIdentityProcessorConfig;
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Pre process the REST request for the specified route.
|
|
22
|
+
* @param request The incoming request.
|
|
23
|
+
* @param response The outgoing response.
|
|
24
|
+
* @param route The route to process.
|
|
25
|
+
* @param requestIdentity The identity context for the request.
|
|
26
|
+
* @param processorState The state handed through the processors.
|
|
27
|
+
*/
|
|
28
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
29
|
+
[id: string]: unknown;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./data/routeProcessor";
|
|
2
|
+
export * from "./identity/staticUserIdentityProcessor";
|
|
3
|
+
export * from "./identity/nodeIdentityProcessor";
|
|
4
|
+
export * from "./logging/loggingProcessor";
|
|
5
|
+
export * from "./models/IRequestLoggingProcessorConfig";
|
|
6
|
+
export * from "./models/IResponseLoggingProcessorConfig";
|
|
7
|
+
export * from "./models/IRouteProcessorConfig";
|
|
8
|
+
export * from "./models/IStaticUserIdentityProcessorConfig";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { IHttpRequestIdentity, IHttpResponse, IHttpRestRouteProcessor, IHttpServerRequest, IRestRoute } from "@twin.org/api-models";
|
|
2
|
+
import type { IRequestLoggingProcessorConfig } from "../models/IRequestLoggingProcessorConfig";
|
|
3
|
+
/**
|
|
4
|
+
* Process the REST request and log its information.
|
|
5
|
+
*/
|
|
6
|
+
export declare class LoggingProcessor implements IHttpRestRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* Runtime name for the class.
|
|
9
|
+
*/
|
|
10
|
+
readonly CLASS_NAME: string;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new instance of RequestLoggingProcessor.
|
|
13
|
+
* @param options Options for the processor.
|
|
14
|
+
* @param options.loggingConnectorType The type for the logging connector, defaults to "logging".
|
|
15
|
+
* @param options.config The configuration for the processor.
|
|
16
|
+
* @returns Promise that resolves when the processor is initialized.
|
|
17
|
+
*/
|
|
18
|
+
constructor(options?: {
|
|
19
|
+
loggingConnectorType?: string;
|
|
20
|
+
config?: IRequestLoggingProcessorConfig;
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* Pre process the REST request for the specified route.
|
|
24
|
+
* @param request The incoming request.
|
|
25
|
+
* @param response The outgoing response.
|
|
26
|
+
* @param route The route to process.
|
|
27
|
+
* @param requestIdentity The identity context for the request.
|
|
28
|
+
* @param processorState The state handed through the processors.
|
|
29
|
+
*/
|
|
30
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
31
|
+
[id: string]: unknown;
|
|
32
|
+
}): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Post process the REST request for the specified route.
|
|
35
|
+
* @param request The incoming request.
|
|
36
|
+
* @param response The outgoing response.
|
|
37
|
+
* @param route The route to process.
|
|
38
|
+
* @param requestIdentity The identity context for the request.
|
|
39
|
+
* @param processorState The state handed through the processors.
|
|
40
|
+
*/
|
|
41
|
+
post(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
42
|
+
[id: string]: unknown;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the request logging processor.
|
|
3
|
+
*/
|
|
4
|
+
export interface IRequestLoggingProcessorConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Include the body objects when logging the information.
|
|
7
|
+
*/
|
|
8
|
+
includeBody?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Show the full base64 content for data, default to abbreviate.
|
|
11
|
+
*/
|
|
12
|
+
fullBase64?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* List of property names to obfuscate, can be regex, defaults to "password".
|
|
15
|
+
*/
|
|
16
|
+
obfuscateProperties?: string[];
|
|
17
|
+
}
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/api-processors - Examples
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Class: LoggingProcessor
|
|
2
|
+
|
|
3
|
+
Process the REST request and log its information.
|
|
4
|
+
|
|
5
|
+
## Implements
|
|
6
|
+
|
|
7
|
+
- `IHttpRestRouteProcessor`
|
|
8
|
+
|
|
9
|
+
## Constructors
|
|
10
|
+
|
|
11
|
+
### new LoggingProcessor()
|
|
12
|
+
|
|
13
|
+
> **new LoggingProcessor**(`options`?): [`LoggingProcessor`](LoggingProcessor.md)
|
|
14
|
+
|
|
15
|
+
Create a new instance of RequestLoggingProcessor.
|
|
16
|
+
|
|
17
|
+
#### Parameters
|
|
18
|
+
|
|
19
|
+
• **options?**
|
|
20
|
+
|
|
21
|
+
Options for the processor.
|
|
22
|
+
|
|
23
|
+
• **options.loggingConnectorType?**: `string`
|
|
24
|
+
|
|
25
|
+
The type for the logging connector, defaults to "logging".
|
|
26
|
+
|
|
27
|
+
• **options.config?**: [`IRequestLoggingProcessorConfig`](../interfaces/IRequestLoggingProcessorConfig.md)
|
|
28
|
+
|
|
29
|
+
The configuration for the processor.
|
|
30
|
+
|
|
31
|
+
#### Returns
|
|
32
|
+
|
|
33
|
+
[`LoggingProcessor`](LoggingProcessor.md)
|
|
34
|
+
|
|
35
|
+
Promise that resolves when the processor is initialized.
|
|
36
|
+
|
|
37
|
+
## Properties
|
|
38
|
+
|
|
39
|
+
### CLASS\_NAME
|
|
40
|
+
|
|
41
|
+
> `readonly` **CLASS\_NAME**: `string`
|
|
42
|
+
|
|
43
|
+
Runtime name for the class.
|
|
44
|
+
|
|
45
|
+
#### Implementation of
|
|
46
|
+
|
|
47
|
+
`IHttpRestRouteProcessor.CLASS_NAME`
|
|
48
|
+
|
|
49
|
+
## Methods
|
|
50
|
+
|
|
51
|
+
### pre()
|
|
52
|
+
|
|
53
|
+
> **pre**(`request`, `response`, `route`, `requestIdentity`, `processorState`): `Promise`\<`void`\>
|
|
54
|
+
|
|
55
|
+
Pre process the REST request for the specified route.
|
|
56
|
+
|
|
57
|
+
#### Parameters
|
|
58
|
+
|
|
59
|
+
• **request**: `IHttpServerRequest`\<`any`\>
|
|
60
|
+
|
|
61
|
+
The incoming request.
|
|
62
|
+
|
|
63
|
+
• **response**: `IHttpResponse`\<`any`\>
|
|
64
|
+
|
|
65
|
+
The outgoing response.
|
|
66
|
+
|
|
67
|
+
• **route**: `undefined` \| `IRestRoute`\<`any`, `any`\>
|
|
68
|
+
|
|
69
|
+
The route to process.
|
|
70
|
+
|
|
71
|
+
• **requestIdentity**: `IHttpRequestIdentity`
|
|
72
|
+
|
|
73
|
+
The identity context for the request.
|
|
74
|
+
|
|
75
|
+
• **processorState**
|
|
76
|
+
|
|
77
|
+
The state handed through the processors.
|
|
78
|
+
|
|
79
|
+
#### Returns
|
|
80
|
+
|
|
81
|
+
`Promise`\<`void`\>
|
|
82
|
+
|
|
83
|
+
#### Implementation of
|
|
84
|
+
|
|
85
|
+
`IHttpRestRouteProcessor.pre`
|
|
86
|
+
|
|
87
|
+
***
|
|
88
|
+
|
|
89
|
+
### post()
|
|
90
|
+
|
|
91
|
+
> **post**(`request`, `response`, `route`, `requestIdentity`, `processorState`): `Promise`\<`void`\>
|
|
92
|
+
|
|
93
|
+
Post process the REST request for the specified route.
|
|
94
|
+
|
|
95
|
+
#### Parameters
|
|
96
|
+
|
|
97
|
+
• **request**: `IHttpServerRequest`\<`any`\>
|
|
98
|
+
|
|
99
|
+
The incoming request.
|
|
100
|
+
|
|
101
|
+
• **response**: `IHttpResponse`\<`any`\>
|
|
102
|
+
|
|
103
|
+
The outgoing response.
|
|
104
|
+
|
|
105
|
+
• **route**: `undefined` \| `IRestRoute`\<`any`, `any`\>
|
|
106
|
+
|
|
107
|
+
The route to process.
|
|
108
|
+
|
|
109
|
+
• **requestIdentity**: `IHttpRequestIdentity`
|
|
110
|
+
|
|
111
|
+
The identity context for the request.
|
|
112
|
+
|
|
113
|
+
• **processorState**
|
|
114
|
+
|
|
115
|
+
The state handed through the processors.
|
|
116
|
+
|
|
117
|
+
#### Returns
|
|
118
|
+
|
|
119
|
+
`Promise`\<`void`\>
|
|
120
|
+
|
|
121
|
+
#### Implementation of
|
|
122
|
+
|
|
123
|
+
`IHttpRestRouteProcessor.post`
|