@aglyn/shared-util-rest-api 1.0.0-beta.143 → 1.0.0-beta.144
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 +63 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
# shared-util-rest-api
|
|
1
|
+
# @aglyn/shared-util-rest-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Helpers for writing JSON API routes in Next.js: one response envelope for both the Pages Router (`NextApiResponse`) and the App Router (Web `Response`), plus small middleware and cookie utilities. Published mainly as a building block for Aglyn's own apps; usable in any Next.js project that wants the same envelope.
|
|
4
|
+
|
|
5
|
+
> Beta. Published from the Aglyn monorepo under the `beta` dist-tag; APIs can change between beta releases.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
npm install @aglyn/shared-util-rest-api@beta
|
|
10
|
+
|
|
11
|
+
Peer dependency: `next` (`16.3.3`).
|
|
12
|
+
|
|
13
|
+
## What's in it
|
|
14
|
+
|
|
15
|
+
Everything is exported from the package root; each module is also reachable as `@aglyn/shared-util-rest-api/<file-name>`.
|
|
16
|
+
|
|
17
|
+
The envelope, `JsonResponse`:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
type JsonResponse = {
|
|
21
|
+
error?: any
|
|
22
|
+
errorCode?: HttpRefCode
|
|
23
|
+
status?: HttpResponseStatus | true
|
|
24
|
+
statusCode?: HttpStatusCode
|
|
25
|
+
statusMessage?: string
|
|
26
|
+
data?: any
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- App Router (`route.ts`): `appHandleJsonResponse(statusCode, options?)`, `appHandleJsonSuccess(data)`, `appHandleJsonError(error)` - each returns a `Response`.
|
|
31
|
+
- Pages Router: `nextHandleJsonResponse(res, statusCode, options?)`, `nextHandleJsonSuccess(res, data)`, `nextHandleJsonError(res, error)` - each writes to a `NextApiResponse`. Both families put the same shape on the wire. The error variants take the status from `error.code` or `error.statusCode` (default 500) and the message from `error.message` or `error.statusMessage`.
|
|
32
|
+
- `createNewJsonResponse(statusCode, options?)` - a bare `Response` with a JSON content type, for middleware.
|
|
33
|
+
- `httpRequestMethodMiddleware(allowed)` - a `next-api-middleware` middleware that answers 405 for methods other than the allowed ones (`OPTIONS` always passes).
|
|
34
|
+
- `initializeMiddleware(middleware)` - wraps a Connect-style `(req, res, next)` middleware in a promise.
|
|
35
|
+
- `requireHeader(name, key, handler)` and `withIdTokenHeader(handler)` - wrap a Pages Router handler and answer 400 when the header (`id-token` for the latter) is missing.
|
|
36
|
+
- `getApiRequestCookie(name, request?)` and `setApiResponseCookie(res, name, value, options?)`. `setApiResponseCookie` takes `maxAge` in milliseconds and serializes object values as `j:` plus JSON.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// app/api/things/route.ts
|
|
42
|
+
import {
|
|
43
|
+
appHandleJsonError,
|
|
44
|
+
appHandleJsonSuccess,
|
|
45
|
+
} from '@aglyn/shared-util-rest-api'
|
|
46
|
+
|
|
47
|
+
export async function GET() {
|
|
48
|
+
try {
|
|
49
|
+
return appHandleJsonSuccess({ things: [] })
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return appHandleJsonError(error)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Note that `appHandleJsonError` and `nextHandleJsonError` include the error object itself in the body, so pass them errors that are safe to show to the caller.
|
|
57
|
+
|
|
58
|
+
## How it fits
|
|
59
|
+
|
|
60
|
+
A `shared` package: generic, with no knowledge of Aglyn's model. Shared packages may only import other shared packages; this one depends on `@aglyn/shared-data-enums` (status codes and reference codes), `@aglyn/shared-util-errors` and `@aglyn/shared-util-http`. No other library in the monorepo depends on it; Aglyn's apps use it in their API routes.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
Apache-2.0. Source: https://github.com/aglyn/aglyn/tree/main/libs/shared/util/rest-api
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aglyn/shared-util-rest-api",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.144",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://aglyn.com",
|
|
6
6
|
"repository": {
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"./package.json": "./package.json"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aglyn/shared-data-enums": "1.0.0-beta.
|
|
29
|
-
"@aglyn/shared-util-errors": "1.0.0-beta.
|
|
30
|
-
"@aglyn/shared-util-http": "1.0.0-beta.
|
|
28
|
+
"@aglyn/shared-data-enums": "1.0.0-beta.144",
|
|
29
|
+
"@aglyn/shared-util-errors": "1.0.0-beta.144",
|
|
30
|
+
"@aglyn/shared-util-http": "1.0.0-beta.144",
|
|
31
31
|
"@swc/helpers": "0.5.23",
|
|
32
32
|
"cookie": "^2.0.1",
|
|
33
33
|
"js-cookie": "^3.0.8",
|