Skip to content

Commit 02699dd

Browse files
committed
build: add type definitions
1 parent f69c19e commit 02699dd

4 files changed

Lines changed: 134 additions & 6 deletions

File tree

‎index.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Accepts.prototype.types = function (types_) {
119119
* ['gzip', 'deflate']
120120
*
121121
* @param {String|Array} encodings...
122-
* @return {String|Array}
122+
* @return {String|Array|Boolean}
123123
* @public
124124
*/
125125

@@ -152,7 +152,7 @@ Accepts.prototype.encodings = function (encodings_) {
152152
* ['utf-8', 'utf-7', 'iso-8859-1']
153153
*
154154
* @param {String|Array} charsets...
155-
* @return {String|Array}
155+
* @return {String|Array|Boolean}
156156
* @public
157157
*/
158158

@@ -185,7 +185,7 @@ Accepts.prototype.charsets = function (charsets_) {
185185
* ['es', 'pt', 'en']
186186
*
187187
* @param {String|Array} langs...
188-
* @return {Array|String}
188+
* @return {String|Array|Boolean}
189189
* @public
190190
*/
191191

‎package.json‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "accepts",
33
"description": "Higher-level content negotiation",
44
"version": "1.3.8",
5+
"types": "types/index.d.ts",
56
"contributors": [
67
"Douglas Christopher Wilson <doug@somethingdoug.com>",
78
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
@@ -13,6 +14,7 @@
1314
"negotiator": "0.6.3"
1415
},
1516
"devDependencies": {
17+
"@types/node": "18.11.18",
1618
"deep-equal": "1.0.1",
1719
"eslint": "7.32.0",
1820
"eslint-config-standard": "14.1.1",
@@ -22,12 +24,14 @@
2224
"eslint-plugin-promise": "4.3.1",
2325
"eslint-plugin-standard": "4.1.0",
2426
"mocha": "9.2.0",
25-
"nyc": "15.1.0"
27+
"nyc": "15.1.0",
28+
"tsd": "^0.25.0"
2629
},
2730
"files": [
2831
"LICENSE",
2932
"HISTORY.md",
30-
"index.js"
33+
"index.js",
34+
"types/index.d.ts"
3135
],
3236
"engines": {
3337
"node": ">= 0.6"
@@ -36,7 +40,8 @@
3640
"lint": "eslint .",
3741
"test": "mocha --reporter spec --check-leaks --bail test/",
3842
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
39-
"test-cov": "nyc --reporter=html --reporter=text npm test"
43+
"test-cov": "nyc --reporter=html --reporter=text npm test",
44+
"test-typescript": "tsd"
4045
},
4146
"keywords": [
4247
"content",

‎types/index.d.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// <reference types="node" />
2+
3+
import { IncomingMessage } from "http";
4+
5+
declare namespace accepts {
6+
interface Accepts {
7+
/**
8+
* Return the first accepted charset. If nothing in `charsets` is accepted, then `false` is returned.
9+
* If no charsets are supplied, all accepted charsets are returned, in the order of the client's preference
10+
* (most preferred first).
11+
*/
12+
charset(): string[];
13+
charset(charsets: string[]): string | string[] | false;
14+
charset(...charsets: string[]): string | string[] | false;
15+
16+
/**
17+
* Return the first accepted charset. If nothing in `charsets` is accepted, then `false` is returned.
18+
* If no charsets are supplied, all accepted charsets are returned, in the order of the client's preference
19+
* (most preferred first).
20+
*/
21+
charsets(): string[];
22+
charsets(charsets: string[]): string | string[] | false;
23+
charsets(...charsets: string[]): string | string[] | false;
24+
25+
/**
26+
* Return the first accepted encoding. If nothing in `encodings` is accepted, then `false` is returned.
27+
* If no encodings are supplied, all accepted encodings are returned, in the order of the client's preference
28+
* (most preferred first).
29+
*/
30+
encoding(): string[];
31+
encoding(encodings: string[]): string | string[] | false;
32+
encoding(...encodings: string[]): string | string[] | false;
33+
34+
/**
35+
* Return the first accepted encoding. If nothing in `encodings` is accepted, then `false` is returned.
36+
* If no encodings are supplied, all accepted encodings are returned, in the order of the client's preference
37+
* (most preferred first).
38+
*/
39+
encodings(): string[];
40+
encodings(encodings: string[]): string | string[] | false;
41+
encodings(...encodings: string[]): string | string[] | false;
42+
43+
/**
44+
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
45+
* If no languaes are supplied, all accepted languages are returned, in the order of the client's preference
46+
* (most preferred first).
47+
*/
48+
language(): string[];
49+
language(languages: string[]): string | string[] | false;
50+
language(...languages: string[]): string | string[] | false;
51+
52+
/**
53+
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
54+
* If no languaes are supplied, all accepted languages are returned, in the order of the client's preference
55+
* (most preferred first).
56+
*/
57+
languages(): string[];
58+
languages(languages: string[]): string | string[] | false;
59+
languages(...languages: string[]): string | string[] | false;
60+
61+
/**
62+
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
63+
* If no languaes are supplied, all accepted languages are returned, in the order of the client's preference
64+
* (most preferred first).
65+
*/
66+
lang(): string[];
67+
lang(languages: string[]): string | string[] | false;
68+
lang(...languages: string[]): string | string[] | false;
69+
70+
/**
71+
* Return the first accepted language. If nothing in `languages` is accepted, then `false` is returned.
72+
* If no languaes are supplied, all accepted languages are returned, in the order of the client's preference
73+
* (most preferred first).
74+
*/
75+
langs(): string[];
76+
langs(languages: string[]): string | string[] | false;
77+
langs(...languages: string[]): string | string[] | false;
78+
79+
/**
80+
* Return the first accepted type (and it is returned as the same text as what appears in the `types` array). If nothing in `types` is accepted, then `false` is returned.
81+
* If no types are supplied, return the entire set of acceptable types.
82+
*
83+
* The `types` array can contain full MIME types or file extensions. Any value that is not a full MIME types is passed to `require('mime-types').lookup`.
84+
*/
85+
type(types: string[]): string[] | string | string[] | false;
86+
type(...types: string[]): string[] | string | string[] | false;
87+
types(types: string[]): string[] | string | string[] | false;
88+
types(...types: string[]): string[] | string | string[] | false;
89+
}
90+
}
91+
92+
declare function accepts(req: IncomingMessage): accepts.Accepts;
93+
94+
export = accepts;

‎types/index.test-d.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import accepts from "..";
2+
3+
const req: any = {};
4+
5+
const accept = accepts(req);
6+
7+
const charsets = accept.charsets();
8+
const charsetFromEmpty = accept.charset();
9+
const charsetFromEmptyArray = accept.charset([]);
10+
const charsetFromParams = accept.charset("json", "text");
11+
const charsetFromArray = accept.charset(["json", "txt"]);
12+
13+
const encodings = accept.encodings();
14+
const encodingFromEmpty = accept.encoding();
15+
const encodingFromEmptyArray = accept.encoding([]);
16+
const encodingFromParams = accept.encoding("json", "text");
17+
const encodingFromArray = accept.encoding(["json", "text"]);
18+
19+
const languages = accept.languages();
20+
const languageFromEmpty = accept.language();
21+
const languageFromEmptyArray = accept.language([]);
22+
const languageFromParams = accept.language("json", "text");
23+
const languageFromArray = accept.language(["json", "text"]);
24+
25+
const types = accept.types();
26+
const typeFromEmpty = accept.type();
27+
const typeFromEmptyArray = accept.type([]);
28+
const typeFromParams = accept.type("json", "text");
29+
const typeFromArray = accept.type(["json", "text"]);

0 commit comments

Comments
 (0)