dn-react-router-toolkit 0.4.2 → 0.5.0
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_api_handler.d.mts +28 -0
- package/dist/api/create_api_handler.d.ts +28 -0
- package/dist/api/create_api_handler.js +148 -0
- package/dist/api/create_api_handler.mjs +132 -0
- package/dist/api/create_handler.d.mts +1 -1
- package/dist/api/create_handler.d.ts +1 -1
- package/dist/api/create_handler.js +29 -26
- package/dist/api/create_handler.mjs +29 -26
- package/dist/api/index.js +29 -26
- package/dist/api/index.mjs +29 -26
- package/dist/api/item_api_handler.d.mts +18 -0
- package/dist/api/item_api_handler.d.ts +18 -0
- package/dist/api/item_api_handler.js +55 -0
- package/dist/api/item_api_handler.mjs +30 -0
- package/dist/auth/index.d.mts +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.js +62 -16
- package/dist/auth/index.mjs +60 -15
- package/dist/auth/with_auth.d.mts +8 -3
- package/dist/auth/with_auth.d.ts +8 -3
- package/dist/auth/with_auth.js +62 -16
- package/dist/auth/with_auth.mjs +60 -15
- package/dist/crud/crud_form.d.mts +13 -0
- package/dist/crud/crud_form.d.ts +13 -0
- package/dist/crud/crud_form.js +88 -0
- package/dist/crud/crud_form.mjs +55 -0
- package/dist/crud/crud_form_provider.d.mts +34 -0
- package/dist/crud/crud_form_provider.d.ts +34 -0
- package/dist/crud/crud_form_provider.js +160 -0
- package/dist/crud/crud_form_provider.mjs +124 -0
- package/dist/crud/crud_loader.d.mts +21 -0
- package/dist/crud/crud_loader.d.ts +21 -0
- package/dist/crud/crud_loader.js +288 -0
- package/dist/crud/crud_loader.mjs +273 -0
- package/dist/crud/crud_page.d.mts +25 -0
- package/dist/crud/crud_page.d.ts +25 -0
- package/dist/crud/crud_page.js +645 -0
- package/dist/crud/crud_page.mjs +616 -0
- package/dist/crud/generate_handlers.d.mts +15 -0
- package/dist/crud/generate_handlers.d.ts +15 -0
- package/dist/crud/generate_handlers.js +39 -0
- package/dist/crud/generate_handlers.mjs +14 -0
- package/dist/crud/generate_pages.d.mts +11 -0
- package/dist/crud/generate_pages.d.ts +11 -0
- package/dist/crud/generate_pages.js +52 -0
- package/dist/crud/generate_pages.mjs +17 -0
- package/dist/crud/generate_routes.d.mts +5 -0
- package/dist/crud/generate_routes.d.ts +5 -0
- package/dist/crud/generate_routes.js +62 -0
- package/dist/crud/generate_routes.mjs +27 -0
- package/dist/crud/index.d.mts +21 -0
- package/dist/crud/index.d.ts +21 -0
- package/dist/crud/index.js +970 -0
- package/dist/crud/index.mjs +945 -0
- package/dist/db/index.d.mts +4 -0
- package/dist/db/index.d.ts +4 -0
- package/dist/db/index.js +46 -0
- package/dist/db/index.mjs +8 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/table/buttons.d.mts +10 -0
- package/dist/table/buttons.d.ts +10 -0
- package/dist/table/buttons.js +102 -0
- package/dist/table/buttons.mjs +71 -0
- package/dist/table/index.d.mts +9 -0
- package/dist/table/index.d.ts +9 -0
- package/dist/table/index.js +570 -0
- package/dist/table/index.mjs +543 -0
- package/dist/table/item_loader.d.mts +12 -0
- package/dist/table/item_loader.d.ts +12 -0
- package/dist/table/item_loader.js +51 -0
- package/dist/table/item_loader.mjs +26 -0
- package/dist/table/loader.d.mts +28 -0
- package/dist/table/loader.d.ts +28 -0
- package/dist/table/loader.js +70 -0
- package/dist/table/loader.mjs +48 -0
- package/dist/table/page.d.mts +27 -0
- package/dist/table/page.d.ts +27 -0
- package/dist/table/page.js +444 -0
- package/dist/table/page.mjs +415 -0
- package/dist/table/repository.d.mts +38 -0
- package/dist/table/repository.d.ts +38 -0
- package/dist/table/repository.js +76 -0
- package/dist/table/repository.mjs +56 -0
- package/dist/table/table.d.mts +27 -0
- package/dist/table/table.d.ts +27 -0
- package/dist/table/table.js +290 -0
- package/dist/table/table.mjs +255 -0
- package/package.json +78 -59
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// src/crud/crud_form_provider.tsx
|
|
2
|
+
import { useNavigate } from "react-router";
|
|
3
|
+
import { useStore } from "dn-react-toolkit/store";
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
import React from "react";
|
|
6
|
+
var FormContext = createContext({});
|
|
7
|
+
function useFormContext() {
|
|
8
|
+
return useContext(FormContext);
|
|
9
|
+
}
|
|
10
|
+
function CrudFormProvider({
|
|
11
|
+
primaryKey = "id",
|
|
12
|
+
name,
|
|
13
|
+
prefix,
|
|
14
|
+
item,
|
|
15
|
+
columns = {},
|
|
16
|
+
children
|
|
17
|
+
}) {
|
|
18
|
+
const apiPrefix = `/api${prefix}`;
|
|
19
|
+
const store = useStore({
|
|
20
|
+
...item || {}
|
|
21
|
+
});
|
|
22
|
+
const navigate = useNavigate();
|
|
23
|
+
const submit = async () => {
|
|
24
|
+
const res = await fetch(apiPrefix, {
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json"
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify(
|
|
30
|
+
Object.entries(store.state).reduce(
|
|
31
|
+
function reducer(acc, [key, value]) {
|
|
32
|
+
const converter = (value2) => {
|
|
33
|
+
if (value2 === void 0) {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
if (value2 === null) {
|
|
37
|
+
return {
|
|
38
|
+
type: "null",
|
|
39
|
+
value: null
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (typeof value2 === "string") {
|
|
43
|
+
return {
|
|
44
|
+
type: "string",
|
|
45
|
+
value: value2
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (typeof value2 === "number") {
|
|
49
|
+
return {
|
|
50
|
+
type: "number",
|
|
51
|
+
value: value2
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (typeof value2 === "boolean") {
|
|
55
|
+
return {
|
|
56
|
+
type: "boolean",
|
|
57
|
+
value: value2
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (value2 instanceof Date) {
|
|
61
|
+
return {
|
|
62
|
+
type: "date",
|
|
63
|
+
value: value2.toISOString()
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(value2)) {
|
|
67
|
+
return value2.map((v) => converter(v));
|
|
68
|
+
}
|
|
69
|
+
if (typeof value2 === "object") {
|
|
70
|
+
return Object.entries(
|
|
71
|
+
value2
|
|
72
|
+
).reduce(reducer, {});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
...acc,
|
|
77
|
+
[key]: converter(value)
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
{}
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
const { message } = await res.json();
|
|
86
|
+
alert(message);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
alert(`${name}\uB97C \uC800\uC7A5\uD588\uC2B5\uB2C8\uB2E4.`);
|
|
90
|
+
const { id } = await res.json();
|
|
91
|
+
navigate(`${prefix}/${id}`);
|
|
92
|
+
};
|
|
93
|
+
const deleteItem = async () => {
|
|
94
|
+
if (!item || !primaryKey) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const ok = confirm("\uC815\uB9D0\uB85C \uC0AD\uC81C\uD558\uC2DC\uACA0\uC2B5\uB2C8\uAE4C?");
|
|
98
|
+
if (!ok) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const res = await fetch(`${apiPrefix}/${item[primaryKey]}`, {
|
|
102
|
+
method: "DELETE"
|
|
103
|
+
});
|
|
104
|
+
if (!res.ok) {
|
|
105
|
+
const { message } = await res.json();
|
|
106
|
+
alert(message);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
alert(`${name}\uB97C \uC0AD\uC81C\uD588\uC2B5\uB2C8\uB2E4.`);
|
|
110
|
+
navigate(`${prefix}`);
|
|
111
|
+
};
|
|
112
|
+
return /* @__PURE__ */ React.createElement(
|
|
113
|
+
FormContext.Provider,
|
|
114
|
+
{
|
|
115
|
+
value: { name, item, store, submit, delete: deleteItem, columns }
|
|
116
|
+
},
|
|
117
|
+
children
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
export {
|
|
121
|
+
FormContext,
|
|
122
|
+
CrudFormProvider as default,
|
|
123
|
+
useFormContext
|
|
124
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LoaderFunctionArgs } from 'react-router';
|
|
2
|
+
import { TableLoaderOptions } from '../table/loader.mjs';
|
|
3
|
+
import { TableItemLoaderOptions } from '../table/item_loader.mjs';
|
|
4
|
+
import { Table } from 'drizzle-orm';
|
|
5
|
+
import { TableRepository } from '../table/repository.mjs';
|
|
6
|
+
import { APIHandlerOptions } from '../api/create_api_handler.mjs';
|
|
7
|
+
import 'drizzle-orm/pg-core';
|
|
8
|
+
import '../auth/with_auth.mjs';
|
|
9
|
+
import 'dn-react-toolkit/auth';
|
|
10
|
+
import 'dn-react-toolkit/auth/server';
|
|
11
|
+
|
|
12
|
+
type CrudHandlerOptions<T extends Table, TSelect> = {
|
|
13
|
+
repository: TableRepository<T, TSelect>;
|
|
14
|
+
apiHandlerOptions: Omit<APIHandlerOptions<T, TSelect>, "repository">;
|
|
15
|
+
loaderOptions: Omit<TableLoaderOptions<T, TSelect>, "repository">;
|
|
16
|
+
itemLoaderOptions: Omit<TableItemLoaderOptions<T, TSelect>, "repository">;
|
|
17
|
+
};
|
|
18
|
+
type CrudHandler = (prefix: string) => (args: LoaderFunctionArgs) => Promise<any>;
|
|
19
|
+
declare function crudHandler<T extends Table, TSelect>({ repository, apiHandlerOptions, loaderOptions, itemLoaderOptions, }: CrudHandlerOptions<T, TSelect>): (prefix: string) => (args: LoaderFunctionArgs) => Promise<any>;
|
|
20
|
+
|
|
21
|
+
export { type CrudHandler, type CrudHandlerOptions, crudHandler };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LoaderFunctionArgs } from 'react-router';
|
|
2
|
+
import { TableLoaderOptions } from '../table/loader.js';
|
|
3
|
+
import { TableItemLoaderOptions } from '../table/item_loader.js';
|
|
4
|
+
import { Table } from 'drizzle-orm';
|
|
5
|
+
import { TableRepository } from '../table/repository.js';
|
|
6
|
+
import { APIHandlerOptions } from '../api/create_api_handler.js';
|
|
7
|
+
import 'drizzle-orm/pg-core';
|
|
8
|
+
import '../auth/with_auth.js';
|
|
9
|
+
import 'dn-react-toolkit/auth';
|
|
10
|
+
import 'dn-react-toolkit/auth/server';
|
|
11
|
+
|
|
12
|
+
type CrudHandlerOptions<T extends Table, TSelect> = {
|
|
13
|
+
repository: TableRepository<T, TSelect>;
|
|
14
|
+
apiHandlerOptions: Omit<APIHandlerOptions<T, TSelect>, "repository">;
|
|
15
|
+
loaderOptions: Omit<TableLoaderOptions<T, TSelect>, "repository">;
|
|
16
|
+
itemLoaderOptions: Omit<TableItemLoaderOptions<T, TSelect>, "repository">;
|
|
17
|
+
};
|
|
18
|
+
type CrudHandler = (prefix: string) => (args: LoaderFunctionArgs) => Promise<any>;
|
|
19
|
+
declare function crudHandler<T extends Table, TSelect>({ repository, apiHandlerOptions, loaderOptions, itemLoaderOptions, }: CrudHandlerOptions<T, TSelect>): (prefix: string) => (args: LoaderFunctionArgs) => Promise<any>;
|
|
20
|
+
|
|
21
|
+
export { type CrudHandler, type CrudHandlerOptions, crudHandler };
|
|
@@ -0,0 +1,288 @@
|
|
|
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/crud/crud_loader.tsx
|
|
21
|
+
var crud_loader_exports = {};
|
|
22
|
+
__export(crud_loader_exports, {
|
|
23
|
+
crudHandler: () => crudHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(crud_loader_exports);
|
|
26
|
+
|
|
27
|
+
// src/table/loader.tsx
|
|
28
|
+
var import_drizzle_orm = require("drizzle-orm");
|
|
29
|
+
function tableLoader({
|
|
30
|
+
repository,
|
|
31
|
+
tableOptions
|
|
32
|
+
}) {
|
|
33
|
+
return async ({ request }) => {
|
|
34
|
+
const searchParams = new URL(request.url).searchParams;
|
|
35
|
+
const { where, searchKey, defaultOrderBy, defaultDirection } = tableOptions;
|
|
36
|
+
const query = searchParams.get("query") ?? void 0;
|
|
37
|
+
const limit = Number(searchParams.get("limit") ?? "10");
|
|
38
|
+
const offset = Number(searchParams.get("offset") ?? "0");
|
|
39
|
+
const orderBy = searchParams.get("orderBy") ?? defaultOrderBy;
|
|
40
|
+
const direction = searchParams.get("direction") ?? defaultDirection;
|
|
41
|
+
const whereClauses = (0, import_drizzle_orm.and)(
|
|
42
|
+
searchKey && query ? (0, import_drizzle_orm.ilike)(
|
|
43
|
+
repository.schema[searchKey],
|
|
44
|
+
`%${query}%`
|
|
45
|
+
) : void 0,
|
|
46
|
+
...where ?? []
|
|
47
|
+
);
|
|
48
|
+
const total = await repository.countTotal({ where: whereClauses });
|
|
49
|
+
const items = await repository.findAll({
|
|
50
|
+
orderBy,
|
|
51
|
+
direction,
|
|
52
|
+
limit,
|
|
53
|
+
offset,
|
|
54
|
+
where: whereClauses
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
table: {
|
|
58
|
+
items,
|
|
59
|
+
total,
|
|
60
|
+
limit,
|
|
61
|
+
offset,
|
|
62
|
+
orderBy,
|
|
63
|
+
direction,
|
|
64
|
+
searchKey
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/table/item_loader.tsx
|
|
71
|
+
var tableItemloader = ({
|
|
72
|
+
loader,
|
|
73
|
+
repository
|
|
74
|
+
}) => {
|
|
75
|
+
return async ({ params }) => {
|
|
76
|
+
const body = loader ? await (async () => {
|
|
77
|
+
const result = await loader({ params });
|
|
78
|
+
if (result instanceof Response) {
|
|
79
|
+
return result.json();
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
})() : {};
|
|
83
|
+
if (params["itemId"] === "new") {
|
|
84
|
+
return { item: void 0, ...body };
|
|
85
|
+
}
|
|
86
|
+
const item = params["itemId"] ? await repository.find(params["itemId"]) : void 0;
|
|
87
|
+
return {
|
|
88
|
+
item,
|
|
89
|
+
...body
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/api/create_api_handler.ts
|
|
95
|
+
var import_http = require("dn-react-toolkit/http");
|
|
96
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
97
|
+
var import_react_router = require("react-router");
|
|
98
|
+
var import_uuid = require("uuid");
|
|
99
|
+
function apiHandler({
|
|
100
|
+
withAuthAction,
|
|
101
|
+
repository,
|
|
102
|
+
validators,
|
|
103
|
+
existingConditions,
|
|
104
|
+
injectUserId
|
|
105
|
+
}) {
|
|
106
|
+
const loader = async ({ request }) => {
|
|
107
|
+
return {};
|
|
108
|
+
};
|
|
109
|
+
const action = withAuthAction((auth) => async ({ request }) => {
|
|
110
|
+
if (!auth || auth.role !== "admin") {
|
|
111
|
+
return (0, import_react_router.redirect)("/login");
|
|
112
|
+
}
|
|
113
|
+
switch (request.method) {
|
|
114
|
+
case "POST":
|
|
115
|
+
case "PUT": {
|
|
116
|
+
const serilaizedParams = await request.json();
|
|
117
|
+
const params = Object.entries(serilaizedParams).reduce(
|
|
118
|
+
function reducer(acc, [key, value]) {
|
|
119
|
+
const converter = (value2) => {
|
|
120
|
+
if (value2.type === "null") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (value2.type === "string") {
|
|
124
|
+
return value2.value;
|
|
125
|
+
}
|
|
126
|
+
if (value2.type === "number") {
|
|
127
|
+
return value2.value;
|
|
128
|
+
}
|
|
129
|
+
if (value2.type === "boolean") {
|
|
130
|
+
return value2.value;
|
|
131
|
+
}
|
|
132
|
+
if (value2.type === "date") {
|
|
133
|
+
return new Date(value2.value);
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(value2)) {
|
|
136
|
+
return value2.map((v) => converter(v));
|
|
137
|
+
}
|
|
138
|
+
if (typeof value2 === "object") {
|
|
139
|
+
return Object.entries(value2).reduce(
|
|
140
|
+
reducer,
|
|
141
|
+
{}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const result = converter(value);
|
|
146
|
+
if (result === void 0) {
|
|
147
|
+
return acc;
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
...acc,
|
|
151
|
+
[key]: result
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
{}
|
|
155
|
+
);
|
|
156
|
+
if (validators) {
|
|
157
|
+
const paramsForValidation = Object.keys(validators).filter(
|
|
158
|
+
(key) => Object.prototype.hasOwnProperty.call(
|
|
159
|
+
validators,
|
|
160
|
+
key
|
|
161
|
+
)
|
|
162
|
+
);
|
|
163
|
+
for (const paramKey of paramsForValidation) {
|
|
164
|
+
const value = params[paramKey];
|
|
165
|
+
const validator = validators[paramKey];
|
|
166
|
+
if (validator?.validate && !validator.validate(value)) {
|
|
167
|
+
throw (0, import_http.BAD_REQUEST)(
|
|
168
|
+
validator.message ? validator.message(value) : void 0
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const itemId = params.id || (0, import_uuid.v4)();
|
|
174
|
+
if (!params.id && existingConditions) {
|
|
175
|
+
const paramsForExistenceCheck = Object.keys(
|
|
176
|
+
existingConditions
|
|
177
|
+
).filter(
|
|
178
|
+
(key) => Object.prototype.hasOwnProperty.call(params, key)
|
|
179
|
+
);
|
|
180
|
+
const where = (0, import_drizzle_orm2.and)(
|
|
181
|
+
...paramsForExistenceCheck.reduce((acc, key) => {
|
|
182
|
+
const condition = existingConditions[key];
|
|
183
|
+
if (condition) {
|
|
184
|
+
acc.push(condition(params[key]));
|
|
185
|
+
}
|
|
186
|
+
return acc;
|
|
187
|
+
}, [])
|
|
188
|
+
);
|
|
189
|
+
const existing = await repository.findAll({
|
|
190
|
+
limit: 1,
|
|
191
|
+
where
|
|
192
|
+
});
|
|
193
|
+
if (existing.length > 0) {
|
|
194
|
+
throw (0, import_http.CONFLICT)("\uC790\uB8CC\uAC00 \uC774\uBBF8 \uC874\uC7AC\uD569\uB2C8\uB2E4.");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const values = {
|
|
198
|
+
id: itemId,
|
|
199
|
+
userId: injectUserId ? auth.userId : void 0,
|
|
200
|
+
...params
|
|
201
|
+
};
|
|
202
|
+
const item = await repository.save(values);
|
|
203
|
+
return (0, import_http.CREATED)(item);
|
|
204
|
+
}
|
|
205
|
+
default:
|
|
206
|
+
throw (0, import_http.METHOD_NOT_ALLOWED)();
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
loader,
|
|
211
|
+
action
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// src/api/item_api_handler.ts
|
|
216
|
+
var import_http2 = require("dn-react-toolkit/http");
|
|
217
|
+
var import_react_router2 = require("react-router");
|
|
218
|
+
function itemApiHandler({
|
|
219
|
+
withAuthAction,
|
|
220
|
+
repository
|
|
221
|
+
}) {
|
|
222
|
+
const loader = async ({ request }) => {
|
|
223
|
+
return {};
|
|
224
|
+
};
|
|
225
|
+
const action = withAuthAction((auth) => async ({ params, request }) => {
|
|
226
|
+
if (!auth || auth.role !== "admin") {
|
|
227
|
+
return (0, import_http2.UNAUTHORIZED)();
|
|
228
|
+
}
|
|
229
|
+
switch (request.method) {
|
|
230
|
+
case "DELETE": {
|
|
231
|
+
const itemId = params.itemId;
|
|
232
|
+
await repository.delete(itemId);
|
|
233
|
+
return {};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
loader,
|
|
239
|
+
action
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/crud/crud_loader.tsx
|
|
244
|
+
function crudHandler({
|
|
245
|
+
repository,
|
|
246
|
+
apiHandlerOptions,
|
|
247
|
+
loaderOptions,
|
|
248
|
+
itemLoaderOptions
|
|
249
|
+
}) {
|
|
250
|
+
return (prefix) => async (args) => {
|
|
251
|
+
const pattern = args.unstable_pattern;
|
|
252
|
+
if (pattern === `/api${prefix}`) {
|
|
253
|
+
const { loader, action } = apiHandler({
|
|
254
|
+
repository,
|
|
255
|
+
...apiHandlerOptions
|
|
256
|
+
});
|
|
257
|
+
if (args.request.method === "GET") {
|
|
258
|
+
return loader(args);
|
|
259
|
+
} else {
|
|
260
|
+
return action(args);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (pattern.startsWith(`/api${prefix}`)) {
|
|
264
|
+
const { loader, action } = itemApiHandler({
|
|
265
|
+
repository,
|
|
266
|
+
...apiHandlerOptions
|
|
267
|
+
});
|
|
268
|
+
if (args.request.method === "GET") {
|
|
269
|
+
return loader(args);
|
|
270
|
+
} else {
|
|
271
|
+
return action(args);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (pattern === prefix) {
|
|
275
|
+
return tableLoader({
|
|
276
|
+
...loaderOptions,
|
|
277
|
+
repository
|
|
278
|
+
})(args);
|
|
279
|
+
}
|
|
280
|
+
if (pattern.startsWith(prefix)) {
|
|
281
|
+
return tableItemloader({ ...itemLoaderOptions, repository })(args);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
286
|
+
0 && (module.exports = {
|
|
287
|
+
crudHandler
|
|
288
|
+
});
|