@universal-packages/adapter-resolver v1.6.1
Adapter Resolver
Module loader based on adapter parameters.
Install
npm install @universal-packages/adapter-resolverGlobal methods
resolveAdapter(options: Object)
Tries to infer an installed module name based on name and options, imports it and infers the desired export based on name and options.
import { resolveAdapter } from '@universal-packages/adapter-resolver'
const adapter = resolveAdapter({ name: 'redis', domain: 'token-registry', type: 'engine' })
console.log(adapter)
// > [class RedisEngine]Options
Adapters need to follow the convention in order to be found. domain and name are use to infer the package name and type is used to infer the export name.
nameStringThe name of the adapter, for exampleredis.domainStringThe domain for which the adapter will work for example if the adapter isredisand is meant to work with@universal-packages/token-registryyou provide heretoken-registry.internalObjectAn object which keys match an internal adapter provided by the root library.import { resolveAdapter } from '@universal-packages/adapter-resolver' const adapter = resolveAdapter('local', { internal: { domain: 'token-registry', type: 'engine', local: LocalAdapter } }) console.log(adapter) // > [class LocalAdapter]typeStringThe package needs to export something in the format<name><type>, for example if the adapter is an "engine", you provide hereengineand it will internally will come up with different case variations of the format to find the export ex: redisEngine, RedisEngine, redis_engine.
gatherAdapters(options: Object)
Gather of the adapters of a type under the same domain.
import { gatherAdapters } from '@universal-packages/adapter-resolver'
const adapters = gatherAdapters({ domain: 'token-registry', type: 'engine' })
console.log(adapters)
// > { redis: [class RedisEngine], local: [class LocalEngine] }Options
domainStringThe domain under which the adapters are meant to work, for example all engines for@universal-packages/token-registrywill have the same domaintoken-registry.internalObjectAn object of named internal adapters provided by the root library to be mixed with the gathered adapters.import { gatherAdapters } from '@universal-packages/adapter-resolver' const adapters = gatherAdapters({ domain: 'token-registry', type: 'engine', internal: { local: InternalAdapter } }) console.log(adapters) // > { internal: [class InternalAdapter], redis: [class RedisEngine], local: [class LocalAdapter] }typeStringThe type of the adapters, for exampleengine.
Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.