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.
@@ -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,14 +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
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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 store = useStore({
80
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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, {
@@ -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,14 +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
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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, {
@@ -79,14 +79,28 @@ function CrudFormProvider({
79
79
  children
80
80
  }) {
81
81
  const apiPrefix = `/api${prefix}`;
82
- const store = useStore({
83
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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, {
@@ -153,14 +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
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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
- const serilaizedParams = await request.json();
444
- const params = deserialize(serilaizedParams);
445
- if (validators) {
446
- const paramsForValidation = Object.keys(validators).filter(
447
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
448
- );
449
- for (const paramKey of paramsForValidation) {
450
- const value = params[paramKey];
451
- const validator = validators[paramKey];
452
- if (validator?.validate && !validator.validate(value)) {
453
- throw (0, import_http.BAD_REQUEST)(
454
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || (0, import_uuid.v4)();
460
- if (!params.id && existingConditions) {
461
- const paramsForExistenceCheck = Object.keys(
462
- existingConditions
463
- ).filter(
464
- (key) => Object.prototype.hasOwnProperty.call(params, key)
465
- );
466
- const where = (0, import_drizzle_orm2.and)(
467
- ...paramsForExistenceCheck.reduce((acc, key) => {
468
- const condition = existingConditions[key];
469
- if (condition) {
470
- acc.push(condition(params[key]));
471
- }
472
- return acc;
473
- }, [])
474
- );
475
- const existing = await repository.findAll({
476
- limit: 1,
477
- where
478
- });
479
- if (existing.length > 0) {
480
- 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
+ }
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)();
@@ -111,14 +111,28 @@ function CrudFormProvider({
111
111
  children
112
112
  }) {
113
113
  const apiPrefix = `/api${prefix2}`;
114
- const store = useStore({
115
- ...Object.entries(item || {}).reduce((acc, [key, value]) => {
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 ?? columns[key]?.defaultValue
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
- const serilaizedParams = await request.json();
414
- const params = deserialize(serilaizedParams);
415
- if (validators) {
416
- const paramsForValidation = Object.keys(validators).filter(
417
- (key) => Object.prototype.hasOwnProperty.call(validators, key)
418
- );
419
- for (const paramKey of paramsForValidation) {
420
- const value = params[paramKey];
421
- const validator = validators[paramKey];
422
- if (validator?.validate && !validator.validate(value)) {
423
- throw BAD_REQUEST(
424
- validator.message ? validator.message(value) : void 0
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
- const itemId = params.id || v4();
430
- if (!params.id && existingConditions) {
431
- const paramsForExistenceCheck = Object.keys(
432
- existingConditions
433
- ).filter(
434
- (key) => Object.prototype.hasOwnProperty.call(params, key)
435
- );
436
- const where = and2(
437
- ...paramsForExistenceCheck.reduce((acc, key) => {
438
- const condition = existingConditions[key];
439
- if (condition) {
440
- acc.push(condition(params[key]));
441
- }
442
- return acc;
443
- }, [])
444
- );
445
- const existing = await repository.findAll({
446
- limit: 1,
447
- where
448
- });
449
- if (existing.length > 0) {
450
- 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
+ }
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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dn-react-router-toolkit",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "types": "./dist/index.d.ts",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.js",