@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/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IntrospectOptions, IntrospectedRoute, SchemaDescriptor, SchemaInput, JsonSchema, OpenAPIGenerateOptions, OpenAPIDocument, ValidationError, DiffResult, DiffEntry, WatchFnClient, EndpointMetrics, AuthFnClient, SecFnRateLimiter, RateLimitInfo, LogFnClient } from './types.cjs';
|
|
2
2
|
export { AuthConfig, ComponentsObject, CorsOptions, EncodingObject, ExampleObject, ExternalDocsObject, HeaderObject, HttpMethod, LinkObject, MediaTypeObject, MockServerOptions, OAuthFlowObject, OAuthFlowsObject, OpenAPIInfoObject, OperationObject, ParameterDescriptor, ParameterObject, PathItemObject, PathsObject, ReferenceObject, RequestBodyDescriptor, RequestBodyObject, ResponseDescriptor, ResponseObject, SchemaDescriptorResponse, SchemaObject, SecurityRequirement, SecuritySchemeObject, ServerObject, SnippetOptions, SnippetTarget, TagObject } from './types.cjs';
|
|
3
|
-
import { Router } from '@superfunctions/http';
|
|
3
|
+
import { Router, Middleware } from '@superfunctions/http';
|
|
4
4
|
export { Middleware, Route, Router } from '@superfunctions/http';
|
|
5
5
|
|
|
6
6
|
declare function introspectRouter(router: Router<unknown>, options?: IntrospectOptions): IntrospectedRoute[];
|
|
@@ -105,8 +105,8 @@ declare function formatDiffAsText(result: DiffResult, colors?: {
|
|
|
105
105
|
* Provides:
|
|
106
106
|
* - `fetchEndpointMetrics()` — fetch performance metrics from a watchfn HTTP API
|
|
107
107
|
* - `createWatchFnClient()` — factory that returns a WatchFnClient
|
|
108
|
-
* - `apifnWatchMiddleware()` — @superfunctions/http
|
|
109
|
-
* metrics to watchfn via its event API
|
|
108
|
+
* - `apifnWatchMiddleware()` — @superfunctions/http `Middleware` that reports per-route
|
|
109
|
+
* metrics to watchfn via its event API and returns the downstream Response unchanged
|
|
110
110
|
*/
|
|
111
111
|
|
|
112
112
|
interface FetchEndpointMetricsOptions {
|
|
@@ -161,10 +161,17 @@ declare function createWatchFnClient(options: CreateWatchFnClientOptions): Watch
|
|
|
161
161
|
* @superfunctions/http middleware that captures route metrics and ships them
|
|
162
162
|
* to a watchfn event API endpoint (WATCH-004).
|
|
163
163
|
*
|
|
164
|
+
* It follows the router's `Middleware` contract: it returns the exact
|
|
165
|
+
* `Response` produced downstream (status, headers and body untouched, body
|
|
166
|
+
* never read) and records that Response's `status`. Telemetry failures never
|
|
167
|
+
* change the response: they are reported with `console.warn` unless `silent`
|
|
168
|
+
* is set.
|
|
169
|
+
*
|
|
164
170
|
* @example
|
|
171
|
+
* const router = createRouter({ routes });
|
|
165
172
|
* router.use(apifnWatchMiddleware({ baseUrl: "http://localhost:7100" }));
|
|
166
173
|
*/
|
|
167
|
-
declare function apifnWatchMiddleware(options: WatchFnMiddlewareOptions):
|
|
174
|
+
declare function apifnWatchMiddleware<TContext = unknown>(options: WatchFnMiddlewareOptions): Middleware<TContext>;
|
|
168
175
|
|
|
169
176
|
interface AuthfnHttpClientRequest {
|
|
170
177
|
method: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IntrospectOptions, IntrospectedRoute, SchemaDescriptor, SchemaInput, JsonSchema, OpenAPIGenerateOptions, OpenAPIDocument, ValidationError, DiffResult, DiffEntry, WatchFnClient, EndpointMetrics, AuthFnClient, SecFnRateLimiter, RateLimitInfo, LogFnClient } from './types.js';
|
|
2
2
|
export { AuthConfig, ComponentsObject, CorsOptions, EncodingObject, ExampleObject, ExternalDocsObject, HeaderObject, HttpMethod, LinkObject, MediaTypeObject, MockServerOptions, OAuthFlowObject, OAuthFlowsObject, OpenAPIInfoObject, OperationObject, ParameterDescriptor, ParameterObject, PathItemObject, PathsObject, ReferenceObject, RequestBodyDescriptor, RequestBodyObject, ResponseDescriptor, ResponseObject, SchemaDescriptorResponse, SchemaObject, SecurityRequirement, SecuritySchemeObject, ServerObject, SnippetOptions, SnippetTarget, TagObject } from './types.js';
|
|
3
|
-
import { Router } from '@superfunctions/http';
|
|
3
|
+
import { Router, Middleware } from '@superfunctions/http';
|
|
4
4
|
export { Middleware, Route, Router } from '@superfunctions/http';
|
|
5
5
|
|
|
6
6
|
declare function introspectRouter(router: Router<unknown>, options?: IntrospectOptions): IntrospectedRoute[];
|
|
@@ -105,8 +105,8 @@ declare function formatDiffAsText(result: DiffResult, colors?: {
|
|
|
105
105
|
* Provides:
|
|
106
106
|
* - `fetchEndpointMetrics()` — fetch performance metrics from a watchfn HTTP API
|
|
107
107
|
* - `createWatchFnClient()` — factory that returns a WatchFnClient
|
|
108
|
-
* - `apifnWatchMiddleware()` — @superfunctions/http
|
|
109
|
-
* metrics to watchfn via its event API
|
|
108
|
+
* - `apifnWatchMiddleware()` — @superfunctions/http `Middleware` that reports per-route
|
|
109
|
+
* metrics to watchfn via its event API and returns the downstream Response unchanged
|
|
110
110
|
*/
|
|
111
111
|
|
|
112
112
|
interface FetchEndpointMetricsOptions {
|
|
@@ -161,10 +161,17 @@ declare function createWatchFnClient(options: CreateWatchFnClientOptions): Watch
|
|
|
161
161
|
* @superfunctions/http middleware that captures route metrics and ships them
|
|
162
162
|
* to a watchfn event API endpoint (WATCH-004).
|
|
163
163
|
*
|
|
164
|
+
* It follows the router's `Middleware` contract: it returns the exact
|
|
165
|
+
* `Response` produced downstream (status, headers and body untouched, body
|
|
166
|
+
* never read) and records that Response's `status`. Telemetry failures never
|
|
167
|
+
* change the response: they are reported with `console.warn` unless `silent`
|
|
168
|
+
* is set.
|
|
169
|
+
*
|
|
164
170
|
* @example
|
|
171
|
+
* const router = createRouter({ routes });
|
|
165
172
|
* router.use(apifnWatchMiddleware({ baseUrl: "http://localhost:7100" }));
|
|
166
173
|
*/
|
|
167
|
-
declare function apifnWatchMiddleware(options: WatchFnMiddlewareOptions):
|
|
174
|
+
declare function apifnWatchMiddleware<TContext = unknown>(options: WatchFnMiddlewareOptions): Middleware<TContext>;
|
|
168
175
|
|
|
169
176
|
interface AuthfnHttpClientRequest {
|
|
170
177
|
method: string;
|
package/dist/index.js
CHANGED
|
@@ -1595,6 +1595,14 @@ function createWatchFnClient(options) {
|
|
|
1595
1595
|
})
|
|
1596
1596
|
};
|
|
1597
1597
|
}
|
|
1598
|
+
function describeError(err) {
|
|
1599
|
+
try {
|
|
1600
|
+
return err instanceof Error ? String(err.message) : String(err);
|
|
1601
|
+
} catch {
|
|
1602
|
+
return "unknown error";
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
var UNHANDLED_ERROR_STATUS = 500;
|
|
1598
1606
|
function apifnWatchMiddleware(options) {
|
|
1599
1607
|
const {
|
|
1600
1608
|
baseUrl,
|
|
@@ -1609,6 +1617,14 @@ function apifnWatchMiddleware(options) {
|
|
|
1609
1617
|
Accept: "application/json"
|
|
1610
1618
|
};
|
|
1611
1619
|
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
|
1620
|
+
function warn(message, err) {
|
|
1621
|
+
if (silent) return;
|
|
1622
|
+
try {
|
|
1623
|
+
const detail = err === void 0 ? "" : `: ${describeError(err)}`;
|
|
1624
|
+
console.warn(`[apifn/watchfn] ${message}${detail}`);
|
|
1625
|
+
} catch {
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1612
1628
|
async function sendEvent(event) {
|
|
1613
1629
|
try {
|
|
1614
1630
|
const controller = new AbortController();
|
|
@@ -1624,38 +1640,43 @@ function apifnWatchMiddleware(options) {
|
|
|
1624
1640
|
} finally {
|
|
1625
1641
|
clearTimeout(timer);
|
|
1626
1642
|
}
|
|
1627
|
-
if (!res.ok
|
|
1628
|
-
|
|
1643
|
+
if (!res.ok) {
|
|
1644
|
+
warn(`Event POST returned HTTP ${res.status}`);
|
|
1629
1645
|
}
|
|
1630
1646
|
} catch (err) {
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1647
|
+
warn("Failed to send event to watchfn", err);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
async function observe(request, statusCode, start) {
|
|
1651
|
+
try {
|
|
1652
|
+
const event = {
|
|
1653
|
+
method: request.method,
|
|
1654
|
+
path: new URL(request.url).pathname,
|
|
1655
|
+
status_code: statusCode,
|
|
1656
|
+
duration_ms: Date.now() - start,
|
|
1657
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1658
|
+
};
|
|
1659
|
+
if (fireAndForget) {
|
|
1660
|
+
sendEvent(event).catch(() => {
|
|
1661
|
+
});
|
|
1662
|
+
} else {
|
|
1663
|
+
await sendEvent(event);
|
|
1634
1664
|
}
|
|
1665
|
+
} catch (err) {
|
|
1666
|
+
warn("Failed to record request telemetry", err);
|
|
1635
1667
|
}
|
|
1636
1668
|
}
|
|
1637
|
-
return async function watchMiddleware(
|
|
1669
|
+
return async function watchMiddleware(request, _context, next) {
|
|
1638
1670
|
const start = Date.now();
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
const statusCode = s.statusCode ?? 200;
|
|
1646
|
-
const event = {
|
|
1647
|
-
method,
|
|
1648
|
-
path,
|
|
1649
|
-
status_code: statusCode,
|
|
1650
|
-
duration_ms: durationMs,
|
|
1651
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1652
|
-
};
|
|
1653
|
-
if (fireAndForget) {
|
|
1654
|
-
sendEvent(event).catch(() => {
|
|
1655
|
-
});
|
|
1656
|
-
} else {
|
|
1657
|
-
await sendEvent(event);
|
|
1671
|
+
let response;
|
|
1672
|
+
try {
|
|
1673
|
+
response = await next();
|
|
1674
|
+
} catch (err) {
|
|
1675
|
+
await observe(request, UNHANDLED_ERROR_STATUS, start);
|
|
1676
|
+
throw err;
|
|
1658
1677
|
}
|
|
1678
|
+
await observe(request, response.status, start);
|
|
1679
|
+
return response;
|
|
1659
1680
|
};
|
|
1660
1681
|
}
|
|
1661
1682
|
|