dn-react-router-toolkit 0.6.2 → 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.
@@ -83,53 +83,65 @@ function apiHandler({
83
83
  switch (request.method) {
84
84
  case "POST":
85
85
  case "PUT": {
86
- const serilaizedParams = await request.json();
87
- const params = deserialize(serilaizedParams);
88
- if (validators) {
89
- const paramsForValidation = Object.keys(validators).filter(
90
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
91
- );
92
- for (const paramKey of paramsForValidation) {
93
- const value = params[paramKey];
94
- const validator = validators[paramKey];
95
- if (validator?.validate && !validator.validate(value)) {
96
- throw (0, import_http.BAD_REQUEST)(
97
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || (0, import_uuid.v4)();
103
- if (!params.id && existingConditions) {
104
- const paramsForExistenceCheck = Object.keys(
105
- existingConditions
106
- ).filter(
107
- (key) => Object.prototype.hasOwnProperty.call(params, key)
108
- );
109
- const where = (0, import_drizzle_orm.and)(
110
- ...paramsForExistenceCheck.reduce((acc, key) => {
111
- const condition = existingConditions[key];
112
- if (condition) {
113
- acc.push(condition(params[key]));
114
- }
115
- return acc;
116
- }, [])
117
- );
118
- const existing = await repository.findAll({
119
- limit: 1,
120
- where
121
- });
122
- if (existing.length > 0) {
123
- throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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
- const serilaizedParams = await request.json();
72
- const params = deserialize(serilaizedParams);
73
- if (validators) {
74
- const paramsForValidation = Object.keys(validators).filter(
75
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
76
- );
77
- for (const paramKey of paramsForValidation) {
78
- const value = params[paramKey];
79
- const validator = validators[paramKey];
80
- if (validator?.validate && !validator.validate(value)) {
81
- throw BAD_REQUEST(
82
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || v4();
88
- if (!params.id && existingConditions) {
89
- const paramsForExistenceCheck = Object.keys(
90
- existingConditions
91
- ).filter(
92
- (key) => Object.prototype.hasOwnProperty.call(params, key)
93
- );
94
- const where = and(
95
- ...paramsForExistenceCheck.reduce((acc, key) => {
96
- const condition = existingConditions[key];
97
- if (condition) {
98
- acc.push(condition(params[key]));
99
- }
100
- return acc;
101
- }, [])
102
- );
103
- const existing = await repository.findAll({
104
- limit: 1,
105
- where
106
- });
107
- if (existing.length > 0) {
108
- throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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,15 +112,28 @@ function CrudFormProvider({
112
112
  children
113
113
  }) {
114
114
  const apiPrefix = `/api${prefix}`;
115
- const store = (0, import_react_store_input.useStore)({
116
- ...item || {},
117
- ...Object.fromEntries(
118
- Object.entries(columns).map(([key, value]) => [
119
- key,
120
- value.defaultValue !== void 0 ? value.defaultValue : void 0
121
- ])
122
- )
123
- });
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
+ }
130
+ return {
131
+ ...acc,
132
+ [key]: value
133
+ };
134
+ }, {});
135
+ };
136
+ const store = (0, import_react_store_input.useStore)(createInitialState());
124
137
  const navigate = (0, import_react_router.useNavigate)();
125
138
  const submit = async () => {
126
139
  const res = await fetch(apiPrefix, {
@@ -76,15 +76,28 @@ function CrudFormProvider({
76
76
  children
77
77
  }) {
78
78
  const apiPrefix = `/api${prefix}`;
79
- const store = useStore({
80
- ...item || {},
81
- ...Object.fromEntries(
82
- Object.entries(columns).map(([key, value]) => [
83
- key,
84
- value.defaultValue !== void 0 ? value.defaultValue : void 0
85
- ])
86
- )
87
- });
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
+ }
94
+ return {
95
+ ...acc,
96
+ [key]: value
97
+ };
98
+ }, {});
99
+ };
100
+ const store = useStore(createInitialState());
88
101
  const navigate = useNavigate();
89
102
  const submit = async () => {
90
103
  const res = await fetch(apiPrefix, {
@@ -144,53 +144,65 @@ function apiHandler({
144
144
  switch (request.method) {
145
145
  case "POST":
146
146
  case "PUT": {
147
- const serilaizedParams = await request.json();
148
- const params = deserialize(serilaizedParams);
149
- if (validators) {
150
- const paramsForValidation = Object.keys(validators).filter(
151
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
152
- );
153
- for (const paramKey of paramsForValidation) {
154
- const value = params[paramKey];
155
- const validator = validators[paramKey];
156
- if (validator?.validate && !validator.validate(value)) {
157
- throw (0, import_http.BAD_REQUEST)(
158
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || (0, import_uuid.v4)();
164
- if (!params.id && existingConditions) {
165
- const paramsForExistenceCheck = Object.keys(
166
- existingConditions
167
- ).filter(
168
- (key) => Object.prototype.hasOwnProperty.call(params, key)
169
- );
170
- const where = (0, import_drizzle_orm2.and)(
171
- ...paramsForExistenceCheck.reduce((acc, key) => {
172
- const condition = existingConditions[key];
173
- if (condition) {
174
- acc.push(condition(params[key]));
175
- }
176
- return acc;
177
- }, [])
178
- );
179
- const existing = await repository.findAll({
180
- limit: 1,
181
- where
182
- });
183
- if (existing.length > 0) {
184
- throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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
- const serilaizedParams = await request.json();
134
- const params = deserialize(serilaizedParams);
135
- if (validators) {
136
- const paramsForValidation = Object.keys(validators).filter(
137
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
138
- );
139
- for (const paramKey of paramsForValidation) {
140
- const value = params[paramKey];
141
- const validator = validators[paramKey];
142
- if (validator?.validate && !validator.validate(value)) {
143
- throw BAD_REQUEST(
144
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || v4();
150
- if (!params.id && existingConditions) {
151
- const paramsForExistenceCheck = Object.keys(
152
- existingConditions
153
- ).filter(
154
- (key) => Object.prototype.hasOwnProperty.call(params, key)
155
- );
156
- const where = and2(
157
- ...paramsForExistenceCheck.reduce((acc, key) => {
158
- const condition = existingConditions[key];
159
- if (condition) {
160
- acc.push(condition(params[key]));
161
- }
162
- return acc;
163
- }, [])
164
- );
165
- const existing = await repository.findAll({
166
- limit: 1,
167
- where
168
- });
169
- if (existing.length > 0) {
170
- throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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();
@@ -113,15 +113,28 @@ function CrudFormProvider({
113
113
  children
114
114
  }) {
115
115
  const apiPrefix = `/api${prefix}`;
116
- const store = (0, import_react_store_input.useStore)({
117
- ...item || {},
118
- ...Object.fromEntries(
119
- Object.entries(columns).map(([key, value]) => [
120
- key,
121
- value.defaultValue !== void 0 ? value.defaultValue : void 0
122
- ])
123
- )
124
- });
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
+ }
131
+ return {
132
+ ...acc,
133
+ [key]: value
134
+ };
135
+ }, {});
136
+ };
137
+ const store = (0, import_react_store_input.useStore)(createInitialState());
125
138
  const navigate = (0, import_react_router.useNavigate)();
126
139
  const submit = async () => {
127
140
  const res = await fetch(apiPrefix, {
@@ -79,15 +79,28 @@ function CrudFormProvider({
79
79
  children
80
80
  }) {
81
81
  const apiPrefix = `/api${prefix}`;
82
- const store = useStore({
83
- ...item || {},
84
- ...Object.fromEntries(
85
- Object.entries(columns).map(([key, value]) => [
86
- key,
87
- value.defaultValue !== void 0 ? value.defaultValue : void 0
88
- ])
89
- )
90
- });
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
+ }
97
+ return {
98
+ ...acc,
99
+ [key]: value
100
+ };
101
+ }, {});
102
+ };
103
+ const store = useStore(createInitialState());
91
104
  const navigate = useNavigate();
92
105
  const submit = async () => {
93
106
  const res = await fetch(apiPrefix, {
@@ -153,15 +153,28 @@ function CrudFormProvider({
153
153
  children
154
154
  }) {
155
155
  const apiPrefix = `/api${prefix2}`;
156
- const store = (0, import_react_store_input.useStore)({
157
- ...item || {},
158
- ...Object.fromEntries(
159
- Object.entries(columns).map(([key, value]) => [
160
- key,
161
- value.defaultValue !== void 0 ? value.defaultValue : void 0
162
- ])
163
- )
164
- });
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
+ }
171
+ return {
172
+ ...acc,
173
+ [key]: value
174
+ };
175
+ }, {});
176
+ };
177
+ const store = (0, import_react_store_input.useStore)(createInitialState());
165
178
  const navigate = (0, import_react_router.useNavigate)();
166
179
  const submit = async () => {
167
180
  const res = await fetch(apiPrefix, {
@@ -441,53 +454,65 @@ function apiHandler({
441
454
  switch (request.method) {
442
455
  case "POST":
443
456
  case "PUT": {
444
- const serilaizedParams = await request.json();
445
- const params = deserialize(serilaizedParams);
446
- if (validators) {
447
- const paramsForValidation = Object.keys(validators).filter(
448
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
449
- );
450
- for (const paramKey of paramsForValidation) {
451
- const value = params[paramKey];
452
- const validator = validators[paramKey];
453
- if (validator?.validate && !validator.validate(value)) {
454
- throw (0, import_http.BAD_REQUEST)(
455
- validator.message ? validator.message(value) : void 0
456
- );
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
+ }
457
477
  }
458
478
  }
459
- }
460
- const itemId = params.id || (0, import_uuid.v4)();
461
- if (!params.id && existingConditions) {
462
- const paramsForExistenceCheck = Object.keys(
463
- existingConditions
464
- ).filter(
465
- (key) => Object.prototype.hasOwnProperty.call(params, key)
466
- );
467
- const where = (0, import_drizzle_orm2.and)(
468
- ...paramsForExistenceCheck.reduce((acc, key) => {
469
- const condition = existingConditions[key];
470
- if (condition) {
471
- acc.push(condition(params[key]));
472
- }
473
- return acc;
474
- }, [])
475
- );
476
- const existing = await repository.findAll({
477
- limit: 1,
478
- where
479
- });
480
- if (existing.length > 0) {
481
- throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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
+ }
482
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);
513
+ }
514
+ throw error;
483
515
  }
484
- const values = {
485
- id: itemId,
486
- userId: injectUserId ? auth.userId : void 0,
487
- ...params
488
- };
489
- const item = await repository.save(values);
490
- return (0, import_http.CREATED)(item);
491
516
  }
492
517
  default:
493
518
  throw (0, import_http.METHOD_NOT_ALLOWED)();
@@ -111,15 +111,28 @@ function CrudFormProvider({
111
111
  children
112
112
  }) {
113
113
  const apiPrefix = `/api${prefix2}`;
114
- const store = useStore({
115
- ...item || {},
116
- ...Object.fromEntries(
117
- Object.entries(columns).map(([key, value]) => [
118
- key,
119
- value.defaultValue !== void 0 ? value.defaultValue : void 0
120
- ])
121
- )
122
- });
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
+ }
129
+ return {
130
+ ...acc,
131
+ [key]: value
132
+ };
133
+ }, {});
134
+ };
135
+ const store = useStore(createInitialState());
123
136
  const navigate = useNavigate();
124
137
  const submit = async () => {
125
138
  const res = await fetch(apiPrefix, {
@@ -385,6 +398,7 @@ import {
385
398
  BAD_REQUEST,
386
399
  CONFLICT,
387
400
  CREATED,
401
+ INTERNAL_SERVER_ERROR,
388
402
  METHOD_NOT_ALLOWED
389
403
  } from "dn-react-toolkit/http";
390
404
  import {
@@ -411,53 +425,65 @@ function apiHandler({
411
425
  switch (request.method) {
412
426
  case "POST":
413
427
  case "PUT": {
414
- const serilaizedParams = await request.json();
415
- const params = deserialize(serilaizedParams);
416
- if (validators) {
417
- const paramsForValidation = Object.keys(validators).filter(
418
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
419
- );
420
- for (const paramKey of paramsForValidation) {
421
- const value = params[paramKey];
422
- const validator = validators[paramKey];
423
- if (validator?.validate && !validator.validate(value)) {
424
- throw BAD_REQUEST(
425
- validator.message ? validator.message(value) : void 0
426
- );
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
+ }
427
448
  }
428
449
  }
429
- }
430
- const itemId = params.id || v4();
431
- if (!params.id && existingConditions) {
432
- const paramsForExistenceCheck = Object.keys(
433
- existingConditions
434
- ).filter(
435
- (key) => Object.prototype.hasOwnProperty.call(params, key)
436
- );
437
- const where = and2(
438
- ...paramsForExistenceCheck.reduce((acc, key) => {
439
- const condition = existingConditions[key];
440
- if (condition) {
441
- acc.push(condition(params[key]));
442
- }
443
- return acc;
444
- }, [])
445
- );
446
- const existing = await repository.findAll({
447
- limit: 1,
448
- where
449
- });
450
- if (existing.length > 0) {
451
- throw CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
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
+ }
452
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);
484
+ }
485
+ throw error;
453
486
  }
454
- const values = {
455
- id: itemId,
456
- userId: injectUserId ? auth.userId : void 0,
457
- ...params
458
- };
459
- const item = await repository.save(values);
460
- return CREATED(item);
461
487
  }
462
488
  default:
463
489
  throw METHOD_NOT_ALLOWED();
package/package.json CHANGED
@@ -1,101 +1,101 @@
1
1
  {
2
- "name": "dn-react-router-toolkit",
3
- "version": "0.6.2",
4
- "types": "./dist/index.d.ts",
5
- "main": "./dist/index.mjs",
6
- "module": "./dist/index.js",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.ts",
10
- "import": "./dist/index.mjs",
11
- "require": "./dist/index.js"
2
+ "name": "dn-react-router-toolkit",
3
+ "version": "0.6.4",
4
+ "types": "./dist/index.d.ts",
5
+ "main": "./dist/index.mjs",
6
+ "module": "./dist/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ },
13
+ "./auth": {
14
+ "types": "./dist/auth/index.d.ts",
15
+ "import": "./dist/auth/index.mjs",
16
+ "require": "./dist/auth/index.js"
17
+ },
18
+ "./api": {
19
+ "types": "./dist/api/index.d.ts",
20
+ "import": "./dist/api/index.mjs",
21
+ "require": "./dist/api/index.js"
22
+ },
23
+ "./client": {
24
+ "types": "./dist/client/index.d.ts",
25
+ "import": "./dist/client/index.mjs",
26
+ "require": "./dist/client/index.js"
27
+ },
28
+ "./seo": {
29
+ "types": "./dist/seo/index.d.ts",
30
+ "import": "./dist/seo/index.mjs",
31
+ "require": "./dist/seo/index.js"
32
+ },
33
+ "./db": {
34
+ "types": "./dist/db/index.d.ts",
35
+ "import": "./dist/db/index.mjs",
36
+ "require": "./dist/db/index.js"
37
+ },
38
+ "./db/backup": {
39
+ "types": "./dist/db/backup/index.d.ts",
40
+ "import": "./dist/db/backup/index.mjs",
41
+ "require": "./dist/db/backup/index.js"
42
+ },
43
+ "./table": {
44
+ "types": "./dist/table/index.d.ts",
45
+ "import": "./dist/table/index.mjs",
46
+ "require": "./dist/table/index.js"
47
+ },
48
+ "./crud": {
49
+ "types": "./dist/crud/index.d.ts",
50
+ "import": "./dist/crud/index.mjs",
51
+ "require": "./dist/crud/index.js"
52
+ },
53
+ "./post": {
54
+ "types": "./dist/post/index.d.ts",
55
+ "import": "./dist/post/index.mjs",
56
+ "require": "./dist/post/index.js"
57
+ },
58
+ "./form": {
59
+ "types": "./dist/form/index.d.ts",
60
+ "import": "./dist/form/index.mjs",
61
+ "require": "./dist/form/index.js"
62
+ }
12
63
  },
13
- "./auth": {
14
- "types": "./dist/auth/index.d.ts",
15
- "import": "./dist/auth/index.mjs",
16
- "require": "./dist/auth/index.js"
64
+ "scripts": {
65
+ "build": "tsup",
66
+ "dev": "tsup --watch"
17
67
  },
18
- "./api": {
19
- "types": "./dist/api/index.d.ts",
20
- "import": "./dist/api/index.mjs",
21
- "require": "./dist/api/index.js"
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/dndnsoft/dn-react-router-toolkit.git"
22
71
  },
23
- "./client": {
24
- "types": "./dist/client/index.d.ts",
25
- "import": "./dist/client/index.mjs",
26
- "require": "./dist/client/index.js"
72
+ "author": "",
73
+ "license": "MIT",
74
+ "bugs": {
75
+ "url": "https://github.com/dndnsoft/dn-react-router-toolkit/issues"
27
76
  },
28
- "./seo": {
29
- "types": "./dist/seo/index.d.ts",
30
- "import": "./dist/seo/index.mjs",
31
- "require": "./dist/seo/index.js"
77
+ "homepage": "https://github.com/dndnsoft/dn-react-router-toolkit#readme",
78
+ "description": "",
79
+ "devDependencies": {
80
+ "@types/node": "^24.10.1",
81
+ "@types/react": "^19",
82
+ "@types/react-dom": "^19",
83
+ "schema-dts": "^1.1.5",
84
+ "tsup": "^8.5.1",
85
+ "typescript": "^5.7.3"
32
86
  },
33
- "./db": {
34
- "types": "./dist/db/index.d.ts",
35
- "import": "./dist/db/index.mjs",
36
- "require": "./dist/db/index.js"
87
+ "dependencies": {
88
+ "@react-router/dev": "^7.11.0",
89
+ "dn-react-text-editor": "^0.2.4",
90
+ "dn-react-toolkit": "^0.2.49",
91
+ "pg": "^8.16.3",
92
+ "react-store-input": "^0.1.7",
93
+ "uuid": "^13.0.0"
37
94
  },
38
- "./db/backup": {
39
- "types": "./dist/db/backup/index.d.ts",
40
- "import": "./dist/db/backup/index.mjs",
41
- "require": "./dist/db/backup/index.js"
42
- },
43
- "./table": {
44
- "types": "./dist/table/index.d.ts",
45
- "import": "./dist/table/index.mjs",
46
- "require": "./dist/table/index.js"
47
- },
48
- "./crud": {
49
- "types": "./dist/crud/index.d.ts",
50
- "import": "./dist/crud/index.mjs",
51
- "require": "./dist/crud/index.js"
52
- },
53
- "./post": {
54
- "types": "./dist/post/index.d.ts",
55
- "import": "./dist/post/index.mjs",
56
- "require": "./dist/post/index.js"
57
- },
58
- "./form": {
59
- "types": "./dist/form/index.d.ts",
60
- "import": "./dist/form/index.mjs",
61
- "require": "./dist/form/index.js"
95
+ "peerDependencies": {
96
+ "drizzle-orm": "^0.45.1",
97
+ "react": "^19",
98
+ "react-dom": "^19",
99
+ "react-router": "^7"
62
100
  }
63
- },
64
- "scripts": {
65
- "build": "tsup",
66
- "dev": "tsup --watch"
67
- },
68
- "repository": {
69
- "type": "git",
70
- "url": "git+https://github.com/dndnsoft/dn-react-router-toolkit.git"
71
- },
72
- "author": "",
73
- "license": "MIT",
74
- "bugs": {
75
- "url": "https://github.com/dndnsoft/dn-react-router-toolkit/issues"
76
- },
77
- "homepage": "https://github.com/dndnsoft/dn-react-router-toolkit#readme",
78
- "description": "",
79
- "devDependencies": {
80
- "@types/node": "^24.10.1",
81
- "@types/react": "^19",
82
- "@types/react-dom": "^19",
83
- "schema-dts": "^1.1.5",
84
- "tsup": "^8.5.1",
85
- "typescript": "^5.7.3"
86
- },
87
- "dependencies": {
88
- "@react-router/dev": "^7.11.0",
89
- "dn-react-text-editor": "^0.2.3",
90
- "dn-react-toolkit": "^0.2.49",
91
- "pg": "^8.16.3",
92
- "react-store-input": "^0.1.7",
93
- "uuid": "^13.0.0"
94
- },
95
- "peerDependencies": {
96
- "drizzle-orm": "^0.45.1",
97
- "react": "^19",
98
- "react-dom": "^19",
99
- "react-router": "^7"
100
- }
101
- }
101
+ }