dn-react-router-toolkit 0.6.3 → 0.6.5

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();
@@ -149,7 +149,17 @@ function CrudForm({
149
149
  AdminHeader,
150
150
  {
151
151
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
152
- actions: /* @__PURE__ */ import_react8.default.createElement(
152
+ actions: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null, form.item && /* @__PURE__ */ import_react8.default.createElement(
153
+ "button",
154
+ {
155
+ type: "button",
156
+ className: "button-outline",
157
+ onClick: () => {
158
+ form.delete();
159
+ }
160
+ },
161
+ "\uC0AD\uC81C\uD558\uAE30"
162
+ ), /* @__PURE__ */ import_react8.default.createElement(
153
163
  "button",
154
164
  {
155
165
  type: "button",
@@ -157,7 +167,7 @@ function CrudForm({
157
167
  onClick: form.submit
158
168
  },
159
169
  "\uC800\uC7A5\uD558\uAE30"
160
- )
170
+ ))
161
171
  }
162
172
  ), /* @__PURE__ */ import_react8.default.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null, Object.entries(form.columns).map(
163
173
  ([name, value]) => {
@@ -113,7 +113,17 @@ function CrudForm({
113
113
  AdminHeader,
114
114
  {
115
115
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
116
- actions: /* @__PURE__ */ React6.createElement(
116
+ actions: /* @__PURE__ */ React6.createElement(React6.Fragment, null, form.item && /* @__PURE__ */ React6.createElement(
117
+ "button",
118
+ {
119
+ type: "button",
120
+ className: "button-outline",
121
+ onClick: () => {
122
+ form.delete();
123
+ }
124
+ },
125
+ "\uC0AD\uC81C\uD558\uAE30"
126
+ ), /* @__PURE__ */ React6.createElement(
117
127
  "button",
118
128
  {
119
129
  type: "button",
@@ -121,7 +131,7 @@ function CrudForm({
121
131
  onClick: form.submit
122
132
  },
123
133
  "\uC800\uC7A5\uD558\uAE30"
124
- )
134
+ ))
125
135
  }
126
136
  ), /* @__PURE__ */ React6.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ React6.createElement(React6.Fragment, null, Object.entries(form.columns).map(
127
137
  ([name, value]) => {
@@ -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, {
@@ -676,7 +690,17 @@ function CrudForm({
676
690
  AdminHeader,
677
691
  {
678
692
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
679
- actions: /* @__PURE__ */ import_react13.default.createElement(
693
+ actions: /* @__PURE__ */ import_react13.default.createElement(import_react13.default.Fragment, null, form.item && /* @__PURE__ */ import_react13.default.createElement(
694
+ "button",
695
+ {
696
+ type: "button",
697
+ className: "button-outline",
698
+ onClick: () => {
699
+ form.delete();
700
+ }
701
+ },
702
+ "\uC0AD\uC81C\uD558\uAE30"
703
+ ), /* @__PURE__ */ import_react13.default.createElement(
680
704
  "button",
681
705
  {
682
706
  type: "button",
@@ -684,7 +708,7 @@ function CrudForm({
684
708
  onClick: form.submit
685
709
  },
686
710
  "\uC800\uC7A5\uD558\uAE30"
687
- )
711
+ ))
688
712
  }
689
713
  ), /* @__PURE__ */ import_react13.default.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ import_react13.default.createElement(import_react13.default.Fragment, null, Object.entries(form.columns).map(
690
714
  ([name, value]) => {
@@ -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, {
@@ -648,7 +662,17 @@ function CrudForm({
648
662
  AdminHeader,
649
663
  {
650
664
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
651
- actions: /* @__PURE__ */ React11.createElement(
665
+ actions: /* @__PURE__ */ React11.createElement(React11.Fragment, null, form.item && /* @__PURE__ */ React11.createElement(
666
+ "button",
667
+ {
668
+ type: "button",
669
+ className: "button-outline",
670
+ onClick: () => {
671
+ form.delete();
672
+ }
673
+ },
674
+ "\uC0AD\uC81C\uD558\uAE30"
675
+ ), /* @__PURE__ */ React11.createElement(
652
676
  "button",
653
677
  {
654
678
  type: "button",
@@ -656,7 +680,7 @@ function CrudForm({
656
680
  onClick: form.submit
657
681
  },
658
682
  "\uC800\uC7A5\uD558\uAE30"
659
- )
683
+ ))
660
684
  }
661
685
  ), /* @__PURE__ */ React11.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ React11.createElement(React11.Fragment, null, Object.entries(form.columns).map(
662
686
  ([name, value]) => {
@@ -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, {
@@ -312,7 +326,17 @@ function CrudForm({
312
326
  AdminHeader,
313
327
  {
314
328
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
315
- actions: /* @__PURE__ */ import_react8.default.createElement(
329
+ actions: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null, form.item && /* @__PURE__ */ import_react8.default.createElement(
330
+ "button",
331
+ {
332
+ type: "button",
333
+ className: "button-outline",
334
+ onClick: () => {
335
+ form.delete();
336
+ }
337
+ },
338
+ "\uC0AD\uC81C\uD558\uAE30"
339
+ ), /* @__PURE__ */ import_react8.default.createElement(
316
340
  "button",
317
341
  {
318
342
  type: "button",
@@ -320,7 +344,7 @@ function CrudForm({
320
344
  onClick: form.submit
321
345
  },
322
346
  "\uC800\uC7A5\uD558\uAE30"
323
- )
347
+ ))
324
348
  }
325
349
  ), /* @__PURE__ */ import_react8.default.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null, Object.entries(form.columns).map(
326
350
  ([name, value]) => {
@@ -440,53 +464,65 @@ function apiHandler({
440
464
  switch (request.method) {
441
465
  case "POST":
442
466
  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
- );
467
+ try {
468
+ const serilaizedParams = await request.json();
469
+ const params = deserialize(serilaizedParams);
470
+ if (validators) {
471
+ const paramsForValidation = Object.keys(
472
+ validators
473
+ ).filter(
474
+ (key) => Object.prototype.hasOwnProperty.call(
475
+ validators,
476
+ key
477
+ )
478
+ );
479
+ for (const paramKey of paramsForValidation) {
480
+ const value = params[paramKey];
481
+ const validator = validators[paramKey];
482
+ if (validator?.validate && !validator.validate(value)) {
483
+ throw (0, import_http.BAD_REQUEST)(
484
+ validator.message ? validator.message(value) : void 0
485
+ );
486
+ }
456
487
  }
457
488
  }
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.");
489
+ const itemId = params.id || (0, import_uuid.v4)();
490
+ if (!params.id && existingConditions) {
491
+ const paramsForExistenceCheck = Object.keys(
492
+ existingConditions
493
+ ).filter(
494
+ (key) => Object.prototype.hasOwnProperty.call(params, key)
495
+ );
496
+ const where = (0, import_drizzle_orm2.and)(
497
+ ...paramsForExistenceCheck.reduce((acc, key) => {
498
+ const condition = existingConditions[key];
499
+ if (condition) {
500
+ acc.push(condition(params[key]));
501
+ }
502
+ return acc;
503
+ }, [])
504
+ );
505
+ const existing = await repository.findAll({
506
+ limit: 1,
507
+ where
508
+ });
509
+ if (existing.length > 0) {
510
+ throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
511
+ }
481
512
  }
513
+ const values = {
514
+ id: itemId,
515
+ userId: injectUserId ? auth.userId : void 0,
516
+ ...params
517
+ };
518
+ const item = await repository.save(values);
519
+ return (0, import_http.CREATED)(item);
520
+ } catch (error) {
521
+ if (error instanceof Error) {
522
+ throw (0, import_http.INTERNAL_SERVER_ERROR)(error.message);
523
+ }
524
+ throw error;
482
525
  }
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
526
  }
491
527
  default:
492
528
  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, {
@@ -270,7 +284,17 @@ function CrudForm({
270
284
  AdminHeader,
271
285
  {
272
286
  title: `${form.name} ${form.item ? "\uC218\uC815" : "\uCD94\uAC00"}`,
273
- actions: /* @__PURE__ */ React6.createElement(
287
+ actions: /* @__PURE__ */ React6.createElement(React6.Fragment, null, form.item && /* @__PURE__ */ React6.createElement(
288
+ "button",
289
+ {
290
+ type: "button",
291
+ className: "button-outline",
292
+ onClick: () => {
293
+ form.delete();
294
+ }
295
+ },
296
+ "\uC0AD\uC81C\uD558\uAE30"
297
+ ), /* @__PURE__ */ React6.createElement(
274
298
  "button",
275
299
  {
276
300
  type: "button",
@@ -278,7 +302,7 @@ function CrudForm({
278
302
  onClick: form.submit
279
303
  },
280
304
  "\uC800\uC7A5\uD558\uAE30"
281
- )
305
+ ))
282
306
  }
283
307
  ), /* @__PURE__ */ React6.createElement("div", { className: "max-w-2xl mx-auto" }, Object.keys(form.columns).length > 0 && /* @__PURE__ */ React6.createElement(React6.Fragment, null, Object.entries(form.columns).map(
284
308
  ([name, value]) => {
@@ -384,6 +408,7 @@ import {
384
408
  BAD_REQUEST,
385
409
  CONFLICT,
386
410
  CREATED,
411
+ INTERNAL_SERVER_ERROR,
387
412
  METHOD_NOT_ALLOWED
388
413
  } from "dn-react-toolkit/http";
389
414
  import {
@@ -410,53 +435,65 @@ function apiHandler({
410
435
  switch (request.method) {
411
436
  case "POST":
412
437
  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
- );
438
+ try {
439
+ const serilaizedParams = await request.json();
440
+ const params = deserialize(serilaizedParams);
441
+ if (validators) {
442
+ const paramsForValidation = Object.keys(
443
+ validators
444
+ ).filter(
445
+ (key) => Object.prototype.hasOwnProperty.call(
446
+ validators,
447
+ key
448
+ )
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 BAD_REQUEST(
455
+ validator.message ? validator.message(value) : void 0
456
+ );
457
+ }
426
458
  }
427
459
  }
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.");
460
+ const itemId = params.id || 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 = and2(
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 CONFLICT("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
482
+ }
451
483
  }
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 CREATED(item);
491
+ } catch (error) {
492
+ if (error instanceof Error) {
493
+ throw INTERNAL_SERVER_ERROR(error.message);
494
+ }
495
+ throw error;
452
496
  }
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
497
  }
461
498
  default:
462
499
  throw METHOD_NOT_ALLOWED();
package/package.json CHANGED
@@ -1,101 +1,101 @@
1
1
  {
2
- "name": "dn-react-router-toolkit",
3
- "version": "0.6.3",
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
- }
2
+ "name": "dn-react-router-toolkit",
3
+ "version": "0.6.5",
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"
63
12
  },
64
- "scripts": {
65
- "build": "tsup",
66
- "dev": "tsup --watch"
13
+ "./auth": {
14
+ "types": "./dist/auth/index.d.ts",
15
+ "import": "./dist/auth/index.mjs",
16
+ "require": "./dist/auth/index.js"
67
17
  },
68
- "repository": {
69
- "type": "git",
70
- "url": "git+https://github.com/dndnsoft/dn-react-router-toolkit.git"
18
+ "./api": {
19
+ "types": "./dist/api/index.d.ts",
20
+ "import": "./dist/api/index.mjs",
21
+ "require": "./dist/api/index.js"
71
22
  },
72
- "author": "",
73
- "license": "MIT",
74
- "bugs": {
75
- "url": "https://github.com/dndnsoft/dn-react-router-toolkit/issues"
23
+ "./client": {
24
+ "types": "./dist/client/index.d.ts",
25
+ "import": "./dist/client/index.mjs",
26
+ "require": "./dist/client/index.js"
76
27
  },
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"
28
+ "./seo": {
29
+ "types": "./dist/seo/index.d.ts",
30
+ "import": "./dist/seo/index.mjs",
31
+ "require": "./dist/seo/index.js"
86
32
  },
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"
33
+ "./db": {
34
+ "types": "./dist/db/index.d.ts",
35
+ "import": "./dist/db/index.mjs",
36
+ "require": "./dist/db/index.js"
94
37
  },
95
- "peerDependencies": {
96
- "drizzle-orm": "^0.45.1",
97
- "react": "^19",
98
- "react-dom": "^19",
99
- "react-router": "^7"
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"
100
62
  }
101
- }
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.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"
94
+ },
95
+ "peerDependencies": {
96
+ "drizzle-orm": "^0.45.1",
97
+ "react": "^19",
98
+ "react-dom": "^19",
99
+ "react-router": "^7"
100
+ }
101
+ }