Skip to content

Commit 2475d59

Browse files
committed
feat: migrated index.js to index.ts to allow proper imports
1 parent 45b6268 commit 2475d59

File tree

8 files changed

+393
-362
lines changed

8 files changed

+393
-362
lines changed

.eslintrc.js

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,48 @@ module.exports = {
44
es2020: true,
55
},
66
extends: [
7-
'airbnb',
8-
'plugin:react/recommended',
9-
'plugin:@typescript-eslint/recommended',
7+
"airbnb",
8+
"plugin:react/recommended",
9+
"plugin:@typescript-eslint/recommended",
1010
],
11-
ignorePatterns: [
12-
'*/build/**/*',
13-
'*.json',
14-
'*.txt',
15-
'yarn.lock',
16-
'*.yaml',
17-
],
18-
parser: '@typescript-eslint/parser',
11+
ignorePatterns: ["*/build/**/*", "*.json", "*.txt", "yarn.lock", "*.yaml"],
12+
parser: "@typescript-eslint/parser",
1913
parserOptions: {
2014
ecmaFeatures: {
2115
jsx: true,
2216
},
2317
ecmaVersion: 11,
24-
sourceType: 'module',
18+
sourceType: "module",
2519
},
26-
plugins: [
27-
'react',
28-
'@typescript-eslint',
29-
],
20+
plugins: ["react", "@typescript-eslint"],
3021
rules: {
31-
semi: ['error', 'never'],
32-
'no-unused-vars': 'off',
33-
'import/extensions': 'off',
34-
'import/no-unresolved': 'off',
35-
'react/jsx-filename-extension': 'off',
36-
indent: [
37-
'error',
38-
2,
39-
],
40-
'linebreak-style': ['error', 'unix'],
41-
'object-curly-newline': 'off',
42-
'@typescript-eslint/no-explicit-any': 'off',
22+
semi: ["error", "always"],
23+
"no-unused-vars": "off",
24+
"import/extensions": "off",
25+
"import/no-unresolved": "off",
26+
"react/jsx-filename-extension": "off",
27+
indent: ["error", 2],
28+
"linebreak-style": ["error", "unix"],
29+
"object-curly-newline": "off",
30+
"@typescript-eslint/no-explicit-any": "off",
4331
},
44-
overrides: [{
45-
files: ['*.tsx'],
46-
rules: {
47-
'react/prop-types': 'off',
48-
'react/jsx-props-no-spreading': 'off',
49-
'import/no-extraneous-dependencies': 'off',
32+
overrides: [
33+
{
34+
files: ["*.tsx"],
35+
rules: {
36+
"react/prop-types": "off",
37+
"react/jsx-props-no-spreading": "off",
38+
"import/no-extraneous-dependencies": "off",
39+
},
5040
},
51-
}, {
52-
files: ['./src/**/*.spec.ts', 'spec/*.ts'],
53-
rules: {
54-
'no-unused-expressions': 'off',
55-
'prefer-arrow-callback': 'off',
56-
'func-names': 'off',
57-
'import/no-extraneous-dependencies': 'off',
41+
{
42+
files: ["./src/**/*.spec.ts", "spec/*.ts"],
43+
rules: {
44+
"no-unused-expressions": "off",
45+
"prefer-arrow-callback": "off",
46+
"func-names": "off",
47+
"import/no-extraneous-dependencies": "off",
48+
},
5849
},
59-
}],
60-
}
50+
],
51+
};

index.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.js renamed to index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
21
/**
32
* @module @admin-bro/sequelize
43
* @subcategory Adapters
@@ -79,7 +78,8 @@
7978
* @type {typeof BaseDatabase}
8079
* @static
8180
*/
82-
const Database = require('./build/database').default
81+
import type { BaseDatabase, BaseResource } from 'admin-bro';
82+
import Database from './src/database';
8383

8484
/**
8585
* Implementation of {@link BaseResource} for Sequelize Adapter
@@ -88,6 +88,10 @@ const Database = require('./build/database').default
8888
* @type {typeof BaseResource}
8989
* @static
9090
*/
91-
const Resource = require('./build/resource').default
91+
import Resource from './src/resource';
9292

93-
module.exports = { Database, Resource }
93+
module.exports = { Database, Resource };
94+
95+
const SequelizeAdapter = { Database, Resource };
96+
97+
export default SequelizeAdapter;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@types/sinon-chai": "^3.2.4",
4040
"@typescript-eslint/eslint-plugin": "^3.7.0",
4141
"@typescript-eslint/parser": "^3.7.0",
42-
"admin-bro": "^3.2.3",
42+
"admin-bro": "^3.4.0",
4343
"chai": "^4.2.0",
4444
"eslint": "^7.5.0",
4545
"eslint-config-airbnb": "^18.2.0",

src/database.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { BaseDatabase, BaseResource } from 'admin-bro'
2-
import { Sequelize } from 'sequelize'
1+
import { BaseDatabase, BaseResource } from 'admin-bro';
2+
import { Sequelize } from 'sequelize';
33

4-
import Resource from './resource'
4+
import Resource from './resource';
55

