diff --git a/types/express-locale/express-locale-tests.ts b/types/express-locale/express-locale-tests.ts new file mode 100644 index 0000000000..6d1e0dbaa8 --- /dev/null +++ b/types/express-locale/express-locale-tests.ts @@ -0,0 +1,25 @@ +import express = require('express'); +import createLocaleMiddleware = require('express-locale'); + +express() + .use(createLocaleMiddleware()) + .use(createLocaleMiddleware({})) + .use( + createLocaleMiddleware({ + priority: ['accept-language', 'default', 'custom', 'query'], + default: 'en-GB', + allowed: ['en-GB'], + hostname: { '127.0.0.1': 'nl-BE' }, + cookie: { name: 'lang' }, + lookups: { + custom: () => 'fr-FR', + }, + map: { en: 'en-CA', fr: 'fr-CA' }, + query: { name: 'l' }, + requestProperty: 'locale', + }), + ) + .use((req, res) => { + res.end(`Request locale: ${req.locale}`); + }) + .listen(3000); diff --git a/types/express-locale/index.d.ts b/types/express-locale/index.d.ts new file mode 100644 index 0000000000..2461000041 --- /dev/null +++ b/types/express-locale/index.d.ts @@ -0,0 +1,108 @@ +// Type definitions for express-locale 2.0 +// Project: https://github.com/smhg/express-locale#readme +// Definitions by: Piotr Błażejewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +import express = require('express'); +import { RequestHandler, Request } from 'express'; + +declare module 'express-serve-static-core' { + interface Request { + locale: locale.Locale; + } +} + +/** + * Express middleware to determine the locale identifier of the incomming request. + * It returns (only) full locale identifiers based on the middleware's configuration. + * Configuration defines possible sources, their order and, optionally, a whitelist. + * For performance reasons, on each request, remaining lookups are ignored as soon as a match is found. + */ +declare function locale(options?: locale.Options): RequestHandler; + +declare namespace locale { + /** + * @see {@link https://github.com/smhg/express-locale/#configuration} + */ + interface Options { + /** + * Defines the order of lookups. + * The first lookup to return a full locale will be the final result. + * @default ['accept-language', 'default'] + */ + priority?: string[]; + + /** + * Add custom lookups or overwrite the default ones + * @default undefined + */ + lookups?: { + [key: string]: (req: Request) => string; + }; + + /** + * The name of the cookie that contains the locale for the cookie lookup. + * + * Use with {@link https://github.com/expressjs/cookie-parser|cookie-parser} middleware. + * + * Note: you are responsible for writing the locale to the cookie. + * + * @default {name: 'locale'} + */ + cookie?: { + [key: string]: unknown; + }; + + /** + * The name of the query string parameter that contains the locale for the query lookup. + * @default {name: 'locale'} + */ + query?: { + [key: string]: unknown; + }; + + /** + * A mapping of hostnames to locales for the hostname lookup. + * @default {} + */ + hostname?: { + [locale: string]: string; + }; + + /** + * Maps lookup results that return only a language to a full locale. + * @default {} + */ + map?: { + [language: string]: string; + }; + + /** + * The default locale for the default lookup. + * @default 'en-GB' + */ + default?: string; + + /** + * Lookup results are validated against this list of allowed locales if provided. + * @default undefined + */ + allowed?: string[]; + + /** + * By default, the locale is attached to `req.locale` but can be configured with the `requestProperty` option. + * @default 'locale' + */ + requestProperty?: string; + } + + /** + * The locale property on the request object + */ + interface Locale { + source: string; + language: string; + region: string; + } +} + +export = locale; diff --git a/types/express-locale/tsconfig.json b/types/express-locale/tsconfig.json new file mode 100644 index 0000000000..fe054bd54b --- /dev/null +++ b/types/express-locale/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "express-locale-tests.ts" + ] +} diff --git a/types/express-locale/tslint.json b/types/express-locale/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/express-locale/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }