@azure-iot/hal
Advanced tools
Comparing version 1.0.0-rc.9 to 1.0.0-rc.10
{ | ||
"name": "@azure-iot/hal", | ||
"description": "Decorator library for HAL-based routes", | ||
"version": "1.0.0-rc.9", | ||
"version": "1.0.0-rc.10", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "prebuild": "npm run lint", |
@@ -35,3 +35,3 @@ # @azure-iot/hal | ||
* `method` [string | Method]: The HTTP method from the `Method` enum or as a string. | ||
* `path` [string]: The route path for this handler. | ||
* `path` [string]: The route path for this handler. Express-style paths and level-1 URI templated paths (as per [RFC 6570](https://tools.ietf.org/html/rfc6570)) are supported. | ||
@@ -38,0 +38,0 @@ #### `@provides(rel, [options])` |
@@ -79,3 +79,3 @@ "use strict"; | ||
for (let provides of method.provides) { | ||
let template = template_1.Template.apply(route.path, provides.options.params); | ||
let template = template_1.Template.apply(route.path, provides.options.params || {}); | ||
linker_1.Linker.registerLink(server, provides.rel, template, Object.assign({ | ||
@@ -88,3 +88,4 @@ verb: route.verb, | ||
const handlers = method.hal.length === 0 ? middleware : middleware.concat((req, res, next) => response_1.Response.create(server, path(req), links, req, res) && next()); | ||
app[route.verb.toLowerCase()].call(app, url.parse(route.path).pathname, handlers, handler); | ||
const uri = template_1.Template.is(route.path) ? template_1.Template.express(route.path) : route.path; | ||
app[route.verb.toLowerCase()].call(app, uri, handlers, handler); | ||
} | ||
@@ -91,0 +92,0 @@ else { |
import { Hal } from './constants'; | ||
import { hal } from './decorators'; | ||
export declare class Template { | ||
private static parse; | ||
private static params(href); | ||
private static decode(href); | ||
static apply(href: string, params: any): string; | ||
static is(href: string): boolean; | ||
static link(resolved: hal.Overrides): Hal.Link; | ||
static express(href: string): string; | ||
} |
"use strict"; | ||
const url = require('url'); | ||
const pathToRegexp = require('path-to-regexp'); | ||
@@ -22,4 +23,12 @@ class Template { | ||
static apply(href, params) { | ||
return Template.decode(pathToRegexp.compile(href)(Object.assign(Template.params(href), params))); | ||
if (Template.is(href)) { | ||
return href.replace(Template.parse, (match, variable) => typeof params[variable] !== 'undefined' ? params[variable] : match); | ||
} | ||
else { | ||
return Template.decode(pathToRegexp.compile(href)(Object.assign(Template.params(href), params))); | ||
} | ||
} | ||
static is(href) { | ||
return Template.parse.test(href); | ||
} | ||
static link(resolved) { | ||
@@ -30,3 +39,3 @@ let link = { href: resolved.href }; | ||
} | ||
if (/\{.+\}/.test(link.href)) { | ||
if (Template.is(link.href)) { | ||
link.templated = true; | ||
@@ -42,4 +51,9 @@ } | ||
} | ||
static express(href) { | ||
let route = href.replace(Template.parse, (match, variable) => `:${variable}`); | ||
return url.parse(route).pathname; | ||
} | ||
} | ||
Template.parse = /\{\W?(\w+)*?\}/g; | ||
exports.Template = Template; | ||
//# sourceMappingURL=template.js.map |
56769
982