diff --git a/content-type/content-type-tests.ts b/content-type/content-type-tests.ts index 872b3cded0..b9181fb3fd 100644 --- a/content-type/content-type-tests.ts +++ b/content-type/content-type-tests.ts @@ -1,47 +1,21 @@ /// +/// -import MediaType = require('content-type'); +import contentType = require('content-type'); +import express = require('express'); -// https://github.com/deoxxa/content-type/blob/master/README.md -function new_test(): void { - var p = new MediaType('text/html;level=1;q=0.5'); - p.q === 0.5; - p.params.level === "1"; - var q = new MediaType('application/json', { profile: 'http://example.com/schema.json' }); - q.type === "application/json"; - q.params.profile === "http://example.com/schema.json"; +var obj = contentType.parse('image/svg+xml; charset=utf-8'); - q.q = 1; - q.toString() === 'application/json;q=1;profile="http://example.com/schema.json"'; -} +console.log(obj.type); // => 'image/svg+xml' +console.log(obj.parameters.charset); // => 'utf-8' -function mediaCmp_test(): void { - MediaType.mediaCmp(MediaType.parseMedia('text/html'), MediaType.parseMedia('text/html')) === 0; - MediaType.mediaCmp(MediaType.parseMedia('*/*'), MediaType.parseMedia('text/html')) === 1; - MediaType.mediaCmp(MediaType.parseMedia('text/html;level=1'), MediaType.parseMedia('text/html')) === -1; - MediaType.mediaCmp(MediaType.parseMedia('application/json;profile="v1.json"'), MediaType.parseMedia('application/json;profile="v2.json"')) === null; -} -// https://github.com/deoxxa/content-type/blob/master/example.js -function example(): void { - var representations = [ - 'application/json', - 'text/html', - 'application/json;profile="schema.json"', - 'application/json;profile="different.json"', - ]; +var req: express.Request; +obj = contentType.parse(req); - var accept = [ - 'text/html;q=0.50', - '*/*;q=0.01', - 'application/json;profile=different.json', - 'application/json;profile="a,b;c.json?d=1;f=2";q=0.2', - ]; +var res: express.Response; +obj = contentType.parse(res); - console.log('Formats:\n\t' + representations.map(MediaType.parseMedia).join('\n\t')); +var str: string = contentType.format({type: 'image/svg+xml'}); - console.log('Accept:\n\t' + accept.map(MediaType.parseMedia).join('\n\t')); - - console.log('Selected:', (MediaType.select(representations.map(MediaType.parseMedia), accept.map(MediaType.parseMedia)) || 'None').toString()); -} \ No newline at end of file diff --git a/content-type/content-type.d.ts b/content-type/content-type.d.ts index 6e901e7f86..d8254fd8ed 100644 --- a/content-type/content-type.d.ts +++ b/content-type/content-type.d.ts @@ -1,32 +1,24 @@ -// Type definitions for content-type v0.0.1 -// Project: https://github.com/deoxxa/content-type -// Definitions by: Pine Mizune -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Type definitions for content-type v1.0.1 +// Project: https://www.npmjs.com/package/content-type +// Definitions by: Hiroki Horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module ContentType { + interface StaticFunctions { + parse(string: string): MediaType; + parse(req: { headers: any; }): MediaType; + parse(res: { getHeader(key: string): string; }): MediaType; + format(obj: MediaType): string; + } -declare namespace ContentType { interface MediaType { type: string; - q?: number; - params: any; - toString(): string; - } - - interface SelectOptions { - sortAvailable?: boolean; - sortAccepted?: boolean; - } - - interface MediaTypeStatic { - new (s: string, p?: any): MediaType; - parseMedia(type: string): MediaType; - splitQuotedString(str: string, delimiter?: string, quote?: string): string[]; - splitContentTypes(str: string): string[]; - select(availableTypes: MediaType[], acceptedTypes: MediaType[], options?: SelectOptions): string; - mediaCmp(a: MediaType, b: MediaType): number; + parameters?: any; } } declare module "content-type" { - var x: ContentType.MediaTypeStatic; + var x: ContentType.StaticFunctions; export = x; } + diff --git a/deoxxa-content-type/content-type-test.ts b/deoxxa-content-type/content-type-test.ts new file mode 100644 index 0000000000..3e419933ef --- /dev/null +++ b/deoxxa-content-type/content-type-test.ts @@ -0,0 +1,47 @@ +/// + +import MediaType = require('content-type'); + +// https://github.com/deoxxa/content-type/blob/master/README.md +function new_test(): void { + var p = new MediaType('text/html;level=1;q=0.5'); + p.q === 0.5; + p.params.level === "1"; + + var q = new MediaType('application/json', { profile: 'http://example.com/schema.json' }); + q.type === "application/json"; + q.params.profile === "http://example.com/schema.json"; + + q.q = 1; + q.toString() === 'application/json;q=1;profile="http://example.com/schema.json"'; +} + +function mediaCmp_test(): void { + MediaType.mediaCmp(MediaType.parseMedia('text/html'), MediaType.parseMedia('text/html')) === 0; + MediaType.mediaCmp(MediaType.parseMedia('*/*'), MediaType.parseMedia('text/html')) === 1; + MediaType.mediaCmp(MediaType.parseMedia('text/html;level=1'), MediaType.parseMedia('text/html')) === -1; + MediaType.mediaCmp(MediaType.parseMedia('application/json;profile="v1.json"'), MediaType.parseMedia('application/json;profile="v2.json"')) === null; +} + +// https://github.com/deoxxa/content-type/blob/master/example.js +function example(): void { + var representations = [ + 'application/json', + 'text/html', + 'application/json;profile="schema.json"', + 'application/json;profile="different.json"', + ]; + + var accept = [ + 'text/html;q=0.50', + '*/*;q=0.01', + 'application/json;profile=different.json', + 'application/json;profile="a,b;c.json?d=1;f=2";q=0.2', + ]; + + console.log('Formats:\n\t' + representations.map(MediaType.parseMedia).join('\n\t')); + + console.log('Accept:\n\t' + accept.map(MediaType.parseMedia).join('\n\t')); + + console.log('Selected:', (MediaType.select(representations.map(MediaType.parseMedia), accept.map(MediaType.parseMedia)) || 'None').toString()); +} diff --git a/deoxxa-content-type/content-type.d.ts b/deoxxa-content-type/content-type.d.ts new file mode 100644 index 0000000000..6e901e7f86 --- /dev/null +++ b/deoxxa-content-type/content-type.d.ts @@ -0,0 +1,32 @@ +// Type definitions for content-type v0.0.1 +// Project: https://github.com/deoxxa/content-type +// Definitions by: Pine Mizune +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace ContentType { + interface MediaType { + type: string; + q?: number; + params: any; + toString(): string; + } + + interface SelectOptions { + sortAvailable?: boolean; + sortAccepted?: boolean; + } + + interface MediaTypeStatic { + new (s: string, p?: any): MediaType; + parseMedia(type: string): MediaType; + splitQuotedString(str: string, delimiter?: string, quote?: string): string[]; + splitContentTypes(str: string): string[]; + select(availableTypes: MediaType[], acceptedTypes: MediaType[], options?: SelectOptions): string; + mediaCmp(a: MediaType, b: MediaType): number; + } +} + +declare module "content-type" { + var x: ContentType.MediaTypeStatic; + export = x; +}