Skip to content

Commit

Permalink
Merge pull request #67 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(module): export Annotation helper
  • Loading branch information
jlenon7 authored Sep 15, 2023
2 parents 71b440b + 2de9b12 commit cec49c7
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 57 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/ioc",
"version": "4.3.0",
"version": "4.4.0",
"description": "Global Ioc helper for Athenna ecosystem. Built on top of awilix.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
2 changes: 1 addition & 1 deletion src/annotations/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export function Service(options?: ServiceOptions): ClassDecorator {
return
}

Annotation.defineServiceMeta(target, options)
Annotation.defineMeta(target, options)
}
}
13 changes: 0 additions & 13 deletions src/constants/MetadataKeys.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/container/Ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ export class Ioc {
this.alias(String.toCamelCase(subAlias), meta.alias)
}

if (meta.name) {
this.alias(meta.name, meta.alias)
}

if (meta.camelAlias) {
this.alias(meta.camelAlias, meta.alias)
}
Expand Down
65 changes: 27 additions & 38 deletions src/helpers/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,67 @@
* file that was distributed with this source code.
*/

import {
IOC_TYPE,
IOC_ALIAS,
IOC_REGISTERED,
IOC_CAMEL_ALIAS
} from '#src/constants/MetadataKeys'
import { Options } from '@athenna/common'
import type { ServiceOptions } from '#src'

export type ServiceMeta = {
type: string
alias: string
camelAlias?: string
isRegistered: boolean
registered: boolean
[key: string]: any
}

export class Annotation {
/**
* Verify if provider is decorated or not.
*/
public static isAnnotated(target: any): boolean {
return Reflect.hasMetadata(IOC_REGISTERED, target)
return Reflect.hasMetadata('ioc:registered', target)
}

/**
* Define all metadata for a service.
*/
public static defineServiceMeta(target: any, options: ServiceOptions) {
Options.whenDefined(options.type, type => {
Reflect.defineMetadata(IOC_TYPE, type, target)
})

Options.whenDefined(options.alias, alias => {
Reflect.defineMetadata(IOC_ALIAS, alias, target)
})
public static defineMeta(target: any, options: ServiceOptions) {
Object.keys(options).forEach(key => {
const value = options[key]

Options.whenDefined(options.camelAlias, camelAlias => {
Reflect.defineMetadata(IOC_CAMEL_ALIAS, camelAlias, target)
Options.whenDefined(value, value => {
Reflect.defineMetadata(`ioc:${key}`, value, target)
})
})

if (Annotation.hasAllMetaDefined(target)) {
Reflect.defineMetadata(IOC_REGISTERED, false, target)
}
Reflect.defineMetadata('ioc:registered', false, target)
}

/**
* Define the service as registered.
*/
public static defineAsRegistered(target: any) {
Reflect.defineMetadata(IOC_REGISTERED, true, target)
}

/**
* Verify if all metadata is defined in the service.
*/
public static hasAllMetaDefined(target: any) {
const hasType = Reflect.hasMetadata(IOC_TYPE, target)
const hasAlias = Reflect.hasMetadata(IOC_ALIAS, target)

return hasType && hasAlias
Reflect.defineMetadata('ioc:registered', true, target)
}

/**
* Get all the metadata from the service.
*/
public static getMeta(target: any): ServiceMeta {
return {
type: Reflect.getMetadata(IOC_TYPE, target) || 'transient',
alias:
Reflect.getMetadata(IOC_ALIAS, target) || `App/Services/${target.name}`,
camelAlias: Reflect.getMetadata(IOC_CAMEL_ALIAS, target),
isRegistered: Reflect.getMetadata(IOC_REGISTERED, target)
const meta: any = {}

Reflect.getMetadataKeys(target).forEach(key => {
const value = Reflect.getMetadata(key, target)

meta[key.replace('ioc:', '')] = value
})

if (!meta.type) {
meta.type = 'transient'
}

if (!meta.alias) {
meta.alias = `App/Services/${target.name}`
}

return meta
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from '#src/globals/Ioc'

export * from '#src/container/Ioc'
export * from '#src/facades/Facade'
export * from '#src/helpers/Annotation'
export * from '#src/annotations/Inject'
export * from '#src/annotations/Service'
export * from '#src/annotations/InjectStub'
Expand Down
2 changes: 2 additions & 0 deletions src/types/ServiceOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export type ServiceOptions = {
* @default 'transient'
*/
type?: 'fake' | 'scoped' | 'singleton' | 'transient'

[key: string]: any
}
4 changes: 3 additions & 1 deletion tests/fixtures/ClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface Client {
name: string
}

@Service()
@Service({
name: 'App/Services/Names/ClientService'
})
export class ClientService {
private signature = Math.random()

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/annotations/ServiceAnnotationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class ServiceAnnotationTest extends BaseTest {
assert.equal(metadata.type, 'transient')
assert.equal(metadata.camelAlias, 'productService')
assert.equal(metadata.alias, 'App/Services/ProductService')
assert.equal(metadata.isRegistered, false)
assert.equal(metadata.registered, false)
}

@Test()
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/container/IocTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export default class IocTest extends BaseTest {

assert.isTrue(container.has('clientService'))
assert.isTrue(container.has('App/Services/ClientService'))
assert.isTrue(container.has('App/Services/Names/ClientService'))
}

@Test()
Expand All @@ -218,5 +219,6 @@ export default class IocTest extends BaseTest {
assert.isTrue(container.has('App/Services/StringHelper'))
assert.isTrue(container.has('clientService'))
assert.isTrue(container.has('App/Services/ClientService'))
assert.isTrue(container.has('App/Services/Names/ClientService'))
}
}

0 comments on commit cec49c7

Please sign in to comment.