Skip to content

Commit

Permalink
fillHref added
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Mar 7, 2018
1 parent 15d299f commit f5ac0ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const pointer = require('json-pointer')
const URI = require('uri-js')
const Ajv = require('ajv')
const omit = require('lodash/omit')
const merge = require('lodash/merge')

const ajv = new Ajv()

Expand Down Expand Up @@ -103,6 +104,13 @@ function resolveLink(ldo, instance, instanceUri) {
resolved.hrefInputTemplates = [ldo.href]
resolved.hrefPrepopulatedInput = getDefaultInputValues(ldo.href, ldo, instance)
resolved.hrefFixedInput = omit(getTemplateData(ldo.href, ldo, instance), Object.keys(resolved.hrefPrepopulatedInput))
resolved.fillHref = function(userSupplied) {
var fixedData = omit(getTemplateData(ldo.href, ldo, instance), Object.keys(resolved.hrefPrepopulatedInput))
var allData = merge({}, userSupplied, fixedData)
var template = uriTemplates(ldo.href)
resolved.targetUri = template.fill(allData)
return resolved.targetUri
}
} else {
let template = uriTemplates(ldo.href)
let uri
Expand Down
33 changes: 30 additions & 3 deletions test/specs/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const chai = require('chai')
const resolver = require('../../src/resolver')
const expect = chai.expect
const _ = require('lodash')

describe('resolver', function() {
var link
Expand Down Expand Up @@ -288,7 +289,7 @@ describe('resolver', function() {
author: 'Martin'
}, 'https://example.com')

expect(resolved).to.eql([{
expect(resolved.map(o => _.omit(o, 'fillHref'))).to.eql([{
contextUri: 'https://example.com',
contextPointer: '',
rel: 'author',
Expand Down Expand Up @@ -321,7 +322,7 @@ describe('resolver', function() {
author: 'Martin'
}, 'https://example.com')

expect(resolved).to.eql([{
expect(resolved.map(o => _.omit(o, 'fillHref'))).to.eql([{
contextUri: 'https://example.com',
contextPointer: '',
rel: 'author',
Expand All @@ -338,7 +339,33 @@ describe('resolver', function() {
})
})

it('provides a function for fully templating the template')
it('provides a function for fully templating the template and enforces prefilled values', function() {
var resolved = resolver.resolve({
links: [{
rel: 'author',
href: '/authors/{author}/{extra}',
hrefSchema: {
properties: {
author: false,
extra: true
}
}
}]
}, {
author: 'Martin'
}, 'https://example.com')

expect(resolved[0].fillHref).to.be.a('function')

var targetUri = resolved[0].fillHref({
author: 'I should not be used',
extra: 'I should be used'
})

expect(targetUri).to.equal('/authors/Martin/' + encodeURIComponent('I should be used'))
expect(targetUri).to.equal(resolved[0].targetUri)
})

it('does not allow changing the hrefFixedInput')

it('considers base')
Expand Down

0 comments on commit f5ac0ab

Please sign in to comment.