mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-13 19:30:30 +00:00
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { camelCase, upperFirst } from 'lodash';
|
|
import { Importable } from './Importable';
|
|
import { getImportableService } from './decorators/Import.decorator';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { ContextIdFactory, ModuleRef } from '@nestjs/core';
|
|
|
|
@Injectable()
|
|
export class ImportableRegistry {
|
|
constructor(private readonly moduleRef: ModuleRef) { }
|
|
/**
|
|
* Retrieves the importable service instance of the given resource name.
|
|
* @param {string} name
|
|
* @returns {Importable}
|
|
*/
|
|
public async getImportable(name: string) {
|
|
const _name = this.sanitizeResourceName(name);
|
|
const importable = getImportableService(_name);
|
|
|
|
if (!importable) {
|
|
throw new Error(
|
|
`No importable service found for resource "${_name}". Make sure the resource has an @ImportableService decorator registered.`,
|
|
);
|
|
}
|
|
const contextId = ContextIdFactory.create();
|
|
|
|
const importableInstance = await this.moduleRef.resolve(importable, contextId, {
|
|
strict: false,
|
|
});
|
|
return importableInstance;
|
|
}
|
|
|
|
private sanitizeResourceName(resource: string) {
|
|
return upperFirst(camelCase(resource));
|
|
}
|
|
}
|