@luvio/utils
Advanced tools
Comparing version 3.6.0 to 4.0.0
@@ -1,2 +0,2 @@ | ||
import type { LoggerService } from './logger'; | ||
import type { NamedLoggerService } from './logger'; | ||
type Position = { | ||
@@ -16,3 +16,3 @@ line: number; | ||
private filePath; | ||
constructor(services: LoggerService, filePath: string); | ||
constructor(services: NamedLoggerService, filePath: string); | ||
trace(position: Position, message: string): void; | ||
@@ -19,0 +19,0 @@ debug(position: Position, message: string): void; |
export { IncludeExclude, includes } from './include-exclude'; | ||
export { emptyServices, satisfies, Service, ServiceVersion, TypeOf } from './service'; | ||
export { satisfies, NamedService, ServiceVersion, ServiceDescriptor } from './service'; | ||
/** | ||
@@ -4,0 +4,0 @@ * Indicates if an object is PromiseLike. |
@@ -1,6 +0,6 @@ | ||
import type { Service, TypeOf } from './service'; | ||
import type { NamedService, ServiceDescriptor } from './service'; | ||
/** | ||
* Service that builds TypeScript source files. | ||
*/ | ||
export type LoggerService = Service<'logger', { | ||
export type LoggerService = { | ||
trace(message: string): void; | ||
@@ -11,4 +11,5 @@ debug(message: string): void; | ||
error(message: string): void; | ||
}>; | ||
export type Logger = TypeOf<LoggerService>; | ||
}; | ||
export type NamedLoggerService<Name extends string = 'logger'> = NamedService<Name, LoggerService>; | ||
export type LoggerServiceDescriptor = ServiceDescriptor<LoggerService, 'logger', '1.0'>; | ||
export declare const LogLevelMap: { | ||
@@ -27,3 +28,3 @@ readonly TRACE: 4; | ||
*/ | ||
export declare class ConsoleLogger implements Logger { | ||
export declare class ConsoleLogger implements LoggerService { | ||
level: LogLevel; | ||
@@ -30,0 +31,0 @@ private printer; |
/** | ||
* A Service is a value (data and/or code) that matches some specificed type. It | ||
* is exposed using a well-defined property on an object to enable (i) aggregating | ||
* services, and (ii) swapping out service implementations. A specific service | ||
* will expose its own Service type that narrows this type, e.g.: | ||
* | ||
* export type FooService = Service<'foo', { | ||
* create(string: s) => number, | ||
* list() => string[] | ||
* }>; | ||
* | ||
* Consumers *must* dereference the service name on each use in order to correctly | ||
* respond to service overrides. E.g. | ||
* | ||
* class Xyz { | ||
* services: FooService & BarService; | ||
* | ||
* constructor(services: FooService & BarService) { | ||
* this.services = services; | ||
* // this.foo = services.foo <== DO NOT DO THIS! | ||
* } | ||
* | ||
* someMethod() { | ||
* return this.services.foo.create('abc'); | ||
* } | ||
* } | ||
* A NamedService is anything that has been given a name. | ||
*/ | ||
export type Service<Name extends string, Type> = Record<Name, Type>; | ||
export type TypeOf<S> = S extends Service<any, infer Type> ? Type : never; | ||
export type NamedService<Name extends string, Service = unknown> = Record<Name, Service>; | ||
/** | ||
* Utility function to generate a strongly-typed, empty set of Services. Aside from | ||
* having the correct type, the returned set behaves as {} except it will throw an | ||
* exception if an attempt is made to get a Service that has not been set. | ||
* | ||
* @typeParam Services the set of Services that will be stored in the set, typically | ||
* expressed as an intersection of Service types, e.g. FooService & BarService | ||
* @returns empty set of Services | ||
* A ServiceDescriptor is an encapsulation of a Service that adds additional | ||
* metadata about the service, including the type (or canonical name) and version | ||
* of the service. | ||
*/ | ||
export declare function emptyServices<Services extends Service<any, any>>(): Services; | ||
export type ServiceDescriptor<Service, Type extends string = string, Version extends ServiceVersion = ServiceVersion> = { | ||
readonly type: Type; | ||
readonly version: Version; | ||
service: Service; | ||
}; | ||
/** | ||
@@ -41,0 +16,0 @@ * Services are versioned using semver major/minor/patch version numbers. Each segment |
@@ -47,21 +47,2 @@ /** | ||
/** | ||
* Utility function to generate a strongly-typed, empty set of Services. Aside from | ||
* having the correct type, the returned set behaves as {} except it will throw an | ||
* exception if an attempt is made to get a Service that has not been set. | ||
* | ||
* @typeParam Services the set of Services that will be stored in the set, typically | ||
* expressed as an intersection of Service types, e.g. FooService & BarService | ||
* @returns empty set of Services | ||
*/ | ||
function emptyServices() { | ||
return new Proxy({}, { | ||
get(services, serviceName, receiver) { | ||
if (!(serviceName in services)) { | ||
throw new Error(`service ${serviceName.toString()} not found`); | ||
} | ||
return Reflect.get(services, serviceName, receiver); | ||
}, | ||
}); | ||
} | ||
/** | ||
* Indicates if a given instance of a service satisfies a request. Note that this function | ||
@@ -146,3 +127,3 @@ * assumes the version numbers are valid strings. | ||
function loggerService(level, printer, formatter) { | ||
return { logger: new ConsoleLogger(level, printer, formatter) }; | ||
return new ConsoleLogger(level, printer, formatter); | ||
} | ||
@@ -425,2 +406,2 @@ | ||
export { isArray as ArrayIsArray, indexOf as ArrayPrototypeIndexOf, push as ArrayPrototypePush, slice as ArrayPrototypeSlice, ConsoleFileParserLogger, ConsoleLogger, DataNotFoundError, stringify as JSONStringify, LogLevelMap, create as ObjectCreate, freeze as ObjectFreeze, keys as ObjectKeys, hasOwnProperty as ObjectPrototypeHasOwnProperty, addAllToSet, bfs, deepEquals, emptyServices, includes, isCacheHitOrError, isDataNotFoundError, isPromiseLike, loggerService, rejectedPromiseLike, resolvedPromiseLike, satisfies, setDifference, setOverlaps, stableJSONStringify, toCamelCase, toError, toPascalCase, toScreamingSnakeCase }; | ||
export { isArray as ArrayIsArray, indexOf as ArrayPrototypeIndexOf, push as ArrayPrototypePush, slice as ArrayPrototypeSlice, ConsoleFileParserLogger, ConsoleLogger, DataNotFoundError, stringify as JSONStringify, LogLevelMap, create as ObjectCreate, freeze as ObjectFreeze, keys as ObjectKeys, hasOwnProperty as ObjectPrototypeHasOwnProperty, addAllToSet, bfs, deepEquals, includes, isCacheHitOrError, isDataNotFoundError, isPromiseLike, loggerService, rejectedPromiseLike, resolvedPromiseLike, satisfies, setDifference, setOverlaps, stableJSONStringify, toCamelCase, toError, toPascalCase, toScreamingSnakeCase }; |
{ | ||
"name": "@luvio/utils", | ||
"version": "3.6.0", | ||
"version": "4.0.0", | ||
"description": "Luvio utils", | ||
@@ -5,0 +5,0 @@ "repository": { |
22378
618