@nano-app/plugins 0.6.2 → 0.7.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.
@@ -1,5 +1,5 @@
1
1
  import Query from "../Query.js";
2
- import { createRoute, isGranted } from "@nano-app/backend";
2
+ import { NanoError, createRoute, isGranted } from "@nano-app/backend";
3
3
  // crud/routes/index.route.ts
4
4
  /** Permission of Better Auth that each method needs on the URL's model */
5
5
  const ACTIONS = {
@@ -14,20 +14,18 @@ const ID_TYPES = {
14
14
  Int: Number,
15
15
  BigInt
16
16
  };
17
+ /** Reads a list, or one record, with the `QueryParams` of the body */
18
+ async function read(oReq, { onBeforeResult }) {
19
+ return Query.read(onBeforeResult?.model, oReq.body ?? {}, onBeforeResult?.where);
20
+ }
17
21
  /** `GET` reads without parameters, `QUERY` with `QueryParams` as its body */
18
22
  var index_route_default = createRoute({
19
23
  expression: "/:model{/:id}",
20
24
  callbacks: { onBefore: async (oReq, sMethod, oParams) => {
21
25
  const sModel = String(oReq.params.model);
22
26
  const { id } = oReq.params;
23
- if (!oParams.models.includes(sModel)) throw {
24
- status: 404,
25
- message: "Not found"
26
- };
27
- if (!await isGranted(oParams, { [sModel]: [ACTIONS[sMethod]] })) throw {
28
- status: 403,
29
- message: "Forbidden"
30
- };
27
+ if (!oParams.models.includes(sModel)) throw new NanoError("Not found", 404);
28
+ if (!await isGranted(oParams, { [sModel]: [ACTIONS[sMethod]] })) throw new NanoError("Forbidden", 403);
31
29
  const oModel = oParams.db[sModel];
32
30
  if (!id) return { model: oModel };
33
31
  const sId = String(id);
@@ -36,8 +34,8 @@ var index_route_default = createRoute({
36
34
  where: { id: ID_TYPES[oModel.fields.id.typeName]?.(sId) ?? sId }
37
35
  };
38
36
  } },
39
- GET: async (oReq, { onBeforeResult }) => Query.read(onBeforeResult?.model, oReq.body ?? {}, onBeforeResult?.where),
40
- QUERY: async (oReq, { onBeforeResult }) => Query.read(onBeforeResult?.model, oReq.body ?? {}, onBeforeResult?.where),
37
+ GET: read,
38
+ QUERY: read,
41
39
  POST: async (oReq, { onBeforeResult }) => ({ result: await onBeforeResult?.model.create({ data: oReq.body }) }),
42
40
  PUT: async (oReq, { onBeforeResult }) => ({ result: await onBeforeResult?.model.update({
43
41
  where: onBeforeResult.where,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nano-app/plugins",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "Official plugins for @nano-app/backend",
5
5
  "type": "module",
6
6
  "exports": {