eslint-plugin-got

eslint rules for got

Usage no npm install needed!

<script type="module">
  import eslintPluginGot from 'https://cdn.skypack.dev/eslint-plugin-got';
</script>

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 ".+/Apiquot; 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": [".+/Apiquot;] }]
}

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": ["^gotquot;, "^kyquot;, "^ky-universalquot;, ".+/Apiquot;] }
  ]
}

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 is https://example.com/foo and the input is /bar, there's ambiguity whether the resulting URL would become https://example.com/foo/bar or https://example.com/bar. The latter is used by browsers.

(Source: https://github.com/sindresorhus/got#prefixurl)