refactor(mesh): @Provider's options no longer advertise export
The framework's public type offered an option whose only behaviour is to throw at boot. Removing the field from the options type means a TypeScript caller gets a compile error at the decorator, where the mistake is, instead of a boot error a deploy away. The runtime throw in collectProviders stays as the JavaScript backstop, so ProviderMeta keeps the field and the decorator still records it through a cast — a JS caller reaches the error naming the provider and its replacement rather than having the option silently ignored. The `as any` in the boot-throw test is now genuinely load-bearing, since no TypeScript caller can write that decorator at all, and says so. test/metadata.test.ts asserted the flag was stored for a provider. That half described the option working, which is the message being removed, and the boot throw it fed is already covered end to end in test/mesh/failure.test.ts. The default is a different claim and stays: collectProviders throws on anything truthy, so every plain provider recording `false` is what keeps ordinary apps booting. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+20
-10
No files matched your search
+11
-7
@@ -15,6 +15,12 @@ export interface ProviderMeta {
|
|||||||
provides: string;
|
provides: string;
|
||||||
needs: string[];
|
needs: string[];
|
||||||
optional: boolean;
|
optional: boolean;
|
||||||
|
/**
|
||||||
|
* Always refused. `@Provider`'s options no longer declare `export`, so TypeScript rejects it at
|
||||||
|
* the decorator and this can only be true for a JavaScript caller. It is still recorded so that
|
||||||
|
* caller reaches `collectProviders`' boot error, which names the provider and the replacement,
|
||||||
|
* rather than having the option silently ignored.
|
||||||
|
*/
|
||||||
export: boolean;
|
export: boolean;
|
||||||
}
|
}
|
||||||
/** Metadata attached by `@Step`: what it provides, what it needs, and its visibility. */
|
/** Metadata attached by `@Step`: what it provides, what it needs, and its visibility. */
|
||||||
@@ -58,18 +64,16 @@ const K = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** Marks a class as a provider: a lazily-built dependency addressable by its `provides` key. */
|
/** Marks a class as a provider: a lazily-built dependency addressable by its `provides` key. */
|
||||||
export function Provider(opts: {
|
export function Provider(opts: { provides: string; needs?: string[]; optional?: boolean }): ClassDecorator {
|
||||||
provides: string;
|
|
||||||
needs?: string[];
|
|
||||||
optional?: boolean;
|
|
||||||
export?: boolean;
|
|
||||||
}): ClassDecorator {
|
|
||||||
return (target) => {
|
return (target) => {
|
||||||
const meta: ProviderMeta = {
|
const meta: ProviderMeta = {
|
||||||
provides: opts.provides,
|
provides: opts.provides,
|
||||||
needs: opts.needs ?? [],
|
needs: opts.needs ?? [],
|
||||||
optional: opts.optional ?? false,
|
optional: opts.optional ?? false,
|
||||||
export: opts.export ?? false,
|
// Read through a cast because the options type deliberately does not offer `export`: only a
|
||||||
|
// `@Step` or a route is exportable, and the option's only behaviour was to throw at boot.
|
||||||
|
// A JavaScript caller can still pass it, and recording it is what routes them to that error.
|
||||||
|
export: (opts as { export?: boolean }).export ?? false,
|
||||||
};
|
};
|
||||||
Reflect.defineMetadata(K.provider, meta, target);
|
Reflect.defineMetadata(K.provider, meta, target);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -124,6 +124,9 @@ describe('mesh link failure', () => {
|
|||||||
|
|
||||||
describe('provider export', () => {
|
describe('provider export', () => {
|
||||||
it('refuses to boot when a provider is exported over the mesh', async () => {
|
it('refuses to boot when a provider is exported over the mesh', async () => {
|
||||||
|
// `as any` is load-bearing, not laziness: `export` is not in `@Provider`'s options type, so a
|
||||||
|
// TypeScript caller cannot write this at all. The cast is how the test reaches the runtime
|
||||||
|
// backstop that a JavaScript caller would hit.
|
||||||
@Provider({ provides: 'db', needs: [], export: true } as any)
|
@Provider({ provides: 'db', needs: [], export: true } as any)
|
||||||
class Db {
|
class Db {
|
||||||
provide() {
|
provide() {
|
||||||
|
|||||||
@@ -67,12 +67,15 @@ describe('HEAD and OPTIONS decorators', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('mesh export flags', () => {
|
describe('mesh export flags', () => {
|
||||||
it('providers/steps carry export (default false)', () => {
|
it('steps carry export, and a provider defaults to not exported', () => {
|
||||||
@Provider({ provides: 'config', export: true }) class C { provide() { return {}; } }
|
|
||||||
@Step({ provides: 'auth', export: true }) class A { run() { return {}; } }
|
@Step({ provides: 'auth', export: true }) class A { run() { return {}; } }
|
||||||
@Provider({ provides: 'priv' }) class P { provide() { return {}; } }
|
@Provider({ provides: 'priv' }) class P { provide() { return {}; } }
|
||||||
expect(getProviderMeta(C)!.export).toBe(true);
|
|
||||||
expect(getStepMeta(A)!.export).toBe(true);
|
expect(getStepMeta(A)!.export).toBe(true);
|
||||||
|
// No `export: true` case for a provider: the option is gone from `@Provider`'s type, and its
|
||||||
|
// only behaviour is the boot throw that `test/mesh/failure.test.ts` covers end to end —
|
||||||
|
// asserting it is *stored* here would assert the option works. The default is a different
|
||||||
|
// claim and still load-bearing: `collectProviders` throws on anything truthy, so every plain
|
||||||
|
// provider recording `false` is what keeps ordinary apps booting.
|
||||||
expect(getProviderMeta(P)!.export).toBe(false);
|
expect(getProviderMeta(P)!.export).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user