eslint-plugin-got
v0.0.1
Published
eslint rules for got
Downloads
5
Maintainers
Readme
eslint-plugin-got
(for the sake of a better name)
Install
yarn add -D eslint-plugin-got
# or
npm install -D eslint-plugin-got
Add to .eslintrc.js
or equivalents in plugins/rules section:
module.exports = {
plugins: ['got'],
rules: {
'got/no-leading-slash': ['error', { imports: ['^got$', '^ky$'] }],
},
}
Disallow leading slashes in requests
got/no-leading-slash
If you are using got
or ky
with the prefixUrl
option and constantly forget that you mustn't use a leading slash, this rule is for you.
This works for all request libraries with the same API and input restrictions.
So you can use it for got
, ky
or any created instances of it. You just need to specify the imports
option in the configuration.
This is auto fixable.
// Api.ts
import got from 'got'
export default got.extend({ prefixUrl: 'https://cats.com' })
// .eslintrc.js
module.exports = {
// all imports matching the pattern ".+/Api$" will be considered for linting
'got/no-leading-slash': ['error', { imports: ['.+/Api$'] }],
}
Pass
import api from './Api'
api.get('unicorn')
Fail
import api from './Api'
// The request input should not start with a leading slash. (at 2:8)
api.get('/unicorn')
// -----^
api.get(`/unicorn/${id}`)
// -----^
The import call itself and the request method shortcuts will be checked:
api('request', ...args)
api.get('request', ...args)
api.post('request', ...args)
api.put('request', ...args)
api.patch('request', ...args)
api.head('request', ...args)
api.delete('request', ...args)
Options
imports
Type: array
Default: []
To enable the lint rule you can add regex pattern(s) which should match the import source (file or package):
{
"got/no-leading-slash": ["error", { "imports": [".+/Api$"] }]
}
If you also want to prevent leading slashes in the calls of the packages got
, ky
and ky-universal
you could use following config:
{
"got/no-leading-slash": [
"error",
{ "imports": ["^got$", "^ky$", "^ky-universal$", ".+/Api$"] }
]
}
Reasoning
This rule enforces that every request going through got
, ky
or an instance of it with the prefixUrl
enabled must not start with a leading slash:
Note: Leading slashes in
input
are disallowed when using this option to enforce consistency and avoid confusion. For example, when the prefix URL ishttps://example.com/foo
and the input is/bar
, there's ambiguity whether the resulting URL would becomehttps://example.com/foo/bar
orhttps://example.com/bar
. The latter is used by browsers.
(Source: https://github.com/sindresorhus/got#prefixurl)