@stainlessdev/xray-fetch 0.6.0-dev.c5f614b → 0.7.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.
Files changed (2) hide show
  1. package/README.md +80 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @stainlessdev/xray-fetch
2
+
3
+ Fetch API adapter for Stainless X-ray request logging. Use this in edge runtimes, web workers, or any environment with the Fetch API.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @stainlessdev/xray-fetch
9
+ ```
10
+
11
+ ## Basic usage (Fetch handler)
12
+
13
+ ```ts
14
+ import { createEmitter, wrapFetch } from '@stainlessdev/xray-fetch';
15
+
16
+ const xray = createEmitter({
17
+ serviceName: 'my-service',
18
+ endpointUrl: 'http://localhost:4318',
19
+ });
20
+
21
+ const handler = wrapFetch(async (_req) => {
22
+ return new Response('ok', { status: 200 });
23
+ }, xray);
24
+ ```
25
+
26
+ ## Preserve request/response objects
27
+
28
+ If you need to keep the original `Request`/`Response` objects (for framework compatibility), use `wrapFetchPreserve`:
29
+
30
+ ```ts
31
+ import { wrapFetchPreserve } from '@stainlessdev/xray-fetch';
32
+
33
+ const handler = wrapFetchPreserve(async (req) => {
34
+ return new Response(await req.text());
35
+ }, xray);
36
+ ```
37
+
38
+ ## Access the X-ray context
39
+
40
+ ```ts
41
+ import { getXrayContext } from '@stainlessdev/xray-fetch';
42
+
43
+ const handler = wrapFetch(async (req) => {
44
+ const ctx = getXrayContext(req);
45
+ ctx?.setUserId('user-123');
46
+ return new Response('ok');
47
+ }, xray);
48
+ ```
49
+
50
+ ## Request IDs and response headers
51
+
52
+ X-ray will **auto-generate a request ID and inject it into your response headers** under the configured name (`requestId.header`, default `request-id`, emitted as `Request-Id`) if the header is missing. If you set your own request ID first (via `options.requestId` or by setting the response header yourself), X-ray preserves it and does not overwrite the header.
53
+
54
+ ## Configuration
55
+
56
+ `createEmitter(config)` accepts `XrayRuntimeConfig` from `@stainlessdev/xray-core`:
57
+
58
+ - `serviceName` (required)
59
+ - `endpointUrl` (required; falls back to `STAINLESS_XRAY_ENDPOINT_URL` when omitted; explicit `endpointUrl` wins)
60
+ - `environment`, `version`, `logger`, `logLevel`
61
+ - `exporter`: `endpointUrl`, `headers`, `timeoutMs`, `spanProcessor`, `instance` (custom SpanExporter)
62
+ - `capture`: request/response headers and bodies
63
+ - `redaction`: headers/query/body JSON-path redaction
64
+ - `requestId`: header name to read/write
65
+ - `route`: normalization options
66
+
67
+ ## Adapter options (WrapOptions)
68
+
69
+ `wrapFetch(handler, xray, options)` and `wrapFetchPreserve(handler, xray, options)` share:
70
+
71
+ - `route`: override the route name for the request
72
+ - `requestId`: explicit request ID to use (prevents auto-generation)
73
+ - `capture`: per-request capture overrides
74
+ - `redaction`: per-request redaction overrides
75
+ - `onRequest(ctx)`, `onResponse(ctx, log)`, `onError(ctx, err)` hooks
76
+
77
+ ## Notes
78
+
79
+ - Requires a global `fetch` when using the default exporter. If `fetch` is not available, provide `exporter.instance` or use `@stainlessdev/xray-node`.
80
+ - This package depends on OpenTelemetry packages as peer dependencies.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stainlessdev/xray-fetch",
3
- "version": "0.6.0-dev.c5f614b",
3
+ "version": "0.7.0",
4
4
  "description": "Fetch API adapter for Stainless X-ray request logging",
5
5
  "files": [
6
6
  "dist"
@@ -22,7 +22,7 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
- "@stainlessdev/xray-core": "0.6.0-dev.c5f614b"
25
+ "@stainlessdev/xray-core": "0.7.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@opentelemetry/api": "^1.9.0",