Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-clouds-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ezycourse/eslint-plugin": minor
---

Added import path for axios import options
12 changes: 12 additions & 0 deletions docs/rules/no-axios-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ import callApi from 'Utils/api';

```

## Options

This rule has an options to specify the importPath of the api util file.

```json
{
"rules": {
"@ezycourse/no-axios-import": ["error", { "importPath": "custom/path" }]
}
}
```

## When Not To Use It

If you think that it is fine to use axios directly in your code, you can disable this rule.
Expand Down
24 changes: 20 additions & 4 deletions lib/rules/no-axios-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ module.exports = {
url: 'https://github.com/AppifyLab/eslint-plugin/blob/main/docs/rules/no-axios-import.md',
},
fixable: 'code',
schema: [], // Add a schema if the rule has options
schema: [
{
type: 'object',
properties: {
importPath: {
type: 'string',
default: 'Utils/api'
}
},
additionalProperties: false
}

],
messages: {
noAxiosImport: "Direct import of axios is not allowed. Use callApi from 'Utils/api' instead."
noAxiosImport: "Direct import of axios is not allowed. Use custom instance from '{{importPath}}' instead."
},
},

create(context) {
// variables should be defined here
const options = context.options[0] || {};
const importPath = options.importPath || 'Utils/api';

//----------------------------------------------------------------------
// Helpers
Expand All @@ -44,8 +57,11 @@ module.exports = {
context.report({
node,
messageId: "noAxiosImport",
data: {
importPath
},
fix(fixer) {
return fixer.replaceText(node.source, "'Utils/api'");
return fixer.replaceText(node.source, importPath);
}
});
}
Expand Down