@decocms/blocks 8.0.0 → 8.1.0-next.1
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/bin/deco.js +13 -0
- package/package.json +32 -82
- package/src/index.ts +35 -8
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +133 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/applySectionConventions.ts +0 -124
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/blockSource.ts +0 -191
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/client.ts +0 -80
- package/src/cms/draftSource.test.ts +0 -343
- package/src/cms/draftSource.ts +0 -362
- package/src/cms/index.ts +0 -151
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loadDecofileDirectory.ts +0 -89
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/loader.ts +0 -304
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/registry.ts +0 -242
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/resolve.ts +0 -2282
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/schema.ts +0 -1215
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaderContext.ts +0 -113
- package/src/cms/sectionLoaders.test.ts +0 -533
- package/src/cms/sectionLoaders.ts +0 -636
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/sectionMixins.ts +0 -153
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/audience.ts +0 -56
- package/src/flags/everyone.ts +0 -29
- package/src/flags/flag.ts +0 -15
- package/src/flags/flags.test.ts +0 -148
- package/src/flags/multivariate/image.ts +0 -11
- package/src/flags/multivariate/message.ts +0 -11
- package/src/flags/multivariate/page.ts +0 -16
- package/src/flags/multivariate/section.ts +0 -16
- package/src/flags/multivariate.ts +0 -1
- package/src/flags/types.ts +0 -59
- package/src/flags/utils/multivariate.ts +0 -20
- package/src/hooks/Image.test.ts +0 -100
- package/src/hooks/Image.tsx +0 -250
- package/src/hooks/JsonLd.tsx +0 -353
- package/src/hooks/LazySection.tsx +0 -121
- package/src/hooks/LiveControls.tsx +0 -122
- package/src/hooks/Picture.tsx +0 -127
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/RenderSection.tsx +0 -91
- package/src/hooks/SectionErrorFallback.tsx +0 -97
- package/src/hooks/index.ts +0 -30
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/builtins.ts +0 -459
- package/src/matchers/countryNames.ts +0 -104
- package/src/matchers/override.test.ts +0 -205
- package/src/matchers/override.ts +0 -136
- package/src/matchers/posthog.ts +0 -154
- package/src/middleware/decoState.ts +0 -55
- package/src/middleware/healthMetrics.ts +0 -133
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/hydrationContext.ts +0 -79
- package/src/middleware/index.ts +0 -88
- package/src/middleware/liveness.ts +0 -21
- package/src/middleware/observability.test.ts +0 -242
- package/src/middleware/observability.ts +0 -643
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/middleware/validateSection.ts +0 -100
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/abTesting.ts +0 -499
- package/src/sdk/analytics.ts +0 -77
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cacheHeaders.ts +0 -433
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoader.ts +0 -503
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/clx.ts +0 -5
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/cn.ts +0 -28
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/composite.ts +0 -114
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/cookie.ts +0 -129
- package/src/sdk/crypto.ts +0 -185
- package/src/sdk/csp.ts +0 -59
- package/src/sdk/detectDevice.ts +0 -45
- package/src/sdk/djb2.ts +0 -20
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/encoding.ts +0 -47
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/env.ts +0 -54
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/flags.ts +0 -108
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/http.ts +0 -124
- package/src/sdk/index.ts +0 -90
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/inflightTimeout.ts +0 -53
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/instrumentedFetch.ts +0 -339
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/invoke.ts +0 -298
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/logger.ts +0 -304
- package/src/sdk/mergeCacheControl.ts +0 -150
- package/src/sdk/normalizeUrls.ts +0 -91
- package/src/sdk/observability.ts +0 -109
- package/src/sdk/otel.test.ts +0 -526
- package/src/sdk/otel.ts +0 -1012
- package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelAdapters.ts +0 -144
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpLog.ts +0 -419
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpMeter.ts +0 -506
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/otelHttpTracer.ts +0 -543
- package/src/sdk/redirects.ts +0 -228
- package/src/sdk/requestContext.ts +0 -281
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/requestContextStorage.browser.ts +0 -43
- package/src/sdk/requestContextStorage.ts +0 -35
- package/src/sdk/retry.ts +0 -45
- package/src/sdk/serverTimings.ts +0 -68
- package/src/sdk/signal.ts +0 -42
- package/src/sdk/sitemap.ts +0 -160
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlRedaction.ts +0 -82
- package/src/sdk/urlUtils.ts +0 -134
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useDevice.ts +0 -89
- package/src/sdk/useDeviceContext.tsx +0 -108
- package/src/sdk/useId.ts +0 -7
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useScript.ts +0 -210
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/src/sdk/useSuggestions.ts +0 -188
- package/src/sdk/wrapCaughtErrors.ts +0 -107
- package/src/setup.ts +0 -120
- package/src/types/index.ts +0 -53
- package/src/types/widgets.ts +0 -14
- package/tsconfig.json +0 -7
package/src/sdk/invoke.ts
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typed invoke proxies for client-side RPC to deco loaders/actions.
|
|
3
|
-
*
|
|
4
|
-
* Two flavors:
|
|
5
|
-
*
|
|
6
|
-
* 1. `createInvokeProxy<T>()` — flat keys (e.g. `invoke["vtex/loaders/productList.ts"](props)`)
|
|
7
|
-
* 2. `createAppInvoke<T>()` — nested keys (e.g. `invoke.vtex.actions.checkout.addItemsToCart(props)`)
|
|
8
|
-
*
|
|
9
|
-
* Both POST to `/deco/invoke/{key}` under the hood.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* // Flat proxy (legacy):
|
|
14
|
-
* const invoke = createInvokeProxy<Loaders>();
|
|
15
|
-
* await invoke["vtex/loaders/productList.ts"]({ query: "shoes" });
|
|
16
|
-
*
|
|
17
|
-
* // Nested proxy (recommended):
|
|
18
|
-
* const invoke = createAppInvoke();
|
|
19
|
-
* await invoke.vtex.actions.checkout.addItemsToCart({ orderFormId, orderItems });
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export type InvokeProxy<TLoaders extends Record<string, (props: any) => Promise<any>>> = {
|
|
24
|
-
[K in keyof TLoaders]: TLoaders[K] extends (props: infer P) => Promise<infer R>
|
|
25
|
-
? (props: P) => Promise<R>
|
|
26
|
-
: never;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Creates a proxy that turns loader key access into fetch calls to `/deco/invoke/:key`.
|
|
31
|
-
*/
|
|
32
|
-
export function createInvokeProxy<TLoaders extends Record<string, (props: any) => Promise<any>>>(
|
|
33
|
-
basePath = "/deco/invoke",
|
|
34
|
-
): InvokeProxy<TLoaders> {
|
|
35
|
-
return new Proxy({} as InvokeProxy<TLoaders>, {
|
|
36
|
-
get(_target, prop: string) {
|
|
37
|
-
return async (props: unknown) => {
|
|
38
|
-
const url = `${basePath}/${encodeURIComponent(prop)}`;
|
|
39
|
-
const response = await fetch(url, {
|
|
40
|
-
method: "POST",
|
|
41
|
-
headers: { "Content-Type": "application/json" },
|
|
42
|
-
body: JSON.stringify(props ?? {}),
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
if (!response.ok) {
|
|
46
|
-
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
47
|
-
throw new Error(
|
|
48
|
-
`Invoke ${prop} failed (${response.status}): ${(error as any).error || response.statusText}`,
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return response.json();
|
|
53
|
-
};
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Batch invoke multiple loaders in a single request.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```ts
|
|
63
|
-
* const results = await batchInvoke("/deco/invoke", {
|
|
64
|
-
* products: { __resolveType: "vtex/loaders/productList.ts", query: "shoes" },
|
|
65
|
-
* details: { __resolveType: "vtex/loaders/productDetailsPage.ts", slug: "shoe-1" },
|
|
66
|
-
* });
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
export async function batchInvoke<T extends Record<string, unknown>>(
|
|
70
|
-
basePath: string,
|
|
71
|
-
payloads: T,
|
|
72
|
-
): Promise<{ [K in keyof T]: unknown }> {
|
|
73
|
-
const response = await fetch(basePath, {
|
|
74
|
-
method: "POST",
|
|
75
|
-
headers: { "Content-Type": "application/json" },
|
|
76
|
-
body: JSON.stringify(payloads),
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
if (!response.ok) {
|
|
80
|
-
throw new Error(`Batch invoke failed (${response.status})`);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return response.json();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Helper to create TanStack Query `queryOptions` for an invoke call.
|
|
88
|
-
* The storefront must have `@tanstack/react-query` installed.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* import { useQuery } from "@tanstack/react-query";
|
|
93
|
-
* import { invokeQueryOptions } from "@decocms/start/sdk/invoke";
|
|
94
|
-
*
|
|
95
|
-
* const options = invokeQueryOptions(
|
|
96
|
-
* "vtex/loaders/productList.ts",
|
|
97
|
-
* { query: "shoes" },
|
|
98
|
-
* { staleTime: 60_000 }
|
|
99
|
-
* );
|
|
100
|
-
*
|
|
101
|
-
* // In a component:
|
|
102
|
-
* const { data } = useQuery(options);
|
|
103
|
-
*
|
|
104
|
-
* // In a route loader:
|
|
105
|
-
* queryClient.ensureQueryData(options);
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
export function invokeQueryOptions<TResult = unknown>(
|
|
109
|
-
key: string,
|
|
110
|
-
props: unknown,
|
|
111
|
-
options?: { staleTime?: number; gcTime?: number; basePath?: string },
|
|
112
|
-
) {
|
|
113
|
-
const basePath = options?.basePath ?? "/deco/invoke";
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
queryKey: ["deco-invoke", key, props] as const,
|
|
117
|
-
queryFn: async (): Promise<TResult> => {
|
|
118
|
-
const url = `${basePath}/${encodeURIComponent(key)}`;
|
|
119
|
-
const response = await fetch(url, {
|
|
120
|
-
method: "POST",
|
|
121
|
-
headers: { "Content-Type": "application/json" },
|
|
122
|
-
body: JSON.stringify(props ?? {}),
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
if (!response.ok) {
|
|
126
|
-
throw new Error(`Invoke ${key} failed (${response.status})`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return response.json();
|
|
130
|
-
},
|
|
131
|
-
staleTime: options?.staleTime,
|
|
132
|
-
gcTime: options?.gcTime,
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// ---------------------------------------------------------------------------
|
|
137
|
-
// Nested invoke proxy — createAppInvoke
|
|
138
|
-
// ---------------------------------------------------------------------------
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Converts flat slash-separated keys into a nested object type.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```ts
|
|
145
|
-
* type Map = {
|
|
146
|
-
* "vtex/actions/checkout/addItemsToCart": (props: CartInput) => Promise<OrderForm>;
|
|
147
|
-
* "vtex/loaders/catalog/getProduct": (props: { slug: string }) => Promise<Product>;
|
|
148
|
-
* };
|
|
149
|
-
* type Nested = NestedFromFlat<Map>;
|
|
150
|
-
* // { vtex: { actions: { checkout: { addItemsToCart: (props: CartInput) => Promise<OrderForm> } } } }
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
type SplitFirst<S extends string> = S extends `${infer Head}/${infer Tail}`
|
|
154
|
-
? [Head, Tail]
|
|
155
|
-
: [S, never];
|
|
156
|
-
|
|
157
|
-
type BuildNested<Key extends string, Value> =
|
|
158
|
-
SplitFirst<Key> extends [infer H extends string, infer T]
|
|
159
|
-
? T extends string
|
|
160
|
-
? { [K in H]: BuildNested<T, Value> }
|
|
161
|
-
: { [K in H]: Value }
|
|
162
|
-
: never;
|
|
163
|
-
|
|
164
|
-
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
|
|
165
|
-
? I
|
|
166
|
-
: never;
|
|
167
|
-
|
|
168
|
-
type DeepMerge<T> = T extends object ? { [K in keyof T]: DeepMerge<T[K]> } : T;
|
|
169
|
-
|
|
170
|
-
export type NestedFromFlat<T extends Record<string, any>> = DeepMerge<
|
|
171
|
-
UnionToIntersection<{ [K in keyof T & string]: BuildNested<K, T[K]> }[keyof T & string]>
|
|
172
|
-
>;
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Creates a typed nested invoke proxy.
|
|
176
|
-
*
|
|
177
|
-
* Each property access accumulates path segments. When called as a function,
|
|
178
|
-
* the segments are joined with "/" and POSTed to `/deco/invoke/{key}`.
|
|
179
|
-
* If the primary key returns 404, a `.ts` suffix variant is tried.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```ts
|
|
183
|
-
* import { createAppInvoke } from "@decocms/start/sdk/invoke";
|
|
184
|
-
*
|
|
185
|
-
* // Untyped (any):
|
|
186
|
-
* const invoke = createAppInvoke();
|
|
187
|
-
* await invoke.vtex.actions.checkout.addItemsToCart({ orderFormId, orderItems });
|
|
188
|
-
*
|
|
189
|
-
* // Typed (with handler map):
|
|
190
|
-
* type Handlers = {
|
|
191
|
-
* "vtex/actions/checkout/addItemsToCart": (props: CartInput) => Promise<OrderForm>;
|
|
192
|
-
* };
|
|
193
|
-
* const invoke = createAppInvoke<Handlers>();
|
|
194
|
-
* await invoke.vtex.actions.checkout.addItemsToCart({ orderFormId, orderItems });
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
export function createAppInvoke(basePath?: string): any;
|
|
198
|
-
export function createAppInvoke<T extends Record<string, any>>(
|
|
199
|
-
basePath?: string,
|
|
200
|
-
): NestedFromFlat<T>;
|
|
201
|
-
export function createAppInvoke(basePath = "/deco/invoke"): any {
|
|
202
|
-
return createAppInvokeWith({ basePath });
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Default nested invoke proxy bound to `/deco/invoke`.
|
|
207
|
-
*
|
|
208
|
-
* Replaces site-level `~/runtime.ts` shims that wrap the same `createAppInvoke`
|
|
209
|
-
* call. Importing this singleton means no per-site boilerplate.
|
|
210
|
-
*
|
|
211
|
-
* @example
|
|
212
|
-
* ```ts
|
|
213
|
-
* import { invoke } from "@decocms/start/sdk/invoke";
|
|
214
|
-
*
|
|
215
|
-
* await invoke.vtex.actions.checkout.addItemsToCart({ orderFormId, orderItems });
|
|
216
|
-
* await invoke.site.loaders.Wishlist.getWishlist({});
|
|
217
|
-
* ```
|
|
218
|
-
*
|
|
219
|
-
* For a custom `basePath` or typed handlers, call `createAppInvoke()` directly:
|
|
220
|
-
* ```ts
|
|
221
|
-
* const invoke = createAppInvoke<Handlers>("/my/invoke");
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
|
-
export const invoke = createAppInvoke();
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* A `fetch`-compatible function. Lets callers route the invoke proxy's HTTP
|
|
228
|
-
* calls through a custom transport — e.g. server-side, an absolute-origin
|
|
229
|
-
* self-fetch that injects the request's AbortSignal (`RequestContext.fetch`).
|
|
230
|
-
*/
|
|
231
|
-
export type InvokeFetcher = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Like {@link createAppInvoke}, but with an injectable `fetcher`.
|
|
235
|
-
*
|
|
236
|
-
* The default `createAppInvoke` targets a relative path (`/deco/invoke`) via
|
|
237
|
-
* the global `fetch`, which only works in the browser. Server-side callers
|
|
238
|
-
* (e.g. a section loader's `ctx.invoke`) need an **absolute** base URL plus a
|
|
239
|
-
* fetcher that forwards the request's AbortSignal. This factory keeps the exact
|
|
240
|
-
* same nested-proxy semantics and 404→`.ts` retry, only swapping the transport.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* ```ts
|
|
244
|
-
* const origin = new URL(req.url).origin;
|
|
245
|
-
* const invoke = createAppInvokeWith({
|
|
246
|
-
* basePath: `${origin}/deco/invoke`,
|
|
247
|
-
* fetcher: (input, init) => RequestContext.fetch(input, init),
|
|
248
|
-
* });
|
|
249
|
-
* await invoke.vtex.loaders.product.detailsPageGQL({ slug });
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
|
-
export function createAppInvokeWith(options?: { basePath?: string; fetcher?: InvokeFetcher }): any;
|
|
253
|
-
export function createAppInvokeWith<T extends Record<string, any>>(options?: {
|
|
254
|
-
basePath?: string;
|
|
255
|
-
fetcher?: InvokeFetcher;
|
|
256
|
-
}): NestedFromFlat<T>;
|
|
257
|
-
export function createAppInvokeWith(options?: { basePath?: string; fetcher?: InvokeFetcher }): any {
|
|
258
|
-
const basePath = options?.basePath ?? "/deco/invoke";
|
|
259
|
-
const fetcher: InvokeFetcher = options?.fetcher ?? ((input, init) => fetch(input, init));
|
|
260
|
-
|
|
261
|
-
function buildProxy(path: string[]): any {
|
|
262
|
-
return new Proxy(
|
|
263
|
-
Object.assign(async (props: any) => {
|
|
264
|
-
const key = path.join("/");
|
|
265
|
-
for (const k of [key, `${key}.ts`]) {
|
|
266
|
-
const response = await fetcher(`${basePath}/${k}`, {
|
|
267
|
-
method: "POST",
|
|
268
|
-
headers: { "Content-Type": "application/json" },
|
|
269
|
-
body: JSON.stringify(props ?? {}),
|
|
270
|
-
});
|
|
271
|
-
if (response.status === 404) {
|
|
272
|
-
await response.body?.cancel();
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
if (!response.ok) {
|
|
276
|
-
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
277
|
-
throw new Error(
|
|
278
|
-
`invoke(${k}) failed (${response.status}): ${(error as any).error || response.statusText}`,
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
return response.json();
|
|
282
|
-
}
|
|
283
|
-
throw new Error(`invoke(${key}) failed: handler not found`);
|
|
284
|
-
}, {}),
|
|
285
|
-
{
|
|
286
|
-
get(_target: any, prop: string | symbol) {
|
|
287
|
-
if (typeof prop === "symbol") return undefined;
|
|
288
|
-
if (prop === "then" || prop === "catch" || prop === "finally") {
|
|
289
|
-
return undefined;
|
|
290
|
-
}
|
|
291
|
-
return buildProxy([...path, prop]);
|
|
292
|
-
},
|
|
293
|
-
},
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return buildProxy([]);
|
|
298
|
-
}
|