@nano-app/backend 0.12.0 → 0.12.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.
package/README.md CHANGED
@@ -34,7 +34,7 @@ createNanoApp({ port: 3000 });
34
34
  import { createRoute } from '@nano-app/backend';
35
35
 
36
36
  export default createRoute({
37
- GET: async () => ({ success: true, result: { status: 'ok' } }),
37
+ GET: async () => ({ result: { status: 'ok' } }),
38
38
  });
39
39
  ```
40
40
 
@@ -53,16 +53,17 @@ Each `*.route.ts` under `mainPlugin.routesDir` default-exports an object with th
53
53
  ```ts
54
54
  export default createRoute({
55
55
  expression: '/:id', // → /users/:id
56
- GET: async req => ({ success: true, result: { id: req.params.id } }),
57
- DELETE: async req => ({ success: true, message: 'Deleted' }),
56
+ GET: async req => ({ result: { id: req.params.id } }),
57
+ DELETE: async req => ({ message: 'Deleted' }),
58
58
  });
59
59
  ```
60
60
 
61
- Each handler receives `(req, params)` and returns an `ApiResponse`. `params` holds the plugin's `options`, `db` (the Prisma client when a database is enabled, see [Database](#database)) and `onBeforeResult` (see [Callbacks](#callbacks)):
61
+ Each handler receives `(req, params)` and returns an `ApiResponse` without `success`: its `status` is the HTTP status (`200` by default) and the framework sets `success` to `status < 400`. `params` holds the plugin's `options`, `db` (the Prisma client when a database is enabled, see [Database](#database)) and `onBeforeResult` (see [Callbacks](#callbacks)):
62
62
 
63
63
  ```ts
64
64
  type ApiResponse<T> = {
65
- success?: boolean; // true → HTTP 200, any other value → HTTP 400
65
+ status?: number; // HTTP status, 200 by default; not sent in the body
66
+ success?: boolean; // set by the framework: status < 400
66
67
  result?: T;
67
68
  message?: string;
68
69
  warning?: boolean;
@@ -88,6 +89,8 @@ Every option is optional.
88
89
 
89
90
  `packages` accepts `morgan`, `cors`, `helmet`, `compression`, `rateLimit`, `express` (`express.json` options), `urlencoded` and `betterAuth` (off by default: see [Better Auth](#better-auth)). The merge is **per section**: passing one key keeps the others; pass `false` to disable one.
90
91
 
92
+ `morgan` logs `:timestamp | :method :path :status` by default: `:path` is the path without its query string, so tokens sent there stay out of the logs. Set `format` with `:url` to log the full URL.
93
+
91
94
  ```ts
92
95
  createNanoApp({
93
96
  settings: { 'trust proxy': 1 },
@@ -135,7 +138,7 @@ import { createRoute } from '@nano-app/backend';
135
138
  import type { PrismaClient } from '../../.nano-app/prisma-clients/app/generated/client';
136
139
 
137
140
  export default createRoute<PrismaClient>({
138
- GET: async (_req, { db }) => ({ success: true, result: await db!.user.findMany() }),
141
+ GET: async (_req, { db }) => ({ result: await db!.user.findMany() }),
139
142
  });
140
143
  ```
141
144
 
@@ -220,7 +223,7 @@ export default createPlugin((options?: { basePath?: string }) => ({
220
223
  import { createRoute } from '@nano-app/backend';
221
224
 
222
225
  export default createRoute({
223
- GET: async () => ({ success: true, result: { ok: true } }),
226
+ GET: async () => ({ result: { ok: true } }),
224
227
  });
225
228
  ```
226
229
 
@@ -261,7 +264,7 @@ The endpoints of one plugin have no guaranteed order: one route's `onInit` must
261
264
 
262
265
  Everything is mounted on one Express application, in this order: base middlewares → `before` plugins → your routes → `after` plugins → 404 → error handler.
263
266
 
264
- A plugin with a database follows the same rules, with its client in `.nano-app/prisma-clients/<name>`: a plugin of yours points `database.schemaPath` at its schema and you apply it with Prisma. Two plugins with the same name and a database fail at startup. If that folder holds the client of another schema from an earlier start, the startup warns and replaces it. An official plugin of `@nano-app/plugins` needs nothing from you: at startup nano-app applies its migrations to its own `nano_*` schema. See [PLUGINS.md](https://github.com/Nano-Nathan/nano-app/blob/main/PLUGINS.md).
267
+ A plugin with a database follows the same rules, with its client in `.nano-app/prisma-clients/<name>`: a plugin of yours points `database.schemaPath` at its schema and you apply it with Prisma. Two plugins with the same name and a database fail at startup. If that folder holds the client of another schema from an earlier start, the startup warns and replaces it. An official plugin of `@nano-app/plugins` needs nothing from you: at startup nano-app applies its migrations to its own `nano_*` schema. See [`@nano-app/plugins`](https://github.com/Nano-Nathan/nano-app/blob/main/plugins/README.md#database).
265
268
 
266
269
  ## Errors
267
270
 
package/build/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import { BetterAuthOptions } from "better-auth";
7
7
  //#region ../core/types/api.d.ts
8
8
  /** API response */
9
9
  type ApiResponse<T = any> = {
10
+ status?: number;
10
11
  success?: boolean;
11
12
  result?: T;
12
13
  message?: string;
package/build/index.js CHANGED
@@ -191,7 +191,12 @@ var Endpoint = class {
191
191
  ...oRequestParams,
192
192
  onBeforeResult
193
193
  })).then((oResponse) => {
194
- oRes.status(oResponse.success ? 200 : 400).json(oResponse);
194
+ const { status, ...oBody } = oResponse;
195
+ const iStatus = status ?? 200;
196
+ oRes.status(iStatus).json({
197
+ ...oBody,
198
+ success: iStatus < 400
199
+ });
195
200
  }).catch(fnNext);
196
201
  };
197
202
  }
@@ -439,8 +444,11 @@ const INITIAL_CONFIG = {
439
444
  /** Initial middlewares */
440
445
  packages: {
441
446
  morgan: {
442
- tokens: { timestamp: () => Formatter.logDate() },
443
- format: ":timestamp | :method :url :status"
447
+ tokens: {
448
+ timestamp: () => Formatter.logDate(),
449
+ path: (oReq) => oReq.path
450
+ },
451
+ format: ":timestamp | :method :path :status"
444
452
  },
445
453
  cors: { methods: [
446
454
  "GET",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nano-app/backend",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "Express server with file-convention routes, plugins and an optional PostgreSQL layer",
5
5
  "type": "module",
6
6
  "main": "build/index.js",
@@ -45,7 +45,7 @@
45
45
  "dependencies": {
46
46
  "compression": "^1.8.2",
47
47
  "cors": "^2.8.6",
48
- "dotenv": "^18.0.8",
48
+ "dotenv": "^18.0.9",
49
49
  "express": "^5.3.0",
50
50
  "express-rate-limit": "^8.7.1",
51
51
  "glob": "^13.0.6",