Skip to content

Commit cef9ffb

Browse files
author
pintux
committed
v1.1.0, JSON-LD middleware added
1 parent 565047d commit cef9ffb

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

README.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Installation
2222
Available Middlewares
2323
---------------------
2424

25-
**jsonExtension**
25+
**jsonExtension()**
2626

2727
This middleware allows to use the dot-notation `.json` in API endpoints URL (Express route paths).
2828
This feature can be used as a shortcut to require JSON representations of resources, instead of using `Accept: application/json` HTTP header only for content-negotiation in an API.
@@ -32,9 +32,16 @@ OK, wait a moment... why using dot-notation when we have HTTP headers?
3232
Be pragmatic! And make your API users happy, as explained in [this guide](https://leanpub.com/thewebapinntux).
3333

3434

35-
Basic Example
35+
36+
**jsonLDExtension()**
37+
This middleware allows to use the dot-notation `.jsonld` in API endpoints URL (Express route paths).
38+
This feature can be used as a shortcut to require JSON-LD representations of resources, instead of using `Accept: application/ld+json` HTTP header only for content-negotiation in an API.
39+
40+
Examples
3641
-------------
3742

43+
**1) Enabling .json requests**
44+
3845
In your Express `app.js` file, set the path:
3946

4047
```js
@@ -72,13 +79,73 @@ router.get('/', (req, res, next) => {
7279
});
7380

7481
```
75-
Using this middleware it will be possible to call your endpoint, also with request like:
82+
Through this middleware it will be possible to call your endpoint with a request like:
83+
84+
`GET /books.json`
85+
86+
or like:
87+
88+
`GET /books`, using the `Accept: application/json` HTTP header.
89+
90+
---
91+
92+
93+
**2) Enabling .json AND .jsonld requests**
94+
95+
In your Express `app.js` file, set the path:
96+
97+
```js
98+
99+
app.use('/books(.json)?(.jsonld)?', books);
100+
```
101+
102+
In your `routes/books.js` Route file:
103+
104+
105+
```js
106+
107+
const middl = require('webapi-utils').middleware;
108+
...
109+
110+
router.use(middl.jsonExtension());
111+
router.use(middl.jsonLDExtension());
112+
...
113+
114+
115+
/* GET books listing. */
116+
router.get('/', (req, res, next) => {
117+
res.format({
118+
text: function(){
119+
res.send(...); //send books as text
120+
},
121+
122+
html: function(){
123+
res.send(...); //send books as html
124+
},
125+
126+
json: function(){
127+
res.send({ books: [...] }); //send books as json
128+
},
129+
130+
'application/ld+json': function(){
131+
res.send({
132+
"@context": "...",
133+
"title": "Web API",
134+
...
135+
});
136+
}
137+
});
138+
});
139+
140+
```
141+
Through this middleware it will be possible to call your endpoint with a request like:
76142

77143
`GET /books.json`
144+
`GET /books.jsonld`
78145

79146
or like:
80147

81-
`GET /books`, with `Accept: application/json` HTTP header.
148+
`GET /books`, using the `Accept: application/ld+json` HTTP header or `Accept: application/json`.
82149

83150

84151
Contributors

middlewares/middlewares.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const debug = require('debug')('webapi-utils.middlewares:jsonExtension');
1010

11+
//JSON, raw
1112
exports.jsonExtension = () => {
1213
return (req, res, next) => {
1314
try {
@@ -16,6 +17,24 @@ exports.jsonExtension = () => {
1617
req.headers['accept'] = 'application/json'
1718
}
1819

20+
} catch (error) {
21+
debug(error);
22+
}
23+
finally {
24+
next();
25+
}
26+
}
27+
};
28+
29+
//JSON-LD
30+
exports.jsonLDExtension = () => {
31+
return (req, res, next) => {
32+
try {
33+
if (req.originalUrl.endsWith('.jsonld')) {
34+
debug('.json-ld extension found');
35+
req.headers['accept'] = 'application/ld+json'
36+
}
37+
1938
} catch (error) {
2039
debug(error);
2140
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webapi-utils",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Node.js / Express utilities and middlewares for Web APIs",
55
"homepage":"https://github.com/pintux/webapi-utils.git",
66
"main": "index.js",

0 commit comments

Comments
 (0)