Compare commits
1
Commits
855e044d2a
...
8b659d827f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b659d827f |
No files matched your search
+1
-1
@@ -41,7 +41,7 @@ import type { App, InspectLine, Explain, RoutePlan, MeshConfig } from './types';
|
|||||||
import { inspectRoute, buildGraphView, explainRoute } from './introspect';
|
import { inspectRoute, buildGraphView, explainRoute } from './introspect';
|
||||||
import { compilePattern } from '../http/router';
|
import { compilePattern } from '../http/router';
|
||||||
|
|
||||||
export type { App, InspectLine, ExplainNode, Explain, MeshConfig } from './types';
|
export type { App, InspectLine, ExplainNode, Explain, MeshConfig, Hooks } from './types';
|
||||||
|
|
||||||
type Runner = (ctx: any) => any;
|
type Runner = (ctx: any) => any;
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -40,8 +40,10 @@ export {
|
|||||||
Options,
|
Options,
|
||||||
Html,
|
Html,
|
||||||
} from './metadata';
|
} from './metadata';
|
||||||
export type { Transport, HttpMethod, RouteDecorator } from './metadata';
|
export type { Transport, HttpMethod, RouteDecorator, TransformerFn } from './metadata';
|
||||||
export type { Plugin } from './plugin';
|
export type { Plugin, PluginApi, ScopeApi, ScopeNode } from './plugin';
|
||||||
|
export type { Hooks } from './app';
|
||||||
|
export type { TeardownFn } from './lifecycle';
|
||||||
|
|
||||||
// Transformers & error signals
|
// Transformers & error signals
|
||||||
export { JsonTransformer } from './transformers';
|
export { JsonTransformer } from './transformers';
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
// Every extension point the docs tell a user to write must be *typeable* from the package, not just
|
||||||
|
// callable. A decorator whose argument type is not exported can be passed a literal and nothing
|
||||||
|
// else — the shape has to be redeclared inline, or borrowed off a value with
|
||||||
|
// `typeof JsonTransformer`, which works and reads like a trick because it is one.
|
||||||
|
//
|
||||||
|
// This file's job is to fail to compile if one of them stops being exported.
|
||||||
|
import { test, expect } from 'vitest';
|
||||||
|
import {
|
||||||
|
JsonTransformer,
|
||||||
|
type TransformerFn,
|
||||||
|
type Plugin,
|
||||||
|
type PluginApi,
|
||||||
|
type ScopeApi,
|
||||||
|
type ScopeNode,
|
||||||
|
type Hooks,
|
||||||
|
type TeardownFn,
|
||||||
|
type ErrorRenderer,
|
||||||
|
type StreamEncoder,
|
||||||
|
} from '../../src';
|
||||||
|
|
||||||
|
// @Transformer(fn) — the case that started this. Prometheus scrapes text, so a /metrics route
|
||||||
|
// needs its own transformer, and this is the annotation that was impossible to write.
|
||||||
|
const prometheusText: TransformerFn = (value) => ({
|
||||||
|
status: 200,
|
||||||
|
headers: { 'content-type': 'text/plain; version=0.0.4; charset=utf-8' },
|
||||||
|
body: String(value),
|
||||||
|
});
|
||||||
|
|
||||||
|
// A plugin split into named functions rather than one inline arrow: `Plugin` alone types the
|
||||||
|
// arrow, and nothing typed its argument.
|
||||||
|
function subscribe(api: PluginApi): void {
|
||||||
|
api.bus.on('request:end', () => {});
|
||||||
|
api.onShutdown(flush);
|
||||||
|
}
|
||||||
|
|
||||||
|
const flush: TeardownFn = () => {};
|
||||||
|
|
||||||
|
function contribute(scope: ScopeApi): void {
|
||||||
|
const node: ScopeNode = { kind: 'provider', name: 'metrics', needs: [], provides: ['metrics'], run: () => ({}) };
|
||||||
|
scope.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
const metricsPlugin: Plugin = (api) => {
|
||||||
|
subscribe(api);
|
||||||
|
contribute(api.scope);
|
||||||
|
};
|
||||||
|
|
||||||
|
// createApp({ hooks }) — declared as a value, which is the point of the option.
|
||||||
|
const hooks: Hooks[] = [{ onShutdown: flush }];
|
||||||
|
|
||||||
|
// The two neighbouring extension points, already exported. Here so a future barrel edit that drops
|
||||||
|
// one is caught by the same test rather than by a user.
|
||||||
|
const renderError: ErrorRenderer = () => ({ status: 500, headers: {}, body: 'boom' });
|
||||||
|
const encoder: StreamEncoder = { headers: {}, encode: (item) => String(item) };
|
||||||
|
|
||||||
|
test('every extension point is typeable from the package barrel', () => {
|
||||||
|
expect(prometheusText('x').body).toBe('x');
|
||||||
|
expect(typeof metricsPlugin).toBe('function');
|
||||||
|
expect(hooks).toHaveLength(1);
|
||||||
|
expect(renderError(new Error('boom'), {} as never).status).toBe(500);
|
||||||
|
expect(encoder.encode(1)).toBe('1');
|
||||||
|
// The workaround the missing export forced, kept as a reminder of what it replaced.
|
||||||
|
expect(typeof JsonTransformer).toBe('function');
|
||||||
|
});
|
||||||
Reference in new issue
Block a user