Skip to content

Commit a2c3309

Browse files
authored
Merge pull request #146 from AthennaIO/develop
chore(npm): update dependencies
2 parents c71ec9e + a9de594 commit a2c3309

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/common",
3-
"version": "5.8.0",
3+
"version": "5.9.0",
44
"description": "The Athenna common helpers to use in any Node.js ESM project.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/helpers/String.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ import * as changeCase from 'change-case'
1212

1313
import { crc32 } from 'crc'
1414
import { debug } from '#src/debug'
15-
import { Path } from '#src/helpers/Path'
16-
import { Module } from '#src/helpers/Module'
1715
import { Options } from '#src/helpers/Options'
1816
import { Macroable } from '#src/helpers/Macroable'
1917
import { createHmac, randomBytes } from 'node:crypto'
2018
import { OrdinalNanException } from '#src/exceptions/OrdinalNanException'
2119
import { NotFoundAthennaConfig } from '#src/exceptions/NotFoundAthennaConfig'
2220

2321
export class String extends Macroable {
24-
public static config: any
25-
2622
/**
2723
* Generate hash for a given value.
2824
*
@@ -36,21 +32,15 @@ export class String extends Macroable {
3632
value: string,
3733
options: { key?: string; prefix?: string } = {}
3834
) {
39-
if (!this.config) {
40-
const require = Module.createRequire(Path.pwd())
41-
42-
try {
43-
this.config = require('@athenna/config')
44-
} catch (_err) {
45-
debug('@athenna/config not found to run String.hash()')
46-
}
35+
if (!global.Config) {
36+
debug('@athenna/config not found running String.hash()')
4737
}
4838

49-
if (!options.key && !this.config) {
39+
if (!options.key && !global.Config) {
5040
throw new NotFoundAthennaConfig()
5141
}
5242

53-
options.key = options.key || this.config.Config.get('app.key')
43+
options.key = options.key || global.Config.get('app.key')
5444

5545
const hash = createHmac('sha256', options.key).update(value).digest('hex')
5646

tests/unit/helpers/StringTest.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
*/
99

1010
import { String } from '#src'
11-
import { Test, type Context } from '@athenna/test'
11+
import { AfterEach, Mock, Test, type Context } from '@athenna/test'
1212
import { OrdinalNanException } from '#src/exceptions/OrdinalNanException'
1313
import { NotFoundAthennaConfig } from '#src/exceptions/NotFoundAthennaConfig'
1414

1515
export default class StringTest {
16+
@AfterEach()
17+
public async afterEach() {
18+
Mock.restoreAll()
19+
}
20+
1621
@Test()
1722
public async shouldBeAbleToHashStringValues({ assert }: Context) {
1823
const value = 'lenon'
@@ -35,6 +40,21 @@ export default class StringTest {
3540
)
3641
}
3742

43+
@Test()
44+
public async shouldBeAbleToHashStringValuesUsingConfigAppKey({ assert }: Context) {
45+
const value = 'lenon'
46+
47+
global.Config = null
48+
49+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
50+
// @ts-ignore
51+
Mock.when(global, 'Config').value({
52+
get: () => 'secret1'
53+
})
54+
55+
assert.deepEqual(String.hash(value), 'f48760e603c17c6abf2eff3dad2495b291ccbac943836518d0db3f41c3853f8b')
56+
}
57+
3858
@Test()
3959
public async shouldThrownNotFoundAthennaConfigExceptionIfNotProvidingASecretKeyForHash({ assert }: Context) {
4060
const value = 'lenon'

0 commit comments

Comments
 (0)