@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
31 lines (30 loc) • 1.03 kB
JavaScript
import assert from "assert";
import { container as globalContainer, instanceCachingFactory } from "tsyringe";
export const CONTAINER_KEY = "$CONTAINER";
export function KEY(serviceOrGroup, service) {
if (typeof service === "function") {
assert(service.name, "Invalid constructor " + service);
service = service.name;
}
if (service) {
serviceOrGroup = `${serviceOrGroup}:${service}`;
}
return serviceOrGroup;
}
export function RESOLVE(container, service) {
return container.resolve(service);
}
export function RESOLVE_ALL(container, service) {
return container.resolveAll(service);
}
export function RESOLVE_MANY(container, ...types) {
return types.map(x => container.resolve(x));
}
export function REGISTER_INSTANCE(token, value) {
globalContainer.register(token, {
useFactory: instanceCachingFactory(() => value)
});
}
export function REGISTER_OPTIONS(service, options) {
REGISTER_INSTANCE(KEY("OPTIONS", service), options);
}