dn-react-router-toolkit 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/{create_handler.d.mts → default_api_handler.d.mts} +3 -3
- package/dist/api/{create_handler.d.ts → default_api_handler.d.ts} +3 -3
- package/dist/api/{create_handler.js → default_api_handler.js} +8 -8
- package/dist/api/{create_handler.mjs → default_api_handler.mjs} +4 -4
- package/dist/api/index.d.mts +5 -5
- package/dist/api/index.d.ts +5 -5
- package/dist/api/index.js +186 -116
- package/dist/api/index.mjs +192 -126
- package/dist/api/put_resource_handler.d.mts +25 -0
- package/dist/api/put_resource_handler.d.ts +25 -0
- package/dist/api/put_resource_handler.js +214 -0
- package/dist/api/put_resource_handler.mjs +194 -0
- package/dist/api/resource_handler.d.mts +26 -0
- package/dist/api/resource_handler.d.ts +26 -0
- package/dist/api/resource_handler.js +280 -0
- package/dist/api/resource_handler.mjs +265 -0
- package/package.json +5 -5
- package/dist/api/create_api_handler.d.mts +0 -30
- package/dist/api/create_api_handler.d.ts +0 -30
- package/dist/api/create_api_handler.js +0 -158
- package/dist/api/create_api_handler.mjs +0 -141
- package/dist/api/item_api_handler.d.mts +0 -22
- package/dist/api/item_api_handler.d.ts +0 -22
- package/dist/api/item_api_handler.js +0 -79
- package/dist/api/item_api_handler.mjs +0 -60
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// src/api/resource_handler.ts
|
|
2
|
+
import {
|
|
3
|
+
httpForbidden as httpForbidden2,
|
|
4
|
+
httpMethodNotAllowed,
|
|
5
|
+
httpNoContent,
|
|
6
|
+
httpNotFound as httpNotFound2
|
|
7
|
+
} from "gw-response";
|
|
8
|
+
import { ok } from "gw-result";
|
|
9
|
+
|
|
10
|
+
// src/api/put_resource_handler.ts
|
|
11
|
+
import {
|
|
12
|
+
httpBadRequest,
|
|
13
|
+
httpConflict,
|
|
14
|
+
httpCreated,
|
|
15
|
+
httpForbidden,
|
|
16
|
+
httpNotFound
|
|
17
|
+
} from "gw-response";
|
|
18
|
+
|
|
19
|
+
// src/crud/crud_form_provider.tsx
|
|
20
|
+
import { useNavigate } from "react-router";
|
|
21
|
+
import { useStore } from "react-store-input";
|
|
22
|
+
import {
|
|
23
|
+
createContext,
|
|
24
|
+
useContext
|
|
25
|
+
} from "react";
|
|
26
|
+
|
|
27
|
+
// src/crud/serialize.ts
|
|
28
|
+
function deserialize(data) {
|
|
29
|
+
if (data === void 0) {
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
|
|
33
|
+
const { type, value } = data;
|
|
34
|
+
switch (type) {
|
|
35
|
+
case "null":
|
|
36
|
+
return null;
|
|
37
|
+
case "string":
|
|
38
|
+
return value;
|
|
39
|
+
case "number":
|
|
40
|
+
return value;
|
|
41
|
+
case "boolean":
|
|
42
|
+
return value;
|
|
43
|
+
case "date":
|
|
44
|
+
return new Date(value);
|
|
45
|
+
case "array":
|
|
46
|
+
return value.map((item) => deserialize(item));
|
|
47
|
+
case "object":
|
|
48
|
+
return Object.entries(value).reduce(
|
|
49
|
+
(acc, [key, value2]) => {
|
|
50
|
+
return {
|
|
51
|
+
...acc,
|
|
52
|
+
[key]: deserialize(value2)
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
{}
|
|
56
|
+
);
|
|
57
|
+
default:
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return void 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/crud/crud_form_provider.tsx
|
|
65
|
+
import { jsx } from "react/jsx-runtime";
|
|
66
|
+
var FormContext = createContext({});
|
|
67
|
+
|
|
68
|
+
// src/form/create_form_component.tsx
|
|
69
|
+
import "react";
|
|
70
|
+
|
|
71
|
+
// src/utils/cn.ts
|
|
72
|
+
function cn(...classes) {
|
|
73
|
+
return classes.filter(Boolean).join(" ").trim();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/utils/date.ts
|
|
77
|
+
import moment from "moment-timezone";
|
|
78
|
+
|
|
79
|
+
// src/form/create_form_component.tsx
|
|
80
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
81
|
+
function createComponent(tag, options) {
|
|
82
|
+
return function FormComponent({ className, ...props }) {
|
|
83
|
+
const Tag = tag;
|
|
84
|
+
return /* @__PURE__ */ jsx2(Tag, { ...props, className: cn(options.className, className) });
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/form/form_components.tsx
|
|
89
|
+
var FormEntry = createComponent("div", {
|
|
90
|
+
className: "flex-1"
|
|
91
|
+
});
|
|
92
|
+
var FormRow = createComponent("div", {
|
|
93
|
+
className: "flex-1 flex gap-4 mb-6"
|
|
94
|
+
});
|
|
95
|
+
var FormLabel = createComponent("label", {
|
|
96
|
+
className: "flex-1 font-semibold mb-2.5 block"
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// src/crud/crud_form.tsx
|
|
100
|
+
import { useStoreComponent } from "react-store-input";
|
|
101
|
+
|
|
102
|
+
// src/client/env_loader.tsx
|
|
103
|
+
import { useRouteLoaderData } from "react-router";
|
|
104
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
105
|
+
|
|
106
|
+
// src/client/file_input.tsx
|
|
107
|
+
import {
|
|
108
|
+
useRef
|
|
109
|
+
} from "react";
|
|
110
|
+
import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
|
|
111
|
+
|
|
112
|
+
// src/client/use_user_agent.tsx
|
|
113
|
+
import { useRouteLoaderData as useRouteLoaderData2 } from "react-router";
|
|
114
|
+
|
|
115
|
+
// src/client/store_text_editor.tsx
|
|
116
|
+
import {
|
|
117
|
+
TextEditor
|
|
118
|
+
} from "dn-react-text-editor";
|
|
119
|
+
import { useStoreController } from "react-store-input";
|
|
120
|
+
import { useImperativeHandle, useRef as useRef2 } from "react";
|
|
121
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
122
|
+
|
|
123
|
+
// src/client/editor.tsx
|
|
124
|
+
import { generateMetadata } from "gw-file/client";
|
|
125
|
+
|
|
126
|
+
// src/crud/crud_form.tsx
|
|
127
|
+
import { Fragment as Fragment2, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
128
|
+
|
|
129
|
+
// src/api/put_resource_handler.ts
|
|
130
|
+
import {
|
|
131
|
+
and
|
|
132
|
+
} from "drizzle-orm";
|
|
133
|
+
import { v4 } from "uuid";
|
|
134
|
+
function putResourceHandler({
|
|
135
|
+
repository,
|
|
136
|
+
validators,
|
|
137
|
+
existingConditions,
|
|
138
|
+
injectUserId,
|
|
139
|
+
isOwnedBy
|
|
140
|
+
}) {
|
|
141
|
+
return async (auth, request) => {
|
|
142
|
+
const serilaizedParams = await request.json();
|
|
143
|
+
const params = deserialize(serilaizedParams) || {};
|
|
144
|
+
const itemId = params.id || v4();
|
|
145
|
+
if (params.id) {
|
|
146
|
+
const existing = await repository.find(itemId);
|
|
147
|
+
if (!existing) {
|
|
148
|
+
return httpNotFound();
|
|
149
|
+
}
|
|
150
|
+
if (isOwnedBy && (!auth || !isOwnedBy(auth, existing))) {
|
|
151
|
+
return httpForbidden();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (validators) {
|
|
155
|
+
const paramsForValidation = Object.keys(validators).filter(
|
|
156
|
+
(key) => Object.prototype.hasOwnProperty.call(validators, key)
|
|
157
|
+
);
|
|
158
|
+
for (const paramKey of paramsForValidation) {
|
|
159
|
+
const value = params[paramKey];
|
|
160
|
+
const validator = validators[paramKey];
|
|
161
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
162
|
+
return httpBadRequest({
|
|
163
|
+
code: "BAD_REQUEST",
|
|
164
|
+
message: validator.message ? validator.message(value) : "\uC798\uBABB\uB41C \uC694\uCCAD\uC785\uB2C8\uB2E4."
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (!params.id && existingConditions) {
|
|
170
|
+
const paramsForExistenceCheck = Object.keys(existingConditions).filter(
|
|
171
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
172
|
+
);
|
|
173
|
+
if (paramsForExistenceCheck.length > 0) {
|
|
174
|
+
const where = and(
|
|
175
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
176
|
+
const condition = existingConditions[key];
|
|
177
|
+
if (condition) {
|
|
178
|
+
acc.push(condition(params[key]));
|
|
179
|
+
}
|
|
180
|
+
return acc;
|
|
181
|
+
}, [])
|
|
182
|
+
);
|
|
183
|
+
const existing = await repository.findAll({
|
|
184
|
+
limit: 1,
|
|
185
|
+
where
|
|
186
|
+
});
|
|
187
|
+
if (existing.length > 0) {
|
|
188
|
+
return httpConflict();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const values = {
|
|
193
|
+
id: itemId,
|
|
194
|
+
userId: injectUserId ? auth?.userId : void 0,
|
|
195
|
+
...params
|
|
196
|
+
};
|
|
197
|
+
const item = await repository.save(values);
|
|
198
|
+
return httpCreated(item);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/api/resource_handler.ts
|
|
203
|
+
function resourceHandler({
|
|
204
|
+
withAuthAction,
|
|
205
|
+
repository,
|
|
206
|
+
validators,
|
|
207
|
+
existingConditions,
|
|
208
|
+
injectUserId,
|
|
209
|
+
roles,
|
|
210
|
+
isOwnedBy
|
|
211
|
+
}) {
|
|
212
|
+
const loader = async ({ request }) => {
|
|
213
|
+
return ok({});
|
|
214
|
+
};
|
|
215
|
+
const action = withAuthAction((auth) => async ({ request, params }) => {
|
|
216
|
+
if (roles && roles.length > 0 && (!auth || !roles.includes(auth.role))) {
|
|
217
|
+
return httpForbidden2({
|
|
218
|
+
code: "FORBIDDEN",
|
|
219
|
+
message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const itemId = params.itemId;
|
|
223
|
+
if (itemId) {
|
|
224
|
+
const existing = await repository.find(itemId);
|
|
225
|
+
if (!existing) {
|
|
226
|
+
return httpNotFound2();
|
|
227
|
+
}
|
|
228
|
+
if (isOwnedBy && (!auth || !isOwnedBy(auth, existing))) {
|
|
229
|
+
return httpForbidden2();
|
|
230
|
+
}
|
|
231
|
+
switch (request.method) {
|
|
232
|
+
case "DELETE": {
|
|
233
|
+
await repository.delete(itemId);
|
|
234
|
+
return httpNoContent();
|
|
235
|
+
}
|
|
236
|
+
default: {
|
|
237
|
+
return httpMethodNotAllowed();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
switch (request.method) {
|
|
242
|
+
case "POST":
|
|
243
|
+
case "PUT": {
|
|
244
|
+
const handler = putResourceHandler({
|
|
245
|
+
repository,
|
|
246
|
+
validators,
|
|
247
|
+
existingConditions,
|
|
248
|
+
injectUserId,
|
|
249
|
+
isOwnedBy
|
|
250
|
+
});
|
|
251
|
+
return handler(auth, request);
|
|
252
|
+
}
|
|
253
|
+
default: {
|
|
254
|
+
return httpMethodNotAllowed();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
return {
|
|
259
|
+
loader,
|
|
260
|
+
action
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
export {
|
|
264
|
+
resourceHandler
|
|
265
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dn-react-router-toolkit",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"types": "./dist/index.d.ts",
|
|
5
5
|
"main": "./dist/index.mjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -87,15 +87,15 @@
|
|
|
87
87
|
},
|
|
88
88
|
"dependencies": {
|
|
89
89
|
"cookie": "^1.1.1",
|
|
90
|
-
"dn-react-text-editor": "^0.3.
|
|
91
|
-
"gw-auth": "^0.1.
|
|
90
|
+
"dn-react-text-editor": "^0.3.11",
|
|
91
|
+
"gw-auth": "^0.1.2",
|
|
92
92
|
"gw-file": "^0.1.1",
|
|
93
|
-
"gw-response": "^0.1.
|
|
93
|
+
"gw-response": "^0.1.8",
|
|
94
94
|
"gw-result": "^0.1.7",
|
|
95
95
|
"moment-timezone": "^0.6.1",
|
|
96
96
|
"pg": "^8.19.0",
|
|
97
97
|
"react-icons": "^5.5.0",
|
|
98
|
-
"react-store-input": "^0.2.
|
|
98
|
+
"react-store-input": "^0.2.4",
|
|
99
99
|
"uuid": "^13.0.0"
|
|
100
100
|
},
|
|
101
101
|
"peerDependencies": {
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { InferSelectModel, SQLWrapper } from 'drizzle-orm';
|
|
2
|
-
import { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
|
|
3
|
-
import { TableRepository } from '../table/repository.mjs';
|
|
4
|
-
import { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
|
5
|
-
import { WithAuthHandler } from '../auth/with_auth.mjs';
|
|
6
|
-
import 'drizzle-orm/node-postgres';
|
|
7
|
-
import 'gw-auth';
|
|
8
|
-
import 'gw-auth/server';
|
|
9
|
-
|
|
10
|
-
type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
|
|
11
|
-
withAuthAction: WithAuthHandler<ActionFunctionArgs>;
|
|
12
|
-
repository: TableRepository<T, TSelect>;
|
|
13
|
-
validators?: {
|
|
14
|
-
[K in keyof InferSelectModel<T>]?: {
|
|
15
|
-
validate?: (value?: InferSelectModel<T>[K]) => boolean;
|
|
16
|
-
message?: (value?: InferSelectModel<T>[K]) => string;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
existingConditions?: {
|
|
20
|
-
[K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
|
|
21
|
-
};
|
|
22
|
-
injectUserId?: boolean;
|
|
23
|
-
roles?: string[];
|
|
24
|
-
};
|
|
25
|
-
declare function apiHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, }: APIHandlerOptions<T, TSelect>): {
|
|
26
|
-
loader: ({ request }: LoaderFunctionArgs) => Promise<{}>;
|
|
27
|
-
action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export { type APIHandlerOptions, apiHandler };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { InferSelectModel, SQLWrapper } from 'drizzle-orm';
|
|
2
|
-
import { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
|
|
3
|
-
import { TableRepository } from '../table/repository.js';
|
|
4
|
-
import { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
|
5
|
-
import { WithAuthHandler } from '../auth/with_auth.js';
|
|
6
|
-
import 'drizzle-orm/node-postgres';
|
|
7
|
-
import 'gw-auth';
|
|
8
|
-
import 'gw-auth/server';
|
|
9
|
-
|
|
10
|
-
type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
|
|
11
|
-
withAuthAction: WithAuthHandler<ActionFunctionArgs>;
|
|
12
|
-
repository: TableRepository<T, TSelect>;
|
|
13
|
-
validators?: {
|
|
14
|
-
[K in keyof InferSelectModel<T>]?: {
|
|
15
|
-
validate?: (value?: InferSelectModel<T>[K]) => boolean;
|
|
16
|
-
message?: (value?: InferSelectModel<T>[K]) => string;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
existingConditions?: {
|
|
20
|
-
[K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
|
|
21
|
-
};
|
|
22
|
-
injectUserId?: boolean;
|
|
23
|
-
roles?: string[];
|
|
24
|
-
};
|
|
25
|
-
declare function apiHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, }: APIHandlerOptions<T, TSelect>): {
|
|
26
|
-
loader: ({ request }: LoaderFunctionArgs) => Promise<{}>;
|
|
27
|
-
action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export { type APIHandlerOptions, apiHandler };
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/api/create_api_handler.ts
|
|
21
|
-
var create_api_handler_exports = {};
|
|
22
|
-
__export(create_api_handler_exports, {
|
|
23
|
-
apiHandler: () => apiHandler
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(create_api_handler_exports);
|
|
26
|
-
var import_drizzle_orm = require("drizzle-orm");
|
|
27
|
-
var import_uuid = require("uuid");
|
|
28
|
-
|
|
29
|
-
// src/crud/serialize.ts
|
|
30
|
-
function deserialize(data) {
|
|
31
|
-
if (data === void 0) {
|
|
32
|
-
return void 0;
|
|
33
|
-
}
|
|
34
|
-
if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
|
|
35
|
-
const { type, value } = data;
|
|
36
|
-
switch (type) {
|
|
37
|
-
case "null":
|
|
38
|
-
return null;
|
|
39
|
-
case "string":
|
|
40
|
-
return value;
|
|
41
|
-
case "number":
|
|
42
|
-
return value;
|
|
43
|
-
case "boolean":
|
|
44
|
-
return value;
|
|
45
|
-
case "date":
|
|
46
|
-
return new Date(value);
|
|
47
|
-
case "array":
|
|
48
|
-
return value.map((item) => deserialize(item));
|
|
49
|
-
case "object":
|
|
50
|
-
return Object.entries(value).reduce(
|
|
51
|
-
(acc, [key, value2]) => {
|
|
52
|
-
return {
|
|
53
|
-
...acc,
|
|
54
|
-
[key]: deserialize(value2)
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
{}
|
|
58
|
-
);
|
|
59
|
-
default:
|
|
60
|
-
return void 0;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return void 0;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// src/api/create_api_handler.ts
|
|
67
|
-
var import_gw_response = require("gw-response");
|
|
68
|
-
function apiHandler({
|
|
69
|
-
withAuthAction,
|
|
70
|
-
repository,
|
|
71
|
-
validators,
|
|
72
|
-
existingConditions,
|
|
73
|
-
injectUserId,
|
|
74
|
-
roles
|
|
75
|
-
}) {
|
|
76
|
-
const loader = async ({ request }) => {
|
|
77
|
-
return {};
|
|
78
|
-
};
|
|
79
|
-
const action = withAuthAction((auth) => async ({ request }) => {
|
|
80
|
-
if (roles && roles.length > 0 && (!auth || !roles.includes(auth.role))) {
|
|
81
|
-
throw (0, import_gw_response.httpUnauthorized)({
|
|
82
|
-
code: "UNAUTHORIZED",
|
|
83
|
-
message: "\uC778\uC99D\uC774 \uD544\uC694\uD569\uB2C8\uB2E4."
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
switch (request.method) {
|
|
87
|
-
case "POST":
|
|
88
|
-
case "PUT": {
|
|
89
|
-
const serilaizedParams = await request.json();
|
|
90
|
-
const params = deserialize(serilaizedParams);
|
|
91
|
-
if (validators) {
|
|
92
|
-
const paramsForValidation = Object.keys(validators).filter(
|
|
93
|
-
(key) => Object.prototype.hasOwnProperty.call(validators, key)
|
|
94
|
-
);
|
|
95
|
-
for (const paramKey of paramsForValidation) {
|
|
96
|
-
const value = params[paramKey];
|
|
97
|
-
const validator = validators[paramKey];
|
|
98
|
-
if (validator?.validate && !validator.validate(value)) {
|
|
99
|
-
throw (0, import_gw_response.httpBadRequest)({
|
|
100
|
-
code: "BAD_REQUEST",
|
|
101
|
-
message: validator.message ? validator.message(value) : "\uC798\uBABB\uB41C \uC694\uCCAD\uC785\uB2C8\uB2E4."
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
const itemId = params.id || (0, import_uuid.v4)();
|
|
107
|
-
if (!params.id && existingConditions) {
|
|
108
|
-
const paramsForExistenceCheck = Object.keys(
|
|
109
|
-
existingConditions
|
|
110
|
-
).filter(
|
|
111
|
-
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
112
|
-
);
|
|
113
|
-
if (paramsForExistenceCheck.length > 0) {
|
|
114
|
-
const where = (0, import_drizzle_orm.and)(
|
|
115
|
-
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
116
|
-
const condition = existingConditions[key];
|
|
117
|
-
if (condition) {
|
|
118
|
-
acc.push(condition(params[key]));
|
|
119
|
-
}
|
|
120
|
-
return acc;
|
|
121
|
-
}, [])
|
|
122
|
-
);
|
|
123
|
-
const existing = await repository.findAll({
|
|
124
|
-
limit: 1,
|
|
125
|
-
where
|
|
126
|
-
});
|
|
127
|
-
if (existing.length > 0) {
|
|
128
|
-
return (0, import_gw_response.httpConflict)({
|
|
129
|
-
code: "CONFLICT",
|
|
130
|
-
message: "\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4."
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const values = {
|
|
136
|
-
id: itemId,
|
|
137
|
-
userId: injectUserId ? auth?.userId : void 0,
|
|
138
|
-
...params
|
|
139
|
-
};
|
|
140
|
-
const item = await repository.save(values);
|
|
141
|
-
return (0, import_gw_response.httpCreated)(item);
|
|
142
|
-
}
|
|
143
|
-
default:
|
|
144
|
-
throw (0, import_gw_response.httpMethodNotAllowed)({
|
|
145
|
-
code: "METHOD_NOT_ALLOWED",
|
|
146
|
-
message: "\uD5C8\uC6A9\uB418\uC9C0 \uC54A\uC740 \uBA54\uC11C\uB4DC\uC785\uB2C8\uB2E4."
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
return {
|
|
151
|
-
loader,
|
|
152
|
-
action
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
156
|
-
0 && (module.exports = {
|
|
157
|
-
apiHandler
|
|
158
|
-
});
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
// src/api/create_api_handler.ts
|
|
2
|
-
import {
|
|
3
|
-
and
|
|
4
|
-
} from "drizzle-orm";
|
|
5
|
-
import { v4 } from "uuid";
|
|
6
|
-
|
|
7
|
-
// src/crud/serialize.ts
|
|
8
|
-
function deserialize(data) {
|
|
9
|
-
if (data === void 0) {
|
|
10
|
-
return void 0;
|
|
11
|
-
}
|
|
12
|
-
if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
|
|
13
|
-
const { type, value } = data;
|
|
14
|
-
switch (type) {
|
|
15
|
-
case "null":
|
|
16
|
-
return null;
|
|
17
|
-
case "string":
|
|
18
|
-
return value;
|
|
19
|
-
case "number":
|
|
20
|
-
return value;
|
|
21
|
-
case "boolean":
|
|
22
|
-
return value;
|
|
23
|
-
case "date":
|
|
24
|
-
return new Date(value);
|
|
25
|
-
case "array":
|
|
26
|
-
return value.map((item) => deserialize(item));
|
|
27
|
-
case "object":
|
|
28
|
-
return Object.entries(value).reduce(
|
|
29
|
-
(acc, [key, value2]) => {
|
|
30
|
-
return {
|
|
31
|
-
...acc,
|
|
32
|
-
[key]: deserialize(value2)
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
{}
|
|
36
|
-
);
|
|
37
|
-
default:
|
|
38
|
-
return void 0;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return void 0;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// src/api/create_api_handler.ts
|
|
45
|
-
import {
|
|
46
|
-
httpBadRequest,
|
|
47
|
-
httpConflict,
|
|
48
|
-
httpCreated,
|
|
49
|
-
httpMethodNotAllowed,
|
|
50
|
-
httpUnauthorized
|
|
51
|
-
} from "gw-response";
|
|
52
|
-
function apiHandler({
|
|
53
|
-
withAuthAction,
|
|
54
|
-
repository,
|
|
55
|
-
validators,
|
|
56
|
-
existingConditions,
|
|
57
|
-
injectUserId,
|
|
58
|
-
roles
|
|
59
|
-
}) {
|
|
60
|
-
const loader = async ({ request }) => {
|
|
61
|
-
return {};
|
|
62
|
-
};
|
|
63
|
-
const action = withAuthAction((auth) => async ({ request }) => {
|
|
64
|
-
if (roles && roles.length > 0 && (!auth || !roles.includes(auth.role))) {
|
|
65
|
-
throw httpUnauthorized({
|
|
66
|
-
code: "UNAUTHORIZED",
|
|
67
|
-
message: "\uC778\uC99D\uC774 \uD544\uC694\uD569\uB2C8\uB2E4."
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
switch (request.method) {
|
|
71
|
-
case "POST":
|
|
72
|
-
case "PUT": {
|
|
73
|
-
const serilaizedParams = await request.json();
|
|
74
|
-
const params = deserialize(serilaizedParams);
|
|
75
|
-
if (validators) {
|
|
76
|
-
const paramsForValidation = Object.keys(validators).filter(
|
|
77
|
-
(key) => Object.prototype.hasOwnProperty.call(validators, key)
|
|
78
|
-
);
|
|
79
|
-
for (const paramKey of paramsForValidation) {
|
|
80
|
-
const value = params[paramKey];
|
|
81
|
-
const validator = validators[paramKey];
|
|
82
|
-
if (validator?.validate && !validator.validate(value)) {
|
|
83
|
-
throw httpBadRequest({
|
|
84
|
-
code: "BAD_REQUEST",
|
|
85
|
-
message: validator.message ? validator.message(value) : "\uC798\uBABB\uB41C \uC694\uCCAD\uC785\uB2C8\uB2E4."
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const itemId = params.id || v4();
|
|
91
|
-
if (!params.id && existingConditions) {
|
|
92
|
-
const paramsForExistenceCheck = Object.keys(
|
|
93
|
-
existingConditions
|
|
94
|
-
).filter(
|
|
95
|
-
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
96
|
-
);
|
|
97
|
-
if (paramsForExistenceCheck.length > 0) {
|
|
98
|
-
const where = and(
|
|
99
|
-
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
100
|
-
const condition = existingConditions[key];
|
|
101
|
-
if (condition) {
|
|
102
|
-
acc.push(condition(params[key]));
|
|
103
|
-
}
|
|
104
|
-
return acc;
|
|
105
|
-
}, [])
|
|
106
|
-
);
|
|
107
|
-
const existing = await repository.findAll({
|
|
108
|
-
limit: 1,
|
|
109
|
-
where
|
|
110
|
-
});
|
|
111
|
-
if (existing.length > 0) {
|
|
112
|
-
return httpConflict({
|
|
113
|
-
code: "CONFLICT",
|
|
114
|
-
message: "\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4."
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
const values = {
|
|
120
|
-
id: itemId,
|
|
121
|
-
userId: injectUserId ? auth?.userId : void 0,
|
|
122
|
-
...params
|
|
123
|
-
};
|
|
124
|
-
const item = await repository.save(values);
|
|
125
|
-
return httpCreated(item);
|
|
126
|
-
}
|
|
127
|
-
default:
|
|
128
|
-
throw httpMethodNotAllowed({
|
|
129
|
-
code: "METHOD_NOT_ALLOWED",
|
|
130
|
-
message: "\uD5C8\uC6A9\uB418\uC9C0 \uC54A\uC740 \uBA54\uC11C\uB4DC\uC785\uB2C8\uB2E4."
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
return {
|
|
135
|
-
loader,
|
|
136
|
-
action
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
export {
|
|
140
|
-
apiHandler
|
|
141
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import * as gw_result from 'gw-result';
|
|
2
|
-
import { LoaderFunctionArgs } from 'react-router';
|
|
3
|
-
import { TableRepository } from '../table/repository.mjs';
|
|
4
|
-
import { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
|
5
|
-
import { WithAuthHandler } from '../auth/with_auth.mjs';
|
|
6
|
-
import { AccessTokenPayload } from 'gw-auth';
|
|
7
|
-
import 'drizzle-orm';
|
|
8
|
-
import 'drizzle-orm/node-postgres';
|
|
9
|
-
import 'gw-auth/server';
|
|
10
|
-
|
|
11
|
-
type ItemAPIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
|
|
12
|
-
withAuthAction: WithAuthHandler<LoaderFunctionArgs>;
|
|
13
|
-
repository: TableRepository<T, TSelect>;
|
|
14
|
-
isOwnedBy?: (item: TSelect, auth: AccessTokenPayload | undefined) => boolean;
|
|
15
|
-
roles?: string[];
|
|
16
|
-
};
|
|
17
|
-
declare function itemApiHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, isOwnedBy, roles, }: ItemAPIHandlerOptions<T, TSelect>): {
|
|
18
|
-
loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
|
|
19
|
-
action: (arg: LoaderFunctionArgs<any>) => Promise<unknown> | unknown;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export { type ItemAPIHandlerOptions, itemApiHandler };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import * as gw_result from 'gw-result';
|
|
2
|
-
import { LoaderFunctionArgs } from 'react-router';
|
|
3
|
-
import { TableRepository } from '../table/repository.js';
|
|
4
|
-
import { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
|
5
|
-
import { WithAuthHandler } from '../auth/with_auth.js';
|
|
6
|
-
import { AccessTokenPayload } from 'gw-auth';
|
|
7
|
-
import 'drizzle-orm';
|
|
8
|
-
import 'drizzle-orm/node-postgres';
|
|
9
|
-
import 'gw-auth/server';
|
|
10
|
-
|
|
11
|
-
type ItemAPIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
|
|
12
|
-
withAuthAction: WithAuthHandler<LoaderFunctionArgs>;
|
|
13
|
-
repository: TableRepository<T, TSelect>;
|
|
14
|
-
isOwnedBy?: (item: TSelect, auth: AccessTokenPayload | undefined) => boolean;
|
|
15
|
-
roles?: string[];
|
|
16
|
-
};
|
|
17
|
-
declare function itemApiHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, isOwnedBy, roles, }: ItemAPIHandlerOptions<T, TSelect>): {
|
|
18
|
-
loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
|
|
19
|
-
action: (arg: LoaderFunctionArgs<any>) => Promise<unknown> | unknown;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export { type ItemAPIHandlerOptions, itemApiHandler };
|