26 lines
746 B
TypeScript
26 lines
746 B
TypeScript
import "../core/metadata.ts";
|
|
import { defineMetadata, getMetadata } from "../core/metadata.ts";
|
|
import type { ProviderScope } from "../core/container.ts";
|
|
|
|
export const SERVICE_METADATA_KEY = "muddle:service";
|
|
|
|
export interface ServiceOptions {
|
|
/** Lifetime of the service. */
|
|
scope?: ProviderScope;
|
|
}
|
|
|
|
/** Declares a service. */
|
|
export function Service(options: ServiceOptions = {}): ClassDecorator {
|
|
return (target) => {
|
|
defineMetadata(SERVICE_METADATA_KEY, options, target as object);
|
|
return target;
|
|
};
|
|
}
|
|
|
|
/** Reads the options of a `@Service()` decorated class. */
|
|
export function getServiceOptions(target: object): ServiceOptions {
|
|
return (
|
|
(getMetadata(SERVICE_METADATA_KEY, target) as ServiceOptions | undefined) ??
|
|
{}
|
|
);
|
|
}
|