@nano-app/backend 0.12.1 → 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 +9 -8
- package/build/index.d.ts +1 -0
- package/build/index.js +6 -1
- package/package.json +2 -2
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 () => ({
|
|
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 => ({
|
|
57
|
-
DELETE: async req => ({
|
|
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
|
-
|
|
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;
|
|
@@ -137,7 +138,7 @@ import { createRoute } from '@nano-app/backend';
|
|
|
137
138
|
import type { PrismaClient } from '../../.nano-app/prisma-clients/app/generated/client';
|
|
138
139
|
|
|
139
140
|
export default createRoute<PrismaClient>({
|
|
140
|
-
GET: async (_req, { db }) => ({
|
|
141
|
+
GET: async (_req, { db }) => ({ result: await db!.user.findMany() }),
|
|
141
142
|
});
|
|
142
143
|
```
|
|
143
144
|
|
|
@@ -222,7 +223,7 @@ export default createPlugin((options?: { basePath?: string }) => ({
|
|
|
222
223
|
import { createRoute } from '@nano-app/backend';
|
|
223
224
|
|
|
224
225
|
export default createRoute({
|
|
225
|
-
GET: async () => ({
|
|
226
|
+
GET: async () => ({ result: { ok: true } }),
|
|
226
227
|
});
|
|
227
228
|
```
|
|
228
229
|
|
|
@@ -263,7 +264,7 @@ The endpoints of one plugin have no guaranteed order: one route's `onInit` must
|
|
|
263
264
|
|
|
264
265
|
Everything is mounted on one Express application, in this order: base middlewares → `before` plugins → your routes → `after` plugins → 404 → error handler.
|
|
265
266
|
|
|
266
|
-
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 [
|
|
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).
|
|
267
268
|
|
|
268
269
|
## Errors
|
|
269
270
|
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -191,7 +191,12 @@ var Endpoint = class {
|
|
|
191
191
|
...oRequestParams,
|
|
192
192
|
onBeforeResult
|
|
193
193
|
})).then((oResponse) => {
|
|
194
|
-
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nano-app/backend",
|
|
3
|
-
"version": "0.12.
|
|
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.
|
|
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",
|