@geeko/meta
v1.4.4
Published
Geeko metadata library for decorators & dependency injection
Downloads
34
Readme
Geeko Meta

Simple dependancy injection library written for Typescript
- Auto Resolve
- Create Application Context
- Custom Injectables
- Get Specific Injectables
- Get Metadata
- Installation
Part of the Geeko ecosystem, this library allows for automatic dependancy injection & context building.
Resolve
Below is an example of how to resolve an instance which has the @Injectable decorator.
import { Reflector } from '@geeko/meta';
const instance: Test = Reflector.get(Test);Test class & @Injectable definition below:
/**
* @public
*/
export interface Encoder {
encode(value: any): string;
decode(value: string): any;
}
/**
* Native @see JSON encoder/decoder
*
* @public
*/
@Injectable('JSON_ENCODER')
export class JsonEncoder implements Encoder {
encode(value: any): string {
return JSON.stringify(value);
}
decode(value: string): any {
return JSON.parse(value);
}
}
/**
* @public
*/
@Injectable()
export class Test {
/**
* Expects @see Encoder interface - in this case the @see JsonEncoder
*
* @public
* @param {Encoder} encoder
*/
public constructor(@Inject('JSON_ENCODER') public encoder: Encoder) {}
}Application Context
Application contexts can be created to fine tune the dependancy output by adding custom factory functions/values & injection tokens, As seen below:
import { Reflector } from '@geeko/meta';
const context: ApplicationContext = Reflector.createApplicationContext({
providers: [Test, {
token: "JSON_ENCODER",
useFactory: (): Encoder => {
return new JsonEncoder();
}
}],
});
const instance: Test = context.get(Test);Custom Injectables
Below is an example of how to create a custom injectable using the SetMetadata:
import { SetMetadata, CustomDecorator, MetadataOptions } from '@geeko/meta';
/**
* Custom @see Encoder based injection declaration.
*
* @public
* @param {InjectableOptions | String} options
* @returns {CustomDecorator<T>}
*/
export const Encoding = (
options?: string | InjectableOptions,
): CustomDecorator => {
let metadata: MetadataOptions = Object.assign(
typeof options === 'string'
? { token: options }
: (options ?? {}),
{
injectable: true,
},
);
return SetMetadata(ENCODER_INJECTABLE_TOKEN, true, metadata);
};
/**
* Custom hex encoder definition
*
* @public
*/
@Encoding('hex')
export class HexEncoder implements Encoder {
encode(value: any): string {
return toHex(value);
}
decode(value: string): any {
return fromHex(value);
}
}Get Specific Injectables
Below is an example of how to get all injectables with a specific token, in this case the Encoding token seen above:
import { Reflector } from '@geeko/meta';
const injectables: Array<any> = Reflector.getFor(ENCODER_INJECTABLE_TOKEN);If the injectable is a function or member of a class
const injectables: Array<any> | undefined = Reflector.getFor(GET_INJECTABLE_TOKEN, {
isProperty: true,
});The response will look like the following:
[
{
metadata: { path: '/test' },
target: [class Test],
token: 'GET_INJECTABLE_TOKEN',
key: 'get'
}
]Get Metadata
Below code will fetch the raw metadata passed to the @Injectable decorator for the Test class example as seen below:
const metadata: any = Reflector.getMetadata(Test);Note: By default the resolver will automatically collect metadata ready for injection on startup, however, if you wish to only use the 'createApplicationContext' method, you can disable the automatic resolver by setting the following process environment variable GEEKO_AUTO_INJECT to 0, e.g:
set GEEKO_AUTO_INJECT 0 node ./app.jsInstallation
NPM
npm i @geeko/meta