@wix/headless-forms 0.0.13 → 0.0.14
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.
|
@@ -60,6 +60,8 @@ type OnSubmit = (formId: string, formValues: FormValues) => Promise<SubmitRespon
|
|
|
60
60
|
export type FormServiceConfig = {
|
|
61
61
|
formId: string;
|
|
62
62
|
onSubmit?: OnSubmit;
|
|
63
|
+
namespace?: string;
|
|
64
|
+
additionalMetadata?: Record<string, string | string[]>;
|
|
63
65
|
} | {
|
|
64
66
|
form: forms.Form;
|
|
65
67
|
onSubmit?: OnSubmit;
|
|
@@ -100,6 +102,8 @@ export declare const FormService: import("@wix/services-definitions").ServiceFac
|
|
|
100
102
|
* a specific form by ID that will be used to configure the FormService.
|
|
101
103
|
*
|
|
102
104
|
* @param {string} formId - The unique identifier of the form to load
|
|
105
|
+
* @param {string} [namespace] - Optional namespace for the form
|
|
106
|
+
* @param {Record<string, string | string[]>} [additionalMetadata] - Optional additional metadata to pass to the API
|
|
103
107
|
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
104
108
|
* @throws {Error} When the form cannot be loaded
|
|
105
109
|
*
|
|
@@ -110,5 +114,5 @@ export declare const FormService: import("@wix/services-definitions").ServiceFac
|
|
|
110
114
|
* const formService = FormService.withConfig(formConfig);
|
|
111
115
|
* ```
|
|
112
116
|
*/
|
|
113
|
-
export declare function loadFormServiceConfig(formId: string): Promise<FormServiceConfig>;
|
|
117
|
+
export declare function loadFormServiceConfig(formId: string, namespace?: string, additionalMetadata?: Record<string, string | string[]>): Promise<FormServiceConfig>;
|
|
114
118
|
export {};
|
|
@@ -5,6 +5,7 @@ exports.loadFormServiceConfig = loadFormServiceConfig;
|
|
|
5
5
|
const forms_1 = require("@wix/forms");
|
|
6
6
|
const services_definitions_1 = require("@wix/services-definitions");
|
|
7
7
|
const signals_1 = require("@wix/services-definitions/core-services/signals");
|
|
8
|
+
const essentials_1 = require("@wix/essentials");
|
|
8
9
|
/**
|
|
9
10
|
* Service definition for the Form service.
|
|
10
11
|
* This defines the contract that the FormService must implement.
|
|
@@ -47,13 +48,13 @@ exports.FormService = services_definitions_1.implementService.withConfig()(expor
|
|
|
47
48
|
const hasSchema = 'form' in config;
|
|
48
49
|
const formSignal = signalsService.signal(hasSchema ? config.form : null);
|
|
49
50
|
if (!hasSchema) {
|
|
50
|
-
loadForm(config.formId);
|
|
51
|
+
loadForm(config.formId, config.namespace, config.additionalMetadata);
|
|
51
52
|
}
|
|
52
|
-
async function loadForm(id) {
|
|
53
|
+
async function loadForm(id, namespace, additionalMetadata) {
|
|
53
54
|
isLoadingSignal.set(true);
|
|
54
55
|
errorSignal.set(null);
|
|
55
56
|
try {
|
|
56
|
-
const result = await fetchForm(id);
|
|
57
|
+
const result = await fetchForm({ id, namespace, additionalMetadata });
|
|
57
58
|
if (result) {
|
|
58
59
|
formSignal.set(result);
|
|
59
60
|
}
|
|
@@ -117,13 +118,41 @@ exports.FormService = services_definitions_1.implementService.withConfig()(expor
|
|
|
117
118
|
submitForm: submitForm,
|
|
118
119
|
};
|
|
119
120
|
});
|
|
120
|
-
|
|
121
|
+
function buildFormFetchQueryParams({ id, namespace, additionalMetadata, }) {
|
|
122
|
+
const params = new URLSearchParams();
|
|
123
|
+
params.append('formId', id);
|
|
124
|
+
if (namespace) {
|
|
125
|
+
params.append('namespace', namespace);
|
|
126
|
+
}
|
|
127
|
+
if (additionalMetadata) {
|
|
128
|
+
Object.entries(additionalMetadata).forEach(([key, value]) => {
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
value.forEach((v) => params.append(key, v));
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
params.append(key, value);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return params;
|
|
138
|
+
}
|
|
139
|
+
async function fetchForm({ id, namespace, additionalMetadata, }) {
|
|
121
140
|
try {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
141
|
+
const params = buildFormFetchQueryParams({
|
|
142
|
+
id,
|
|
143
|
+
namespace,
|
|
144
|
+
additionalMetadata,
|
|
145
|
+
});
|
|
146
|
+
const url = `https://edge.wixapis.com/form-schema-service/v4/forms/${id}?${params.toString()}`;
|
|
147
|
+
const response = await essentials_1.httpClient.fetchWithAuth(url);
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
throw new Error(`Failed to fetch form: ${response.status} ${response.statusText}`);
|
|
150
|
+
}
|
|
151
|
+
const data = await response.json();
|
|
152
|
+
if (data && data.form) {
|
|
153
|
+
return data.form;
|
|
125
154
|
}
|
|
126
|
-
|
|
155
|
+
throw new Error('Invalid response format from Forms API');
|
|
127
156
|
}
|
|
128
157
|
catch (err) {
|
|
129
158
|
console.error('Failed to load form:', id, err);
|
|
@@ -136,6 +165,8 @@ async function fetchForm(id) {
|
|
|
136
165
|
* a specific form by ID that will be used to configure the FormService.
|
|
137
166
|
*
|
|
138
167
|
* @param {string} formId - The unique identifier of the form to load
|
|
168
|
+
* @param {string} [namespace] - Optional namespace for the form
|
|
169
|
+
* @param {Record<string, string | string[]>} [additionalMetadata] - Optional additional metadata to pass to the API
|
|
139
170
|
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
140
171
|
* @throws {Error} When the form cannot be loaded
|
|
141
172
|
*
|
|
@@ -146,7 +177,7 @@ async function fetchForm(id) {
|
|
|
146
177
|
* const formService = FormService.withConfig(formConfig);
|
|
147
178
|
* ```
|
|
148
179
|
*/
|
|
149
|
-
async function loadFormServiceConfig(formId) {
|
|
150
|
-
const form = await fetchForm(formId);
|
|
180
|
+
async function loadFormServiceConfig(formId, namespace, additionalMetadata) {
|
|
181
|
+
const form = await fetchForm({ id: formId, namespace, additionalMetadata });
|
|
151
182
|
return { form };
|
|
152
183
|
}
|
|
@@ -60,6 +60,8 @@ type OnSubmit = (formId: string, formValues: FormValues) => Promise<SubmitRespon
|
|
|
60
60
|
export type FormServiceConfig = {
|
|
61
61
|
formId: string;
|
|
62
62
|
onSubmit?: OnSubmit;
|
|
63
|
+
namespace?: string;
|
|
64
|
+
additionalMetadata?: Record<string, string | string[]>;
|
|
63
65
|
} | {
|
|
64
66
|
form: forms.Form;
|
|
65
67
|
onSubmit?: OnSubmit;
|
|
@@ -100,6 +102,8 @@ export declare const FormService: import("@wix/services-definitions").ServiceFac
|
|
|
100
102
|
* a specific form by ID that will be used to configure the FormService.
|
|
101
103
|
*
|
|
102
104
|
* @param {string} formId - The unique identifier of the form to load
|
|
105
|
+
* @param {string} [namespace] - Optional namespace for the form
|
|
106
|
+
* @param {Record<string, string | string[]>} [additionalMetadata] - Optional additional metadata to pass to the API
|
|
103
107
|
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
104
108
|
* @throws {Error} When the form cannot be loaded
|
|
105
109
|
*
|
|
@@ -110,5 +114,5 @@ export declare const FormService: import("@wix/services-definitions").ServiceFac
|
|
|
110
114
|
* const formService = FormService.withConfig(formConfig);
|
|
111
115
|
* ```
|
|
112
116
|
*/
|
|
113
|
-
export declare function loadFormServiceConfig(formId: string): Promise<FormServiceConfig>;
|
|
117
|
+
export declare function loadFormServiceConfig(formId: string, namespace?: string, additionalMetadata?: Record<string, string | string[]>): Promise<FormServiceConfig>;
|
|
114
118
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { submissions } from '@wix/forms';
|
|
2
2
|
import { defineService, implementService } from '@wix/services-definitions';
|
|
3
3
|
import { SignalsServiceDefinition, } from '@wix/services-definitions/core-services/signals';
|
|
4
|
+
import { httpClient } from '@wix/essentials';
|
|
4
5
|
/**
|
|
5
6
|
* Service definition for the Form service.
|
|
6
7
|
* This defines the contract that the FormService must implement.
|
|
@@ -43,13 +44,13 @@ export const FormService = implementService.withConfig()(FormServiceDefinition,
|
|
|
43
44
|
const hasSchema = 'form' in config;
|
|
44
45
|
const formSignal = signalsService.signal(hasSchema ? config.form : null);
|
|
45
46
|
if (!hasSchema) {
|
|
46
|
-
loadForm(config.formId);
|
|
47
|
+
loadForm(config.formId, config.namespace, config.additionalMetadata);
|
|
47
48
|
}
|
|
48
|
-
async function loadForm(id) {
|
|
49
|
+
async function loadForm(id, namespace, additionalMetadata) {
|
|
49
50
|
isLoadingSignal.set(true);
|
|
50
51
|
errorSignal.set(null);
|
|
51
52
|
try {
|
|
52
|
-
const result = await fetchForm(id);
|
|
53
|
+
const result = await fetchForm({ id, namespace, additionalMetadata });
|
|
53
54
|
if (result) {
|
|
54
55
|
formSignal.set(result);
|
|
55
56
|
}
|
|
@@ -113,13 +114,41 @@ export const FormService = implementService.withConfig()(FormServiceDefinition,
|
|
|
113
114
|
submitForm: submitForm,
|
|
114
115
|
};
|
|
115
116
|
});
|
|
116
|
-
|
|
117
|
+
function buildFormFetchQueryParams({ id, namespace, additionalMetadata, }) {
|
|
118
|
+
const params = new URLSearchParams();
|
|
119
|
+
params.append('formId', id);
|
|
120
|
+
if (namespace) {
|
|
121
|
+
params.append('namespace', namespace);
|
|
122
|
+
}
|
|
123
|
+
if (additionalMetadata) {
|
|
124
|
+
Object.entries(additionalMetadata).forEach(([key, value]) => {
|
|
125
|
+
if (Array.isArray(value)) {
|
|
126
|
+
value.forEach((v) => params.append(key, v));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
params.append(key, value);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return params;
|
|
134
|
+
}
|
|
135
|
+
async function fetchForm({ id, namespace, additionalMetadata, }) {
|
|
117
136
|
try {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
137
|
+
const params = buildFormFetchQueryParams({
|
|
138
|
+
id,
|
|
139
|
+
namespace,
|
|
140
|
+
additionalMetadata,
|
|
141
|
+
});
|
|
142
|
+
const url = `https://edge.wixapis.com/form-schema-service/v4/forms/${id}?${params.toString()}`;
|
|
143
|
+
const response = await httpClient.fetchWithAuth(url);
|
|
144
|
+
if (!response.ok) {
|
|
145
|
+
throw new Error(`Failed to fetch form: ${response.status} ${response.statusText}`);
|
|
146
|
+
}
|
|
147
|
+
const data = await response.json();
|
|
148
|
+
if (data && data.form) {
|
|
149
|
+
return data.form;
|
|
121
150
|
}
|
|
122
|
-
|
|
151
|
+
throw new Error('Invalid response format from Forms API');
|
|
123
152
|
}
|
|
124
153
|
catch (err) {
|
|
125
154
|
console.error('Failed to load form:', id, err);
|
|
@@ -132,6 +161,8 @@ async function fetchForm(id) {
|
|
|
132
161
|
* a specific form by ID that will be used to configure the FormService.
|
|
133
162
|
*
|
|
134
163
|
* @param {string} formId - The unique identifier of the form to load
|
|
164
|
+
* @param {string} [namespace] - Optional namespace for the form
|
|
165
|
+
* @param {Record<string, string | string[]>} [additionalMetadata] - Optional additional metadata to pass to the API
|
|
135
166
|
* @returns {Promise<FormServiceConfig>} Configuration object with pre-loaded form data
|
|
136
167
|
* @throws {Error} When the form cannot be loaded
|
|
137
168
|
*
|
|
@@ -142,7 +173,7 @@ async function fetchForm(id) {
|
|
|
142
173
|
* const formService = FormService.withConfig(formConfig);
|
|
143
174
|
* ```
|
|
144
175
|
*/
|
|
145
|
-
export async function loadFormServiceConfig(formId) {
|
|
146
|
-
const form = await fetchForm(formId);
|
|
176
|
+
export async function loadFormServiceConfig(formId, namespace, additionalMetadata) {
|
|
177
|
+
const form = await fetchForm({ id: formId, namespace, additionalMetadata });
|
|
147
178
|
return { form };
|
|
148
179
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/headless-forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"groupId": "com.wixpress.headless-components"
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
|
-
"falconPackageHash": "
|
|
61
|
+
"falconPackageHash": "d647857993c0f94b27c514b7f73c6118a7ae3c6ee5ee21c998802545"
|
|
62
62
|
}
|