From a2cdbb8d730c5427b3e76f9159079715e181a8b6 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Tue, 31 Oct 2017 10:38:09 +0900 Subject: [PATCH] Fix incorrect uritemplate typings --- types/uritemplate/index.d.ts | 69 ++++++++++---------------- types/uritemplate/uritemplate-tests.ts | 12 ++--- 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/types/uritemplate/index.d.ts b/types/uritemplate/index.d.ts index 7a3f7f4d5d..338a27bd98 100644 --- a/types/uritemplate/index.d.ts +++ b/types/uritemplate/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for URI Template JS 0.3.4 // Project: https://github.com/fxa/uritemplate-js // Definitions by: Chui Tey +// Ruben Taelman // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /* @@ -8,49 +9,33 @@ Available via the MIT license. */ -declare namespace uritemplate { - - interface UriTemplate { - - /** - * Expands template into a string using parameter - * supplied - */ - expand(data: {}): string; - } - - interface UriTemplateErrorOptions { - expressionText: string - message: string - position: number - } - - interface UriTemplateError { - new (options: UriTemplateErrorOptions): UriTemplateError - } - - - interface UriTemplateStatic { - - /** - * parse template text returning instance of UriTemplate - * @param template text - * @return instance of UriTemplate - * @example - * import UriTemplate = require('uritemplate'); - * let template = UriTemplate.parse('http://localhost/query{?name,city}'); - * let result = template.expand({name: 'Jayden', city: 'Wodonga'}); - * // returns http://localhost/query?name=Jayden&city=Wodonga - */ - parse(templateText: string): uritemplate.UriTemplate; - - UriTemplateError: new (options: uritemplate.UriTemplateErrorOptions) => uritemplate.UriTemplateError; - } +export class UriTemplate { + /** + * Expands template into a string using parameter + * supplied + */ + expand(data: {}): string; } -declare module 'uritemplate' { - - export var UriTemplate: uritemplate.UriTemplateStatic; - +interface UriTemplateErrorOptions { + expressionText: string + message: string + position: number } + +export class UriTemplateError { + constructor(options: UriTemplateErrorOptions); +} + +/** + * parse template text returning instance of UriTemplate + * @param template text + * @return instance of UriTemplate + * @example + * import UriTemplate = require('uritemplate'); + * let template = UriTemplate.parse('http://localhost/query{?name,city}'); + * let result = template.expand({name: 'Jayden', city: 'Wodonga'}); + * // returns http://localhost/query?name=Jayden&city=Wodonga + */ +export function parse(templateText: string): UriTemplate; diff --git a/types/uritemplate/uritemplate-tests.ts b/types/uritemplate/uritemplate-tests.ts index 07ce1dbcfb..a89e12024e 100644 --- a/types/uritemplate/uritemplate-tests.ts +++ b/types/uritemplate/uritemplate-tests.ts @@ -1,28 +1,28 @@ -import { UriTemplate } from 'uritemplate'; +import {parse as parseUriTemplate, UriTemplate, UriTemplateError} from "uritemplate"; function test_uritemplate() { // syntax check: UriTemplate.parse - var template = UriTemplate.parse('http://localhost/categories{/categoryId}{?sort,pageNumber}'); + var template: UriTemplate = parseUriTemplate('http://localhost/categories{/categoryId}{?sort,pageNumber}'); // syntax check: template.expand - var url = template.expand({ + var url: string = template.expand({ categoryId : 'shoes', sort: 'price', pageNumber: 8 }); // import module check - var expectedUrl = 'http://localhost/categories/shoes?sort=price&pageNumber=8'; + var expectedUrl: string = 'http://localhost/categories/shoes?sort=price&pageNumber=8'; if (expectedUrl != url) { throw `Expected ${expectedUrl}, got ${url}`; } else { console.log('Test passed'); } - // syntax check: new UriTemplate.UriTemplateError - let error = new UriTemplate.UriTemplateError({ + // syntax check: new UriTemplateError + let error: UriTemplateError = new UriTemplateError({ expressionText: 'error expression', message: 'error message', position: 5