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

Function create_instance

packages/kit/src/runtime/app/server/remote/form.js:25–121  ·  view source on GitHub ↗

* @param {string | number | boolean} [key]

(key)

Source from the content-addressed store, hash-verified

23 * @param {string | number | boolean} [key]
24 */
25 function create_instance(key) {
26 /** @type {RemoteForm<T>} */
27 const instance = {};
28
29 instance.method = 'POST';
30 instance.onsubmit = () => {};
31
32 Object.defineProperty(instance, 'enhance', {
33 value: () => {
34 return { action: instance.action, method: instance.method, onsubmit: instance.onsubmit };
35 }
36 });
37
38 const button_props = {
39 type: 'submit',
40 onclick: () => {}
41 };
42
43 Object.defineProperty(button_props, 'enhance', {
44 value: () => {
45 return { type: 'submit', formaction: instance.buttonProps.formaction, onclick: () => {} };
46 }
47 });
48
49 Object.defineProperty(instance, 'buttonProps', {
50 value: button_props
51 });
52
53 /** @type {RemoteInfo} */
54 const __ = {
55 type: 'form',
56 name: '',
57 id: '',
58 /** @param {FormData} form_data */
59 fn: async (form_data) => {
60 const event = getRequestEvent();
61 const state = get_event_state(event);
62
63 state.refreshes ??= {};
64
65 const result = await run_remote_function(event, true, form_data, (d) => d, fn);
66
67 // We don't need to care about args or deduplicating calls, because uneval results are only relevant in full page reloads
68 // where only one form submission is active at the same time
69 if (!event.isRemoteRequest) {
70 (state.remote_data ??= {})[__.id] = result;
71 }
72
73 return result;
74 }
75 };
76
77 Object.defineProperty(instance, '__', { value: __ });
78
79 Object.defineProperty(instance, 'action', {
80 get: () => `?/remote=${__.id}`,
81 enumerable: true
82 });

Callers 1

formFunction · 0.70

Calls 4

getRequestEventFunction · 0.90
get_event_stateFunction · 0.90
run_remote_functionFunction · 0.90
setMethod · 0.80

Tested by

no test coverage detected