66
class Database extends BaseDatabase {
77
private sequelize: Sequelize
88

99
static isAdapterFor(database: any): boolean {
1010
return (database.sequelize
1111
&& database.sequelize.constructor.name === 'Sequelize')
12-
|| database.constructor.name === 'Sequelize'
12+
|| database.constructor.name === 'Sequelize';
1313
}
1414

1515
constructor(database: any) {
16-
super(database)
17-
this.sequelize = database.sequelize || database
16+
super(database);
17+
this.sequelize = database.sequelize || database;
1818
}
1919

2020
resources(): Array<BaseResource> {
2121
return Object.keys(this.sequelize.models).map((key) => (
2222
new Resource(this.sequelize.models[key])
23-
))
23+
));
2424
}
2525
}
2626

27-
export default Database
27+
export default Database;

src/utils/convert-filter.ts

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,90 @@
1-
import escape from 'escape-regexp'
2-
import {
3-
Op,
4-
} from 'sequelize'
1+
import escape from 'escape-regexp';
2+
import { Op } from 'sequelize';
53

64
const convertFilter = (filter) => {
75
if (!filter) {
8-
return {}
6+
return {};
97
}
108
return filter.reduce((memo, filterProperty) => {
11-
const { property, value, path: filterPath } = filterProperty
9+
const { property, value, path: filterPath } = filterProperty;
1210
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13-
const [_, index] = filterPath.split('.')
14-
const isArray = typeof index !== 'undefined' && !Number.isNaN(Number(index))
15-
const previousValue = memo[property.name()] || {}
11+
const [_, index] = filterPath.split('.');
12+
const isArray = typeof index !== 'undefined' && !Number.isNaN(Number(index));
13+
const previousValue = memo[property.name()] || {};
1614
switch (property.type()) {
1715
case 'string': {
1816
if (property.sequelizePath.values) {
1917
return {
2018
[property.name()]: { [Op.eq]: `${escape(value)}` },
2119
...memo,
22-
}
20+
};
2321
}
2422
if (isArray) {
2523
return {
2624
...memo,
2725
[property.name()]: {
28-
[Op.in]: [
29-
...(previousValue[Op.in] || []),
30-
escape(value),
31-
],
26+
[Op.in]: [...(previousValue[Op.in] || []), escape(value)],
3227
},
33-
}
28+
};
3429
}
3530
return {
3631
...memo,
3732
[Op.and]: [
3833
...(memo[Op.and] || []),
3934
{
4035
[property.name()]: {
41-
[Op.iLike as unknown as string]: `%${escape(value)}%`,
36+
[(Op.iLike as unknown) as string]: `%${escape(value)}%`,
4237
},
4338
},
4439
],
45-
}
40+
};
4641
}
4742
case 'number': {
4843
if (!Number.isNaN(Number(value))) {
4944
if (isArray) {
5045
return {
5146
...memo,
5247
[property.name()]: {
53-
[Op.in]: [
54-
...(previousValue[Op.in] || []),
55-
Number(value),
56-
],
48+
[Op.in]: [...(previousValue[Op.in] || []), Number(value)],
5749
},
58-
}
50+
};
5951
}
6052
return {
6153
[property.name()]: Number(value),
6254
...memo,
63-
}
55+
};
6456
}
65-
return memo
57+
return memo;
6658
}
6759
case 'date':
6860
case 'datetime': {
6961
if (value.from || value.to) {
7062
return {
7163
[property.name()]: {
72-
...value.from && { [Op.gte]: value.from },
73-
...value.to && { [Op.lte]: value.to },
64+
...(value.from && { [Op.gte]: value.from }),
65+
...(value.to && { [Op.lte]: value.to }),
7466
},
7567
...memo,
76-
}
68+
};
7769
}
78-
break
70+
break;
7971
}
8072
default:
81-
break
73+
break;
8274
}
8375
if (isArray) {
8476
return {
8577
...memo,
8678
[property.name()]: {
87-
[Op.in]: [
88-
...(previousValue[Op.in] || []),
89-
value,
90-
],
79+
[Op.in]: [...(previousValue[Op.in] || []), value],
9180
},
92-
}
81+
};
9382
}
9483
return {
9584
[property.name()]: value,
9685
...memo,
97-
}
98-
}, {})
99-
}
86+
};
87+
}, {});
88+
};
10089

101-
export default convertFilter
90+
export default convertFilter;

src/utils/create-validation-error.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ValidationError } from 'admin-bro'
1+
import { ValidationError } from "admin-bro";
22

3-
const createValidationError = (originalError) => {
3+
const createValidationError = (originalError: any): ValidationError => {
44
const errors = Object.keys(originalError.errors).reduce((memo, key) => {
5-
const { path, message, validatorKey } = originalError.errors[key]
6-
memo[path] = { message, kind: validatorKey } // eslint-disable-line no-param-reassign
7-
return memo
8-
}, {})
9-
return new ValidationError(errors)
10-
}
5+
const { path, message, validatorKey } = originalError.errors[key];
6+
memo[path] = { message, kind: validatorKey }; // eslint-disable-line no-param-reassign
7+
return memo;
8+
}, {});
9+
return new ValidationError(errors);
10+
};
1111

12-
export default createValidationError
12+
export default createValidationError;

0 commit comments

Comments
 (0)