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.
@@ -1 +1,229 @@
1
- "use strict";var f=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var P=(t,n)=>{for(var r in n)f(t,r,{get:n[r],enumerable:!0})},_=(t,n,r,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of O(n))!N.call(t,o)&&o!==r&&f(t,o,{get:()=>n[o],enumerable:!(e=C(n,o))||e.enumerable});return t};var w=t=>_(f({},"__esModule",{value:!0}),t);var rt={};P(rt,{defineCheckbox:()=>J,defineConfig:()=>q,defineCustomInput:()=>nt,defineInput:()=>z,defineRadio:()=>Z,defineSelect:()=>Q,defineTextArea:()=>tt});module.exports=w(rt);var D="input",M="input/email",U="input/number",k="input/password",X="input/tel",v="input/text",m=[D,M,U,k,X,v],h="textarea",A="radio",y="checkbox",V="select",j="select/month",F="select/year",I=[V,j,F];var u="http://luna.internal";function R(t){return t!==null&&Object.prototype.toString.call(t)==="[object Object]"}function i(t){return typeof t=="string"}function a(t){return i(t)?/{([^}]+)}/.test(t):Array.isArray(t)?t.some(n=>a(n)):R(t)?Object.values(t).some(n=>a(n)):!1}var b={error:(...t)=>{l()&&!x()&&p().error("[Luna Form]",...t)},warn:(...t)=>{l()&&!x()&&p().warn("[Luna Form]",...t)},info:(...t)=>{l()&&!x()&&p().info("[Luna Form]",...t)}},l=()=>typeof p()<"u",x=()=>!0,p=()=>globalThis.console;function S(t){try{return t instanceof FormData?null:i(t)?t:JSON.stringify(t)}catch{return b.error("Failed to stringify body:",t),null}}var G=/(^|[\/?=&])(null|undefined)([\/?=&]|$)/;async function L(t){let[n,r]=H(t),e=t.body,o=$(t);if(e&&!d(r)){let g=S(e);g!==null&&(e=g,o=B(o))}let c=n.origin===u?n.toString().replace(u,""):n.toString(),T=await fetch(c,{body:W(r,e),cache:t.cache,headers:o,method:r}),E=await T.json();if(T.ok)return E;throw E}function H(t){let n=t.url?.trim();if(!n||!K(n))throw new Error(`Invalid URL: ${t.url}`);let r=new URL(n,u),e=Y(t);return t.body&&d(e)&&Object.entries(t.body).filter(([,o])=>o!==void 0&&o!=="undefined").forEach(([o,c])=>{r.searchParams.append(o,String(c))}),[r,e]}function B(t){return{...t,"Content-Type":"application/json"}}function K(t){return!i(t)||a(t)?!1:!G.test(t)}function Y(t){return t.method??"GET"}function $(t){return{Accept:"application/json",...t.headers??{}}}function d(t){return t.toUpperCase()==="GET"}function W(t,n){if(!d(t))return n}function q(t){let n={alert:t.alert,env:t.env,fetcher:{provider:L,remotePatterns:t.fetcher?.remotePatterns},inputs:{},style:t.style};return n.validation=t.validation??{blur:!0,change:!0,showError:!0,submit:!0},t.inputs.forEach(({types:r,input:e})=>{(Array.isArray(r)?r:[r]).forEach(c=>{n.inputs[c]=e})}),n}var J=s([y]),z=s(m),Z=s([A]),Q=s(I),tt=s([h]);function nt(t,n){return s(t)(n)}function s(t){return n=>({types:t,input:n})}
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
+ }
@@ -1 +1,206 @@
1
- var L="input",C="input/email",O="input/number",N="input/password",P="input/tel",_="input/text",g=[L,C,O,N,P,_],m="textarea",h="radio",A="checkbox",w="select",D="select/month",M="select/year",y=[w,D,M];var u="http://luna.internal";function I(t){return t!==null&&Object.prototype.toString.call(t)==="[object Object]"}function i(t){return typeof t=="string"}function a(t){return i(t)?/{([^}]+)}/.test(t):Array.isArray(t)?t.some(n=>a(n)):I(t)?Object.values(t).some(n=>a(n)):!1}var R={error:(...t)=>{f()&&!l()&&p().error("[Luna Form]",...t)},warn:(...t)=>{f()&&!l()&&p().warn("[Luna Form]",...t)},info:(...t)=>{f()&&!l()&&p().info("[Luna Form]",...t)}},f=()=>typeof p()<"u",l=()=>!0,p=()=>globalThis.console;function b(t){try{return t instanceof FormData?null:i(t)?t:JSON.stringify(t)}catch{return R.error("Failed to stringify body:",t),null}}var U=/(^|[\/?=&])(null|undefined)([\/?=&]|$)/;async function S(t){let[n,r]=k(t),o=t.body,e=j(t);if(o&&!x(r)){let E=b(o);E!==null&&(o=E,e=X(e))}let c=n.origin===u?n.toString().replace(u,""):n.toString(),d=await fetch(c,{body:F(r,o),cache:t.cache,headers:e,method:r}),T=await d.json();if(d.ok)return T;throw T}function k(t){let n=t.url?.trim();if(!n||!v(n))throw new Error(`Invalid URL: ${t.url}`);let r=new URL(n,u),o=V(t);return t.body&&x(o)&&Object.entries(t.body).filter(([,e])=>e!==void 0&&e!=="undefined").forEach(([e,c])=>{r.searchParams.append(e,String(c))}),[r,o]}function X(t){return{...t,"Content-Type":"application/json"}}function v(t){return!i(t)||a(t)?!1:!U.test(t)}function V(t){return t.method??"GET"}function j(t){return{Accept:"application/json",...t.headers??{}}}function x(t){return t.toUpperCase()==="GET"}function F(t,n){if(!x(t))return n}function ot(t){let n={alert:t.alert,env:t.env,fetcher:{provider:S,remotePatterns:t.fetcher?.remotePatterns},inputs:{},style:t.style};return n.validation=t.validation??{blur:!0,change:!0,showError:!0,submit:!0},t.inputs.forEach(({types:r,input:o})=>{(Array.isArray(r)?r:[r]).forEach(c=>{n.inputs[c]=o})}),n}var et=s([A]),it=s(g),st=s([h]),ct=s(y),ut=s([m]);function at(t,n){return s(t)(n)}function s(t){return n=>({types:t,input:n})}export{et as defineCheckbox,ot as defineConfig,at as defineCustomInput,it as defineInput,st as defineRadio,ct as defineSelect,ut as defineTextArea};
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
+ };