@nxgt/janus-hono 0.3.5 → 0.3.6

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
@@ -60,9 +60,29 @@ declarations import without extensions, so `nodenext` is not supported.
60
60
  | `bindJanus({ auth?, access? })` | The functions above with the instances bound: `session(options?)`, `sendSession(c, signedIn)` and `signOut(c)` with `auth`; `permission(permission, type, load, options?)` with `access`; `provide()` always |
61
61
  | `provide({ auth?, access? })` | Middleware. Sets `c.var.auth` and `c.var.access` to the instances given — only those — for a route that writes users or tuples |
62
62
  | `ObjectData<C, Type>`, `PermissionOptions`, `Instances` | The types of `load`'s answer, of `permission()`'s options and of `provide()`'s argument |
63
+ | `JanusErrorsOptions` | `{ report?, fallback? }`, the options of `janusErrors()` — for a wrapper of your own |
64
+ | `Bound<I>` | What `bindJanus(instances)` answers, for `I` the type of `instances` — to pass a bound `j` to a module of routes |
65
+ | `Bindable` | What `bindJanus()` takes: `{ auth?, access? }`, the constraint on `Bound`'s `I` |
66
+ | `BoundAuth<typeof auth>` | The part of `Bound` that `auth` brings: `session`, `sendSession` and `signOut` |
67
+ | `BoundSession<typeof auth>` | `j.session`: `session(auth, options?)` with `auth` bound, typed as `session()` is — `{ required: true }` types `c.var.user` as never `null` |
68
+ | `BoundPermission<C>` | `j.permission`: `permission(access, …)` with `access` bound, for `C` the model's config: `ConfigOf<typeof model>` from `@nxgt/janus/permissions`, or take the whole type as `Bound<…>['permission']` |
63
69
 
64
70
  The whole status table is in [`@nxgt/janus`'s errors guide](https://github.com/softistx/nxgt-janus/blob/develop/packages/janus/docs/guide/errors.md).
65
71
 
72
+ ### A wrapper of your own
73
+
74
+ A wrapper around `janusErrors()` takes the same options, with your defaults:
75
+
76
+ ```ts
77
+ import { type JanusErrorsOptions, janusErrors } from '@nxgt/janus-hono';
78
+ import { logger } from './logger'; // your own
79
+
80
+ export const appErrors = (options: JanusErrorsOptions = {}) =>
81
+ janusErrors({ report: (error) => logger.error(error), ...options });
82
+
83
+ app.onError(appErrors());
84
+ ```
85
+
66
86
  ## Second factor
67
87
 
68
88
  With `janus({ secondFactor })`, `signIn` answers a session or a challenge.
@@ -165,6 +185,24 @@ const app = new Hono()
165
185
 
166
186
  Only what is given is bound: `bindJanus({ auth })` has no `permission`.
167
187
 
188
+ A module of routes that receives `j` types it `Bound<…>`, over the types of
189
+ the instances it was given:
190
+
191
+ ```ts
192
+ import { type Bound, bindJanus, byParam } from '@nxgt/janus-hono';
193
+
194
+ export type Janus = Bound<{ auth: typeof auth; access: typeof access }>;
195
+
196
+ export const recordRoutes = (j: Janus) =>
197
+ new Hono()
198
+ .use(j.session(), j.provide())
199
+ .get('/records/:id', j.permission('view', 'record', byParam('id', (id) => records.find(id))), (c) =>
200
+ c.json(c.var.object), // typed, as with the unbound functions
201
+ );
202
+
203
+ app.route('/', recordRoutes(bindJanus({ auth, access })));
204
+ ```
205
+
168
206
  Each side is usable alone. An application that signs users in some other way
169
207
  passes `{ subject: (c) => … }` to `permission()`; one without permissions uses
170
208
  `session()` and `janusErrors()` only.
@@ -39,6 +39,16 @@ app.onError((error, c) => {
39
39
  an `HTTPException` answers its own response and anything else is a logged
40
40
  500, as Hono does by default.
41
41
 
42
+ A wrapper around `janusErrors()` takes the same options, `JanusErrorsOptions`,
43
+ with your defaults:
44
+
45
+ ```ts
46
+ import { type JanusErrorsOptions, janusErrors } from '@nxgt/janus-hono';
47
+
48
+ export const appErrors = (options: JanusErrorsOptions = {}) =>
49
+ janusErrors({ report: (error) => logger.error(error), ...options });
50
+ ```
51
+
42
52
  ### Binding the instances once
43
53
 
44
54
  Every function of this package takes `auth` or `access` first.
@@ -57,6 +67,27 @@ app.post('/sign-in', async (c) => {
57
67
  });
58
68
  ```
59
69
 
70
+ A module of routes that receives `j` rather than importing it types it
71
+ `Bound<…>`, over the types of the instances it was given — only what is given
72
+ is bound, so `Bound<{ auth: typeof auth }>` has no `permission`:
73
+
74
+ ```ts
75
+ import { type Bound, byParam } from '@nxgt/janus-hono';
76
+
77
+ export type Janus = Bound<{ auth: typeof auth; access: typeof access }>;
78
+
79
+ export const recordRoutes = (j: Janus) =>
80
+ new Hono()
81
+ .use(j.session(), j.provide())
82
+ .get('/records/:id', j.permission('view', 'record', byParam('id', (id) => records.find(id))), (c) =>
83
+ c.json(c.var.object),
84
+ );
85
+ ```
86
+
87
+ `BoundSession`, `BoundAuth` and `BoundPermission` are its parts — `j.session`,
88
+ the three functions `auth` brings, and `j.permission` — for a helper that needs
89
+ only one of them.
90
+
60
91
  The rest of this guide writes the unbound form; each call reads the same with
61
92
  `j.` and without its first argument.
62
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxgt/janus-hono",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "Hono middleware for @nxgt/janus: the signed-in user on every request, the session cookie, routes guarded by a permission, and every error as its status",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -49,13 +49,13 @@
49
49
  ]
50
50
  },
51
51
  "devDependencies": {
52
- "@nxgt/janus": "^0.10.0",
52
+ "@nxgt/janus": "^0.11.0",
53
53
  "@types/bun": "^1.4.2",
54
54
  "hono": "^4.13.9",
55
55
  "zod": "^4.6.5"
56
56
  },
57
57
  "peerDependencies": {
58
- "@nxgt/janus": "^0.10.0",
58
+ "@nxgt/janus": "^0.11.0",
59
59
  "hono": "^4.13.4",
60
60
  "typescript": "^6.0.3"
61
61
  }