Skip to content

Commit 4fbdf70

Browse files
committed
feat: custom rewriter is now accessible from the module root
1 parent d3b73d3 commit 4fbdf70

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

README.MD

+10-8
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ json-server-auth db.json -r routes.json
230230
# with json-server-auth installed globally
231231
```
232232

233-
###### _Other custom routes_
234-
235233
You can still add any other _normal_ custom routes :
236234

237235
```json
@@ -246,7 +244,7 @@ You can still add any other _normal_ custom routes :
246244

247245
If you go the programmatic way and [use JSON Server as a module](https://github.com/typicode/json-server#module), there is an extra step to properly integrate JSON Server Auth :
248246

249-
⚠️ You must bind the router property `db` to the created app, like the [JSON Server CLI does](https://github.com/typicode/json-server/blob/master/src/cli/run.js#L74).
247+
⚠️ You must bind the router property `db` to the created app, like the [JSON Server CLI does](https://github.com/typicode/json-server/blob/master/src/cli/run.js#L74), and you must apply the middlewares in a specific order.
250248

251249
```js
252250
const jsonServer = require('json-server')
@@ -258,28 +256,31 @@ const router = jsonServer.router('db.json')
258256
// /!\ Bind the router db to the app
259257
app.db = router.db
260258

259+
// You must apply the auth middleware before the router
261260
app.use(auth)
262261
app.use(router)
263262
app.listen(3000)
264263
```
265264

266-
##### Permisssions Rewriter
265+
#### Permisssions Rewriter
267266

268-
The custom rewriter is accessible in `json-server-auth/guards` :
267+
The custom rewriter is accessible via a subproperty :
269268

270269
```js
271-
const { rewriter } = require('json-server-auth/guards')
270+
const auth = require('json-server-auth')
272271

273-
const rules = rewriter({
272+
const rules = auth.rewriter({
274273
// Permission rules
275274
users: 600,
276275
messages: 640,
277276
// Other rules
278277
'/posts/:category': '/posts?category=:category',
279278
})
280279

281-
// Add this before app.use(router)
280+
// You must apply the middlewares in the following order
282281
app.use(rules)
282+
app.use(auth)
283+
app.use(router)
283284
```
284285

285286
## TODO 📜
@@ -290,5 +291,6 @@ app.use(rules)
290291
- [ ] Users collection name
291292
- [ ] Minimum password length
292293
- [ ] JWT expiry time
294+
- [ ] JWT property name in response
293295
- [ ] Implement JWT Refresh Token
294296
- [ ] Possibility to disable password encryption ?

src/__tests__/shared/tools.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Application } from 'express'
22
import * as jsonServer from 'json-server'
33
import * as jsonServerAuth from '../..'
4-
import { rewriter } from '../../guards'
54

65
export const USER = { email: '[email protected]', password: '123456', name: 'Jeremy' }
76

@@ -15,7 +14,7 @@ export function inMemoryJsonServer(
1514
// https://github.com/typicode/json-server/blob/master/src/cli/run.js#L74
1615
app['db'] = router['db']
1716

18-
app.use(rewriter(resourceGuardMap))
17+
app.use(jsonServerAuth.rewriter(resourceGuardMap))
1918
app.use(jsonServerAuth)
2019
app.use(router)
2120

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { RequestHandler } from 'express'
2-
import guardsRouter from './guards'
2+
import guardsRouter, { rewriter } from './guards'
33
import usersRouter from './users'
44

5-
const middlewares: RequestHandler[] = [usersRouter, guardsRouter]
5+
interface MiddlewaresWithRewriter extends Array<RequestHandler> {
6+
rewriter: typeof rewriter
7+
}
8+
9+
// @ts-ignore shut the compiler up about defining in two steps
10+
const middlewares: MiddlewaresWithRewriter = [usersRouter, guardsRouter]
11+
Object.defineProperty(middlewares, 'rewriter', { value: rewriter, enumerable: false })
612

713
// export middlewares as is, so we can simply pass the module to json-server `--middlewares` flag
814
export = middlewares

0 commit comments

Comments
 (0)