@visulima/crud 1.0.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/CHANGELOG.md +14 -0
- package/LICENSE.md +21 -0
- package/README.md +101 -0
- package/dist/chunk-FJWRITBO.js +52 -0
- package/dist/chunk-FJWRITBO.js.map +1 -0
- package/dist/chunk-UBXIGP5H.mjs +52 -0
- package/dist/chunk-UBXIGP5H.mjs.map +1 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +1101 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1101 -0
- package/dist/index.mjs.map +1 -0
- package/dist/next/index.d.ts +8 -0
- package/dist/next/index.js +729 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/index.mjs +729 -0
- package/dist/next/index.mjs.map +1 -0
- package/dist/types.d-6817d247.d.ts +155 -0
- package/package.json +136 -0
- package/src/adapter/prisma/index.ts +241 -0
- package/src/adapter/prisma/types.d.ts +46 -0
- package/src/adapter/prisma/utils/models-to-route-names.ts +12 -0
- package/src/adapter/prisma/utils/parse-cursor.ts +26 -0
- package/src/adapter/prisma/utils/parse-order-by.ts +21 -0
- package/src/adapter/prisma/utils/parse-recursive.ts +26 -0
- package/src/adapter/prisma/utils/parse-where.ts +197 -0
- package/src/base-crud-handler.ts +181 -0
- package/src/handler/create.ts +21 -0
- package/src/handler/delete.ts +27 -0
- package/src/handler/list.ts +62 -0
- package/src/handler/read.ts +27 -0
- package/src/handler/update.ts +29 -0
- package/src/index.ts +27 -0
- package/src/next/api/edge/index.ts +23 -0
- package/src/next/api/node/index.ts +27 -0
- package/src/next/index.ts +2 -0
- package/src/query-parser.ts +94 -0
- package/src/swagger/adapter/prisma/index.ts +95 -0
- package/src/swagger/json-schema-parser.ts +456 -0
- package/src/swagger/parameters.ts +83 -0
- package/src/swagger/types.d.ts +53 -0
- package/src/swagger/utils/format-example-ref.ts +4 -0
- package/src/swagger/utils/format-schema-ref.ts +4 -0
- package/src/swagger/utils/get-models-accessible-routes.ts +23 -0
- package/src/swagger/utils/get-swagger-paths.ts +244 -0
- package/src/swagger/utils/get-swagger-tags.ts +13 -0
- package/src/types.d.ts +124 -0
- package/src/utils/format-resource-id.ts +3 -0
- package/src/utils/get-accessible-routes.ts +18 -0
- package/src/utils/get-resource-name-from-url.ts +23 -0
- package/src/utils/get-route-type.ts +99 -0
- package/src/utils/is-primitive.ts +5 -0
- package/src/utils/validate-adapter-methods.ts +15 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
|
|
3
|
+
import { RouteType } from "../types.d";
|
|
4
|
+
|
|
5
|
+
type PathMatch = { id: string };
|
|
6
|
+
|
|
7
|
+
const getRouteType: (
|
|
8
|
+
method: string,
|
|
9
|
+
url: string,
|
|
10
|
+
resourceName: string,
|
|
11
|
+
) => GetRouteType = (method, url, resourceName) => {
|
|
12
|
+
// Exclude the query params from the path
|
|
13
|
+
const realPath = url.split("?")[0];
|
|
14
|
+
|
|
15
|
+
if (typeof realPath === "undefined") {
|
|
16
|
+
throw new TypeError("Path is undefined");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!realPath.includes(`/${resourceName}`)) {
|
|
20
|
+
throw new Error(`invalid resource name '${resourceName}' for route '${realPath}'`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const entityMatcher = match<PathMatch>([`/(.*)/${resourceName}`, `/(.*)/${resourceName}/:id`], { decode: decodeURIComponent });
|
|
24
|
+
const simpleMatcher = match(`/(.*)/${resourceName}`, {
|
|
25
|
+
decode: decodeURIComponent,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
switch (method) {
|
|
29
|
+
case "GET": {
|
|
30
|
+
const pathMatch = entityMatcher(realPath);
|
|
31
|
+
|
|
32
|
+
// If we got a /something after the resource name, we are reading 1 entity
|
|
33
|
+
if (pathMatch && pathMatch.params.id) {
|
|
34
|
+
return {
|
|
35
|
+
routeType: RouteType.READ_ONE,
|
|
36
|
+
resourceId: pathMatch.params.id,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
routeType: RouteType.READ_ALL,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
case "POST": {
|
|
45
|
+
const pathMatch = simpleMatcher(realPath);
|
|
46
|
+
|
|
47
|
+
if (pathMatch) {
|
|
48
|
+
return {
|
|
49
|
+
routeType: RouteType.CREATE,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
routeType: null,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
case "PUT":
|
|
58
|
+
case "PATCH": {
|
|
59
|
+
const pathMatch = entityMatcher(realPath);
|
|
60
|
+
|
|
61
|
+
if (pathMatch && pathMatch.params.id) {
|
|
62
|
+
return {
|
|
63
|
+
routeType: RouteType.UPDATE,
|
|
64
|
+
resourceId: pathMatch.params.id,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
routeType: null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
case "DELETE": {
|
|
73
|
+
const pathMatch = entityMatcher(realPath);
|
|
74
|
+
|
|
75
|
+
if (pathMatch && pathMatch.params.id) {
|
|
76
|
+
return {
|
|
77
|
+
routeType: RouteType.DELETE,
|
|
78
|
+
resourceId: pathMatch.params.id,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
routeType: null,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
default: {
|
|
87
|
+
return {
|
|
88
|
+
routeType: null,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type GetRouteType = {
|
|
95
|
+
routeType: RouteType | null;
|
|
96
|
+
resourceId?: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export default getRouteType;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import createHttpError from "http-errors";
|
|
2
|
+
|
|
3
|
+
import type { Adapter } from "../types.d";
|
|
4
|
+
|
|
5
|
+
const adapterMethods = ["create" || "delete" || "getAll" || "getOne" || "parseQuery" || "update" || "getPaginationData" || "getModels"];
|
|
6
|
+
|
|
7
|
+
const validateAdapterMethods = <T, Q>(adapter: Adapter<T, Q>) => {
|
|
8
|
+
adapterMethods.forEach((method) => {
|
|
9
|
+
if (!adapter[method as keyof Adapter<T, Q>]) {
|
|
10
|
+
throw createHttpError(500, `Adapter must implement the "${method}" method.`);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default validateAdapterMethods;
|