MCPcopy
hub / github.com/sveltejs/kit / command

Function command

packages/kit/src/runtime/client/remote-functions/command.js:16–71  ·  view source on GitHub ↗
(id)

Source from the content-addressed store, hash-verified

14 * @returns {RemoteCommand<any, any>}
15 */
16export function command(id) {
17 // Careful: This function MUST be synchronous (can't use the async keyword) because the return type has to be a promise with an updates() method.
18 // If we make it async, the return type will be a promise that resolves to a promise with an updates() method, which is not what we want.
19 return (arg) => {
20 /** @type {Array<Query<any> | RemoteQueryOverride>} */
21 let updates = [];
22
23 /** @type {Promise<any> & { updates: (...args: any[]) => any }} */
24 const promise = (async () => {
25 // Wait a tick to give room for the `updates` method to be called
26 await Promise.resolve();
27
28 const response = await fetch(`${base}/${app_dir}/remote/${id}`, {
29 method: 'POST',
30 body: JSON.stringify({
31 payload: stringify_remote_arg(arg, app.hooks.transport),
32 refreshes: updates.map((u) => u._key)
33 }),
34 headers: {
35 'Content-Type': 'application/json'
36 }
37 });
38
39 if (!response.ok) {
40 release_overrides(updates);
41 // We only end up here in case of a network error or if the server has an internal error
42 // (which shouldn't happen because we handle errors on the server and always send a 200 response)
43 throw new Error('Failed to execute remote function');
44 }
45
46 const result = /** @type {RemoteFunctionResponse} */ (await response.json());
47 if (result.type === 'redirect') {
48 release_overrides(updates);
49 throw new Error(
50 'Redirects are not allowed in commands. Return a result instead and use goto on the client'
51 );
52 } else if (result.type === 'error') {
53 release_overrides(updates);
54 throw new HttpError(result.status ?? 500, result.error);
55 } else {
56 refresh_queries(result.refreshes, updates);
57
58 return devalue.parse(result.result, app.decoders);
59 }
60 })();
61
62 promise.updates = (/** @type {any} */ ...args) => {
63 updates = args;
64 // @ts-expect-error Don't allow updates to be called multiple times
65 delete promise.updates;
66 return promise;
67 };
68
69 return promise;
70 };
71}

Callers

nothing calls this directly

Calls 4

stringify_remote_argFunction · 0.90
release_overridesFunction · 0.90
refresh_queriesFunction · 0.90
fetchFunction · 0.50

Tested by

no test coverage detected