Skip to content

Commit

Permalink
Merge pull request #144 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(npm): require @athenna/config
  • Loading branch information
jlenon7 authored Jan 22, 2025
2 parents 0e66773 + c877e30 commit 252c3e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "5.6.0",
"version": "5.7.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -106,7 +106,7 @@
"youch-terminal": "^2.2.3"
},
"devDependencies": {
"@athenna/test": "^5.2.0",
"@athenna/test": "^5.3.0",
"@athenna/tsconfig": "^5.0.0",
"@japa/expect-type": "^2.0.3",
"@types/bytes": "^3.1.5",
Expand Down
20 changes: 16 additions & 4 deletions src/helpers/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import pluralize from 'pluralize'
import * as changeCase from 'change-case'

import { crc32 } from 'crc'
import { debug } from '#src/debug'
import { Path } from '#src/helpers/Path'
import { Module } from '#src/helpers/Module'
import { Options } from '#src/helpers/Options'
import { Macroable } from '#src/helpers/Macroable'
import { createHmac, randomBytes } from 'node:crypto'
import { OrdinalNanException } from '#src/exceptions/OrdinalNanException'
import { NotFoundAthennaConfig } from '#src/exceptions/NotFoundAthennaConfig'

const config = await Module.safeImport('@athenna/config')

export class String extends Macroable {
public static config: any

/**
* Generate hash for a given value.
*
Expand All @@ -34,11 +36,21 @@ export class String extends Macroable {
value: string,
options: { key?: string; prefix?: string } = {}
) {
if (!options.key && !config) {
if (!this.config) {
const require = Module.createRequire(Path.pwd())

try {
this.config = require('@athenna/config')
} catch (_err) {
debug('@athenna/config not found to run String.hash()')
}
}

if (!options.key && !this.config) {
throw new NotFoundAthennaConfig()
}

options.key = options.key || config.Config.get('app.key')
options.key = options.key || this.config.Config.get('app.key')

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

Expand Down

0 comments on commit 252c3e9

Please sign in to comment.