dn-react-router-toolkit 0.6.1 → 0.6.2

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.
@@ -39,12 +39,72 @@ var import_react_router = require("react-router");
39
39
  var import_react_store_input = require("react-store-input");
40
40
  var import_react = require("react");
41
41
  var import_react2 = __toESM(require("react"));
42
+
43
+ // src/crud/serialize.ts
44
+ function serialize(value) {
45
+ if (value === void 0) {
46
+ return void 0;
47
+ }
48
+ if (value === null) {
49
+ return {
50
+ type: "null",
51
+ value: null
52
+ };
53
+ }
54
+ if (typeof value === "string") {
55
+ return {
56
+ type: "string",
57
+ value
58
+ };
59
+ }
60
+ if (typeof value === "number") {
61
+ return {
62
+ type: "number",
63
+ value
64
+ };
65
+ }
66
+ if (typeof value === "boolean") {
67
+ return {
68
+ type: "boolean",
69
+ value
70
+ };
71
+ }
72
+ if (value instanceof Date) {
73
+ return {
74
+ type: "date",
75
+ value: value.toISOString()
76
+ };
77
+ }
78
+ if (Array.isArray(value)) {
79
+ return {
80
+ type: "array",
81
+ value: value.map((item) => serialize(item))
82
+ };
83
+ }
84
+ if (typeof value === "object") {
85
+ return {
86
+ type: "object",
87
+ value: Object.entries(value).reduce(
88
+ (acc, [key, value2]) => {
89
+ return {
90
+ ...acc,
91
+ [key]: serialize(value2)
92
+ };
93
+ },
94
+ {}
95
+ )
96
+ };
97
+ }
98
+ return void 0;
99
+ }
100
+
101
+ // src/crud/crud_form_provider.tsx
42
102
  var FormContext = (0, import_react.createContext)({});
43
103
  function useFormContext() {
44
104
  return (0, import_react.useContext)(FormContext);
45
105
  }
