Skip to content

Commit c00f65e

Browse files
committed
feat: adding types definition and ESM usage to README
1 parent 628fd36 commit c00f65e

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,29 @@ or:
1414

1515
`yarn add accept-language-middleware`
1616

17-
Usage:
17+
Usage (ESM):
1818

19+
```js
20+
import express from "express";
21+
import acceptLanguageMiddleware from "accept-language-middleware";
22+
23+
const app = express();
24+
app.use(acceptLanguageMiddleware());
25+
app.get("/", (req, res, next) => {
26+
console.log(req.language); // 'en'
27+
console.log(req.locale); // 'en-US'
28+
});
1929
```
20-
var express = require('express');
30+
31+
Usage (CJS):
32+
33+
```js
34+
var express = require("express");
2135
var app = express();
2236
var acceptLanguageMiddleware = require("accept-language-middleware");
2337

2438
app.use(acceptLanguageMiddleware());
25-
app.get('/', function(req, res, next) {
39+
app.get("/", function (req, res, next) {
2640
console.log(req.language); // 'en'
2741
console.log(req.locale); // 'en-US'
2842
});
@@ -34,15 +48,15 @@ app.get('/', function(req, res, next) {
3448

3549
Specify a default language to fallback on if none is passed:
3650

37-
```
51+
```js
3852
app.use(acceptLanguageMiddleware({ default: "es" }));
3953
```
4054

4155
### supported (by default all languages are supported)
4256

4357
Specify a set of supported languages, if the incoming Accept-Language header does not contain a language in the supported list, then the default language will be used.
4458

45-
```
59+
```js
4660
app.use(acceptLanguageMiddleware({ supported: ["en", "es", "zh"] }));
4761
```
4862

index.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { RequestHandler } from "express";
2+
3+
declare namespace AcceptLanguageMiddleware {
4+
interface Options {
5+
default?: string;
6+
supported?: string[];
7+
}
8+
9+
interface AugmentedRequest {
10+
language?: string;
11+
locale?: string;
12+
}
13+
}
14+
15+
declare module "express-serve-static-core" {
16+
interface Request {
17+
language?: string;
18+
locale?: string;
19+
}
20+
}
21+
22+
declare function acceptLanguageMiddleware(
23+
options?: AcceptLanguageMiddleware.Options
24+
): RequestHandler;
25+
26+
export = acceptLanguageMiddleware;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
"scripts": {
1515
"test": "tap test/*.js"
1616
},
17+
"types": "index.d.ts",
1718
"files": [
1819
"LICENSE",
1920
"README.md",
20-
"index.js"
21+
"index.js",
22+
"index.d.ts"
2123
],
2224
"engines": {
2325
"node": ">=18.0.0"

0 commit comments

Comments
 (0)