feat(express-simple-locale): new definition (#46833)

Definition types for Express middleware:
- definition file
- tests

https://www.npmjs.com/package/express-simple-locale
https://github.com/n26/express-simple-locale#options

Thanks!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-08-28 01:40:32 +02:00 committed by GitHub
parent b4db98d643
commit 3e769c3a18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,19 @@
import express = require('express');
import locale = require('express-simple-locale');
const localeMiddlewareOptions = {
key: 'userLocale',
supportedLocales: ['en', 'fr', 'it', 'es', 'de'],
defaultLocale: 'en',
cookieName: 'c00ki3z',
queryParams: ['locale', 'lang'],
};
express()
.use(locale())
.use(locale({}))
.use(locale(localeMiddlewareOptions))
.use((request, response, next) => {
request.userLocale; // $ExpectType string
next();
});

56
types/express-simple-locale/index.d.ts vendored Normal file
View File

@ -0,0 +1,56 @@
// Type definitions for express-simple-locale 0.3
// Project: https://github.com/n26/express-simple-locale#readme
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import express = require('express');
import { RequestHandler } from 'express';
declare module 'express-serve-static-core' {
interface Request {
userLocale: locale.ShortLocale;
}
}
/**
* A simple Express middleware to guess the short-locale of a user.
* It then saves the found locale on the request for further usage.
*/
declare function locale(options?: locale.Options): RequestHandler;
declare namespace locale {
/**
* @see {@link https://github.com/n26/express-simple-locale#options}
*/
interface Options {
/**
* the key to save locale to on the request
* @default 'locale'
*/
key?: string;
/**
* available locales for the app
* @default []
*/
supportedLocales?: string[];
/**
* locale to fallback to
* @default 'en'
*/
defaultLocale?: string;
/**
* cookie to try getting the locale from
* @default 'locale'
*/
cookieName?: string;
/**
* the query parameter(s) to look the locale from
* @default ['locale']
*/
queryParams?: string | string[];
}
/** The short-locale of a user */
type ShortLocale = string;
}
export = locale;

View File

@ -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-simple-locale-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }