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.
@@ -0,0 +1,194 @@
1
+ // src/api/put_resource_handler.ts
2
+ import {
3
+ httpBadRequest,
4
+ httpConflict,
5
+ httpCreated,
6
+ httpForbidden,
7
+ httpNotFound
8
+ } from "gw-response";
9
+
10
+ // src/crud/crud_form_provider.tsx
11
+ import { useNavigate } from "react-router";
12
+ import { useStore } from "react-store-input";
13
+ import {
14
+ createContext,
15
+ useContext
16
+ } from "react";
17
+
18
+ // src/crud/serialize.ts
19
+ function deserialize(data) {
20
+ if (data === void 0) {
21
+ return void 0;
22
+ }
23
+ if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
24
+ const { type, value } = data;
25
+ switch (type) {
26
+ case "null":
27
+ return null;
28
+ case "string":
29
+ return value;
30
+ case "number":
31
+ return value;
32
+ case "boolean":
33
+ return value;
34
+ case "date":
35
+ return new Date(value);
36
+ case "array":
37
+ return value.map((item) => deserialize(item));
38
+ case "object":
39
+ return Object.entries(value).reduce(
40
+ (acc, [key, value2]) => {
41
+ return {
42
+ ...acc,
43
+ [key]: deserialize(value2)
44
+ };
45
+ },
46
+ {}
47
+ );
48
+ default:
49
+ return void 0;
50
+ }
51
+ }
52
+ return void 0;
53
+ }
54
+
55
+ // src/crud/crud_form_provider.tsx
56
+ import { jsx } from "react/jsx-runtime";
57
+ var FormContext = createContext({});
58
+
59
+ // src/form/create_form_component.tsx
60
+ import "react";
61
+
62
+ // src/utils/cn.ts
63
+ function cn(...classes) {
64
+ return classes.filter(Boolean).join(" ").trim();
65
+ }
66
+
67
+ // src/utils/date.ts
68
+ import moment from "moment-timezone";
69
+
70
+ // src/form/create_form_component.tsx
71
+ import { jsx as jsx2 } from "react/jsx-runtime";
72
+ function createComponent(tag, options) {
73
+ return function FormComponent({ className, ...props }) {
74
+ const Tag = tag;
75
+ return /* @__PURE__ */ jsx2(Tag, { ...props, className: cn(options.className, className) });
76
+ };
77
+ }
78
+
79
+ // src/form/form_components.tsx
80
+ var FormEntry = createComponent("div", {
81
+ className: "flex-1"
82
+ });
83
+ var FormRow = createComponent("div", {
84
+ className: "flex-1 flex gap-4 mb-6"
85
+ });
86
+ var FormLabel = createComponent("label", {
87
+ className: "flex-1 font-semibold mb-2.5 block"
88
+ });
89
+
90
+ // src/crud/crud_form.tsx
91
+ import { useStoreComponent } from "react-store-input";
92
+
93
+ // src/client/env_loader.tsx
94
+ import { useRouteLoaderData } from "react-router";
95
+ import { jsx as jsx3 } from "react/jsx-runtime";
96
+
97
+ // src/client/file_input.tsx
98
+ import {
99
+ useRef
100
+ } from "react";
101
+ import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
102
+
103
+ // src/client/use_user_agent.tsx
104
+ import { useRouteLoaderData as useRouteLoaderData2 } from "react-router";
105
+
106
+ // src/client/store_text_editor.tsx
107
+ import {
108
+ TextEditor
109
+ } from "dn-react-text-editor";
110
+ import { useStoreController } from "react-store-input";
111
+ import { useImperativeHandle, useRef as useRef2 } from "react";
112
+ import { jsx as jsx5 } from "react/jsx-runtime";
113
+
114
+ // src/client/editor.tsx
115
+ import { generateMetadata } from "gw-file/client";
116
+
117
+ // src/crud/crud_form.tsx
118
+ import { Fragment as Fragment2, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
119
+
120
+ // src/api/put_resource_handler.ts
121
+ import {
122
+ and
123
+ } from "drizzle-orm";
124
+ import { v4 } from "uuid";
125
+ function putResourceHandler({
126
+ repository,
127
+ validators,
128
+ existingConditions,
129
+ injectUserId,
130
+ isOwnedBy
131
+ }) {
132
+ return async (auth, request) => {
133
+ const serilaizedParams = await request.json();
134
+ const params = deserialize(serilaizedParams) || {};
135
+ const itemId = params.id || v4();
136
+ if (params.id) {
137
+ const existing = await repository.find(itemId);
138
+ if (!existing) {
139
+ return httpNotFound();
140
+ }
141
+ if (isOwnedBy && (!auth || !isOwnedBy(auth, existing))) {
142
+ return httpForbidden();
143
+ }
144
+ }
145
+ if (validators) {
146
+ const paramsForValidation = Object.keys(validators).filter(
147
+ (key) => Object.prototype.hasOwnProperty.call(validators, key)
148
+ );
149
+ for (const paramKey of paramsForValidation) {
150
+ const value = params[paramKey];
151
+ const validator = validators[paramKey];
152
+ if (validator?.validate && !validator.validate(value)) {
153
+ return httpBadRequest({
154
+ code: "BAD_REQUEST",
155
+ message: validator.message ? validator.message(value) : "\uC798\uBABB\uB41C \uC694\uCCAD\uC785\uB2C8\uB2E4."
156
+ });
157
+ }
158
+ }
159
+ }
160
+ if (!params.id && existingConditions) {
161
+ const paramsForExistenceCheck = Object.keys(existingConditions).filter(
162
+ (key) => Object.prototype.hasOwnProperty.call(params, key)
163
+ );
164
+ if (paramsForExistenceCheck.length > 0) {
165
+ const where = and(
166
+ ...paramsForExistenceCheck.reduce((acc, key) => {
167
+ const condition = existingConditions[key];
168
+ if (condition) {
169
+ acc.push(condition(params[key]));
170
+ }
171
+ return acc;
172
+ }, [])
173
+ );
174
+ const existing = await repository.findAll({
175
+ limit: 1,
176
+ where
177
+ });
178
+ if (existing.length > 0) {
179
+ return httpConflict();
180
+ }
181
+ }
182
+ }
183
+ const values = {
184
+ id: itemId,
185
+ userId: injectUserId ? auth?.userId : void 0,
186
+ ...params
187
+ };
188
+ const item = await repository.save(values);
189
+ return httpCreated(item);
190
+ };
191
+ }
192
+ export {
193
+ putResourceHandler
194
+ };
@@ -0,0 +1,26 @@
1
+ import * as gw_result from 'gw-result';
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 { PutResourceValidators, PutResourceExistingConditions } from './put_resource_handler.mjs';
7
+ import { AccessTokenPayload } from 'gw-auth';
8
+ import 'drizzle-orm';
9
+ import 'drizzle-orm/node-postgres';
10
+ import 'gw-auth/server';
11
+
12
+ type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
13
+ withAuthAction: WithAuthHandler<ActionFunctionArgs>;
14
+ repository: TableRepository<T, TSelect>;
15
+ validators?: PutResourceValidators<T>;
16
+ existingConditions?: PutResourceExistingConditions<T>;
17
+ injectUserId?: boolean;
18
+ roles?: string[];
19
+ isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
20
+ };
21
+ declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, }: APIHandlerOptions<T, TSelect>): {
22
+ loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
23
+ action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
24
+ };
25
+
26
+ export { type APIHandlerOptions, resourceHandler };
@@ -0,0 +1,26 @@
1
+ import * as gw_result from 'gw-result';
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 { PutResourceValidators, PutResourceExistingConditions } from './put_resource_handler.js';
7
+ import { AccessTokenPayload } from 'gw-auth';
8
+ import 'drizzle-orm';
9
+ import 'drizzle-orm/node-postgres';
10
+ import 'gw-auth/server';
11
+
12
+ type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
13
+ withAuthAction: WithAuthHandler<ActionFunctionArgs>;
14
+ repository: TableRepository<T, TSelect>;
15
+ validators?: PutResourceValidators<T>;
16
+ existingConditions?: PutResourceExistingConditions<T>;
17
+ injectUserId?: boolean;
18
+ roles?: string[];
19
+ isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
20
+ };
21
+ declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, }: APIHandlerOptions<T, TSelect>): {
22
+ loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
23
+ action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
24
+ };
25
+
26
+ export { type APIHandlerOptions, resourceHandler };
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/api/resource_handler.ts
31
+ var resource_handler_exports = {};
32
+ __export(resource_handler_exports, {
33
+ resourceHandler: () => resourceHandler
34
+ });
35
+ module.exports = __toCommonJS(resource_handler_exports);
36
+ var import_gw_response2 = require("gw-response");
37
+ var import_gw_result = require("gw-result");
38
+
39
+ // src/api/put_resource_handler.ts
40
+ var import_gw_response = require("gw-response");
41
+
42
+ // src/crud/crud_form_provider.tsx
43
+ var import_react_router = require("react-router");
44
+ var import_react_store_input = require("react-store-input");
45
+ var import_react = require("react");
46
+
47
+ // src/crud/serialize.ts
48
+ function deserialize(data) {
49
+ if (data === void 0) {
50
+ return void 0;
51
+ }
52
+ if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
53
+ const { type, value } = data;
54
+ switch (type) {
55
+ case "null":
56
+ return null;
57
+ case "string":
58
+ return value;
59
+ case "number":
60
+ return value;
61
+ case "boolean":
62
+ return value;
63
+ case "date":
64
+ return new Date(value);
65
+ case "array":
66
+ return value.map((item) => deserialize(item));
67
+ case "object":
68
+ return Object.entries(value).reduce(
69
+ (acc, [key, value2]) => {
70
+ return {
71
+ ...acc,
72
+ [key]: deserialize(value2)
73
+ };
74
+ },
75
+ {}
76
+ );
77
+ default:
78
+ return void 0;
79
+ }
80
+ }
81
+ return void 0;
82
+ }
83
+
84
+ // src/crud/crud_form_provider.tsx
85
+ var import_jsx_runtime = require("react/jsx-runtime");
86
+ var FormContext = (0, import_react.createContext)({});
87
+
88
+ // src/form/create_form_component.tsx
89
+ var import_react2 = require("react");
90
+
91
+ // src/utils/cn.ts
92
+ function cn(...classes) {
93
+ return classes.filter(Boolean).join(" ").trim();
94
+ }
95
+
96
+ // src/utils/date.ts
97
+ var import_moment_timezone = __toESM(require("moment-timezone"));
98
+
99
+ // src/form/create_form_component.tsx
100
+ var import_jsx_runtime2 = require("react/jsx-runtime");
101
+ function createComponent(tag, options) {
102
+ return function FormComponent({ className, ...props }) {
103
+ const Tag = tag;
104
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Tag, { ...props, className: cn(options.className, className) });
105
+ };
106
+ }
107
+
108
+ // src/form/form_components.tsx
109
+ var FormEntry = createComponent("div", {
110
+ className: "flex-1"
111
+ });
112
+ var FormRow = createComponent("div", {
113
+ className: "flex-1 flex gap-4 mb-6"
114
+ });
115
+ var FormLabel = createComponent("label", {
116
+ className: "flex-1 font-semibold mb-2.5 block"
117
+ });
118
+
119
+ // src/crud/crud_form.tsx
120
+ var import_react_store_input3 = require("react-store-input");
121
+
122
+ // src/client/env_loader.tsx
123
+ var import_react_router2 = require("react-router");
124
+ var import_jsx_runtime3 = require("react/jsx-runtime");
125
+
126
+ // src/client/file_input.tsx
127
+ var import_react3 = require("react");
128
+ var import_jsx_runtime4 = require("react/jsx-runtime");
129
+
130
+ // src/client/use_user_agent.tsx
131
+ var import_react_router3 = require("react-router");
132
+
133
+ // src/client/store_text_editor.tsx
134
+ var import_dn_react_text_editor = require("dn-react-text-editor");
135
+ var import_react_store_input2 = require("react-store-input");
136
+ var import_react4 = require("react");
137
+ var import_jsx_runtime5 = require("react/jsx-runtime");
138
+
139
+ // src/client/editor.tsx
140
+ var import_client = require("gw-file/client");
141
+
142
+ // src/crud/crud_form.tsx
143
+ var import_jsx_runtime6 = require("react/jsx-runtime");
144
+
145
+ // src/api/put_resource_handler.ts
146
+ var import_drizzle_orm = require("drizzle-orm");
147
+ var import_uuid = require("uuid");
148
+ function putResourceHandler({
149
+ repository,
150
+ validators,
151
+ existingConditions,
152
+ injectUserId,
153
+ isOwnedBy
154
+ }) {
155
+ return async (auth, request) => {
156
+ const serilaizedParams = await request.json();
157
+ const params = deserialize(serilaizedParams) || {};
158
+ const itemId = params.id || (0, import_uuid.v4)();
159
+ if (params.id) {
160
+ const existing = await repository.find(itemId);
161
+ if (!existing) {
162
+ return (0, import_gw_response.httpNotFound)();
163
+ }
164
+ if (isOwnedBy && (!auth || !isOwnedBy(auth, existing))) {
165
+ return (0, import_gw_response.httpForbidden)();
166
+ }
167
+ }
168
+ if (validators) {
169
+ const paramsForValidation = Object.keys(validators).filter(
170
+ (key) => Object.prototype.hasOwnProperty.call(validators, key)
171
+ );
172
+ for (const paramKey of paramsForValidation) {
173
+ const value = params[paramKey];
174
+ const validator = validators[paramKey];
175
+ if (validator?.validate && !validator.validate(value)) {
176
+ return (0, import_gw_response.httpBadRequest)({
177
+ code: "BAD_REQUEST",
178
+ message: validator.message ? validator.message(value) : "\uC798\uBABB\uB41C \uC694\uCCAD\uC785\uB2C8\uB2E4."
179
+ });
180
+ }
181
+ }
182
+ }
183
+ if (!params.id && existingConditions) {
184
+ const paramsForExistenceCheck = Object.keys(existingConditions).filter(
185
+ (key) => Object.prototype.hasOwnProperty.call(params, key)
186
+ );
187
+ if (paramsForExistenceCheck.length > 0) {
188
+ const where = (0, import_drizzle_orm.and)(
189
+ ...paramsForExistenceCheck.reduce((acc, key) => {
190
+ const condition = existingConditions[key];
191
+ if (condition) {
192
+ acc.push(condition(params[key]));
193
+ }
194
+ return acc;
195
+ }, [])
196
+ );
197
+ const existing = await repository.findAll({
198
+ limit: 1,
199
+ where
200
+ });
201
+ if (existing.length > 0) {
202
+ return (0, import_gw_response.httpConflict)();
203
+ }
204
+ }
205
+ }
206
+ const values = {
207
+ id: itemId,
208
+ userId: injectUserId ? auth?.userId : void 0,
209
+ ...params
210
+ };
211
+ const item = await repository.save(values);
212
+ return (0, import_gw_response.httpCreated)(item);
213
+ };
214
+ }
215
+
216
+ // src/api/resource_handler.ts
217
+ function resourceHandler({
218
+ withAuthAction,
219
+ repository,
220
+ validators,
221
+ existingConditions,
222
+ injectUserId,
223
+ roles,
224
+ isOwnedBy
225
+ }) {
226
+ const loader = async ({ request }) => {
227
+ return (0, import_gw_result.ok)({});
228
+ };
229
+ const action = withAuthAction((auth) => async ({ request, params }) => {
230
+ if (roles && roles.length > 0 && (!auth || !roles.includes(auth.role))) {
231
+ return (0, import_gw_response2.httpForbidden)({
232
+ code: "FORBIDDEN",
233
+ message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
234
+ });
235
+ }
236
+ const itemId = params.itemId;
237
+ if (itemId) {
238
+ const existing = await repository.find(itemId);
239
+ if (!existing) {
240
+ return (0, import_gw_response2.httpNotFound)();
241
+ }
242
+ if (isOwnedBy && (!auth || !isOwnedBy(auth, existing))) {
243
+ return (0, import_gw_response2.httpForbidden)();
244
+ }
245
+ switch (request.method) {
246
+ case "DELETE": {
247
+ await repository.delete(itemId);
248
+ return (0, import_gw_response2.httpNoContent)();
249
+ }
250
+ default: {
251
+ return (0, import_gw_response2.httpMethodNotAllowed)();
252
+ }
253
+ }
254
+ }
255
+ switch (request.method) {
256
+ case "POST":
257
+ case "PUT": {
258
+ const handler = putResourceHandler({
259
+ repository,
260
+ validators,
261
+ existingConditions,
262
+ injectUserId,
263
+ isOwnedBy
264
+ });
265
+ return handler(auth, request);
266
+ }
267
+ default: {
268
+ return (0, import_gw_response2.httpMethodNotAllowed)();
269
+ }
270
+ }
271
+ });
272
+ return {
273
+ loader,
274
+ action
275
+ };
276
+ }
277
+ // Annotate the CommonJS export names for ESM import in node:
278
+ 0 && (module.exports = {
279
+ resourceHandler
280
+ });