contexify
Version:
A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.
116 lines • 4.39 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { BindingKey } from "../binding/binding-key.js";
import { ContextView } from "../context/context-view.js";
import { getDeepProperty } from "../utils/value-promise.js";
import { assertTargetType, inject } from "./inject.js";
function config(propertyPath, metadata) {
let resolvedPropertyPath = propertyPath ?? "";
let resolvedMetadata = metadata;
if (typeof propertyPath === "object") {
resolvedMetadata = propertyPath;
resolvedPropertyPath = "";
}
const finalMetadata = Object.assign({
propertyPath: resolvedPropertyPath,
decorator: "@config",
optional: true
}, resolvedMetadata);
return inject("", finalMetadata, resolveFromConfig);
}
__name(config, "config");
(function(config2) {
config2.getter = /* @__PURE__ */ __name(function injectConfigGetter(propertyPath, metadata) {
let resolvedPropertyPath = propertyPath ?? "";
let resolvedMetadata = metadata;
if (typeof propertyPath === "object") {
resolvedMetadata = propertyPath;
resolvedPropertyPath = "";
}
const finalMetadata = Object.assign({
propertyPath: resolvedPropertyPath,
decorator: "@config.getter",
optional: true
}, resolvedMetadata);
return inject("", finalMetadata, resolveAsGetterFromConfig);
}, "injectConfigGetter");
config2.view = /* @__PURE__ */ __name(function injectConfigView(propertyPath, metadata) {
let resolvedPropertyPath = propertyPath ?? "";
let resolvedMetadata = metadata;
if (typeof propertyPath === "object") {
resolvedMetadata = propertyPath;
resolvedPropertyPath = "";
}
const finalMetadata = Object.assign({
propertyPath: resolvedPropertyPath,
decorator: "@config.view",
optional: true
}, resolvedMetadata);
return inject("", finalMetadata, resolveAsViewFromConfig);
}, "injectConfigView");
})(config || (config = {}));
function getCurrentBindingKey(session) {
return session.currentBinding?.key;
}
__name(getCurrentBindingKey, "getCurrentBindingKey");
function getTargetBindingKey(injection, session) {
return injection.metadata.fromBinding || getCurrentBindingKey(session);
}
__name(getTargetBindingKey, "getTargetBindingKey");
function resolveFromConfig(ctx, injection, session) {
const bindingKey = getTargetBindingKey(injection, session);
if (!bindingKey) return void 0;
const meta = injection.metadata;
return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
session,
optional: meta.optional
});
}
__name(resolveFromConfig, "resolveFromConfig");
function resolveAsGetterFromConfig(ctx, injection, session) {
assertTargetType(injection, Function, "Getter function");
const bindingKey = getTargetBindingKey(injection, session);
const meta = injection.metadata;
return /* @__PURE__ */ __name(async function getter() {
if (!bindingKey) return void 0;
return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
// Start with a new session for `getter` resolution to avoid
// possible circular dependencies
session: void 0,
optional: meta.optional
});
}, "getter");
}
__name(resolveAsGetterFromConfig, "resolveAsGetterFromConfig");
function resolveAsViewFromConfig(ctx, injection, session) {
assertTargetType(injection, ContextView);
const bindingKey = getTargetBindingKey(injection, session);
if (!bindingKey) return void 0;
const view = new ConfigView(ctx, (binding) => binding.key === BindingKey.buildKeyForConfig(bindingKey).toString(), injection.metadata.propertyPath);
view.open();
return view;
}
__name(resolveAsViewFromConfig, "resolveAsViewFromConfig");
let ConfigView = class ConfigView2 extends ContextView {
static {
__name(this, "ConfigView");
}
propertyPath;
constructor(ctx, filter, propertyPath) {
super(ctx, filter), this.propertyPath = propertyPath;
}
/**
* Get values for the configuration with a property path
* @param session - Resolution session
*/
async values(session) {
const configValues = await super.values(session);
const propertyPath = this.propertyPath;
if (!propertyPath) return configValues;
return configValues.map((v) => getDeepProperty(v, propertyPath));
}
};
export {
config
};
//# sourceMappingURL=inject-config.js.map