api-observe 1.0.1 → 1.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.
- package/README.md +2 -2
- package/lib/interceptor.js +2 -2
- package/lib/plugin.js +9 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,10 +40,10 @@ await fastify.register(apiObserve, {
|
|
|
40
40
|
// Attach to your HTTP clients after registration
|
|
41
41
|
fastify.after(() => {
|
|
42
42
|
// Option A: single client
|
|
43
|
-
fastify.
|
|
43
|
+
fastify.observer.attach(myHttpClient);
|
|
44
44
|
|
|
45
45
|
// Option B: auto-attach to all clients on an object
|
|
46
|
-
fastify.
|
|
46
|
+
fastify.observer.attachToAll(fastify.services);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
await fastify.listen({ port: 3000 });
|
package/lib/interceptor.js
CHANGED
|
@@ -33,8 +33,8 @@ function attachInterceptor(client, store, opts = {}) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// Mark this client so we don't double-attach
|
|
36
|
-
if (client.
|
|
37
|
-
client.
|
|
36
|
+
if (client.__observerAttached) return;
|
|
37
|
+
client.__observerAttached = true;
|
|
38
38
|
|
|
39
39
|
// ── Eject existing response interceptors ───────────���────────────────────
|
|
40
40
|
// Save them, clear them, add ours first, then re-add the originals.
|
package/lib/plugin.js
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Usage in a consuming service:
|
|
9
9
|
*
|
|
10
|
-
* const
|
|
10
|
+
* const observer = require('@pruservices/ps-observe');
|
|
11
11
|
*
|
|
12
|
-
* fastify.register(
|
|
12
|
+
* fastify.register(observer, {
|
|
13
13
|
* // (optional) max failure records to keep in memory (default: 500)
|
|
14
14
|
* maxEntries: 500,
|
|
15
15
|
*
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
* // After registering your service clients, attach the interceptors:
|
|
27
27
|
* fastify.after(() => {
|
|
28
28
|
* // Option A: Attach to individual clients
|
|
29
|
-
* fastify.
|
|
29
|
+
* fastify.observer.attach(myClient);
|
|
30
30
|
*
|
|
31
31
|
* // Option B: Auto-attach to all clients on an object
|
|
32
|
-
* fastify.
|
|
32
|
+
* fastify.observer.attachToAll(fastify.services);
|
|
33
33
|
* });
|
|
34
34
|
*/
|
|
35
35
|
|
|
@@ -37,7 +37,7 @@ const { FailureStore } = require('./failure-store');
|
|
|
37
37
|
const { attachInterceptor, attachToAll } = require('./interceptor');
|
|
38
38
|
const { registerRoutes } = require('./routes');
|
|
39
39
|
|
|
40
|
-
async function
|
|
40
|
+
async function observerPlugin(fastify, opts) {
|
|
41
41
|
const store = new FailureStore({
|
|
42
42
|
maxEntries: opts.maxEntries,
|
|
43
43
|
ttlMs: opts.ttlMs,
|
|
@@ -46,7 +46,7 @@ async function psObservePlugin(fastify, opts) {
|
|
|
46
46
|
const sanitizeOpts = opts.sanitize ? { sanitize: opts.sanitize } : {};
|
|
47
47
|
|
|
48
48
|
// Decorate fastify with the observe API so consumers can attach clients
|
|
49
|
-
fastify.decorate('
|
|
49
|
+
fastify.decorate('observer', {
|
|
50
50
|
/** Attach interceptor to a single BaseHttpClient instance */
|
|
51
51
|
attach(client) {
|
|
52
52
|
attachInterceptor(client, store, sanitizeOpts);
|
|
@@ -151,7 +151,7 @@ async function psObservePlugin(fastify, opts) {
|
|
|
151
151
|
registerRoutes(fastify, store);
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
// Skip encapsulation so
|
|
155
|
-
|
|
154
|
+
// Skip encapsulation so observer decorator is visible to the parent scope
|
|
155
|
+
observerPlugin[Symbol.for('skip-override')] = true;
|
|
156
156
|
|
|
157
|
-
module.exports =
|
|
157
|
+
module.exports = observerPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-observe",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Framework-agnostic plugin that captures and displays API failure details — works with Fastify, Express, NestJS, Koa, or plain Node.js HTTP",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|