diff --git a/deno.json b/deno.json index 3a2fd5b..5af1930 100644 --- a/deno.json +++ b/deno.json @@ -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, diff --git a/deno.lock b/deno.lock index 4bb7fd1..efe1539 100644 --- a/deno.lock +++ b/deno.lock @@ -55,7 +55,8 @@ "hyperid": { "uuid": "11.1.1" } - } + }, + "undici": "7.29.0" } } } diff --git a/src/index.ts b/src/index.ts index eabc7a1..2d51c16 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/metadata.ts b/src/metadata.ts index 8a1f3ec..660225a 100644 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -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 { diff --git a/src/params.ts b/src/params.ts index 495bca9..696301a 100644 --- a/src/params.ts +++ b/src/params.ts @@ -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 {