Skip to content

Commit f32541d

Browse files
committed
allow URIs in prefix option
1 parent 860ef4b commit f32541d

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ server.listen(8080)
3737

3838
The `options` object is as follows:
3939

40-
- `prefix`: hyperlink prefix, without leading or trailing slashes. Default: `""` (empty string).
40+
- `prefix`: hyperlink prefix. If this prefix starts with `/`, then it will rewrite paths relative to the prefix. For example, a prefix valued `/api` will handle requests at that route like `/api/users/1`. Default: `""` (empty string).
4141
- `inflectType`: pluralize and dasherize the record type name in the URI. Can be Boolean to enable/disable all inflections or an object specifying each type in specific with unreferenced types set to default, ex: `{ faculty: false }`. Default: `true`.
4242
- `inflectKeys`: camelize the field names per record. Default: `true`.
4343
- `maxLimit`: maximum number of records to show per page. Default: `1000`.

lib/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function initializeContext (contextRequest, request, response) {
6060
methodMap[request.method]
6161

6262
// URL rewriting for prefix parameter.
63-
if (prefix && request.url.indexOf(prefix) === 0)
63+
if ((prefix || '').charAt(0) === '/' && request.url.indexOf(prefix) === 0)
6464
request.url = request.url.slice(prefix.length)
6565

6666
// Decode URI Component only for the query string.

lib/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ module.exports = Serializer => {
7575
Object.getOwnPropertyNames(this.recordTypes)
7676
)
7777

78-
// Automatically prefix leading slash.
79-
if (options.prefix && options.prefix.charAt(0) !== '/')
80-
options.prefix = `/${options.prefix}`
81-
8278
const uriTemplate = uriTemplates((options ?
8379
options.uriTemplate : null) || defaults.uriTemplate)
8480

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fortune-json-api",
33
"description": "JSON API serializer for Fortune.",
4-
"version": "2.2.3",
4+
"version": "2.3.0",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ const uncastNumericIdsTest = httpTest.bind(null, {
4545
]
4646
]
4747
})
48+
const prefixTest = httpTest.bind(null, {
49+
serializers: [
50+
[
51+
jsonApi, {
52+
prefix: 'https://example.com'
53+
}
54+
]
55+
]
56+
})
4857

4958

5059
run((assert, comment) => {
@@ -773,3 +782,14 @@ run((assert, comment) => {
773782
'content type is correct')
774783
})
775784
})
785+
786+
run((assert, comment) => {
787+
comment('deserialize prefix')
788+
return prefixTest('/', null, response => {
789+
assert(response.status === 200, 'status is correct')
790+
for (const key in response.body.links) {
791+
const value = response.body.links[key]
792+
assert(value.indexOf('https://') === 0, 'prefix is correct')
793+
}
794+
})
795+
})

0 commit comments

Comments
 (0)