Skip to content

Commit

Permalink
[content-type_v1.x.x] Add definitions (#3675)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienw authored and pascalduez committed Dec 14, 2019
1 parent 01cfe94 commit 65e0953
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare module 'content-type' {
declare type contentType$OutputParsedContentType = {|
type: string,
parameters: {| [string]: string |},
|};

declare type contentType$InputParsedContentType = {|
+type: string,
+parameters?: { [string]: string | number, ... },
|};

declare function parse(
string | http$IncomingMessage<> | http$ServerResponse
): contentType$OutputParsedContentType;

declare function format(contentType$InputParsedContentType): string;

declare module.exports: {|
+parse: typeof parse,
+format: typeof format,
|};
}
49 changes: 49 additions & 0 deletions definitions/npm/content-type_v1.x.x/test_content-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it } from 'flow-typed-test';
const contentType = require('content-type');

describe('content-type', () => {
it('parse normal usage', () => {
const parsedValue = contentType.parse('application/json');
(parsedValue.type: string);
(parsedValue.parameters.foo: string);
(parsedValue.parameters['foo']: string);
});

it('format normal usage', () => {
const typeValue = contentType.format({
type: 'application/json',
parameters: { version: '1.0' },
});

(typeValue: string);

contentType.format({
type: 'application/json',
parameters: { version: 1 },
});

contentType.format({
type: 'application/json',
});

const parameters = {};
parameters['charset'] = 'utf-8';
parameters['version'] = 8;
contentType.format({
type: 'application/json',
parameters,
});
});

it('finds errors when we miss the type property', () => {
// $ExpectError
contentType.format({
foo: 'bar',
});

// $ExpectError
contentType.format({
parameters: { version: '1.0' }
});
});
});

0 comments on commit 65e0953

Please sign in to comment.