@inferencebureau/apps-sdk 0.16.9 → 0.16.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/dist/index.d.ts CHANGED
@@ -1864,6 +1864,28 @@ export declare function parseDsn(dsn: string): {
1864
1864
  * take it. Never throws, and gives up after three seconds.
1865
1865
  */
1866
1866
  export declare function captureException(error: unknown, o?: CaptureOptions): Promise<string | null>;
1867
+ /** What Next.js passes onRequestError (instrumentation.ts), as far as the report reads it. */
1868
+ export interface RequestErrorRequest {
1869
+ path: string;
1870
+ method: string;
1871
+ headers: Record<string, string | string[] | undefined>;
1872
+ }
1873
+ export interface RequestErrorContext {
1874
+ routerKind?: string;
1875
+ routePath?: string;
1876
+ routeType?: string;
1877
+ }
1878
+ export interface ReportRequestErrorOptions {
1879
+ url?: string;
1880
+ token?: string;
1881
+ app?: string;
1882
+ environment?: string;
1883
+ fetch?: typeof fetch;
1884
+ }
1885
+ /** Reports an error the app threw handling a request to its Logs. Never throws, and gives up after three seconds. */
1886
+ export declare function reportRequestError(error: unknown, request: RequestErrorRequest, context?: RequestErrorContext, o?: ReportRequestErrorOptions): Promise<void>;
1887
+ /** Next.js's instrumentation hook: `export { onRequestError } from "@inferencebureau/apps-sdk"`. Builds add it for you. */
1888
+ export declare const onRequestError: (error: unknown, request: RequestErrorRequest, context: RequestErrorContext) => Promise<void>;
1867
1889
  export interface ShopifyPrivacyCustomer {
1868
1890
  id: number;
1869
1891
  email?: string | null;
package/dist/index.js CHANGED
@@ -1017,3 +1017,31 @@ export async function captureException(error, o = {}) {
1017
1017
  return null;
1018
1018
  }
1019
1019
  }
1020
+ /** Reports an error the app threw handling a request to its Logs. Never throws, and gives up after three seconds. */
1021
+ export async function reportRequestError(error, request, context = {}, o = {}) {
1022
+ const token = o.token ?? envVar("IB_ENVIRONMENT_TOKEN");
1023
+ const app = o.app ?? envVar("IB_APP");
1024
+ const err = error instanceof Error ? error : new Error(typeof error === "string" ? error : JSON.stringify(error));
1025
+ const id = request.headers["x-ib-request-id"];
1026
+ const digest = err.digest;
1027
+ const report = {
1028
+ requestId: typeof id === "string" ? id : null, method: request.method, path: String(request.path ?? "/").split("?")[0],
1029
+ message: err.message, name: err.name, digest: typeof digest === "string" ? digest : null,
1030
+ stack: err.stack ?? null, routerKind: context.routerKind === "Pages Router" ? "pages" : context.routerKind ? "app" : null,
1031
+ routePath: context.routePath ?? null, routeType: context.routeType ?? null,
1032
+ };
1033
+ const timeout = typeof AbortSignal.timeout === "function" ? { signal: AbortSignal.timeout(3000) } : {};
1034
+ const sends = [
1035
+ captureException(err, { handled: false, tags: { route: context.routePath, route_type: context.routeType, digest: report.digest }, ...(o.fetch ? { fetch: o.fetch } : {}) }),
1036
+ ];
1037
+ if (token && app) {
1038
+ const url = (o.url ?? envVar("IB_API_URL") ?? "https://api.inferencebureau.com").replace(/\/$/, "");
1039
+ const environment = o.environment ?? envVar("IB_ENVIRONMENT") ?? "development";
1040
+ sends.push((o.fetch ?? fetch)(`${url}/v1/apps/${encodeURIComponent(app)}/environments/${encodeURIComponent(environment)}/request-errors`, {
1041
+ method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(report), ...timeout,
1042
+ }));
1043
+ }
1044
+ await Promise.allSettled(sends);
1045
+ }
1046
+ /** Next.js's instrumentation hook: `export { onRequestError } from "@inferencebureau/apps-sdk"`. Builds add it for you. */
1047
+ export const onRequestError = (error, request, context) => reportRequestError(error, request, context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencebureau/apps-sdk",
3
- "version": "0.16.9",
3
+ "version": "0.16.10",
4
4
  "description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client, React hooks with AutoTable and AutoForm, and a test harness for its actions.",
5
5
  "license": "UNLICENSED",
6
6
  "keywords": [