@web-ts-toolkit/access-router 0.32.0 → 0.34.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.
package/llms.txt CHANGED
@@ -5,49 +5,72 @@ ACL-aware Express routers and in-memory data services for Mongoose-backed APIs.
5
5
  ## Main Patterns
6
6
 
7
7
  ```ts
8
- import acl from '@web-ts-toolkit/access-router';
8
+ import express from 'express';
9
+ import mongoose from 'mongoose';
10
+ import acl, { permissionsPlugin } from '@web-ts-toolkit/access-router';
9
11
 
10
- // configure global permissions
11
- acl.setGlobalOptions({ globalPermissions(req) { return []; } });
12
+ acl.setGlobalOptions({
13
+ requestPermissionField: '_permissions',
14
+ globalPermissions(req) { return req.headers.user === 'admin' ? ['isAdmin'] : []; },
15
+ });
16
+
17
+ const userSchema = new mongoose.Schema({ name: String, role: String, public: Boolean });
18
+ userSchema.plugin(permissionsPlugin, { modelName: 'User' });
19
+ mongoose.model('User', userSchema);
12
20
 
13
- // model CRUD router from a Mongoose model name
14
21
  const userRouter = acl.createRouter('User', { basePath: '/users' });
15
22
 
16
- // in-memory data router
23
+ // Also: pass a mongoose.Model instance to preserve a non-default connection.
24
+ // acl.createRouter(UserModel, { basePath: '/users' });
25
+
17
26
  const fruitRouter = acl.createDataRouter('fruit', {
18
27
  basePath: '/fruit',
19
28
  idField: 'id',
20
29
  data: [{ id: 'apple', name: 'Apple' }],
21
30
  });
22
31
 
23
- // OpenAPI + Swagger UI
24
32
  const docsRouter = acl.createOpenApiRouter({ title: 'Example API', version: '1.0.0' });
25
33
  ```
26
34
 
27
35
  Validation adapters:
28
36
 
29
37
  ```ts
38
+ import { z } from 'zod';
30
39
  import { fromZod, defineRequestSchema } from '@web-ts-toolkit/access-router';
31
-
32
40
  const validator = fromZod(z.object({ name: z.string() }));
33
41
  const schema = defineRequestSchema(validator);
34
42
  ```
35
43
 
44
+ Isolated runtime (no global state shared with default `acl`):
45
+
46
+ ```ts
47
+ import { createAccessRuntime } from '@web-ts-toolkit/access-router';
48
+ import mongoose from 'mongoose';
49
+ const runtime = createAccessRuntime();
50
+ runtime.setGlobalOptions({ globalPermissions: () => [] });
51
+ runtime.createRouter('User', { basePath: '/users' });
52
+ const UserModel = mongoose.model('User', new mongoose.Schema({ name: String }));
53
+ runtime.registerModelInstance('User', UserModel);
54
+ ```
55
+
36
56
  Subpath imports:
37
57
 
38
58
  ```ts
39
59
  import { copyAndDepopulate } from '@web-ts-toolkit/access-router/processors';
40
- import { AccessRuntime, defaultRuntime } from '@web-ts-toolkit/access-router/advanced';
60
+ import { parseBody, Codes, MIDDLEWARE } from '@web-ts-toolkit/access-router/advanced';
41
61
  ```
42
62
 
43
63
  ## Gotchas
44
64
 
45
65
  - peer dependencies: `express >= 5` and `mongoose >= 8`
46
- - default export `acl` and named exports are both available; default is preferred for the runtime API
47
- - `createRouter('User', ...)` takes a Mongoose model name string, not the model instance
66
+ - default export `acl` is preferred for the default-runtime API; named exports are also available
67
+ - `acl.createOpenApiRouter(options)` uses the default runtime; the standalone `createOpenApiRouter(runtime, options)` export requires the runtime as the first argument
68
+ - `createRouter('User', ...)` accepts a Mongoose model name OR a `mongoose.Model` instance — passing the instance registers the model with the runtime and preserves non-default `mongoose.createConnection()` instances
69
+ - `/advanced` does NOT export `acl`, `defaultRuntime`, `createRouter`, or `createAccessRuntime` — those live on the root entry only
48
70
  - `createDataRouter(...)` holds data in-memory; use it for lightweight/non-persisted resources
71
+ - `package.json` declares `sideEffects` array listing the bundled runtime entries that run the lazy `mongoose-schema-jsonschema` patch on first runtime construction — bundlers must retain those entries
49
72
 
50
73
  ## Pointers
51
74
 
52
- - README: installation, quickstart, main exports
75
+ - README: installation, quickstart (Express + Mongoose end-to-end), main exports, runtime isolation, createRouter overloads
53
76
  - website/docs/packages/access-router/: full documentation (routing, configuration, hooks, validation, openapi)
package/package.json CHANGED
@@ -2,8 +2,13 @@
2
2
  "name": "@web-ts-toolkit/access-router",
3
3
  "description": "ACL-aware Express routers and in-memory data services for Mongoose-backed APIs",
4
4
  "homepage": "https://web-ts-toolkit.pages.dev/docs/packages/access-router",
5
- "version": "0.32.0",
6
- "sideEffects": false,
5
+ "version": "0.34.0",
6
+ "sideEffects": [
7
+ "./**/index.js",
8
+ "./**/index.mjs",
9
+ "./**/advanced.js",
10
+ "./**/advanced.mjs"
11
+ ],
7
12
  "keywords": [
8
13
  "express",
9
14
  "mongoose",
@@ -37,11 +42,11 @@
37
42
  }
38
43
  },
39
44
  "engines": {
40
- "node": ">=20"
45
+ "node": ">=22"
41
46
  },
42
47
  "dependencies": {
43
- "@web-ts-toolkit/express-json-router": "0.32.0",
44
- "@web-ts-toolkit/utils": "0.32.0",
48
+ "@web-ts-toolkit/express-json-router": "0.34.0",
49
+ "@web-ts-toolkit/utils": "0.34.0",
45
50
  "just-diff": "^6.0.2",
46
51
  "mongoose-schema-jsonschema": "^4.0.1",
47
52
  "sift": "^17.1.3",