UNPKG

jsonv-ts

Version:

JSON Schema builder and validator for TypeScript with static type inference, Hono middleware for OpenAPI generation and validation, and MCP server/client implementation. Lightweight, dependency-free, and built on Web Standards.

88 lines (87 loc) 3.44 kB
import type { MaybePromise, ResourceCompletionResult, ResourceCompletionResultLike } from "./utils"; type ExtractParams<T> = T extends `${infer _Start}{${infer Param}}${infer Rest}` ? { [K in Param | keyof ExtractParams<Rest>]: string; } : {}; export type TResourceUri = `${string}://${string}`; export declare function extractParamValues(template: string, actual: string): Record<string, string>; export declare function matchPath(template: string, actual: string): boolean; export type ResourceConfig = { mimeType?: string; title?: string; description?: string; size?: number; list?: MaybePromise<ResourceCompletionResultLike>; complete?: { [key: string]: (value: string, context: any) => MaybePromise<ResourceCompletionResultLike>; }; _meta?: { [key: string]: unknown; }; }; export type ResourceHandlerCtx<Context extends object = object> = { text: (text: string, opts?: ResourceResponse) => ResourceResponse; json: (json: object, opts?: ResourceResponse) => ResourceResponse; binary: (binary: Uint8Array, opts?: ResourceResponse) => ResourceResponse; context: Context; uri: TResourceUri; raw?: unknown; }; export type ResourceHandler<Uri extends TResourceUri, Context extends object = {}> = (ctx: ResourceHandlerCtx<Context>, params: Uri extends TResourceUri ? ExtractParams<Uri> : never) => MaybePromise<ResourceResponse>; export type ResourceResponse = { mimeType?: string; title?: string; description?: string; } & ({ text?: string; } | { blob?: string; }); export type ResourceJson = ReturnType<Resource["toJSON"]>; export declare class Resource<Name extends string = string, Uri extends TResourceUri = TResourceUri, Context extends object = {}, Params = Uri extends TResourceUri ? ExtractParams<Uri> : never> { readonly name: Name; readonly uri: Uri; readonly handler: (ctx: ResourceHandlerCtx<Context>, params: Params) => MaybePromise<ResourceResponse>; readonly options: ResourceConfig; constructor(name: Name, uri: Uri, handler: (ctx: ResourceHandlerCtx<Context>, params: Params) => MaybePromise<ResourceResponse>, options?: ResourceConfig); isDynamic(): boolean; matches(uri: Uri): boolean; suggest(name: string, value: string, context: object): Promise<ResourceCompletionResult>; call(uri: TResourceUri, context: Context, raw?: unknown): Promise<ResourceResponse>; toJSONContent(context: Context, uri?: TResourceUri, request?: Request): Promise<{ mimeType: string; title?: string; description?: string; text?: string; uri: `${string}://${string}`; name: string; size: number | undefined; _meta: { [key: string]: unknown; } | undefined; } | { mimeType: string; title?: string; description?: string; blob?: string; uri: `${string}://${string}`; name: string; size: number | undefined; _meta: { [key: string]: unknown; } | undefined; }>; toJSON(): { [x: string]: string | number | { [key: string]: unknown; } | undefined; name: Name; title: string | undefined; description: string | undefined; mimeType: string | undefined; size: number | undefined; _meta: { [key: string]: unknown; } | undefined; }; } export {};