@apifn/core 0.1.2 → 0.2.0
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 +4 -1
- package/dist/index.cjs +46 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.js +46 -25
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -186,10 +186,13 @@ const metrics = await fetchEndpointMetrics({
|
|
|
186
186
|
|
|
187
187
|
const client = createWatchFnClient({ baseUrl: "https://watch.example.com" });
|
|
188
188
|
|
|
189
|
-
// Middleware records per-endpoint timing to watchfn
|
|
189
|
+
// Middleware records per-endpoint timing and status to watchfn
|
|
190
|
+
const router = createRouter({ routes }); // from @superfunctions/http
|
|
190
191
|
router.use(apifnWatchMiddleware({ baseUrl: "https://watch.example.com" }));
|
|
191
192
|
```
|
|
192
193
|
|
|
194
|
+
`apifnWatchMiddleware` is a `@superfunctions/http` `Middleware`, so it also works in `createRouter({ middleware: [...] })` and in a route's `middleware` list. It returns the downstream `Response` unchanged (status, headers and body, which it never reads) and records that response's status. When the router's error handler turns a thrown error into a response, it records that response's status. Telemetry failures never change the response: they are logged with `console.warn` unless `silent: true`. By default (`fireAndForget: true`) it returns the response without waiting for the event POST to finish.
|
|
195
|
+
|
|
193
196
|
### authfn
|
|
194
197
|
|
|
195
198
|
Mint short-lived test tokens (TTL ≤ 3600s) and inject bearer auth into collection runs.
|
package/dist/index.cjs
CHANGED
|
@@ -1715,6 +1715,14 @@ function createWatchFnClient(options) {
|
|
|
1715
1715
|
})
|
|
1716
1716
|
};
|
|
1717
1717
|
}
|
|
1718
|
+
function describeError(err) {
|
|
1719
|
+
try {
|
|
1720
|
+
return err instanceof Error ? String(err.message) : String(err);
|
|
1721
|
+
} catch {
|
|
1722
|
+
return "unknown error";
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
var UNHANDLED_ERROR_STATUS = 500;
|
|
1718
1726
|
function apifnWatchMiddleware(options) {
|
|
1719
1727
|
const {
|
|
1720
1728
|
baseUrl,
|
|
@@ -1729,6 +1737,14 @@ function apifnWatchMiddleware(options) {
|
|
|
1729
1737
|
Accept: "application/json"
|
|
1730
1738
|
};
|
|
1731
1739
|
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
|
1740
|
+
function warn(message, err) {
|
|
1741
|
+
if (silent) return;
|
|
1742
|
+
try {
|
|
1743
|
+
const detail = err === void 0 ? "" : `: ${describeError(err)}`;
|
|
1744
|
+
console.warn(`[apifn/watchfn] ${message}${detail}`);
|
|
1745
|
+
} catch {
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1732
1748
|
async function sendEvent(event) {
|
|
1733
1749
|
try {
|
|
1734
1750
|
const controller = new AbortController();
|
|
@@ -1744,38 +1760,43 @@ function apifnWatchMiddleware(options) {
|
|
|
1744
1760
|
} finally {
|
|
1745
1761
|
clearTimeout(timer);
|
|
1746
1762
|
}
|
|
1747
|
-
if (!res.ok
|
|
1748
|
-
|
|
1763
|
+
if (!res.ok) {
|
|
1764
|
+
warn(`Event POST returned HTTP ${res.status}`);
|
|
1749
1765
|
}
|
|
1750
1766
|
} catch (err) {
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1767
|
+
warn("Failed to send event to watchfn", err);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
async function observe(request, statusCode, start) {
|
|
1771
|
+
try {
|
|
1772
|
+
const event = {
|
|
1773
|
+
method: request.method,
|
|
1774
|
+
path: new URL(request.url).pathname,
|
|
1775
|
+
status_code: statusCode,
|
|
1776
|
+
duration_ms: Date.now() - start,
|
|
1777
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1778
|
+
};
|
|
1779
|
+
if (fireAndForget) {
|
|
1780
|
+
sendEvent(event).catch(() => {
|
|
1781
|
+
});
|
|
1782
|
+
} else {
|
|
1783
|
+
await sendEvent(event);
|
|
1754
1784
|
}
|
|
1785
|
+
} catch (err) {
|
|
1786
|
+
warn("Failed to record request telemetry", err);
|
|
1755
1787
|
}
|
|
1756
1788
|
}
|
|
1757
|
-
return async function watchMiddleware(
|
|
1789
|
+
return async function watchMiddleware(request, _context, next) {
|
|
1758
1790
|
const start = Date.now();
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
const statusCode = s.statusCode ?? 200;
|
|
1766
|
-
const event = {
|
|
1767
|
-
method,
|
|
1768
|
-
path,
|
|
1769
|
-
status_code: statusCode,
|
|
1770
|
-
duration_ms: durationMs,
|
|
1771
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1772
|
-
};
|
|
1773
|
-
if (fireAndForget) {
|
|
1774
|
-
sendEvent(event).catch(() => {
|
|
1775
|
-
});
|
|
1776
|
-
} else {
|
|
1777
|
-
await sendEvent(event);
|
|
1791
|
+
let response;
|
|
1792
|
+
try {
|
|
1793
|
+
response = await next();
|
|
1794
|
+
} catch (err) {
|
|
1795
|
+
await observe(request, UNHANDLED_ERROR_STATUS, start);
|
|
1796
|
+
throw err;
|
|
1778
1797
|
}
|
|
1798
|
+
await observe(request, response.status, start);
|
|
1799
|
+
return response;
|
|
1779
1800
|
};
|
|
1780
1801
|
}
|
|
1781
1802
|
|