46
106
  function CrudFormProvider({
47
- primaryKey = "id",
107
+ primaryKey,
48
108
  name,
49
109
  prefix,
50
110
  item,
@@ -53,7 +113,13 @@ function CrudFormProvider({
53
113
  }) {
54
114
  const apiPrefix = `/api${prefix}`;
55
115
  const store = (0, import_react_store_input.useStore)({
56
- ...item || {}
116
+ ...item || {},
117
+ ...Object.fromEntries(
118
+ Object.entries(columns).map(([key, value]) => [
119
+ key,
120
+ value.defaultValue !== void 0 ? value.defaultValue : void 0
121
+ ])
122
+ )
57
123
  });
58
124
  const navigate = (0, import_react_router.useNavigate)();
59
125
  const submit = async () => {
@@ -62,57 +128,7 @@ function CrudFormProvider({
62
128
  headers: {
63
129
  "Content-Type": "application/json"
64
130
  },
65
- body: JSON.stringify(
66
- Object.entries(store.state).reduce(function reducer(acc, [key, value]) {
67
- const converter = (value2) => {
68
- if (value2 === void 0) {
69
- return void 0;
70
- }
71
- if (value2 === null) {
72
- return {
73
- type: "null",
74
- value: null
75
- };
76
- }
77
- if (typeof value2 === "string") {
78
- return {
79
- type: "string",
80
- value: value2
81
- };
82
- }
83
- if (typeof value2 === "number") {
84
- return {
85
- type: "number",
86
- value: value2
87
- };
88
- }
89
- if (typeof value2 === "boolean") {
90
- return {
91
- type: "boolean",
92
- value: value2
93
- };
94
- }
95
- if (value2 instanceof Date) {
96
- return {
97
- type: "date",
98
- value: value2.toISOString()
99
- };
100
- }
101
- if (Array.isArray(value2)) {
102
- return value2.map((v) => converter(v));
103
- }
104
- if (typeof value2 === "object") {
105
- return Object.entries(
106
- value2
107
- ).reduce(reducer, {});
108
- }
109
- };
110
- return {
111
- ...acc,
112
- [key]: converter(value)
113
- };
114
- }, {})
115
- )
131
+ body: JSON.stringify(serialize(store.state))
116
132
  });
117
133
  if (!res.ok) {
118
134
  const { message } = await res.json();
@@ -3,12 +3,72 @@ import { useNavigate } from "react-router";
3
3
  import { useStore } from "react-store-input";
4
4
  import { createContext, useContext } from "react";
5
5
  import React from "react";
6
+
7
+ // src/crud/serialize.ts
8
+ function serialize(value) {
9
+ if (value === void 0) {
10
+ return void 0;
11
+ }
12
+ if (value === null) {
13
+ return {
14
+ type: "null",
15
+ value: null
16
+ };
17
+ }
18
+ if (typeof value === "string") {
19
+ return {
20
+ type: "string",
21
+ value
22
+ };
23
+ }
24
+ if (typeof value === "number") {
25
+ return {
26
+ type: "number",
27
+ value
28
+ };
29
+ }
30
+ if (typeof value === "boolean") {
31
+ return {
32
+ type: "boolean",
33
+ value
34
+ };
35
+ }
36
+ if (value instanceof Date) {
37
+ return {
38
+ type: "date",
39
+ value: value.toISOString()
40
+ };
41
+ }
42
+ if (Array.isArray(value)) {
43
+ return {
44
+ type: "array",
45
+ value: value.map((item) => serialize(item))
46
+ };
47
+ }
48
+ if (typeof value === "object") {
49
+ return {
50
+ type: "object",
51
+ value: Object.entries(value).reduce(
52
+ (acc, [key, value2]) => {
53
+ return {
54
+ ...acc,
55
+ [key]: serialize(value2)
56
+ };
57
+ },
58
+ {}
59
+ )
60
+ };
61
+ }
62
+ return void 0;
63
+ }
64
+
65
+ // src/crud/crud_form_provider.tsx
6
66
  var FormContext = createContext({});
7
67
  function useFormContext() {
8
68
  return useContext(FormContext);
9
69
  }
10
70
  function CrudFormProvider({
11
- primaryKey = "id",
71
+ primaryKey,
12
72
  name,
13
73
  prefix,
14
74
  item,
@@ -17,7 +77,13 @@ function CrudFormProvider({
17
77
  }) {
18
78
  const apiPrefix = `/api${prefix}`;
19
79
  const store = useStore({
20
- ...item || {}
80
+ ...item || {},
81
+ ...Object.fromEntries(
82
+ Object.entries(columns).map(([key, value]) => [
83
+ key,
84
+ value.defaultValue !== void 0 ? value.defaultValue : void 0
85
+ ])
86
+ )
21
87
  });
22
88
  const navigate = useNavigate();
23
89
  const submit = async () => {
@@ -26,57 +92,7 @@ function CrudFormProvider({
26
92
  headers: {
27
93
  "Content-Type": "application/json"
28
94
  },
29
- body: JSON.stringify(
30
- Object.entries(store.state).reduce(function reducer(acc, [key, value]) {
31
- const converter = (value2) => {
32
- if (value2 === void 0) {
33
- return void 0;
34
- }
35
- if (value2 === null) {
36
- return {
37
- type: "null",
38
- value: null
39
- };
40
- }
41
- if (typeof value2 === "string") {
42
- return {
43
- type: "string",
44
- value: value2
45
- };
46
- }
47
- if (typeof value2 === "number") {
48
- return {
49
- type: "number",
50
- value: value2
51
- };
52
- }
53
- if (typeof value2 === "boolean") {
54
- return {
55
- type: "boolean",
56
- value: value2
57
- };
58
- }
59
- if (value2 instanceof Date) {
60
- return {
61
- type: "date",
62
- value: value2.toISOString()
63
- };
64
- }
65
- if (Array.isArray(value2)) {
66
- return value2.map((v) => converter(v));
67
- }
68
- if (typeof value2 === "object") {
69
- return Object.entries(
70
- value2
71
- ).reduce(reducer, {});
72
- }
73
- };
74
- return {
75
- ...acc,
76
- [key]: converter(value)
77
- };
78
- }, {})
79
- )
95
+ body: JSON.stringify(serialize(store.state))
80
96
  });
81
97
  if (!res.ok) {
82
98
  const { message } = await res.json();
@@ -88,6 +88,45 @@ var import_http = require("dn-react-toolkit/http");
88
88
  var import_drizzle_orm2 = require("drizzle-orm");
89
89
  var import_react_router = require("react-router");
90
90
  var import_uuid = require("uuid");
91
+
92
+ // src/crud/serialize.ts
93
+ function deserialize(data) {
94
+ if (data === void 0) {
95
+ return void 0;
96
+ }
97
+ if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
98
+ const { type, value } = data;
99
+ switch (type) {
100
+ case "null":
101
+ return null;
102
+ case "string":
103
+ return value;
104
+ case "number":
105
+ return value;
106
+ case "boolean":
107
+ return value;
108
+ case "date":
109
+ return new Date(value);
110
+ case "array":
111
+ return value.map((item) => deserialize(item));
112
+ case "object":
113
+ return Object.entries(value).reduce(
114
+ (acc, [key, value2]) => {
115
+ return {
116
+ ...acc,
117
+ [key]: deserialize(value2)
118
+ };
119
+ },
120
+ {}
121
+ );
122
+ default:
123
+ return void 0;
124
+ }
125
+ }
126
+ return void 0;
127
+ }
128
+
129
+ // src/api/create_api_handler.ts
91
130
  function apiHandler({
92
131
  withAuthAction,
93
132
  repository,
@@ -106,51 +145,10 @@ function apiHandler({
106
145
  case "POST":
107
146
  case "PUT": {
108
147
  const serilaizedParams = await request.json();
109
- const params = Object.entries(serilaizedParams).reduce(
110
- function reducer(acc, [key, value]) {
111
- const converter = (value2) => {
112
- if (value2.type === "null") {
113
- return null;
114
- }
115
- if (value2.type === "string") {
116
- return value2.value;
117
- }
118
- if (value2.type === "number") {
119
- return value2.value;
120
- }
121
- if (value2.type === "boolean") {
122
- return value2.value;
123
- }
124
- if (value2.type === "date") {
125
- return new Date(value2.value);
126
- }
127
- if (Array.isArray(value2)) {
128
- return value2.map((v) => converter(v));
129
- }
130
- if (typeof value2 === "object") {
131
- return Object.entries(value2).reduce(
132
- reducer,
133
- {}
134
- );
135
- }
136
- };
137
- const result = converter(value);
138
- if (result === void 0) {
139
- return acc;
140
- }
141
- return {
142
- ...acc,
143
- [key]: result
144
- };
145
- },
146
- {}
147
- );
148
+ const params = deserialize(serilaizedParams);
148
149
  if (validators) {
149
150
  const paramsForValidation = Object.keys(validators).filter(
150
- (key) => Object.prototype.hasOwnProperty.call(
151
- validators,
152
- key
153
- )
151
+ (key) => Object.prototype.hasOwnProperty.call(validators, key)
154
152
  );
155
153
  for (const paramKey of paramsForValidation) {
156
154
  const value = params[paramKey];
@@ -74,6 +74,45 @@ import {
74
74
  redirect
75
75
  } from "react-router";
76
76
  import { v4 } from "uuid";
77
+
78
+ // src/crud/serialize.ts
79
+ function deserialize(data) {
80
+ if (data === void 0) {
81
+ return void 0;
82
+ }
83
+ if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
84
+ const { type, value } = data;
85
+ switch (type) {
86
+ case "null":
87
+ return null;
88
+ case "string":
89
+ return value;
90
+ case "number":
91
+ return value;
92
+ case "boolean":
93
+ return value;
94
+ case "date":
95
+ return new Date(value);
96
+ case "array":
97
+ return value.map((item) => deserialize(item));
98
+ case "object":
99
+ return Object.entries(value).reduce(
100
+ (acc, [key, value2]) => {
101
+ return {
102
+ ...acc,
103
+ [key]: deserialize(value2)
104
+ };
105
+ },
106
+ {}
107
+ );
108
+ default:
109
+ return void 0;
110
+ }
111
+ }
112
+ return void 0;
113
+ }
114
+
115
+ // src/api/create_api_handler.ts
77
116
  function apiHandler({
78
117
  withAuthAction,
79
118
  repository,
@@ -92,51 +131,10 @@ function apiHandler({
92
131
  case "POST":
93
132
  case "PUT": {
94
133
  const serilaizedParams = await request.json();
95
- const params = Object.entries(serilaizedParams).reduce(
96
- function reducer(acc, [key, value]) {
97
- const converter = (value2) => {
98
- if (value2.type === "null") {
99
- return null;
100
- }
101
- if (value2.type === "string") {
102
- return value2.value;
103
- }
104
- if (value2.type === "number") {
105
- return value2.value;
106
- }
107
- if (value2.type === "boolean") {
108
- return value2.value;
109
- }
110
- if (value2.type === "date") {
111
- return new Date(value2.value);
112
- }
113
- if (Array.isArray(value2)) {
114
- return value2.map((v) => converter(v));
115
- }
116
- if (typeof value2 === "object") {
117
- return Object.entries(value2).reduce(
118
- reducer,
119
- {}
120
- );
121
- }
122
- };
123
- const result = converter(value);
124
- if (result === void 0) {
125
- return acc;
126
- }
127
- return {
128
- ...acc,
129
- [key]: result
130
- };
131
- },
132
- {}
133
- );
134
+ const params = deserialize(serilaizedParams);
134
135
  if (validators) {
135
136
  const paramsForValidation = Object.keys(validators).filter(
136
- (key) => Object.prototype.hasOwnProperty.call(
137
- validators,
138
- key
139
- )
137
+ (key) => Object.prototype.hasOwnProperty.call(validators, key)
140
138
  );
141
139
  for (const paramKey of paramsForValidation) {
142
140
  const value = params[paramKey];
@@ -4,14 +4,15 @@ import React__default from 'react';
4
4
  import 'react-store-input';
5
5
  import '../table/table.mjs';
6
6
 
7
- type CrudPageOptions<TModel, TPrimaryKey extends keyof TModel> = {
7
+ type CrudPageOptions<TModel> = {
8
8
  name: string;
9
+ primaryKey: keyof TModel;
9
10
  tablePageOptions: Omit<TablePageOptions<TModel>, "name">;
10
11
  formOptions: {
11
12
  form?: React__default.FC;
12
- columns: CrudFormProps<TModel, TPrimaryKey>["columns"];
13
+ columns: CrudFormProps<TModel>["columns"];
13
14
  };
14
- AdminHeader: React__default.FC<{
15
+ header: React__default.FC<{
15
16
  title: string;
16
17
  actions?: React__default.ReactNode;
17
18
  className?: string;
@@ -19,6 +20,6 @@ type CrudPageOptions<TModel, TPrimaryKey extends keyof TModel> = {
19
20
  }>;
20
21
  };
21
22
  type CrudPage = (prefix: string) => React__default.FC;
22
- declare function crudPage<TModel, TPrimaryKey extends keyof TModel = "id" extends keyof TModel ? "id" : never>({ name, tablePageOptions, formOptions, AdminHeader, }: CrudPageOptions<TModel, TPrimaryKey>): CrudPage;
23
+ declare function crudPage<TModel>({ name, primaryKey, tablePageOptions, formOptions, header: header, }: CrudPageOptions<TModel>): CrudPage;
23
24
 
24
25
  export { type CrudPage, type CrudPageOptions, crudPage };
@@ -4,14 +4,15 @@ import React__default from 'react';
4
4
  import 'react-store-input';
5
5
  import '../table/table.js';
6
6
 
7
- type CrudPageOptions<TModel, TPrimaryKey extends keyof TModel> = {
7
+ type CrudPageOptions<TModel> = {
8
8
  name: string;
9
+ primaryKey: keyof TModel;
9
10
  tablePageOptions: Omit<TablePageOptions<TModel>, "name">;
10
11
  formOptions: {
11
12
  form?: React__default.FC;
12
- columns: CrudFormProps<TModel, TPrimaryKey>["columns"];
13
+ columns: CrudFormProps<TModel>["columns"];
13
14
  };
14
- AdminHeader: React__default.FC<{
15
+ header: React__default.FC<{
15
16
  title: string;
16
17
  actions?: React__default.ReactNode;
17
18
  className?: string;
@@ -19,6 +20,6 @@ type CrudPageOptions<TModel, TPrimaryKey extends keyof TModel> = {
19
20
  }>;
20
21
  };
21
22
  type CrudPage = (prefix: string) => React__default.FC;
22
- declare function crudPage<TModel, TPrimaryKey extends keyof TModel = "id" extends keyof TModel ? "id" : never>({ name, tablePageOptions, formOptions, AdminHeader, }: CrudPageOptions<TModel, TPrimaryKey>): CrudPage;
23
+ declare function crudPage<TModel>({ name, primaryKey, tablePageOptions, formOptions, header: header, }: CrudPageOptions<TModel>): CrudPage;
23
24
 
24
25
  export { type CrudPage, type CrudPageOptions, crudPage };