Skip to content

Commit

Permalink
refactor(guard-clause): refactor guard factory to guard store
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisAnubiz committed Jan 9, 2025
1 parent ee151e3 commit 62d2e63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 54 deletions.
74 changes: 21 additions & 53 deletions packages/guard-clause/src/factory/guard.factory.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,36 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import type { GuardError } from '../errors/index.js'
import type { AbstractGuardExtensionFactoryOptions } from './abstract-guard.extension.factory.js'
import type { AbstractGuardExtensionFactory } from './abstract-guard.extension.factory.js'
import type { GuardError } from '../errors/index.js'
import type { AbstractGuardExtension } from './abstract-guard.extension.js'

import { GuardErrors } from '../errors/index.js'
import { GuardErrors } from '../errors/index.js'

export class GuardFactory {
private static extensions: Map<
AbstractGuardExtensionFactory['constructor'],
AbstractGuardExtensionFactory
> = new Map()
export class GuardStore {
private static extensions: Map<string | symbol, Array<AbstractGuardExtension>> = new Map()

private static extensionsByTargetMethod: Map<
any,
Map<string, Set<AbstractGuardExtensionFactory['constructor']>>
> = new Map()

static registerExtension(extension: AbstractGuardExtensionFactory): void {
this.extensions.set(extension.constructor, extension)
}

static register(
extension: typeof AbstractGuardExtensionFactory,
target: any,
methodName: string,
paramIndex: number,
options: AbstractGuardExtensionFactoryOptions
): void {
if (this.extensions.has(extension)) {
this.extensions.get(extension)!.register(target, methodName, paramIndex, options)

if (!this.extensionsByTargetMethod.has(target)) {
this.extensionsByTargetMethod.set(target, new Map())
}

if (!this.extensionsByTargetMethod.get(target)!.has(methodName)) {
this.extensionsByTargetMethod.get(target)!.set(methodName, new Set())
}

this.extensionsByTargetMethod.get(target)!.get(methodName)?.add(extension)
} else {
throw new Error(`Extension for ${extension.constructor.name} not registered`)
static add(key: string | symbol, extension: AbstractGuardExtension): void {
const extensions = GuardStore.extensions.get(key)
if (!extensions) {
GuardStore.extensions.set(key, [extension])
return
}
extensions.push(extension)
}

static perform(target: any, methodName: string, paramValues: Array<any>): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static perform(key: string | symbol, paramValue: any): void {
const errors: Array<GuardError> = []

this.extensionsByTargetMethod
.get(target)
?.get(methodName)
?.forEach((extension) => {
this.extensions
.get(extension)!
.perform(target, methodName, paramValues)
.reverse()
.forEach((error) => {
errors.push(error)
})
})
const extensions = this.extensions.get(key)
if (!extensions) return

extensions.forEach((extension: AbstractGuardExtension) => {
const performErrors = extension.perform(paramValue)
errors.push(...performErrors)
})

if (errors.length > 0) {
throw new GuardErrors(errors.reverse())
throw new GuardErrors(errors)
}
}
}
2 changes: 1 addition & 1 deletion packages/guard-clause/src/factory/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './abstract-guard.extension.factory.js'
export * from './abstract-guard.extension.js'
export * from './guard.factory.js'

0 comments on commit 62d2e63

Please sign in to comment.