Skip to content

Commit fdbae5e

Browse files
committed
Merge remote-tracking branch 'origin/feat/v4' into beta
2 parents da6cb6d + e649a04 commit fdbae5e

File tree

7 files changed

+937
-1774
lines changed

7 files changed

+937
-1774
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,5 @@ build
161161
.adminjs
162162

163163
example-app/cypress/videos
164+
165+
.nova

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"license": "MIT",
1414
"scripts": {
1515
"release": "semantic-release",
16+
"prepare": "husky install",
1617
"build": "tsc",
1718
"test": "mocha --loader=ts-node/esm ./src/**/*.spec.ts",
1819
"lint": "eslint './src/**/*'",
@@ -28,17 +29,16 @@
2829
"adminjs": ">=6.0.0"
2930
},
3031
"devDependencies": {
31-
"@adminjs/design-system": "^4.0.0-beta-v4.1",
32-
"@commitlint/cli": "^17.4.4",
32+
"@commitlint/cli": "^17.5.1",
3333
"@commitlint/config-conventional": "^17.4.4",
3434
"@semantic-release/git": "^10.0.1",
3535
"@types/chai": "^4.3.4",
3636
"@types/mocha": "^10.0.1",
3737
"@types/sinon": "^10.0.13",
3838
"@types/sinon-chai": "^3.2.9",
39-
"@typescript-eslint/eslint-plugin": "^5.56.0",
40-
"@typescript-eslint/parser": "^5.56.0",
41-
"adminjs": "^7.0.0-beta-v7.1",
39+
"@typescript-eslint/eslint-plugin": "^5.57.0",
40+
"@typescript-eslint/parser": "^5.57.0",
41+
"adminjs": "^7.0.0-beta-v7.3",
4242
"argon2": "^0.30.3",
4343
"chai": "^4.3.7",
4444
"eslint": "^8.36.0",
@@ -47,13 +47,13 @@
4747
"eslint-plugin-jsx-a11y": "^6.7.1",
4848
"eslint-plugin-react": "^7.32.2",
4949
"eslint-plugin-react-hooks": "^4.6.0",
50-
"husky": "^4.2.5",
5150
"mocha": "^10.2.0",
51+
"husky": "^8.0.3",
5252
"semantic-release": "^20.1.3",
5353
"semantic-release-slack-bot": "^4.0.0",
54-
"sinon": "^15.0.2",
54+
"sinon": "^15.0.3",
5555
"sinon-chai": "^3.7.0",
5656
"ts-node": "^10.9.1",
57-
"typescript": "^4.9.5"
57+
"typescript": "^5.0.2"
5858
}
5959
}

src/bundle-component.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'path'
2+
import * as url from 'url'
3+
4+
import type { ComponentLoader } from 'adminjs'
5+
6+
// eslint-disable-next-line no-underscore-dangle
7+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
8+
9+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
10+
const bundleComponent = (
11+
loader: ComponentLoader,
12+
componentName: string,
13+
) => {
14+
const componentPath = path.join(__dirname, `./components/${componentName}`)
15+
return loader.add(componentName, componentPath)
16+
}
17+
18+
export default bundleComponent

src/passwords.feature.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import AdminJS, { ActionContext, ActionRequest, ActionResponse, After, Before } from 'adminjs'
1+
import AdminJS, { ActionContext, ActionRequest, ActionResponse, After, Before, ComponentLoader } from 'adminjs'
2+
23
import { expect } from 'chai'
34
import sinon, { SinonStub } from 'sinon'
45

@@ -10,6 +11,7 @@ describe('passwordsFeature', () => {
1011
let context: ActionContext
1112
let response: ActionResponse
1213
let hash: SinonStub<[string], Promise<string>>
14+
const componentLoader = new ComponentLoader()
1315

1416
beforeEach(() => {
1517
properties = {
@@ -37,12 +39,18 @@ describe('passwordsFeature', () => {
3739
})
3840

3941
it('returns password feature', async () => {
40-
expect(typeof passwordsFeature({ hash })).to.have.eq('function')
42+
expect(typeof passwordsFeature({ componentLoader, hash })).to.have.eq('function')
43+
})
44+
45+
it('throws an error when componentLoader function is not defined', () => {
46+
expect(() => {
47+
passwordsFeature({} as any)
48+
}).to.throw()
4149
})
4250

4351
it('throws an error when hashing function is not defined', () => {
4452
expect(() => {
45-
passwordsFeature()
53+
passwordsFeature({ componentLoader } as any)
4654
}).to.throw()
4755
})
4856

@@ -55,7 +63,7 @@ describe('passwordsFeature', () => {
5563
}
5664

5765
beforeEach(() => {
58-
encryptPassword = getBeforeHook({ properties, hash })
66+
encryptPassword = getBeforeHook({ componentLoader, properties, hash })
5967
})
6068

6169
it('does nothing when method is get', async () => {
@@ -102,7 +110,7 @@ describe('passwordsFeature', () => {
102110
}
103111

104112
beforeEach(() => {
105-
movePasswordErrors = getAfterHook({ properties, hash })
113+
movePasswordErrors = getAfterHook({ componentLoader, properties, hash })
106114
})
107115

108116
it('does nothing when payload doesn\'t have errors', async () => {

src/passwords.feature.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import AdminJS, { ActionResponse, After, Before, buildFeature, FeatureType } from 'adminjs'
1+
import { ActionResponse, After, Before, buildFeature, ComponentLoader, FeatureType } from 'adminjs'
2+
import bundleComponent from './bundle-component.js'
23

34
/**
45
* Hashing function used to convert the password
@@ -21,6 +22,10 @@ export type HashingFunction = (
2122
* @memberof module:@adminjs/passwords
2223
*/
2324
export type PasswordsOptions = {
25+
/**
26+
* Your ComponentLoader instance. It is required for the feature to add it's components.
27+
*/
28+
componentLoader: ComponentLoader;
2429
/**
2530
* Names of the properties used by the feature
2631
*/
@@ -47,17 +52,17 @@ export type Custom = {
4752
[T in keyof NonNullable<PasswordsOptions['properties']>]: NonNullable<T>
4853
}
4954

50-
const editComponent = AdminJS.bundle('./components/edit', 'PasswordFeatureEdit' as any)
51-
52-
export const passwordsFeature = (options?: PasswordsOptions): FeatureType => {
53-
const passwordProperty = options?.properties?.password || 'password'
54-
const encryptedPasswordProperty = options?.properties?.encryptedPassword || 'encryptedPassword'
55-
const { hash } = options || {}
55+
const passwordsFeature = (options: PasswordsOptions): FeatureType => {
56+
const passwordProperty = options.properties?.password || 'password'
57+
const encryptedPasswordProperty = options.properties?.encryptedPassword || 'encryptedPassword'
58+
const { componentLoader, hash } = options
5659

5760
if (!hash) {
5861
throw new Error('You have to pass "hash" option in "PasswordOptions" of "passwordsFeature"')
5962
}
6063

64+
const editComponent = bundleComponent(componentLoader, 'PasswordEditComponent')
65+
6166
const encryptPassword: Before = async (request) => {
6267
const { method } = request
6368
const { [passwordProperty]: newPassword, ...rest } = request.payload || {}

0 commit comments

Comments
 (0)