react-luna-form 0.0.23 → 0.0.24
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/dist/client/cjs/index.js +2261 -1
- package/dist/client/esm/index.js +2228 -1
- package/dist/config/cjs/index.js +229 -1
- package/dist/config/esm/index.js +206 -1
- package/dist/server/cjs/index.js +1085 -1
- package/dist/server/esm/index.js +1062 -1
- package/package.json +2 -2
package/dist/config/cjs/index.js
CHANGED
|
@@ -1 +1,229 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/config/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
defineCheckbox: () => defineCheckbox,
|
|
24
|
+
defineConfig: () => defineConfig,
|
|
25
|
+
defineCustomInput: () => defineCustomInput,
|
|
26
|
+
defineInput: () => defineInput,
|
|
27
|
+
defineRadio: () => defineRadio,
|
|
28
|
+
defineSelect: () => defineSelect,
|
|
29
|
+
defineTextArea: () => defineTextArea
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// ../luna-core/src/util/constant.ts
|
|
34
|
+
var INPUT = "input";
|
|
35
|
+
var INPUT_EMAIL = "input/email";
|
|
36
|
+
var INPUT_NUMBER = "input/number";
|
|
37
|
+
var INPUT_PASSWORD = "input/password";
|
|
38
|
+
var INPUT_TEL = "input/tel";
|
|
39
|
+
var INPUT_TEXT = "input/text";
|
|
40
|
+
var INPUTS = [
|
|
41
|
+
INPUT,
|
|
42
|
+
INPUT_EMAIL,
|
|
43
|
+
INPUT_NUMBER,
|
|
44
|
+
INPUT_PASSWORD,
|
|
45
|
+
INPUT_TEL,
|
|
46
|
+
INPUT_TEXT
|
|
47
|
+
];
|
|
48
|
+
var TEXTAREA = "textarea";
|
|
49
|
+
var RADIO = "radio";
|
|
50
|
+
var CHECKBOX = "checkbox";
|
|
51
|
+
var SELECT = "select";
|
|
52
|
+
var SELECT_MONTH = "select/month";
|
|
53
|
+
var SELECT_YEAR = "select/year";
|
|
54
|
+
var SELECTS = [SELECT, SELECT_MONTH, SELECT_YEAR];
|
|
55
|
+
var COMMON_URL = "http://luna.internal";
|
|
56
|
+
|
|
57
|
+
// ../luna-core/src/util/is-type.ts
|
|
58
|
+
function isObject(value) {
|
|
59
|
+
return value !== null && Object.prototype.toString.call(value) === "[object Object]";
|
|
60
|
+
}
|
|
61
|
+
function isString(value) {
|
|
62
|
+
return typeof value === "string";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../luna-core/src/util/string.ts
|
|
66
|
+
function isInterpolated(template) {
|
|
67
|
+
if (isString(template)) {
|
|
68
|
+
return /{([^}]+)}/.test(template);
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(template)) {
|
|
71
|
+
return template.some((item) => isInterpolated(item));
|
|
72
|
+
}
|
|
73
|
+
if (isObject(template)) {
|
|
74
|
+
return Object.values(template).some((value) => isInterpolated(value));
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ../luna-core/src/util/logger.ts
|
|
80
|
+
var logger = {
|
|
81
|
+
error: (...args) => {
|
|
82
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
83
|
+
getConsole().error("[Luna Form]", ...args);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
warn: (...args) => {
|
|
87
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
88
|
+
getConsole().warn("[Luna Form]", ...args);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
info: (...args) => {
|
|
92
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
93
|
+
getConsole().info("[Luna Form]", ...args);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var isConsoleAvailable = () => typeof getConsole() !== "undefined";
|
|
98
|
+
var isProduction = () => false;
|
|
99
|
+
var getConsole = () => globalThis.console;
|
|
100
|
+
|
|
101
|
+
// ../luna-core/src/util/stringify.ts
|
|
102
|
+
function stringify(body) {
|
|
103
|
+
try {
|
|
104
|
+
if (body instanceof FormData) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
if (isString(body)) {
|
|
108
|
+
return body;
|
|
109
|
+
}
|
|
110
|
+
return JSON.stringify(body);
|
|
111
|
+
} catch {
|
|
112
|
+
logger.error("Failed to stringify body:", body);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ../luna-core/src/fetcher.ts
|
|
118
|
+
var REGEX_INVALID_URL_SEGMENTS = /(^|[\/?=&])(null|undefined)([\/?=&]|$)/;
|
|
119
|
+
async function fetcher(dataSource) {
|
|
120
|
+
const [url, method] = buildRequest(dataSource);
|
|
121
|
+
let body = dataSource.body;
|
|
122
|
+
let headers = buildHeaders(dataSource);
|
|
123
|
+
if (body && !isGetMethod(method)) {
|
|
124
|
+
const bodyStringify = stringify(body);
|
|
125
|
+
if (bodyStringify !== null) {
|
|
126
|
+
body = bodyStringify;
|
|
127
|
+
headers = asJson(headers);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const target = url.origin === COMMON_URL ? url.toString().replace(COMMON_URL, "") : url.toString();
|
|
131
|
+
const request = await fetch(target, {
|
|
132
|
+
body: buildBody(method, body),
|
|
133
|
+
cache: dataSource.cache,
|
|
134
|
+
headers,
|
|
135
|
+
method
|
|
136
|
+
});
|
|
137
|
+
const response = await request.json();
|
|
138
|
+
if (request.ok) {
|
|
139
|
+
return response;
|
|
140
|
+
}
|
|
141
|
+
throw response;
|
|
142
|
+
}
|
|
143
|
+
function buildRequest(dataSource) {
|
|
144
|
+
const current = dataSource.url?.trim();
|
|
145
|
+
if (!current || !isValid(current)) {
|
|
146
|
+
throw new Error(`Invalid URL: ${dataSource.url}`);
|
|
147
|
+
}
|
|
148
|
+
const url = new URL(current, COMMON_URL);
|
|
149
|
+
const method = buildMethod(dataSource);
|
|
150
|
+
if (dataSource.body && isGetMethod(method)) {
|
|
151
|
+
Object.entries(dataSource.body).filter(([, value]) => value !== void 0 && value !== "undefined").forEach(([key, value]) => {
|
|
152
|
+
url.searchParams.append(key, String(value));
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return [url, method];
|
|
156
|
+
}
|
|
157
|
+
function asJson(headers) {
|
|
158
|
+
return {
|
|
159
|
+
...headers,
|
|
160
|
+
"Content-Type": "application/json"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function isValid(value) {
|
|
164
|
+
if (!isString(value) || isInterpolated(value)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return !REGEX_INVALID_URL_SEGMENTS.test(value);
|
|
168
|
+
}
|
|
169
|
+
function buildMethod(dataSource) {
|
|
170
|
+
return dataSource.method ?? "GET";
|
|
171
|
+
}
|
|
172
|
+
function buildHeaders(dataSource) {
|
|
173
|
+
const headers = dataSource.headers ?? {};
|
|
174
|
+
return {
|
|
175
|
+
Accept: "application/json",
|
|
176
|
+
...headers
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function isGetMethod(method) {
|
|
180
|
+
return method.toUpperCase() === "GET";
|
|
181
|
+
}
|
|
182
|
+
function buildBody(method, body) {
|
|
183
|
+
if (!isGetMethod(method)) {
|
|
184
|
+
return body;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/config/index.ts
|
|
189
|
+
function defineConfig(options) {
|
|
190
|
+
const config = {
|
|
191
|
+
alert: options.alert,
|
|
192
|
+
env: options.env,
|
|
193
|
+
fetcher: {
|
|
194
|
+
provider: fetcher,
|
|
195
|
+
remotePatterns: options.fetcher?.remotePatterns
|
|
196
|
+
},
|
|
197
|
+
inputs: {},
|
|
198
|
+
style: options.style
|
|
199
|
+
};
|
|
200
|
+
config.validation = options.validation ?? {
|
|
201
|
+
blur: true,
|
|
202
|
+
change: true,
|
|
203
|
+
showError: true,
|
|
204
|
+
submit: true
|
|
205
|
+
};
|
|
206
|
+
options.inputs.forEach(({ types, input }) => {
|
|
207
|
+
const type = Array.isArray(types) ? types : [types];
|
|
208
|
+
type.forEach((t) => {
|
|
209
|
+
config.inputs[t] = input;
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
return config;
|
|
213
|
+
}
|
|
214
|
+
var defineCheckbox = createDefineFunction([CHECKBOX]);
|
|
215
|
+
var defineInput = createDefineFunction(INPUTS);
|
|
216
|
+
var defineRadio = createDefineFunction([RADIO]);
|
|
217
|
+
var defineSelect = createDefineFunction(SELECTS);
|
|
218
|
+
var defineTextArea = createDefineFunction([TEXTAREA]);
|
|
219
|
+
function defineCustomInput(types, input) {
|
|
220
|
+
return createDefineFunction(types)(input);
|
|
221
|
+
}
|
|
222
|
+
function createDefineFunction(types) {
|
|
223
|
+
return (input) => {
|
|
224
|
+
return {
|
|
225
|
+
types,
|
|
226
|
+
input
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
}
|
package/dist/config/esm/index.js
CHANGED
|
@@ -1 +1,206 @@
|
|
|
1
|
-
|
|
1
|
+
// ../luna-core/src/util/constant.ts
|
|
2
|
+
var INPUT = "input";
|
|
3
|
+
var INPUT_EMAIL = "input/email";
|
|
4
|
+
var INPUT_NUMBER = "input/number";
|
|
5
|
+
var INPUT_PASSWORD = "input/password";
|
|
6
|
+
var INPUT_TEL = "input/tel";
|
|
7
|
+
var INPUT_TEXT = "input/text";
|
|
8
|
+
var INPUTS = [
|
|
9
|
+
INPUT,
|
|
10
|
+
INPUT_EMAIL,
|
|
11
|
+
INPUT_NUMBER,
|
|
12
|
+
INPUT_PASSWORD,
|
|
13
|
+
INPUT_TEL,
|
|
14
|
+
INPUT_TEXT
|
|
15
|
+
];
|
|
16
|
+
var TEXTAREA = "textarea";
|
|
17
|
+
var RADIO = "radio";
|
|
18
|
+
var CHECKBOX = "checkbox";
|
|
19
|
+
var SELECT = "select";
|
|
20
|
+
var SELECT_MONTH = "select/month";
|
|
21
|
+
var SELECT_YEAR = "select/year";
|
|
22
|
+
var SELECTS = [SELECT, SELECT_MONTH, SELECT_YEAR];
|
|
23
|
+
var COMMON_URL = "http://luna.internal";
|
|
24
|
+
|
|
25
|
+
// ../luna-core/src/util/is-type.ts
|
|
26
|
+
function isObject(value) {
|
|
27
|
+
return value !== null && Object.prototype.toString.call(value) === "[object Object]";
|
|
28
|
+
}
|
|
29
|
+
function isString(value) {
|
|
30
|
+
return typeof value === "string";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ../luna-core/src/util/string.ts
|
|
34
|
+
function isInterpolated(template) {
|
|
35
|
+
if (isString(template)) {
|
|
36
|
+
return /{([^}]+)}/.test(template);
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(template)) {
|
|
39
|
+
return template.some((item) => isInterpolated(item));
|
|
40
|
+
}
|
|
41
|
+
if (isObject(template)) {
|
|
42
|
+
return Object.values(template).some((value) => isInterpolated(value));
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ../luna-core/src/util/logger.ts
|
|
48
|
+
var logger = {
|
|
49
|
+
error: (...args) => {
|
|
50
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
51
|
+
getConsole().error("[Luna Form]", ...args);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
warn: (...args) => {
|
|
55
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
56
|
+
getConsole().warn("[Luna Form]", ...args);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
info: (...args) => {
|
|
60
|
+
if (isConsoleAvailable() && !isProduction()) {
|
|
61
|
+
getConsole().info("[Luna Form]", ...args);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var isConsoleAvailable = () => typeof getConsole() !== "undefined";
|
|
66
|
+
var isProduction = () => false;
|
|
67
|
+
var getConsole = () => globalThis.console;
|
|
68
|
+
|
|
69
|
+
// ../luna-core/src/util/stringify.ts
|
|
70
|
+
function stringify(body) {
|
|
71
|
+
try {
|
|
72
|
+
if (body instanceof FormData) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (isString(body)) {
|
|
76
|
+
return body;
|
|
77
|
+
}
|
|
78
|
+
return JSON.stringify(body);
|
|
79
|
+
} catch {
|
|
80
|
+
logger.error("Failed to stringify body:", body);
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ../luna-core/src/fetcher.ts
|
|
86
|
+
var REGEX_INVALID_URL_SEGMENTS = /(^|[\/?=&])(null|undefined)([\/?=&]|$)/;
|
|
87
|
+
async function fetcher(dataSource) {
|
|
88
|
+
const [url, method] = buildRequest(dataSource);
|
|
89
|
+
let body = dataSource.body;
|
|
90
|
+
let headers = buildHeaders(dataSource);
|
|
91
|
+
if (body && !isGetMethod(method)) {
|
|
92
|
+
const bodyStringify = stringify(body);
|
|
93
|
+
if (bodyStringify !== null) {
|
|
94
|
+
body = bodyStringify;
|
|
95
|
+
headers = asJson(headers);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const target = url.origin === COMMON_URL ? url.toString().replace(COMMON_URL, "") : url.toString();
|
|
99
|
+
const request = await fetch(target, {
|
|
100
|
+
body: buildBody(method, body),
|
|
101
|
+
cache: dataSource.cache,
|
|
102
|
+
headers,
|
|
103
|
+
method
|
|
104
|
+
});
|
|
105
|
+
const response = await request.json();
|
|
106
|
+
if (request.ok) {
|
|
107
|
+
return response;
|
|
108
|
+
}
|
|
109
|
+
throw response;
|
|
110
|
+
}
|
|
111
|
+
function buildRequest(dataSource) {
|
|
112
|
+
const current = dataSource.url?.trim();
|
|
113
|
+
if (!current || !isValid(current)) {
|
|
114
|
+
throw new Error(`Invalid URL: ${dataSource.url}`);
|
|
115
|
+
}
|
|
116
|
+
const url = new URL(current, COMMON_URL);
|
|
117
|
+
const method = buildMethod(dataSource);
|
|
118
|
+
if (dataSource.body && isGetMethod(method)) {
|
|
119
|
+
Object.entries(dataSource.body).filter(([, value]) => value !== void 0 && value !== "undefined").forEach(([key, value]) => {
|
|
120
|
+
url.searchParams.append(key, String(value));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return [url, method];
|
|
124
|
+
}
|
|
125
|
+
function asJson(headers) {
|
|
126
|
+
return {
|
|
127
|
+
...headers,
|
|
128
|
+
"Content-Type": "application/json"
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function isValid(value) {
|
|
132
|
+
if (!isString(value) || isInterpolated(value)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
return !REGEX_INVALID_URL_SEGMENTS.test(value);
|
|
136
|
+
}
|
|
137
|
+
function buildMethod(dataSource) {
|
|
138
|
+
return dataSource.method ?? "GET";
|
|
139
|
+
}
|
|
140
|
+
function buildHeaders(dataSource) {
|
|
141
|
+
const headers = dataSource.headers ?? {};
|
|
142
|
+
return {
|
|
143
|
+
Accept: "application/json",
|
|
144
|
+
...headers
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function isGetMethod(method) {
|
|
148
|
+
return method.toUpperCase() === "GET";
|
|
149
|
+
}
|
|
150
|
+
function buildBody(method, body) {
|
|
151
|
+
if (!isGetMethod(method)) {
|
|
152
|
+
return body;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/config/index.ts
|
|
157
|
+
function defineConfig(options) {
|
|
158
|
+
const config = {
|
|
159
|
+
alert: options.alert,
|
|
160
|
+
env: options.env,
|
|
161
|
+
fetcher: {
|
|
162
|
+
provider: fetcher,
|
|
163
|
+
remotePatterns: options.fetcher?.remotePatterns
|
|
164
|
+
},
|
|
165
|
+
inputs: {},
|
|
166
|
+
style: options.style
|
|
167
|
+
};
|
|
168
|
+
config.validation = options.validation ?? {
|
|
169
|
+
blur: true,
|
|
170
|
+
change: true,
|
|
171
|
+
showError: true,
|
|
172
|
+
submit: true
|
|
173
|
+
};
|
|
174
|
+
options.inputs.forEach(({ types, input }) => {
|
|
175
|
+
const type = Array.isArray(types) ? types : [types];
|
|
176
|
+
type.forEach((t) => {
|
|
177
|
+
config.inputs[t] = input;
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
return config;
|
|
181
|
+
}
|
|
182
|
+
var defineCheckbox = createDefineFunction([CHECKBOX]);
|
|
183
|
+
var defineInput = createDefineFunction(INPUTS);
|
|
184
|
+
var defineRadio = createDefineFunction([RADIO]);
|
|
185
|
+
var defineSelect = createDefineFunction(SELECTS);
|
|
186
|
+
var defineTextArea = createDefineFunction([TEXTAREA]);
|
|
187
|
+
function defineCustomInput(types, input) {
|
|
188
|
+
return createDefineFunction(types)(input);
|
|
189
|
+
}
|
|
190
|
+
function createDefineFunction(types) {
|
|
191
|
+
return (input) => {
|
|
192
|
+
return {
|
|
193
|
+
types,
|
|
194
|
+
input
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
export {
|
|
199
|
+
defineCheckbox,
|
|
200
|
+
defineConfig,
|
|
201
|
+
defineCustomInput,
|
|
202
|
+
defineInput,
|
|
203
|
+
defineRadio,
|
|
204
|
+
defineSelect,
|
|
205
|
+
defineTextArea
|
|
206
|
+
};
|