UNPKG

@alepha/server-cache

Version:

Adds ETag and Cache-Control headers to server responses.

78 lines (77 loc) 2.85 kB
import * as _alepha_core1 from "@alepha/core"; import { Alepha } from "@alepha/core"; import * as _alepha_logger0 from "@alepha/logger"; import * as _alepha_cache0 from "@alepha/cache"; import { CacheDescriptorOptions } from "@alepha/cache"; import { DateTimeProvider, DurationLike } from "@alepha/datetime"; import { RequestConfigSchema, ServerRequest, ServerRoute } from "@alepha/server"; //#region src/providers/ServerCacheProvider.d.ts declare module "@alepha/server" { interface ServerRoute { cache?: ServerRouteCache; } interface ActionDescriptor<TConfig extends RequestConfigSchema> { invalidate: () => Promise<void>; } } declare class ServerCacheProvider { protected readonly log: _alepha_logger0.Logger; protected readonly alepha: Alepha; protected readonly time: DateTimeProvider; protected readonly cache: _alepha_cache0.CacheDescriptorFn<RouteCacheEntry, any[]>; generateETag(content: string): string; invalidate(route: ServerRoute): Promise<void>; protected readonly onActionRequest: _alepha_core1.HookDescriptor<"action:onRequest">; protected readonly onActionResponse: _alepha_core1.HookDescriptor<"action:onResponse">; protected readonly onRequest: _alepha_core1.HookDescriptor<"server:onRequest">; protected readonly onResponse: _alepha_core1.HookDescriptor<"server:onResponse">; protected getCacheOptions(cache: ServerRouteCache): { provider?: (_alepha_core1.InstantiableClass<_alepha_cache0.CacheProvider> | "memory") | undefined; name?: string | undefined; ttl?: DurationLike | undefined; disabled?: boolean | undefined; }; protected createCacheKey(route: ServerRoute, config?: ServerRequest): string; } type ServerRouteCache = boolean | DurationLike | Omit<CacheDescriptorOptions<any>, "handler" | "key">; interface RouteCacheEntry { contentType?: string; body: any; status?: number; lastModified: string; hash: string; } //#endregion //#region src/index.d.ts /** * Plugin for Alepha Server that provides server-side caching capabilities. * It uses the Alepha Cache module to cache responses from server actions ($action). * It also provides a ETag-based cache invalidation mechanism. * * @example * ```ts * import { Alepha } from "alepha"; * import { $action } from "alepha/server"; * import { AlephaServerCache } from "alepha/server/cache"; * * class ApiServer { * hello = $action({ * cache: true, * handler: () => "Hello, World!", * }); * } * * const alepha = Alepha.create() * .with(AlephaServerCache) * .with(ApiServer); * * run(alepha); * ``` * * @see {@link ServerCacheProvider} * @module alepha.server.cache */ declare const AlephaServerCache: _alepha_core1.Service<_alepha_core1.Module>; //#endregion export { AlephaServerCache, ServerCacheProvider, ServerRouteCache }; //# sourceMappingURL=index.d.cts.map