@tinyhttp/accepts
Advanced tools
Comparing version 0.4.0 to 0.4.1
# @tinyhttp/accepts | ||
## 0.4.1 | ||
### Patch Changes | ||
- Fix typings for args | ||
## 0.4.0 | ||
@@ -4,0 +10,0 @@ |
@@ -23,4 +23,4 @@ /// <reference types="node" /> | ||
*/ | ||
encodings(encodings: string[], ...args: string[]): string | false | string[]; | ||
get encoding(): (encodings: string[], ...args: string[]) => string | false | string[]; | ||
encodings(encodings: string | string[], ...args: string[]): string | false | string[]; | ||
get encoding(): (encodings: string | string[], ...args: string[]) => string | false | string[]; | ||
/** | ||
@@ -34,4 +34,4 @@ * Return accepted charsets or best fit based on `charsets`. | ||
*/ | ||
charsets(charsets?: string[], ...args: string[]): string | false | string[]; | ||
get charset(): (charsets?: string[], ...args: string[]) => string | false | string[]; | ||
charsets(charsets?: string | string[], ...args: string[]): string | false | string[]; | ||
get charset(): (charsets?: string | string[], ...args: string[]) => string | false | string[]; | ||
/** | ||
@@ -46,6 +46,6 @@ * Return accepted languages or best fit based on `langs`. | ||
*/ | ||
languages(languages: string[], ...args: string[]): string | false | string[]; | ||
get lang(): (languages: string[], ...args: string[]) => string | false | string[]; | ||
get langs(): (languages: string[], ...args: string[]) => string | false | string[]; | ||
get language(): (languages: string[], ...args: string[]) => string | false | string[]; | ||
languages(languages: string | string[], ...args: string[]): string | false | string[]; | ||
get lang(): (languages: string | string[], ...args: string[]) => string | false | string[]; | ||
get langs(): (languages: string | string[], ...args: string[]) => string | false | string[]; | ||
get language(): (languages: string | string[], ...args: string[]) => string | false | string[]; | ||
} |
import Negotiator from 'negotiator'; | ||
import { lookup } from 'es-mime-types'; | ||
function extToMime(type) { | ||
return type.indexOf('/') == -1 ? lookup(type) : type; | ||
} | ||
function validMime(type) { | ||
return typeof type == 'string'; | ||
} | ||
const extToMime = (type) => (type.indexOf('/') == -1 ? lookup(type) : type); | ||
const validMime = (type) => typeof type == 'string'; | ||
class Accepts { | ||
@@ -54,11 +50,12 @@ constructor(req) { | ||
encodings(encodings, ...args) { | ||
let _encodings = encodings; | ||
// support flattened arguments | ||
if (encodings && !Array.isArray(encodings)) { | ||
encodings = [encodings, ...args]; | ||
if (_encodings && !Array.isArray(_encodings)) { | ||
_encodings = [_encodings, ...args]; | ||
} | ||
// no encodings, return all requested encodings | ||
if (!encodings || encodings.length == 0) { | ||
if (!_encodings || _encodings.length == 0) { | ||
return this.negotiator.encodings(); | ||
} | ||
return this.negotiator.encodings(encodings)[0] || false; | ||
return this.negotiator.encodings(_encodings)[0] || false; | ||
} | ||
@@ -77,11 +74,12 @@ get encoding() { | ||
charsets(charsets, ...args) { | ||
let _charsets = charsets; | ||
// support flattened arguments | ||
if (charsets && !Array.isArray(charsets)) { | ||
charsets = [charsets, ...args]; | ||
if (_charsets && !Array.isArray(_charsets)) { | ||
_charsets = [_charsets, ...args]; | ||
} | ||
// no charsets, return all requested charsets | ||
if (!charsets || charsets.length == 0) { | ||
if (!_charsets || _charsets.length == 0) { | ||
return this.negotiator.charsets(); | ||
} | ||
return this.negotiator.charsets(charsets)[0] || false; | ||
return this.negotiator.charsets(_charsets)[0] || false; | ||
} | ||
@@ -101,11 +99,12 @@ get charset() { | ||
languages(languages, ...args) { | ||
let _languages = languages; | ||
// support flattened arguments | ||
if (languages && !Array.isArray(languages)) { | ||
languages = [languages, ...args]; | ||
if (_languages && !Array.isArray(_languages)) { | ||
_languages = [_languages, ...args]; | ||
} | ||
// no languages, return all requested languages | ||
if (!languages || languages.length == 0) { | ||
if (!_languages || _languages.length == 0) { | ||
return this.negotiator.languages(); | ||
} | ||
return this.negotiator.languages(languages)[0] || false; | ||
return this.negotiator.languages(_languages)[0] || false; | ||
} | ||
@@ -112,0 +111,0 @@ get lang() { |
{ | ||
"name": "@tinyhttp/accepts", | ||
"description": "accepts rewrite in TypeScript with ESM and CommonJS targets", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"homepage": "https://github.com/talentlessguy/tinyhttp#readme", | ||
@@ -27,4 +27,3 @@ "repository": { | ||
"devDependencies": { | ||
"@types/negotiator": "^0.6.1", | ||
"@types/node": "^14.11.1" | ||
"@types/negotiator": "^0.6.1" | ||
}, | ||
@@ -31,0 +30,0 @@ "dependencies": { |
@@ -5,4 +5,2 @@ # @tinyhttp/accepts | ||
# MOVED TO [@TINYHTTP/ACCEPTS](https://github.com/talentlessguy/tinyhttp/tree/master/packages/accepts) | ||
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator). | ||
@@ -9,0 +7,0 @@ Extracted from [koa](https://www.npmjs.com/package/koa) for general use. |
@@ -5,9 +5,5 @@ import Negotiator from 'negotiator' | ||
function extToMime(type: string) { | ||
return type.indexOf('/') == -1 ? lookup(type) : type | ||
} | ||
const extToMime = (type: string) => (type.indexOf('/') == -1 ? lookup(type) : type) | ||
function validMime(type: unknown) { | ||
return typeof type == 'string' | ||
} | ||
const validMime = (type: unknown) => typeof type == 'string' | ||
@@ -63,14 +59,16 @@ export class Accepts { | ||
*/ | ||
encodings(encodings: string[], ...args: string[]) { | ||
encodings(encodings: string | string[], ...args: string[]) { | ||
let _encodings: string[] = encodings as string[] | ||
// support flattened arguments | ||
if (encodings && !Array.isArray(encodings)) { | ||
encodings = [encodings, ...args] | ||
if (_encodings && !Array.isArray(_encodings)) { | ||
_encodings = [_encodings, ...args] | ||
} | ||
// no encodings, return all requested encodings | ||
if (!encodings || encodings.length == 0) { | ||
if (!_encodings || _encodings.length == 0) { | ||
return this.negotiator.encodings() | ||
} | ||
return this.negotiator.encodings(encodings)[0] || false | ||
return this.negotiator.encodings(_encodings)[0] || false | ||
} | ||
@@ -88,14 +86,16 @@ get encoding() { | ||
*/ | ||
charsets(charsets?: string[], ...args: string[]) { | ||
charsets(charsets?: string | string[], ...args: string[]) { | ||
let _charsets: string[] = charsets as string[] | ||
// support flattened arguments | ||
if (charsets && !Array.isArray(charsets)) { | ||
charsets = [charsets, ...args] | ||
if (_charsets && !Array.isArray(_charsets)) { | ||
_charsets = [_charsets, ...args] | ||
} | ||
// no charsets, return all requested charsets | ||
if (!charsets || charsets.length == 0) { | ||
if (!_charsets || _charsets.length == 0) { | ||
return this.negotiator.charsets() | ||
} | ||
return this.negotiator.charsets(charsets)[0] || false | ||
return this.negotiator.charsets(_charsets)[0] || false | ||
} | ||
@@ -114,14 +114,16 @@ get charset() { | ||
*/ | ||
languages(languages: string[], ...args: string[]) { | ||
languages(languages: string | string[], ...args: string[]) { | ||
let _languages: string[] = languages as string[] | ||
// support flattened arguments | ||
if (languages && !Array.isArray(languages)) { | ||
languages = [languages, ...args] | ||
if (_languages && !Array.isArray(_languages)) { | ||
_languages = [_languages, ...args] | ||
} | ||
// no languages, return all requested languages | ||
if (!languages || languages.length == 0) { | ||
if (!_languages || _languages.length == 0) { | ||
return this.negotiator.languages() | ||
} | ||
return this.negotiator.languages(languages)[0] || false | ||
return this.negotiator.languages(_languages)[0] || false | ||
} | ||
@@ -128,0 +130,0 @@ get lang() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20763
1
416
120