@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.
Files changed (53) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +101 -0
  4. package/dist/chunk-FJWRITBO.js +52 -0
  5. package/dist/chunk-FJWRITBO.js.map +1 -0
  6. package/dist/chunk-UBXIGP5H.mjs +52 -0
  7. package/dist/chunk-UBXIGP5H.mjs.map +1 -0
  8. package/dist/index.d.ts +155 -0
  9. package/dist/index.js +1101 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/index.mjs +1101 -0
  12. package/dist/index.mjs.map +1 -0
  13. package/dist/next/index.d.ts +8 -0
  14. package/dist/next/index.js +729 -0
  15. package/dist/next/index.js.map +1 -0
  16. package/dist/next/index.mjs +729 -0
  17. package/dist/next/index.mjs.map +1 -0
  18. package/dist/types.d-6817d247.d.ts +155 -0
  19. package/package.json +136 -0
  20. package/src/adapter/prisma/index.ts +241 -0
  21. package/src/adapter/prisma/types.d.ts +46 -0
  22. package/src/adapter/prisma/utils/models-to-route-names.ts +12 -0
  23. package/src/adapter/prisma/utils/parse-cursor.ts +26 -0
  24. package/src/adapter/prisma/utils/parse-order-by.ts +21 -0
  25. package/src/adapter/prisma/utils/parse-recursive.ts +26 -0
  26. package/src/adapter/prisma/utils/parse-where.ts +197 -0
  27. package/src/base-crud-handler.ts +181 -0
  28. package/src/handler/create.ts +21 -0
  29. package/src/handler/delete.ts +27 -0
  30. package/src/handler/list.ts +62 -0
  31. package/src/handler/read.ts +27 -0
  32. package/src/handler/update.ts +29 -0
  33. package/src/index.ts +27 -0
  34. package/src/next/api/edge/index.ts +23 -0
  35. package/src/next/api/node/index.ts +27 -0
  36. package/src/next/index.ts +2 -0
  37. package/src/query-parser.ts +94 -0
  38. package/src/swagger/adapter/prisma/index.ts +95 -0
  39. package/src/swagger/json-schema-parser.ts +456 -0
  40. package/src/swagger/parameters.ts +83 -0
  41. package/src/swagger/types.d.ts +53 -0
  42. package/src/swagger/utils/format-example-ref.ts +4 -0
  43. package/src/swagger/utils/format-schema-ref.ts +4 -0
  44. package/src/swagger/utils/get-models-accessible-routes.ts +23 -0
  45. package/src/swagger/utils/get-swagger-paths.ts +244 -0
  46. package/src/swagger/utils/get-swagger-tags.ts +13 -0
  47. package/src/types.d.ts +124 -0
  48. package/src/utils/format-resource-id.ts +3 -0
  49. package/src/utils/get-accessible-routes.ts +18 -0
  50. package/src/utils/get-resource-name-from-url.ts +23 -0
  51. package/src/utils/get-route-type.ts +99 -0
  52. package/src/utils/is-primitive.ts +5 -0
  53. 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,5 @@
1
+ const primitiveTypes = new Set(["string", "boolean", "number"]);
2
+
3
+ const isPrimitive = (value: any): boolean => primitiveTypes.has(typeof value);
4
+
5
+ export default isPrimitive;
@@ -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;