Minimalist, strongly typed, immutable plugin system.
"type": "module"
)Links:
pnpm add immutable-plugin-system
ImmutablePlugin<C>
.ImmutableHost<P>
.ImmutableEntities<K, V>
: inner entity map
(Readonly<Record<K, NonNullable<V>>>
).ImmutableEntitiesRecord
: mapping of entity types to inner maps.ImmutablePlugin<C>
: plugin with name: PluginURN
and entities: C
.ImmutableHost<P>
: host that builds entities
collections across all
plugins.ImmutableEntityCollection<K, E>
: iterable view with get
, entries
,
flat
, map
, flatMap
.Key rules enforced by runtime guards:
"0"
,
"-1"
, "1.5"
are rejected).undefined
is forbidden). Omit the key instead.name
must be non‑empty, and equal the URN key in the plugins record.import {
ImmutableHost,
type ImmutablePlugin,
type ImmutableEntities,
} from 'immutable-plugin-system';
// 1) Define your integration’s entities schema
type Command = (...args: string[]) => string;
type Entities = {
assets: ImmutableEntities<string, string>;
commands: ImmutableEntities<string, Command>;
};
// 2) Model your plugin
interface Plugin extends ImmutablePlugin<Entities> {
description: string;
}
const pluginA: Plugin = {
name: 'pluginA',
description: 'this is plugin A',
entities: {
assets: { foo: 'this is `foo`', duplicate: 'duplicate from A' },
commands: { hello: (...args) => `hello(${args.join(',')})` },
},
};
const pluginB: Plugin = {
name: 'pluginB',
description: 'this is plugin B',
entities: {
assets: { bar: 'this is `bar`', duplicate: 'duplicate from B' },
commands: { world: (...args) => `world(${args.join(',')})` },
},
};
// 3) Create the host; entity completeness is enforced automatically
const host = new ImmutableHost<Plugin>({ pluginA, pluginB });
// 4) Discover entities
for (const [value, key, plugin] of host.entities.assets) {
console.log(`asset ${String(key)} from ${plugin}: ${value}`);
}
// Get by key (returns all contributors)
const dup = host.entities.assets.get('duplicate'); // ['duplicate from A','duplicate from B']
// Call a command (integration decides on uniqueness/selection semantics)
const results = host.entities.commands.get('hello').map((fn) => fn('arg'));
get(key: K): E[]
— returns all entities for a key. Returns a fresh array
copy; mutating it does not affect the collection.size: number
— returns the number of unique keys in the collection.keys(): Iterator<K>
— returns an iterator over unique keys.values(): Iterator<E[]>
— returns an iterator over entity arrays per key.entries(): Iterator<[K, E[]]>
— per‑key grouped arrays.flat(): [E, K, PluginURN][]
— flattened entities with provenance.map(fn)
/ flatMap(fn)
— transform grouped or individual items.for (const [e, k, p] of collection) { … }
.Importable helpers validate shapes and invariants when needed by your integration:
isPlainObject
, assertPlainObject
isEntityRecord
, assertEntityRecord
isEntitiesRecord
, assertEntitiesRecord
isImmutablePlugin
, assertImmutablePlugin
isImmutablePlugins
, assertImmutablePlugins
The host uses the same rules internally.
Notes:
Guards return booleans (is*
) or throw on violation (assert*
). Host
constructor performs the same validations and throws TypeError
with
descriptive messages on failure.
Validate a single plugin or a plugin record before constructing the host:
import {
assertImmutablePlugin,
assertImmutablePlugins,
type ImmutableEntities,
type ImmutablePlugin,
} from 'immutable-plugin-system';
type P = ImmutablePlugin<{
assets: ImmutableEntities<string, string>;
commands: ImmutableEntities<string, string>;
}>;
const candidate: unknown = {
name: 'p',
entities: {
assets: { foo: 'bar' },
commands: {},
},
};
assertImmutablePlugin(candidate); // narrows to ImmutablePlugin<P>
assertImmutablePlugins({ p: candidate });
Missing entity types trigger TypeError
during guard checks or host
construction. Supply empty maps for entity types that have nothing to
contribute.
pnpm -s install
pnpm -s test:fix
(auto‑fix + build + type + tests)pnpm -s test
pnpm -s lint
pnpm -s build
Conventions:
any
is forbidden. Minimal, defensible unknown
only with guards.tsdoc/syntax
).installed-node-modules
.These are integration concerns; see the specification for discussion and recommendations.
Every entity type is required; represent “no entities” with empty
ImmutableEntities
maps. The host enforces this invariant at runtime and
throws on omissions.
Entity keys exclude numbers and numeric‑like strings to avoid JS coercion ambiguity. Prefer textual identifiers or symbols.
Performance: Host construction groups inner maps by entity type with straightforward iteration. Complexity is linear in the number of plugins and entity maps; large sets are covered by tests.
Collections are plain JavaScript iterables. Iteration order follows the
standard Map
insertion order semantics and is not a stability guarantee
across different plugin sets.
pnpm -s install
pnpm -s test:fix
pnpm -s test
pnpm -s lint
.any
is forbidden, unknown
only with proper
guards.This package is pre‑1.0 (0.x
). While the specification is stable (v1.1.0), the
public API may evolve before 1.0.0
. Follow semver and consult the changelog
once released.
MIT — see LICENSE