mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
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:
parent
b4db98d643
commit
3e769c3a18
19
types/express-simple-locale/express-simple-locale-tests.ts
Normal file
19
types/express-simple-locale/express-simple-locale-tests.ts
Normal 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
56
types/express-simple-locale/index.d.ts
vendored
Normal 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;
|
||||
23
types/express-simple-locale/tsconfig.json
Normal file
23
types/express-simple-locale/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/express-simple-locale/tslint.json
Normal file
1
types/express-simple-locale/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user