@decocms/blocks 7.72.0 → 8.1.0-next.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 (148) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -3
  3. package/src/cms/schema.ts +1 -1
  4. package/src/hooks/LiveControls.tsx +2 -0
  5. package/src/hooks/useLoadMore.ts +2 -0
  6. package/src/index.ts +37 -0
  7. package/src/protocol/assets.ts +124 -0
  8. package/src/protocol/canonical.ts +7 -0
  9. package/src/protocol/ciphertext.ts +6 -0
  10. package/src/protocol/client.ts +170 -0
  11. package/src/protocol/conformance/cases/assets.ts +81 -0
  12. package/src/protocol/conformance/cases/guards.ts +314 -0
  13. package/src/protocol/conformance/cases/reads.ts +168 -0
  14. package/src/protocol/conformance/cases/types.ts +9 -0
  15. package/src/protocol/conformance/cases/wire.ts +138 -0
  16. package/src/protocol/conformance/cases/writes.ts +345 -0
  17. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  18. package/src/protocol/conformance/context.ts +222 -0
  19. package/src/protocol/conformance/index.ts +140 -0
  20. package/src/protocol/errors.ts +142 -0
  21. package/src/protocol/index.ts +39 -0
  22. package/src/protocol/keys.ts +279 -0
  23. package/src/protocol/params.ts +67 -0
  24. package/src/protocol/secrets.ts +231 -0
  25. package/src/protocol/server/assets.ts +119 -0
  26. package/src/protocol/server/auth.ts +49 -0
  27. package/src/protocol/server/bodyCache.ts +84 -0
  28. package/src/protocol/server/content.ts +175 -0
  29. package/src/protocol/server/core.ts +112 -0
  30. package/src/protocol/server/handler.ts +102 -0
  31. package/src/protocol/server/http.ts +139 -0
  32. package/src/protocol/server/index.ts +11 -0
  33. package/src/protocol/server/methods/apply.ts +341 -0
  34. package/src/protocol/server/methods/read.ts +156 -0
  35. package/src/protocol/server/rpc.ts +136 -0
  36. package/src/protocol/storage/fs/hash.ts +21 -0
  37. package/src/protocol/storage/fs/index.ts +338 -0
  38. package/src/protocol/storage/fs/lock.ts +68 -0
  39. package/src/protocol/storage/fs/transaction.ts +183 -0
  40. package/src/protocol/storage/fs/unsupported.ts +15 -0
  41. package/src/protocol/storage/memory.ts +238 -0
  42. package/src/protocol/storage.ts +182 -0
  43. package/src/protocol/types.ts +172 -0
  44. package/src/v8/analytics.ts +50 -0
  45. package/src/v8/background.ts +107 -0
  46. package/src/v8/builtins/data.ts +23 -0
  47. package/src/v8/builtins/index.ts +26 -0
  48. package/src/v8/builtins/lazy.ts +23 -0
  49. package/src/v8/builtins/legacy.ts +65 -0
  50. package/src/v8/builtins/matchers.ts +31 -0
  51. package/src/v8/builtins/multivariate.ts +25 -0
  52. package/src/v8/builtins/secret.ts +82 -0
  53. package/src/v8/canonical.ts +117 -0
  54. package/src/v8/ciphertext.ts +158 -0
  55. package/src/v8/cli/builtins.ts +20 -0
  56. package/src/v8/cli/check/index.ts +774 -0
  57. package/src/v8/cli/check/messages.ts +168 -0
  58. package/src/v8/cli/content.ts +287 -0
  59. package/src/v8/cli/index.ts +17 -0
  60. package/src/v8/cli/log.ts +18 -0
  61. package/src/v8/cli/main.ts +12 -0
  62. package/src/v8/cli/root.ts +66 -0
  63. package/src/v8/cli/run.ts +236 -0
  64. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  65. package/src/v8/cli/schema/generate.ts +507 -0
  66. package/src/v8/cli/schema/index.ts +63 -0
  67. package/src/v8/cli/schema/tsProgram.ts +497 -0
  68. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  69. package/src/v8/cli/serve/server.ts +303 -0
  70. package/src/v8/cli/watch.ts +86 -0
  71. package/src/v8/client.ts +147 -0
  72. package/src/v8/cms.ts +237 -0
  73. package/src/v8/content.ts +195 -0
  74. package/src/v8/draft.ts +136 -0
  75. package/src/v8/errors.ts +59 -0
  76. package/src/v8/fetch.ts +152 -0
  77. package/src/v8/identity.ts +65 -0
  78. package/src/v8/index.ts +39 -0
  79. package/src/v8/json.ts +74 -0
  80. package/src/v8/matchRoute.ts +204 -0
  81. package/src/v8/remoteLoader.ts +282 -0
  82. package/src/v8/resolver.ts +278 -0
  83. package/src/v8/secrets.ts +17 -0
  84. package/src/v8/telemetry.ts +533 -0
  85. package/src/v8/types.ts +240 -0
  86. package/src/cms/blockSource.test.ts +0 -76
  87. package/src/cms/client.browserBundle.test.ts +0 -44
  88. package/src/cms/deferredTrigger.test.ts +0 -32
  89. package/src/cms/draftSource.test.ts +0 -519
  90. package/src/cms/layoutCacheRace.test.ts +0 -181
  91. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  92. package/src/cms/loader.test.ts +0 -557
  93. package/src/cms/registry.test.ts +0 -110
  94. package/src/cms/renderJson.test.ts +0 -167
  95. package/src/cms/resolve.test.ts +0 -1492
  96. package/src/cms/schema.test.ts +0 -356
  97. package/src/cms/sectionLoaderContext.test.ts +0 -111
  98. package/src/cms/sectionLoaders.test.ts +0 -625
  99. package/src/cms/sectionMixins.test.ts +0 -167
  100. package/src/cms/stickyFlags.test.ts +0 -117
  101. package/src/flags/flags.test.ts +0 -148
  102. package/src/hooks/Image.test.ts +0 -239
  103. package/src/hooks/RenderSection.test.tsx +0 -81
  104. package/src/hooks/Stats.test.ts +0 -112
  105. package/src/hooks/useLoadMore.test.ts +0 -205
  106. package/src/matchers/builtins.test.ts +0 -318
  107. package/src/matchers/override.test.ts +0 -205
  108. package/src/middleware/hydrationContext.test.ts +0 -61
  109. package/src/middleware/observability.test.ts +0 -335
  110. package/src/middleware/validateSection.test.ts +0 -147
  111. package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
  112. package/src/preview/DraftPreviewBadge.test.ts +0 -55
  113. package/src/sdk/abTesting.test.ts +0 -381
  114. package/src/sdk/cacheHeaders.test.ts +0 -258
  115. package/src/sdk/cacheStorage.test.ts +0 -242
  116. package/src/sdk/cachedLoader.test.ts +0 -122
  117. package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
  118. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  119. package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
  120. package/src/sdk/cn.test.ts +0 -34
  121. package/src/sdk/composite.test.ts +0 -121
  122. package/src/sdk/cookie.test.ts +0 -108
  123. package/src/sdk/crypto.test.ts +0 -65
  124. package/src/sdk/deepOmit.test.ts +0 -45
  125. package/src/sdk/encoding.test.ts +0 -71
  126. package/src/sdk/env.test.ts +0 -33
  127. package/src/sdk/experiments.test.ts +0 -328
  128. package/src/sdk/fetchCache.test.ts +0 -164
  129. package/src/sdk/flags.test.ts +0 -91
  130. package/src/sdk/http.test.ts +0 -71
  131. package/src/sdk/inflightTimeout.test.ts +0 -35
  132. package/src/sdk/instrumentedFetch.test.ts +0 -559
  133. package/src/sdk/invoke.test.ts +0 -115
  134. package/src/sdk/logger.test.ts +0 -432
  135. package/src/sdk/otel.test.ts +0 -691
  136. package/src/sdk/otelAdapters.test.ts +0 -89
  137. package/src/sdk/otelHttpLog.test.ts +0 -457
  138. package/src/sdk/otelHttpMeter.test.ts +0 -395
  139. package/src/sdk/otelHttpTracer.test.ts +0 -474
  140. package/src/sdk/replaceStream.test.ts +0 -117
  141. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  142. package/src/sdk/splitExactRedirects.test.ts +0 -154
  143. package/src/sdk/urlRedaction.test.ts +0 -73
  144. package/src/sdk/urlUtils.test.ts +0 -34
  145. package/src/sdk/useDevice.test.ts +0 -130
  146. package/src/sdk/useScript.test.ts +0 -128
  147. package/src/sdk/useSuggestions.test.ts +0 -230
  148. package/tsconfig.json +0 -7
@@ -1,559 +0,0 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
- import { createInstrumentedFetch } from "./instrumentedFetch";
3
- import { configureLogger, defaultLoggerAdapter } from "./logger";
4
- import type { Span, TracerAdapter } from "./observability";
5
- import { configureTracer, setObservabilitySpanStore } from "./observability";
6
-
7
- function makeFakeTracer(): {
8
- tracer: TracerAdapter;
9
- startSpan: ReturnType<typeof vi.fn>;
10
- spans: Array<ReturnType<typeof makeFakeSpan>>;
11
- } {
12
- const spans: Array<ReturnType<typeof makeFakeSpan>> = [];
13
- const startSpan = vi.fn((name: string, attrs?: Record<string, string | number | boolean>) => {
14
- const s = makeFakeSpan(name, attrs);
15
- spans.push(s);
16
- return s.span;
17
- });
18
- return { tracer: { startSpan } as TracerAdapter, startSpan, spans };
19
- }
20
-
21
- function makeFakeSpan(
22
- name: string,
23
- initialAttrs?: Record<string, string | number | boolean>,
24
- ctx?: { traceId: string; spanId: string; traceFlags: number },
25
- ) {
26
- const attrs: Record<string, string | number | boolean> = { ...(initialAttrs ?? {}) };
27
- const span: Span = {
28
- end: vi.fn(),
29
- setError: vi.fn(),
30
- setAttribute: vi.fn((k: string, v: string | number | boolean) => {
31
- attrs[k] = v;
32
- }),
33
- spanContext: ctx ? () => ctx : undefined,
34
- };
35
- return { name, span, attrs };
36
- }
37
-
38
- describe("createInstrumentedFetch — URL redaction", () => {
39
- afterEach(() => {
40
- configureTracer({ startSpan: () => ({ end: () => {} }) });
41
- setObservabilitySpanStore(undefined);
42
- configureLogger(defaultLoggerAdapter);
43
- vi.restoreAllMocks();
44
- });
45
-
46
- it("stamps a redacted http.url on the span, not the raw URL", async () => {
47
- const { tracer, spans } = makeFakeTracer();
48
- configureTracer(tracer);
49
- const baseFetch = vi.fn(async () => new Response("ok", { status: 200 }));
50
-
51
- const f = createInstrumentedFetch({
52
- name: "vtex",
53
- baseFetch: baseFetch as unknown as typeof fetch,
54
- });
55
-
56
- await f("https://api.test/search?token=SECRET123&page=2");
57
-
58
- expect(spans).toHaveLength(1);
59
- expect(spans[0].attrs["http.url"]).toBe("https://api.test/search?token=REDACTED&page=REDACTED");
60
- });
61
-
62
- it("honors keepQueryKeys for benign query params", async () => {
63
- const { tracer, spans } = makeFakeTracer();
64
- configureTracer(tracer);
65
- const baseFetch = vi.fn(async () => new Response("ok"));
66
-
67
- const f = createInstrumentedFetch({
68
- name: "vtex",
69
- baseFetch: baseFetch as unknown as typeof fetch,
70
- keepQueryKeys: ["page", "sort"],
71
- });
72
-
73
- await f("https://api.test/search?token=SECRET&page=2&sort=name");
74
-
75
- expect(spans[0].attrs["http.url"]).toBe(
76
- "https://api.test/search?token=REDACTED&page=2&sort=name",
77
- );
78
- });
79
-
80
- it("emits the structured `outgoing fetch` log with host+path when OTEL_LOG_OUTGOING_FETCH=true", async () => {
81
- // The breadcrumb is gated behind an env flag to avoid log explosion
82
- // in production; we flip it on for the test and assert on the
83
- // payload to keep this test honest about what it verifies.
84
- const captured: Array<{
85
- level: string;
86
- msg: string;
87
- attrs?: Record<string, unknown>;
88
- }> = [];
89
- configureLogger({
90
- log: (level, msg, attrs) => {
91
- captured.push({ level, msg, attrs });
92
- },
93
- });
94
-
95
- const previous = process.env.OTEL_LOG_OUTGOING_FETCH;
96
- process.env.OTEL_LOG_OUTGOING_FETCH = "true";
97
-
98
- try {
99
- const baseFetch = vi.fn(async () => new Response("ok", { status: 200 }));
100
- const f = createInstrumentedFetch({
101
- name: "vtex",
102
- baseFetch: baseFetch as unknown as typeof fetch,
103
- logging: false,
104
- });
105
-
106
- const res = await f("https://api.test/items?id=42");
107
- expect(res.status).toBe(200);
108
-
109
- const breadcrumb = captured.find((c) => c.msg === "outgoing fetch");
110
- expect(breadcrumb).toBeDefined();
111
- expect(breadcrumb?.level).toBe("info");
112
- expect(breadcrumb?.attrs).toMatchObject({
113
- app: "vtex",
114
- host: "api.test",
115
- path: "/items",
116
- method: "GET",
117
- status: 200,
118
- ok: true,
119
- });
120
- } finally {
121
- if (previous === undefined) {
122
- delete process.env.OTEL_LOG_OUTGOING_FETCH;
123
- } else {
124
- process.env.OTEL_LOG_OUTGOING_FETCH = previous;
125
- }
126
- }
127
- });
128
-
129
- it("does NOT emit the `outgoing fetch` breadcrumb when the env flag is unset", async () => {
130
- const captured: Array<{ msg: string }> = [];
131
- configureLogger({
132
- log: (_level, msg) => captured.push({ msg }),
133
- });
134
-
135
- const previous = process.env.OTEL_LOG_OUTGOING_FETCH;
136
- delete process.env.OTEL_LOG_OUTGOING_FETCH;
137
-
138
- try {
139
- const baseFetch = vi.fn(async () => new Response("ok"));
140
- const f = createInstrumentedFetch({
141
- name: "vtex",
142
- baseFetch: baseFetch as unknown as typeof fetch,
143
- logging: false,
144
- });
145
- await f("https://api.test/items?id=42");
146
-
147
- expect(captured.find((c) => c.msg === "outgoing fetch")).toBeUndefined();
148
- } finally {
149
- if (previous !== undefined) process.env.OTEL_LOG_OUTGOING_FETCH = previous;
150
- }
151
- });
152
- });
153
-
154
- describe("createInstrumentedFetch — traceparent injection", () => {
155
- afterEach(() => {
156
- configureTracer({ startSpan: () => ({ end: () => {} }) });
157
- setObservabilitySpanStore(undefined);
158
- vi.restoreAllMocks();
159
- });
160
-
161
- it("injects traceparent on outbound calls when a span is active", async () => {
162
- // Install a tracer that creates a fake span whose spanContext()
163
- // returns a known id, AND wire the spanStore so getActiveSpan()
164
- // can find it across the await boundary inside withTracing.
165
- const knownCtx = {
166
- traceId: "0123456789abcdef0123456789abcdef",
167
- spanId: "fedcba9876543210",
168
- traceFlags: 1,
169
- };
170
-
171
- // The redacted "active span" is the one createInstrumentedFetch starts.
172
- // injectTraceContext reads `getActiveSpan()`, which only works inside
173
- // a `withTracing` / spanStore.run scope. The simplest stub: install a
174
- // tracer that returns a span with spanContext, AND make the spanStore
175
- // resolve that span when fetched.
176
- const fakeSpan = makeFakeSpan("vtex.fetch", undefined, knownCtx);
177
- configureTracer({
178
- startSpan: () => fakeSpan.span,
179
- });
180
-
181
- // Custom span store that returns the fake span on every get(). This
182
- // models the host's ALS-backed store with a single active span.
183
- setObservabilitySpanStore({
184
- get: () => fakeSpan.span,
185
- run: (_span, fn) => fn(),
186
- });
187
-
188
- const baseFetch = vi.fn(async (_input: unknown, init?: RequestInit) => {
189
- const h = new Headers(init?.headers);
190
- return new Response(h.get("traceparent") ?? "<missing>", { status: 200 });
191
- });
192
-
193
- const f = createInstrumentedFetch({
194
- name: "vtex",
195
- baseFetch: baseFetch as unknown as typeof fetch,
196
- logging: false,
197
- });
198
-
199
- const res = await f("https://api.test/x");
200
- const body = await res.text();
201
- expect(body).toBe(`00-${knownCtx.traceId}-${knownCtx.spanId}-01`);
202
- });
203
-
204
- it("does NOT inject traceparent when injectTraceparent: false", async () => {
205
- const fakeSpan = makeFakeSpan("vtex.fetch", undefined, {
206
- traceId: "11111111111111111111111111111111",
207
- spanId: "2222222222222222",
208
- traceFlags: 1,
209
- });
210
- configureTracer({ startSpan: () => fakeSpan.span });
211
- setObservabilitySpanStore({
212
- get: () => fakeSpan.span,
213
- run: (_s, fn) => fn(),
214
- });
215
-
216
- const baseFetch = vi.fn(async (_i: unknown, init?: RequestInit) => {
217
- const h = new Headers(init?.headers);
218
- return new Response(JSON.stringify({ traceparent: h.get("traceparent") }), { status: 200 });
219
- });
220
-
221
- const f = createInstrumentedFetch({
222
- name: "vtex",
223
- baseFetch: baseFetch as unknown as typeof fetch,
224
- logging: false,
225
- injectTraceparent: false,
226
- });
227
-
228
- const res = await f("https://api.test/x");
229
- const body = (await res.json()) as { traceparent: string | null };
230
- expect(body.traceparent).toBeNull();
231
- });
232
-
233
- it("is a safe no-op when no span is active", async () => {
234
- // No tracer configured, no spanStore — injectTraceContext returns early.
235
- configureTracer({ startSpan: () => ({ end: () => {} }) });
236
- setObservabilitySpanStore(undefined);
237
-
238
- const baseFetch = vi.fn(async (_i: unknown, init?: RequestInit) => {
239
- const h = new Headers(init?.headers);
240
- return new Response(h.get("traceparent") ?? "<missing>", { status: 200 });
241
- });
242
-
243
- const f = createInstrumentedFetch({
244
- name: "vtex",
245
- baseFetch: baseFetch as unknown as typeof fetch,
246
- logging: false,
247
- });
248
-
249
- const res = await f("https://api.test/x");
250
- expect(await res.text()).toBe("<missing>");
251
- });
252
-
253
- it("preserves caller-supplied headers when injecting traceparent", async () => {
254
- const fakeSpan = makeFakeSpan("vtex.fetch", undefined, {
255
- traceId: "33333333333333333333333333333333",
256
- spanId: "4444444444444444",
257
- traceFlags: 1,
258
- });
259
- configureTracer({ startSpan: () => fakeSpan.span });
260
- setObservabilitySpanStore({
261
- get: () => fakeSpan.span,
262
- run: (_s, fn) => fn(),
263
- });
264
-
265
- const baseFetch = vi.fn(async (_i: unknown, init?: RequestInit) => {
266
- const h = new Headers(init?.headers);
267
- return new Response(
268
- JSON.stringify({
269
- auth: h.get("authorization"),
270
- tp: h.get("traceparent"),
271
- }),
272
- { status: 200 },
273
- );
274
- });
275
-
276
- const f = createInstrumentedFetch({
277
- name: "vtex",
278
- baseFetch: baseFetch as unknown as typeof fetch,
279
- logging: false,
280
- });
281
-
282
- const res = await f("https://api.test/x", {
283
- headers: { authorization: "Bearer abc" },
284
- });
285
- const body = (await res.json()) as { auth: string; tp: string };
286
- expect(body.auth).toBe("Bearer abc");
287
- expect(body.tp).toBe(
288
- `00-${fakeSpan.span.spanContext!().traceId}-${fakeSpan.span.spanContext!().spanId}-01`,
289
- );
290
- });
291
-
292
- it("honors Fetch-spec semantics: init.headers REPLACES Request headers (does not union)", async () => {
293
- // Per the Fetch spec, when both a Request and `init.headers` are
294
- // passed to `fetch()`, init.headers replace Request.headers — not
295
- // union. Verify our wrapper preserves that contract: a Request with
296
- // header `x-from-req` and an init with header `x-from-init` should
297
- // surface only `x-from-init` on the wire, plus our injected
298
- // traceparent.
299
- const fakeSpan = makeFakeSpan("vtex.fetch", undefined, {
300
- traceId: "55555555555555555555555555555555",
301
- spanId: "6666666666666666",
302
- traceFlags: 1,
303
- });
304
- configureTracer({ startSpan: () => fakeSpan.span });
305
- setObservabilitySpanStore({
306
- get: () => fakeSpan.span,
307
- run: (_s, fn) => fn(),
308
- });
309
-
310
- const baseFetch = vi.fn(async (_i: unknown, init?: RequestInit) => {
311
- const h = new Headers(init?.headers);
312
- return new Response(
313
- JSON.stringify({
314
- fromReq: h.get("x-from-req"),
315
- fromInit: h.get("x-from-init"),
316
- tp: h.get("traceparent"),
317
- }),
318
- { status: 200 },
319
- );
320
- });
321
-
322
- const f = createInstrumentedFetch({
323
- name: "vtex",
324
- baseFetch: baseFetch as unknown as typeof fetch,
325
- logging: false,
326
- });
327
-
328
- const req = new Request("https://api.test/x", {
329
- headers: { "x-from-req": "REQ" },
330
- });
331
- const res = await f(req, { headers: { "x-from-init": "INIT" } });
332
- const body = (await res.json()) as {
333
- fromReq: string | null;
334
- fromInit: string | null;
335
- tp: string | null;
336
- };
337
- // init.headers replaces, so x-from-req must NOT leak through.
338
- expect(body.fromReq).toBeNull();
339
- expect(body.fromInit).toBe("INIT");
340
- expect(body.tp).toMatch(/^00-/);
341
- });
342
-
343
- it("uses init.operation to name the span", async () => {
344
- const { tracer, spans } = makeFakeTracer();
345
- configureTracer(tracer);
346
- const baseFetch = vi.fn(async () => new Response("ok", { status: 200 }));
347
-
348
- const f = createInstrumentedFetch({
349
- name: "vtex",
350
- baseFetch: baseFetch as unknown as typeof fetch,
351
- logging: false,
352
- });
353
-
354
- await f("https://api.test/whatever", {
355
- method: "POST",
356
- operation: "intelligent-search.product_search",
357
- });
358
-
359
- expect(spans).toHaveLength(1);
360
- expect(spans[0].name).toBe("vtex.intelligent-search.product_search");
361
- expect(spans[0].attrs["fetch.operation"]).toBe("intelligent-search.product_search");
362
- });
363
-
364
- it("strips `operation` from init before calling baseFetch", async () => {
365
- const baseFetch = vi.fn(async (_input: unknown, init?: RequestInit) => {
366
- // `operation` is an extension on InstrumentedFetchInit, NOT a
367
- // valid RequestInit property. It must be removed before the
368
- // underlying fetch sees the init.
369
- expect(init && "operation" in init).toBe(false);
370
- return new Response("ok");
371
- });
372
- const f = createInstrumentedFetch({
373
- name: "vtex",
374
- baseFetch: baseFetch as unknown as typeof fetch,
375
- logging: false,
376
- });
377
- await f("https://api.test/x", {
378
- method: "POST",
379
- headers: { "content-type": "application/json" },
380
- operation: "checkout.orderForm",
381
- });
382
- expect(baseFetch).toHaveBeenCalledOnce();
383
- });
384
-
385
- it("falls back to defaultOperation when init.operation is omitted", async () => {
386
- const { tracer, spans } = makeFakeTracer();
387
- configureTracer(tracer);
388
- const baseFetch = vi.fn(async () => new Response("ok"));
389
-
390
- const f = createInstrumentedFetch({
391
- name: "resend",
392
- baseFetch: baseFetch as unknown as typeof fetch,
393
- logging: false,
394
- defaultOperation: "emails.send",
395
- });
396
- await f("https://api.resend.com/emails");
397
-
398
- expect(spans[0].name).toBe("resend.emails.send");
399
- expect(spans[0].attrs["fetch.operation"]).toBe("emails.send");
400
- });
401
-
402
- it("falls back to resolveOperation when init.operation + defaultOperation are unset", async () => {
403
- const { tracer, spans } = makeFakeTracer();
404
- configureTracer(tracer);
405
- const baseFetch = vi.fn(async () => new Response("ok"));
406
-
407
- const resolveOperation = vi.fn((url: string, _method: string) => {
408
- const u = new URL(url);
409
- // Toy router: `/api/checkout/...` → checkout.<segment>
410
- const m = u.pathname.match(/^\/api\/checkout\/(\w+)/);
411
- if (m) return `checkout.${m[1]}`;
412
- return undefined;
413
- });
414
-
415
- const f = createInstrumentedFetch({
416
- name: "vtex",
417
- baseFetch: baseFetch as unknown as typeof fetch,
418
- logging: false,
419
- resolveOperation,
420
- });
421
- await f("https://api.vtex.test/api/checkout/orderForm");
422
-
423
- expect(resolveOperation).toHaveBeenCalledWith(
424
- "https://api.vtex.test/api/checkout/orderForm",
425
- "GET",
426
- );
427
- expect(spans[0].name).toBe("vtex.checkout.orderForm");
428
- });
429
-
430
- it("falls back to the literal '.fetch' suffix when resolveOperation returns undefined", async () => {
431
- const { tracer, spans } = makeFakeTracer();
432
- configureTracer(tracer);
433
- const baseFetch = vi.fn(async () => new Response("ok"));
434
-
435
- const f = createInstrumentedFetch({
436
- name: "vtex",
437
- baseFetch: baseFetch as unknown as typeof fetch,
438
- logging: false,
439
- resolveOperation: () => undefined,
440
- });
441
- await f("https://api.vtex.test/unknown");
442
-
443
- expect(spans[0].name).toBe("vtex.fetch");
444
- expect(spans[0].attrs["fetch.operation"]).toBe("fetch");
445
- });
446
-
447
- it("explicit init.operation wins over defaultOperation AND resolveOperation", async () => {
448
- const { tracer, spans } = makeFakeTracer();
449
- configureTracer(tracer);
450
- const baseFetch = vi.fn(async () => new Response("ok"));
451
-
452
- const f = createInstrumentedFetch({
453
- name: "vtex",
454
- baseFetch: baseFetch as unknown as typeof fetch,
455
- logging: false,
456
- defaultOperation: "from-default",
457
- resolveOperation: () => "from-router",
458
- });
459
- await f("https://api.vtex.test/x", { operation: "from-call" });
460
-
461
- expect(spans[0].name).toBe("vtex.from-call");
462
- });
463
-
464
- it("derives method from a Request input when init.method is omitted", async () => {
465
- // Without this, `fetch(new Request(url, { method: "POST" }))` would
466
- // surface as GET on the span and in the URL-router callback —
467
- // mislabeling POST traffic in dashboards.
468
- const { tracer, spans } = makeFakeTracer();
469
- configureTracer(tracer);
470
- const resolveOperation = vi.fn(
471
- (_url: string, method: string) => `inferred.${method.toLowerCase()}`,
472
- );
473
- const baseFetch = vi.fn(async () => new Response("ok"));
474
-
475
- const f = createInstrumentedFetch({
476
- name: "vtex",
477
- baseFetch: baseFetch as unknown as typeof fetch,
478
- logging: false,
479
- resolveOperation,
480
- });
481
- await f(new Request("https://api.test/x", { method: "POST" }));
482
-
483
- expect(resolveOperation).toHaveBeenCalledWith("https://api.test/x", "POST");
484
- expect(spans[0].attrs["http.method"]).toBe("POST");
485
- expect(spans[0].name).toBe("vtex.inferred.post");
486
- });
487
-
488
- it("init.method overrides the Request's method (Fetch spec)", async () => {
489
- const { tracer, spans } = makeFakeTracer();
490
- configureTracer(tracer);
491
- const baseFetch = vi.fn(async () => new Response("ok"));
492
-
493
- const f = createInstrumentedFetch({
494
- name: "vtex",
495
- baseFetch: baseFetch as unknown as typeof fetch,
496
- logging: false,
497
- });
498
- await f(new Request("https://api.test/x", { method: "POST" }), {
499
- method: "DELETE",
500
- });
501
-
502
- expect(spans[0].attrs["http.method"]).toBe("DELETE");
503
- });
504
-
505
- it("passes the resolved operation to onComplete", async () => {
506
- const baseFetch = vi.fn(async () => new Response("ok"));
507
- const onComplete = vi.fn();
508
- const f = createInstrumentedFetch({
509
- name: "vtex",
510
- baseFetch: baseFetch as unknown as typeof fetch,
511
- logging: false,
512
- defaultOperation: "catalog.fallback",
513
- onComplete,
514
- });
515
- await f("https://api.vtex.test/x", { operation: "intelligent-search.product_search" });
516
- expect(onComplete).toHaveBeenCalledWith(
517
- expect.objectContaining({ operation: "intelligent-search.product_search" }),
518
- );
519
- });
520
-
521
- it("preserves Request headers when init.headers is omitted", async () => {
522
- const fakeSpan = makeFakeSpan("vtex.fetch", undefined, {
523
- traceId: "77777777777777777777777777777777",
524
- spanId: "8888888888888888",
525
- traceFlags: 1,
526
- });
527
- configureTracer({ startSpan: () => fakeSpan.span });
528
- setObservabilitySpanStore({
529
- get: () => fakeSpan.span,
530
- run: (_s, fn) => fn(),
531
- });
532
-
533
- const baseFetch = vi.fn(async (_i: unknown, init?: RequestInit) => {
534
- const h = new Headers(init?.headers);
535
- return new Response(
536
- JSON.stringify({
537
- fromReq: h.get("x-from-req"),
538
- tp: h.get("traceparent"),
539
- }),
540
- { status: 200 },
541
- );
542
- });
543
-
544
- const f = createInstrumentedFetch({
545
- name: "vtex",
546
- baseFetch: baseFetch as unknown as typeof fetch,
547
- logging: false,
548
- });
549
-
550
- const req = new Request("https://api.test/x", {
551
- headers: { "x-from-req": "REQ" },
552
- });
553
- // No init.headers — Request headers must reach the wire.
554
- const res = await f(req);
555
- const body = (await res.json()) as { fromReq: string | null; tp: string | null };
556
- expect(body.fromReq).toBe("REQ");
557
- expect(body.tp).toMatch(/^00-/);
558
- });
559
- });
@@ -1,115 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import { createAppInvoke, invoke } from "./invoke";
3
-
4
- describe("createAppInvoke", () => {
5
- let fetchMock: ReturnType<typeof vi.fn>;
6
-
7
- beforeEach(() => {
8
- fetchMock = vi.fn();
9
- vi.stubGlobal("fetch", fetchMock);
10
- });
11
-
12
- afterEach(() => {
13
- vi.unstubAllGlobals();
14
- });
15
-
16
- it("collects nested property access into slash-separated key", async () => {
17
- fetchMock.mockResolvedValue(new Response(JSON.stringify({ ok: true }), { status: 200 }));
18
-
19
- const proxy = createAppInvoke();
20
- const result = await proxy.vtex.actions.checkout.addItemsToCart({ qty: 1 });
21
-
22
- expect(result).toEqual({ ok: true });
23
- expect(fetchMock).toHaveBeenCalledTimes(1);
24
- const [url, init] = fetchMock.mock.calls[0];
25
- expect(url).toBe("/deco/invoke/vtex/actions/checkout/addItemsToCart");
26
- expect(init.method).toBe("POST");
27
- expect(init.headers).toEqual({ "Content-Type": "application/json" });
28
- expect(JSON.parse(init.body)).toEqual({ qty: 1 });
29
- });
30
-
31
- it("falls back to .ts suffix when first key returns 404", async () => {
32
- fetchMock
33
- .mockResolvedValueOnce(new Response(null, { status: 404 }))
34
- .mockResolvedValueOnce(new Response(JSON.stringify({ via: "ts" }), { status: 200 }));
35
-
36
- const proxy = createAppInvoke();
37
- const result = await proxy.site.loaders.Wishlist({});
38
-
39
- expect(result).toEqual({ via: "ts" });
40
- expect(fetchMock).toHaveBeenCalledTimes(2);
41
- expect(fetchMock.mock.calls[0][0]).toBe("/deco/invoke/site/loaders/Wishlist");
42
- expect(fetchMock.mock.calls[1][0]).toBe("/deco/invoke/site/loaders/Wishlist.ts");
43
- });
44
-
45
- it("throws 'handler not found' when both key and .ts variant 404", async () => {
46
- fetchMock.mockResolvedValue(new Response(null, { status: 404 }));
47
-
48
- const proxy = createAppInvoke();
49
- await expect(proxy.unknown({})).rejects.toThrow(/invoke\(unknown\) failed: handler not found/);
50
- expect(fetchMock).toHaveBeenCalledTimes(2);
51
- });
52
-
53
- it("throws with status and error body on non-OK non-404 response", async () => {
54
- fetchMock.mockResolvedValue(new Response(JSON.stringify({ error: "boom" }), { status: 500 }));
55
-
56
- const proxy = createAppInvoke();
57
- await expect(proxy.broken({})).rejects.toThrow(/invoke\(broken\) failed \(500\): boom/);
58
- });
59
-
60
- it("defaults to empty object body when called with no props", async () => {
61
- fetchMock.mockResolvedValue(new Response(JSON.stringify({}), { status: 200 }));
62
-
63
- const proxy = createAppInvoke();
64
- await proxy.foo.bar();
65
-
66
- const init = fetchMock.mock.calls[0][1];
67
- expect(JSON.parse(init.body)).toEqual({});
68
- });
69
-
70
- it("traps `then`, `catch`, `finally` to avoid being awaited as a thenable", () => {
71
- const proxy = createAppInvoke();
72
- expect(proxy.then).toBeUndefined();
73
- expect(proxy.catch).toBeUndefined();
74
- expect(proxy.finally).toBeUndefined();
75
- // And on a deeper path:
76
- expect(proxy.foo.bar.then).toBeUndefined();
77
- });
78
-
79
- it("returns undefined for symbol property access (no spurious sub-proxy)", () => {
80
- const proxy = createAppInvoke();
81
- expect(proxy[Symbol.toPrimitive]).toBeUndefined();
82
- expect(proxy[Symbol.iterator]).toBeUndefined();
83
- });
84
-
85
- it("respects custom basePath", async () => {
86
- fetchMock.mockResolvedValue(new Response(JSON.stringify({}), { status: 200 }));
87
-
88
- const proxy = createAppInvoke("/custom/invoke");
89
- await proxy.foo.bar({});
90
-
91
- expect(fetchMock.mock.calls[0][0]).toBe("/custom/invoke/foo/bar");
92
- });
93
- });
94
-
95
- describe("default invoke singleton", () => {
96
- let fetchMock: ReturnType<typeof vi.fn>;
97
-
98
- beforeEach(() => {
99
- fetchMock = vi.fn();
100
- vi.stubGlobal("fetch", fetchMock);
101
- });
102
-
103
- afterEach(() => {
104
- vi.unstubAllGlobals();
105
- });
106
-
107
- it("is a usable nested proxy bound to /deco/invoke", async () => {
108
- fetchMock.mockResolvedValue(new Response(JSON.stringify({ ok: true }), { status: 200 }));
109
-
110
- await (invoke as any).site.loaders.example({ x: 1 });
111
-
112
- expect(fetchMock).toHaveBeenCalledTimes(1);
113
- expect(fetchMock.mock.calls[0][0]).toBe("/deco/invoke/site/loaders/example");
114
- });
115
- });