chore(types): name the decorator shapes so JSR sees no slow types
deno publish --dry-run reported 17 slow types across metadata.ts and params.ts: two factories with inferred return types, and the fifteen exported decorators built from them. Naming RouteDecorator and ArgDecorator fixes all seventeen, since annotating each factory resolves its constants with it. Fixed rather than waived with --allow-slow-types. That flag degrades the generated declarations and makes type checking 1.5-2x slower for consumers, which is a strange thing to ship in the framework whose central claim is type safety. The two names are public API and worth having on their own: a caller can now give a decorator of either shape a name. The factories also lost their inline parameter types, which the declared return type now supplies. deno.json gains the JSR manifest. Exports mirror npm's four entry points on purpose — a docs example importing @green-tea/core/bun has to work whichever registry the reader installed from. Dry run is clean with no flags; claiming the @green-tea scope on jsr.io is the remaining step. Refs: Green-Tea/core#19 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+88
-52
No files matched your search
@@ -1,4 +1,19 @@
|
||||
{
|
||||
"name": "@green-tea/core",
|
||||
"version": "26.8.0-beta.0",
|
||||
"license": "MIT",
|
||||
// JSR serves TypeScript source, not the tsup build, so these point at `src/` where npm's
|
||||
// `exports` point at `dist/`. Same four entry points on purpose: a docs example that imports
|
||||
// `@green-tea/core/bun` has to work whichever registry the reader installed from.
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./deno": "./src/deno.ts",
|
||||
"./bun": "./src/bun.ts",
|
||||
"./edge": "./src/edge.ts"
|
||||
},
|
||||
"publish": {
|
||||
"include": ["src", "README.md", "CHANGELOG.md", "LICENSE", "deno.json"]
|
||||
},
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
|
||||
@@ -55,7 +55,8 @@
|
||||
"hyperid": {
|
||||
"uuid": "11.1.1"
|
||||
}
|
||||
}
|
||||
},
|
||||
"undici": "7.29.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -40,7 +40,7 @@ export {
|
||||
Options,
|
||||
Html,
|
||||
} from './metadata';
|
||||
export type { Transport, HttpMethod } from './metadata';
|
||||
export type { Transport, HttpMethod, RouteDecorator } from './metadata';
|
||||
export type { Plugin } from './plugin';
|
||||
|
||||
// Transformers & error signals
|
||||
@@ -63,7 +63,7 @@ export type { StepFn, CompiledFlow } from './flow';
|
||||
|
||||
// Parameter decorators
|
||||
export { needs, ctx, param, query, body, headers, header, inbound, abort } from './params';
|
||||
export type { Query, Headers, Context, ArgSpec } from './params';
|
||||
export type { Query, Headers, Context, ArgSpec, ArgDecorator } from './params';
|
||||
|
||||
// Streaming
|
||||
export { channel, isAsyncIterable } from './channel';
|
||||
|
||||
+39
-30
@@ -100,48 +100,57 @@ export function Route(prefix: string): ClassDecorator {
|
||||
};
|
||||
}
|
||||
|
||||
function routeDecorator(method: HttpMethod, transport: Transport) {
|
||||
return (
|
||||
path: string,
|
||||
opts?: { export?: boolean; duplicates?: 'array' | 'last'; maxBodyBytes?: number; maxParts?: number },
|
||||
): MethodDecorator =>
|
||||
(target, propertyKey) => {
|
||||
const ctor = target.constructor;
|
||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
||||
routes.push({
|
||||
method,
|
||||
path,
|
||||
handlerName: String(propertyKey),
|
||||
transport,
|
||||
export: opts?.export ?? false,
|
||||
duplicates: opts?.duplicates,
|
||||
maxBodyBytes: opts?.maxBodyBytes,
|
||||
maxParts: opts?.maxParts,
|
||||
});
|
||||
Reflect.defineMetadata(K.routes, routes, ctor);
|
||||
};
|
||||
/**
|
||||
* The shape every route decorator shares: a path, optional per-route overrides, and the
|
||||
* `MethodDecorator` that records them against the controller.
|
||||
*
|
||||
* Named rather than inferred because JSR rejects an inferred type on a public symbol — an inferred
|
||||
* one forces every consumer's type checker to re-derive it from the implementation instead of
|
||||
* reading it from the declaration. It also gives callers a name for the shape.
|
||||
*/
|
||||
export type RouteDecorator = (
|
||||
path: string,
|
||||
opts?: { export?: boolean; duplicates?: 'array' | 'last'; maxBodyBytes?: number; maxParts?: number },
|
||||
) => MethodDecorator;
|
||||
|
||||
function routeDecorator(method: HttpMethod, transport: Transport): RouteDecorator {
|
||||
return (path, opts) => (target, propertyKey) => {
|
||||
const ctor = target.constructor;
|
||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
||||
routes.push({
|
||||
method,
|
||||
path,
|
||||
handlerName: String(propertyKey),
|
||||
transport,
|
||||
export: opts?.export ?? false,
|
||||
duplicates: opts?.duplicates,
|
||||
maxBodyBytes: opts?.maxBodyBytes,
|
||||
maxParts: opts?.maxParts,
|
||||
});
|
||||
Reflect.defineMetadata(K.routes, routes, ctor);
|
||||
};
|
||||
}
|
||||
|
||||
/** Declares a buffered `GET` route. */
|
||||
export const Get = routeDecorator('GET', 'buffer');
|
||||
export const Get: RouteDecorator = routeDecorator('GET', 'buffer');
|
||||
/** Declares a buffered `HEAD` route. */
|
||||
export const Head = routeDecorator('HEAD', 'buffer');
|
||||
export const Head: RouteDecorator = routeDecorator('HEAD', 'buffer');
|
||||
/** Declares a buffered `POST` route. */
|
||||
export const Post = routeDecorator('POST', 'buffer');
|
||||
export const Post: RouteDecorator = routeDecorator('POST', 'buffer');
|
||||
/** Declares a buffered `PUT` route. */
|
||||
export const Put = routeDecorator('PUT', 'buffer');
|
||||
export const Put: RouteDecorator = routeDecorator('PUT', 'buffer');
|
||||
/** Declares a buffered `PATCH` route. */
|
||||
export const Patch = routeDecorator('PATCH', 'buffer');
|
||||
export const Patch: RouteDecorator = routeDecorator('PATCH', 'buffer');
|
||||
/** Declares a buffered `DELETE` route. */
|
||||
export const Delete = routeDecorator('DELETE', 'buffer');
|
||||
export const Delete: RouteDecorator = routeDecorator('DELETE', 'buffer');
|
||||
/** Declares a buffered `OPTIONS` route. */
|
||||
export const Options = routeDecorator('OPTIONS', 'buffer');
|
||||
export const Options: RouteDecorator = routeDecorator('OPTIONS', 'buffer');
|
||||
/** Declares a `GET` route streamed as Server-Sent Events. */
|
||||
export const Sse = routeDecorator('GET', 'sse');
|
||||
export const Sse: RouteDecorator = routeDecorator('GET', 'sse');
|
||||
/** Declares a `GET` route whose transport is content-negotiated at request time. */
|
||||
export const Stream = routeDecorator('GET', 'negotiate');
|
||||
export const Stream: RouteDecorator = routeDecorator('GET', 'negotiate');
|
||||
/** Declares a `GET` route upgraded to a WebSocket. */
|
||||
export const Ws = routeDecorator('GET', 'ws');
|
||||
export const Ws: RouteDecorator = routeDecorator('GET', 'ws');
|
||||
|
||||
/** Attaches a response transformer to a handler method. */
|
||||
export function Transformer(fn: TransformerFn): MethodDecorator {
|
||||
|
||||
+30
-19
@@ -32,34 +32,45 @@ function record(target: object, methodKey: string | symbol | undefined, spec: Ar
|
||||
Reflect.defineMetadata(ARGS, map, ctor);
|
||||
}
|
||||
|
||||
function envelope(source: ArgSource) {
|
||||
return (arg?: string | string[] | StandardSchemaV1, schema?: StandardSchemaV1): ParameterDecorator =>
|
||||
(target, methodKey, index) => {
|
||||
const spec: ArgSpec = { index, source };
|
||||
if (Array.isArray(arg)) spec.keys = arg;
|
||||
else if (typeof arg === 'string') spec.key = arg;
|
||||
else if (isStandardSchema(arg)) spec.schema = arg;
|
||||
else if (arg !== undefined) throw new Error(`@${source}: expected a Standard Schema or a string/array key`);
|
||||
/**
|
||||
* The shape every argument decorator shares: an optional key, key list or schema, an optional
|
||||
* second schema, and the `ParameterDecorator` that records the resulting spec.
|
||||
*
|
||||
* Named for the same reason as `RouteDecorator` in `./metadata` — JSR rejects an inferred type on a
|
||||
* public symbol, because consumers would have to re-derive it from the implementation on every check.
|
||||
*/
|
||||
export type ArgDecorator = (
|
||||
arg?: string | string[] | StandardSchemaV1,
|
||||
schema?: StandardSchemaV1,
|
||||
) => ParameterDecorator;
|
||||
|
||||
if (schema !== undefined) {
|
||||
if (!isStandardSchema(schema)) throw new Error(`@${source}: second argument must be a Standard Schema`);
|
||||
spec.schema = schema;
|
||||
}
|
||||
function envelope(source: ArgSource): ArgDecorator {
|
||||
return (arg, schema) => (target, methodKey, index) => {
|
||||
const spec: ArgSpec = { index, source };
|
||||
if (Array.isArray(arg)) spec.keys = arg;
|
||||
else if (typeof arg === 'string') spec.key = arg;
|
||||
else if (isStandardSchema(arg)) spec.schema = arg;
|
||||
else if (arg !== undefined) throw new Error(`@${source}: expected a Standard Schema or a string/array key`);
|
||||
|
||||
record(target, methodKey, spec);
|
||||
};
|
||||
if (schema !== undefined) {
|
||||
if (!isStandardSchema(schema)) throw new Error(`@${source}: second argument must be a Standard Schema`);
|
||||
spec.schema = schema;
|
||||
}
|
||||
|
||||
record(target, methodKey, spec);
|
||||
};
|
||||
}
|
||||
|
||||
/** Inject a route path parameter; pass a key to pick one, or a schema/array to shape it. */
|
||||
export const param = envelope('params');
|
||||
export const param: ArgDecorator = envelope('params');
|
||||
/** Inject a query-string value; pass a key to pick one, or a schema/array to shape it. */
|
||||
export const query = envelope('query');
|
||||
export const query: ArgDecorator = envelope('query');
|
||||
/** Inject the request body; pass a schema to validate it. */
|
||||
export const body = envelope('body');
|
||||
export const body: ArgDecorator = envelope('body');
|
||||
/** Inject request headers; pass a key to pick one, an array to pick several, or no arg for the whole bag. */
|
||||
export const headers = envelope('headers');
|
||||
export const headers: ArgDecorator = envelope('headers');
|
||||
/** Inject a single request header by name (singular alias of {@link headers}); accepts a schema to validate it. */
|
||||
export const header = envelope('headers');
|
||||
export const header: ArgDecorator = envelope('headers');
|
||||
|
||||
/** Inject a value produced upstream by a step or provider, addressed by key. */
|
||||
export function needs(key: string): ParameterDecorator {
|
||||
|
||||
Reference in new issue
Block a user