dn-react-router-toolkit 0.6.3 → 0.6.4
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/api/create_api_handler.js +55 -43
- package/dist/api/create_api_handler.mjs +56 -43
- package/dist/crud/crud_form_provider.js +19 -5
- package/dist/crud/crud_form_provider.mjs +19 -5
- package/dist/crud/crud_loader.js +55 -43
- package/dist/crud/crud_loader.mjs +56 -43
- package/dist/crud/crud_page.js +19 -5
- package/dist/crud/crud_page.mjs +19 -5
- package/dist/crud/index.js +74 -48
- package/dist/crud/index.mjs +75 -48
- package/package.json +1 -1
|
@@ -83,53 +83,65 @@ function apiHandler({
|
|
|
83
83
|
switch (request.method) {
|
|
84
84
|
case "POST":
|
|
85
85
|
case "PUT": {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
86
|
+
try {
|
|
87
|
+
const serilaizedParams = await request.json();
|
|
88
|
+
const params = deserialize(serilaizedParams);
|
|
89
|
+
if (validators) {
|
|
90
|
+
const paramsForValidation = Object.keys(
|
|
91
|
+
validators
|
|
92
|
+
).filter(
|
|
93
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
94
|
+
validators,
|
|
95
|
+
key
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
for (const paramKey of paramsForValidation) {
|
|
99
|
+
const value = params[paramKey];
|
|
100
|
+
const validator = validators[paramKey];
|
|
101
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
102
|
+
throw (0, import_http.BAD_REQUEST)(
|
|
103
|
+
validator.message ? validator.message(value) : void 0
|
|
104
|
+
);
|
|
105
|
+
}
|
|
99
106
|
}
|
|
100
107
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
108
|
+
const itemId = params.id || (0, import_uuid.v4)();
|
|
109
|
+
if (!params.id && existingConditions) {
|
|
110
|
+
const paramsForExistenceCheck = Object.keys(
|
|
111
|
+
existingConditions
|
|
112
|
+
).filter(
|
|
113
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
114
|
+
);
|
|
115
|
+
const where = (0, import_drizzle_orm.and)(
|
|
116
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
117
|
+
const condition = existingConditions[key];
|
|
118
|
+
if (condition) {
|
|
119
|
+
acc.push(condition(params[key]));
|
|
120
|
+
}
|
|
121
|
+
return acc;
|
|
122
|
+
}, [])
|
|
123
|
+
);
|
|
124
|
+
const existing = await repository.findAll({
|
|
125
|
+
limit: 1,
|
|
126
|
+
where
|
|
127
|
+
});
|
|
128
|
+
if (existing.length > 0) {
|
|
129
|
+
throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const values = {
|
|
133
|
+
id: itemId,
|
|
134
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
135
|
+
...params
|
|
136
|
+
};
|
|
137
|
+
const item = await repository.save(values);
|
|
138
|
+
return (0, import_http.CREATED)(item);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error instanceof Error) {
|
|
141
|
+
throw (0, import_http.INTERNAL_SERVER_ERROR)(error.message);
|
|
124
142
|
}
|
|
143
|
+
throw error;
|
|
125
144
|
}
|
|
126
|
-
const values = {
|
|
127
|
-
id: itemId,
|
|
128
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
129
|
-
...params
|
|
130
|
-
};
|
|
131
|
-
const item = await repository.save(values);
|
|
132
|
-
return (0, import_http.CREATED)(item);
|
|
133
145
|
}
|
|
134
146
|
default:
|
|
135
147
|
throw (0, import_http.METHOD_NOT_ALLOWED)();
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
BAD_REQUEST,
|
|
4
4
|
CONFLICT,
|
|
5
5
|
CREATED,
|
|
6
|
+
INTERNAL_SERVER_ERROR,
|
|
6
7
|
METHOD_NOT_ALLOWED
|
|
7
8
|
} from "dn-react-toolkit/http";
|
|
8
9
|
import {
|
|
@@ -68,53 +69,65 @@ function apiHandler({
|
|
|
68
69
|
switch (request.method) {
|
|
69
70
|
case "POST":
|
|
70
71
|
case "PUT": {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
72
|
+
try {
|
|
73
|
+
const serilaizedParams = await request.json();
|
|
74
|
+
const params = deserialize(serilaizedParams);
|
|
75
|
+
if (validators) {
|
|
76
|
+
const paramsForValidation = Object.keys(
|
|
77
|
+
validators
|
|
78
|
+
).filter(
|
|
79
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
80
|
+
validators,
|
|
81
|
+
key
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
for (const paramKey of paramsForValidation) {
|
|
85
|
+
const value = params[paramKey];
|
|
86
|
+
const validator = validators[paramKey];
|
|
87
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
88
|
+
throw BAD_REQUEST(
|
|
89
|
+
validator.message ? validator.message(value) : void 0
|
|
90
|
+
);
|
|
91
|
+
}
|
|
84
92
|
}
|
|
85
93
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
94
|
+
const itemId = params.id || v4();
|
|
95
|
+
if (!params.id && existingConditions) {
|
|
96
|
+
const paramsForExistenceCheck = Object.keys(
|
|
97
|
+
existingConditions
|
|
98
|
+
).filter(
|
|
99
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
100
|
+
);
|
|
101
|
+
const where = and(
|
|
102
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
103
|
+
const condition = existingConditions[key];
|
|
104
|
+
if (condition) {
|
|
105
|
+
acc.push(condition(params[key]));
|
|
106
|
+
}
|
|
107
|
+
return acc;
|
|
108
|
+
}, [])
|
|
109
|
+
);
|
|
110
|
+
const existing = await repository.findAll({
|
|
111
|
+
limit: 1,
|
|
112
|
+
where
|
|
113
|
+
});
|
|
114
|
+
if (existing.length > 0) {
|
|
115
|
+
throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const values = {
|
|
119
|
+
id: itemId,
|
|
120
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
121
|
+
...params
|
|
122
|
+
};
|
|
123
|
+
const item = await repository.save(values);
|
|
124
|
+
return CREATED(item);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error instanceof Error) {
|
|
127
|
+
throw INTERNAL_SERVER_ERROR(error.message);
|
|
109
128
|
}
|
|
129
|
+
throw error;
|
|
110
130
|
}
|
|
111
|
-
const values = {
|
|
112
|
-
id: itemId,
|
|
113
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
114
|
-
...params
|
|
115
|
-
};
|
|
116
|
-
const item = await repository.save(values);
|
|
117
|
-
return CREATED(item);
|
|
118
131
|
}
|
|
119
132
|
default:
|
|
120
133
|
throw METHOD_NOT_ALLOWED();
|
|
@@ -112,14 +112,28 @@ function CrudFormProvider({
|
|
|
112
112
|
children
|
|
113
113
|
}) {
|
|
114
114
|
const apiPrefix = `/api${prefix}`;
|
|
115
|
-
const
|
|
116
|
-
|
|
115
|
+
const createInitialState = () => {
|
|
116
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
117
|
+
const value = item ? item[key] : void 0;
|
|
118
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
119
|
+
if (typeof value === "number") {
|
|
120
|
+
return {
|
|
121
|
+
...acc,
|
|
122
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
...acc,
|
|
127
|
+
[key]: value || columns[key]?.defaultValue
|
|
128
|
+
};
|
|
129
|
+
}
|
|
117
130
|
return {
|
|
118
131
|
...acc,
|
|
119
|
-
[key]: value
|
|
132
|
+
[key]: value
|
|
120
133
|
};
|
|
121
|
-
}, {})
|
|
122
|
-
}
|
|
134
|
+
}, {});
|
|
135
|
+
};
|
|
136
|
+
const store = (0, import_react_store_input.useStore)(createInitialState());
|
|
123
137
|
const navigate = (0, import_react_router.useNavigate)();
|
|
124
138
|
const submit = async () => {
|
|
125
139
|
const res = await fetch(apiPrefix, {
|
|
@@ -76,14 +76,28 @@ function CrudFormProvider({
|
|
|
76
76
|
children
|
|
77
77
|
}) {
|
|
78
78
|
const apiPrefix = `/api${prefix}`;
|
|
79
|
-
const
|
|
80
|
-
|
|
79
|
+
const createInitialState = () => {
|
|
80
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
81
|
+
const value = item ? item[key] : void 0;
|
|
82
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
83
|
+
if (typeof value === "number") {
|
|
84
|
+
return {
|
|
85
|
+
...acc,
|
|
86
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
...acc,
|
|
91
|
+
[key]: value || columns[key]?.defaultValue
|
|
92
|
+
};
|
|
93
|
+
}
|
|
81
94
|
return {
|
|
82
95
|
...acc,
|
|
83
|
-
[key]: value
|
|
96
|
+
[key]: value
|
|
84
97
|
};
|
|
85
|
-
}, {})
|
|
86
|
-
}
|
|
98
|
+
}, {});
|
|
99
|
+
};
|
|
100
|
+
const store = useStore(createInitialState());
|
|
87
101
|
const navigate = useNavigate();
|
|
88
102
|
const submit = async () => {
|
|
89
103
|
const res = await fetch(apiPrefix, {
|
package/dist/crud/crud_loader.js
CHANGED
|
@@ -144,53 +144,65 @@ function apiHandler({
|
|
|
144
144
|
switch (request.method) {
|
|
145
145
|
case "POST":
|
|
146
146
|
case "PUT": {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
147
|
+
try {
|
|
148
|
+
const serilaizedParams = await request.json();
|
|
149
|
+
const params = deserialize(serilaizedParams);
|
|
150
|
+
if (validators) {
|
|
151
|
+
const paramsForValidation = Object.keys(
|
|
152
|
+
validators
|
|
153
|
+
).filter(
|
|
154
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
155
|
+
validators,
|
|
156
|
+
key
|
|
157
|
+
)
|
|
158
|
+
);
|
|
159
|
+
for (const paramKey of paramsForValidation) {
|
|
160
|
+
const value = params[paramKey];
|
|
161
|
+
const validator = validators[paramKey];
|
|
162
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
163
|
+
throw (0, import_http.BAD_REQUEST)(
|
|
164
|
+
validator.message ? validator.message(value) : void 0
|
|
165
|
+
);
|
|
166
|
+
}
|
|
160
167
|
}
|
|
161
168
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
169
|
+
const itemId = params.id || (0, import_uuid.v4)();
|
|
170
|
+
if (!params.id && existingConditions) {
|
|
171
|
+
const paramsForExistenceCheck = Object.keys(
|
|
172
|
+
existingConditions
|
|
173
|
+
).filter(
|
|
174
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
175
|
+
);
|
|
176
|
+
const where = (0, import_drizzle_orm2.and)(
|
|
177
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
178
|
+
const condition = existingConditions[key];
|
|
179
|
+
if (condition) {
|
|
180
|
+
acc.push(condition(params[key]));
|
|
181
|
+
}
|
|
182
|
+
return acc;
|
|
183
|
+
}, [])
|
|
184
|
+
);
|
|
185
|
+
const existing = await repository.findAll({
|
|
186
|
+
limit: 1,
|
|
187
|
+
where
|
|
188
|
+
});
|
|
189
|
+
if (existing.length > 0) {
|
|
190
|
+
throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const values = {
|
|
194
|
+
id: itemId,
|
|
195
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
196
|
+
...params
|
|
197
|
+
};
|
|
198
|
+
const item = await repository.save(values);
|
|
199
|
+
return (0, import_http.CREATED)(item);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (error instanceof Error) {
|
|
202
|
+
throw (0, import_http.INTERNAL_SERVER_ERROR)(error.message);
|
|
185
203
|
}
|
|
204
|
+
throw error;
|
|
186
205
|
}
|
|
187
|
-
const values = {
|
|
188
|
-
id: itemId,
|
|
189
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
190
|
-
...params
|
|
191
|
-
};
|
|
192
|
-
const item = await repository.save(values);
|
|
193
|
-
return (0, import_http.CREATED)(item);
|
|
194
206
|
}
|
|
195
207
|
default:
|
|
196
208
|
throw (0, import_http.METHOD_NOT_ALLOWED)();
|
|
@@ -65,6 +65,7 @@ import {
|
|
|
65
65
|
BAD_REQUEST,
|
|
66
66
|
CONFLICT,
|
|
67
67
|
CREATED,
|
|
68
|
+
INTERNAL_SERVER_ERROR,
|
|
68
69
|
METHOD_NOT_ALLOWED
|
|
69
70
|
} from "dn-react-toolkit/http";
|
|
70
71
|
import {
|
|
@@ -130,53 +131,65 @@ function apiHandler({
|
|
|
130
131
|
switch (request.method) {
|
|
131
132
|
case "POST":
|
|
132
133
|
case "PUT": {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
134
|
+
try {
|
|
135
|
+
const serilaizedParams = await request.json();
|
|
136
|
+
const params = deserialize(serilaizedParams);
|
|
137
|
+
if (validators) {
|
|
138
|
+
const paramsForValidation = Object.keys(
|
|
139
|
+
validators
|
|
140
|
+
).filter(
|
|
141
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
142
|
+
validators,
|
|
143
|
+
key
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
for (const paramKey of paramsForValidation) {
|
|
147
|
+
const value = params[paramKey];
|
|
148
|
+
const validator = validators[paramKey];
|
|
149
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
150
|
+
throw BAD_REQUEST(
|
|
151
|
+
validator.message ? validator.message(value) : void 0
|
|
152
|
+
);
|
|
153
|
+
}
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
156
|
+
const itemId = params.id || v4();
|
|
157
|
+
if (!params.id && existingConditions) {
|
|
158
|
+
const paramsForExistenceCheck = Object.keys(
|
|
159
|
+
existingConditions
|
|
160
|
+
).filter(
|
|
161
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
162
|
+
);
|
|
163
|
+
const where = and2(
|
|
164
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
165
|
+
const condition = existingConditions[key];
|
|
166
|
+
if (condition) {
|
|
167
|
+
acc.push(condition(params[key]));
|
|
168
|
+
}
|
|
169
|
+
return acc;
|
|
170
|
+
}, [])
|
|
171
|
+
);
|
|
172
|
+
const existing = await repository.findAll({
|
|
173
|
+
limit: 1,
|
|
174
|
+
where
|
|
175
|
+
});
|
|
176
|
+
if (existing.length > 0) {
|
|
177
|
+
throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const values = {
|
|
181
|
+
id: itemId,
|
|
182
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
183
|
+
...params
|
|
184
|
+
};
|
|
185
|
+
const item = await repository.save(values);
|
|
186
|
+
return CREATED(item);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (error instanceof Error) {
|
|
189
|
+
throw INTERNAL_SERVER_ERROR(error.message);
|
|
171
190
|
}
|
|
191
|
+
throw error;
|
|
172
192
|
}
|
|
173
|
-
const values = {
|
|
174
|
-
id: itemId,
|
|
175
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
176
|
-
...params
|
|
177
|
-
};
|
|
178
|
-
const item = await repository.save(values);
|
|
179
|
-
return CREATED(item);
|
|
180
193
|
}
|
|
181
194
|
default:
|
|
182
195
|
throw METHOD_NOT_ALLOWED();
|
package/dist/crud/crud_page.js
CHANGED
|
@@ -113,14 +113,28 @@ function CrudFormProvider({
|
|
|
113
113
|
children
|
|
114
114
|
}) {
|
|
115
115
|
const apiPrefix = `/api${prefix}`;
|
|
116
|
-
const
|
|
117
|
-
|
|
116
|
+
const createInitialState = () => {
|
|
117
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
118
|
+
const value = item ? item[key] : void 0;
|
|
119
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
120
|
+
if (typeof value === "number") {
|
|
121
|
+
return {
|
|
122
|
+
...acc,
|
|
123
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
...acc,
|
|
128
|
+
[key]: value || columns[key]?.defaultValue
|
|
129
|
+
};
|
|
130
|
+
}
|
|
118
131
|
return {
|
|
119
132
|
...acc,
|
|
120
|
-
[key]: value
|
|
133
|
+
[key]: value
|
|
121
134
|
};
|
|
122
|
-
}, {})
|
|
123
|
-
}
|
|
135
|
+
}, {});
|
|
136
|
+
};
|
|
137
|
+
const store = (0, import_react_store_input.useStore)(createInitialState());
|
|
124
138
|
const navigate = (0, import_react_router.useNavigate)();
|
|
125
139
|
const submit = async () => {
|
|
126
140
|
const res = await fetch(apiPrefix, {
|
package/dist/crud/crud_page.mjs
CHANGED
|
@@ -79,14 +79,28 @@ function CrudFormProvider({
|
|
|
79
79
|
children
|
|
80
80
|
}) {
|
|
81
81
|
const apiPrefix = `/api${prefix}`;
|
|
82
|
-
const
|
|
83
|
-
|
|
82
|
+
const createInitialState = () => {
|
|
83
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
84
|
+
const value = item ? item[key] : void 0;
|
|
85
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
86
|
+
if (typeof value === "number") {
|
|
87
|
+
return {
|
|
88
|
+
...acc,
|
|
89
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
...acc,
|
|
94
|
+
[key]: value || columns[key]?.defaultValue
|
|
95
|
+
};
|
|
96
|
+
}
|
|
84
97
|
return {
|
|
85
98
|
...acc,
|
|
86
|
-
[key]: value
|
|
99
|
+
[key]: value
|
|
87
100
|
};
|
|
88
|
-
}, {})
|
|
89
|
-
}
|
|
101
|
+
}, {});
|
|
102
|
+
};
|
|
103
|
+
const store = useStore(createInitialState());
|
|
90
104
|
const navigate = useNavigate();
|
|
91
105
|
const submit = async () => {
|
|
92
106
|
const res = await fetch(apiPrefix, {
|
package/dist/crud/index.js
CHANGED
|
@@ -153,14 +153,28 @@ function CrudFormProvider({
|
|
|
153
153
|
children
|
|
154
154
|
}) {
|
|
155
155
|
const apiPrefix = `/api${prefix2}`;
|
|
156
|
-
const
|
|
157
|
-
|
|
156
|
+
const createInitialState = () => {
|
|
157
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
158
|
+
const value = item ? item[key] : void 0;
|
|
159
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
160
|
+
if (typeof value === "number") {
|
|
161
|
+
return {
|
|
162
|
+
...acc,
|
|
163
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
...acc,
|
|
168
|
+
[key]: value || columns[key]?.defaultValue
|
|
169
|
+
};
|
|
170
|
+
}
|
|
158
171
|
return {
|
|
159
172
|
...acc,
|
|
160
|
-
[key]: value
|
|
173
|
+
[key]: value
|
|
161
174
|
};
|
|
162
|
-
}, {})
|
|
163
|
-
}
|
|
175
|
+
}, {});
|
|
176
|
+
};
|
|
177
|
+
const store = (0, import_react_store_input.useStore)(createInitialState());
|
|
164
178
|
const navigate = (0, import_react_router.useNavigate)();
|
|
165
179
|
const submit = async () => {
|
|
166
180
|
const res = await fetch(apiPrefix, {
|
|
@@ -440,53 +454,65 @@ function apiHandler({
|
|
|
440
454
|
switch (request.method) {
|
|
441
455
|
case "POST":
|
|
442
456
|
case "PUT": {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
457
|
+
try {
|
|
458
|
+
const serilaizedParams = await request.json();
|
|
459
|
+
const params = deserialize(serilaizedParams);
|
|
460
|
+
if (validators) {
|
|
461
|
+
const paramsForValidation = Object.keys(
|
|
462
|
+
validators
|
|
463
|
+
).filter(
|
|
464
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
465
|
+
validators,
|
|
466
|
+
key
|
|
467
|
+
)
|
|
468
|
+
);
|
|
469
|
+
for (const paramKey of paramsForValidation) {
|
|
470
|
+
const value = params[paramKey];
|
|
471
|
+
const validator = validators[paramKey];
|
|
472
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
473
|
+
throw (0, import_http.BAD_REQUEST)(
|
|
474
|
+
validator.message ? validator.message(value) : void 0
|
|
475
|
+
);
|
|
476
|
+
}
|
|
456
477
|
}
|
|
457
478
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
479
|
+
const itemId = params.id || (0, import_uuid.v4)();
|
|
480
|
+
if (!params.id && existingConditions) {
|
|
481
|
+
const paramsForExistenceCheck = Object.keys(
|
|
482
|
+
existingConditions
|
|
483
|
+
).filter(
|
|
484
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
485
|
+
);
|
|
486
|
+
const where = (0, import_drizzle_orm2.and)(
|
|
487
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
488
|
+
const condition = existingConditions[key];
|
|
489
|
+
if (condition) {
|
|
490
|
+
acc.push(condition(params[key]));
|
|
491
|
+
}
|
|
492
|
+
return acc;
|
|
493
|
+
}, [])
|
|
494
|
+
);
|
|
495
|
+
const existing = await repository.findAll({
|
|
496
|
+
limit: 1,
|
|
497
|
+
where
|
|
498
|
+
});
|
|
499
|
+
if (existing.length > 0) {
|
|
500
|
+
throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
const values = {
|
|
504
|
+
id: itemId,
|
|
505
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
506
|
+
...params
|
|
507
|
+
};
|
|
508
|
+
const item = await repository.save(values);
|
|
509
|
+
return (0, import_http.CREATED)(item);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
if (error instanceof Error) {
|
|
512
|
+
throw (0, import_http.INTERNAL_SERVER_ERROR)(error.message);
|
|
481
513
|
}
|
|
514
|
+
throw error;
|
|
482
515
|
}
|
|
483
|
-
const values = {
|
|
484
|
-
id: itemId,
|
|
485
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
486
|
-
...params
|
|
487
|
-
};
|
|
488
|
-
const item = await repository.save(values);
|
|
489
|
-
return (0, import_http.CREATED)(item);
|
|
490
516
|
}
|
|
491
517
|
default:
|
|
492
518
|
throw (0, import_http.METHOD_NOT_ALLOWED)();
|
package/dist/crud/index.mjs
CHANGED
|
@@ -111,14 +111,28 @@ function CrudFormProvider({
|
|
|
111
111
|
children
|
|
112
112
|
}) {
|
|
113
113
|
const apiPrefix = `/api${prefix2}`;
|
|
114
|
-
const
|
|
115
|
-
|
|
114
|
+
const createInitialState = () => {
|
|
115
|
+
return Object.keys(item || columns).reduce((acc, key) => {
|
|
116
|
+
const value = item ? item[key] : void 0;
|
|
117
|
+
if (columns[key]?.defaultValue !== void 0) {
|
|
118
|
+
if (typeof value === "number") {
|
|
119
|
+
return {
|
|
120
|
+
...acc,
|
|
121
|
+
[key]: value ?? columns[key]?.defaultValue
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
...acc,
|
|
126
|
+
[key]: value || columns[key]?.defaultValue
|
|
127
|
+
};
|
|
128
|
+
}
|
|
116
129
|
return {
|
|
117
130
|
...acc,
|
|
118
|
-
[key]: value
|
|
131
|
+
[key]: value
|
|
119
132
|
};
|
|
120
|
-
}, {})
|
|
121
|
-
}
|
|
133
|
+
}, {});
|
|
134
|
+
};
|
|
135
|
+
const store = useStore(createInitialState());
|
|
122
136
|
const navigate = useNavigate();
|
|
123
137
|
const submit = async () => {
|
|
124
138
|
const res = await fetch(apiPrefix, {
|
|
@@ -384,6 +398,7 @@ import {
|
|
|
384
398
|
BAD_REQUEST,
|
|
385
399
|
CONFLICT,
|
|
386
400
|
CREATED,
|
|
401
|
+
INTERNAL_SERVER_ERROR,
|
|
387
402
|
METHOD_NOT_ALLOWED
|
|
388
403
|
} from "dn-react-toolkit/http";
|
|
389
404
|
import {
|
|
@@ -410,53 +425,65 @@ function apiHandler({
|
|
|
410
425
|
switch (request.method) {
|
|
411
426
|
case "POST":
|
|
412
427
|
case "PUT": {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
428
|
+
try {
|
|
429
|
+
const serilaizedParams = await request.json();
|
|
430
|
+
const params = deserialize(serilaizedParams);
|
|
431
|
+
if (validators) {
|
|
432
|
+
const paramsForValidation = Object.keys(
|
|
433
|
+
validators
|
|
434
|
+
).filter(
|
|
435
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
436
|
+
validators,
|
|
437
|
+
key
|
|
438
|
+
)
|
|
439
|
+
);
|
|
440
|
+
for (const paramKey of paramsForValidation) {
|
|
441
|
+
const value = params[paramKey];
|
|
442
|
+
const validator = validators[paramKey];
|
|
443
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
444
|
+
throw BAD_REQUEST(
|
|
445
|
+
validator.message ? validator.message(value) : void 0
|
|
446
|
+
);
|
|
447
|
+
}
|
|
426
448
|
}
|
|
427
449
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
450
|
+
const itemId = params.id || v4();
|
|
451
|
+
if (!params.id && existingConditions) {
|
|
452
|
+
const paramsForExistenceCheck = Object.keys(
|
|
453
|
+
existingConditions
|
|
454
|
+
).filter(
|
|
455
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
456
|
+
);
|
|
457
|
+
const where = and2(
|
|
458
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
459
|
+
const condition = existingConditions[key];
|
|
460
|
+
if (condition) {
|
|
461
|
+
acc.push(condition(params[key]));
|
|
462
|
+
}
|
|
463
|
+
return acc;
|
|
464
|
+
}, [])
|
|
465
|
+
);
|
|
466
|
+
const existing = await repository.findAll({
|
|
467
|
+
limit: 1,
|
|
468
|
+
where
|
|
469
|
+
});
|
|
470
|
+
if (existing.length > 0) {
|
|
471
|
+
throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const values = {
|
|
475
|
+
id: itemId,
|
|
476
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
477
|
+
...params
|
|
478
|
+
};
|
|
479
|
+
const item = await repository.save(values);
|
|
480
|
+
return CREATED(item);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
if (error instanceof Error) {
|
|
483
|
+
throw INTERNAL_SERVER_ERROR(error.message);
|
|
451
484
|
}
|
|
485
|
+
throw error;
|
|
452
486
|
}
|
|
453
|
-
const values = {
|
|
454
|
-
id: itemId,
|
|
455
|
-
userId: injectUserId ? auth.userId : void 0,
|
|
456
|
-
...params
|
|
457
|
-
};
|
|
458
|
-
const item = await repository.save(values);
|
|
459
|
-
return CREATED(item);
|
|
460
487
|
}
|
|
461
488
|
default:
|
|
462
489
|
throw METHOD_NOT_ALLOWED();
|