@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
200 lines • 5.97 kB
TypeScript
/**
* Detect the current edge runtime platform.
*/
export declare function detectRuntime(): RuntimeInfo;
/**
* Check if running in an edge environment.
*/
export declare function isEdgeEnvironment(): boolean;
/**
* Check if running in a server environment (including edge).
*/
export declare function isServerEnvironment(): boolean;
/**
* Create a platform-agnostic environment accessor.
*/
export declare function createEnvAccessor(platformEnv?: Record<string, unknown>): EnvAccessor;
/**
* Create a platform-agnostic KV namespace.
*/
export declare function createKVNamespace(platformKV?: unknown): KVNamespace | undefined;
/**
* Create a platform-agnostic cache.
*/
export declare function createEdgeCache(): EdgeCache | undefined;
/**
* Create an edge handler.
*/
export declare function createEdgeHandler(config: EdgeHandlerConfig): void;
/**
* Create a streaming response for edge platforms.
*/
export declare function createStreamingResponse(stream: ReadableStream<Uint8Array>, options?: ResponseInit): Response;
/**
* Create a text encoder stream.
*/
export declare function createTextEncoderStream(): TransformStream<string, Uint8Array>;
/**
* Pipe strings to a streaming response.
*/
export declare function stringToStream(strings: AsyncIterable<string>): ReadableStream<Uint8Array>;
/**
* Create a Cloudflare Workers handler.
*/
export declare function createCloudflareHandler(config: EdgeHandlerConfig): {
fetch: (request: Request, env: Record<string, unknown>, ctx: { waitUntil: (p: Promise<unknown>) => void }) => Promise<Response>
};
/**
* Create a Deno Deploy handler.
*/
export declare function createDenoHandler(config: EdgeHandlerConfig): (request: Request) => Promise<Response>;
/**
* Create a Vercel Edge handler.
*/
export declare function createVercelHandler(config: EdgeHandlerConfig): (request: Request) => Promise<Response>;
/**
* Create a Netlify Edge handler.
*/
export declare function createNetlifyHandler(config: EdgeHandlerConfig): (request: Request, context: { geo?: GeoInfo }) => Promise<Response>;
/**
* Parse cookies from request.
*/
export declare function parseCookies(request: Request): Record<string, string>;
/**
* Create a Set-Cookie header value.
*/
export declare function createCookie(name: string, value: string, options?: {
maxAge?: number
expires?: Date
path?: string
domain?: string
secure?: boolean
httpOnly?: boolean
sameSite?: 'Strict' | 'Lax' | 'None'
}): string;
/**
* JSON response helper.
*/
export declare function jsonResponse(data: unknown, init?: ResponseInit): Response;
/**
* Redirect response helper.
*/
export declare function redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
/**
* Not found response helper.
*/
export declare function notFound(message?: any): Response;
/** Edge runtime detection result */
export declare interface RuntimeInfo {
platform: EdgePlatform
version?: string
supportsStreaming: boolean
supportsCrypto: boolean
supportsKV: boolean
supportsCache: boolean
capabilities: string[]
}
/** Edge handler configuration */
export declare interface EdgeHandlerConfig {
render: (request: Request, context: EdgeContext) => Promise<Response | string>
onError?: (error: Error, request: Request) => Response
middleware?: EdgeMiddleware[]
cache?: EdgeCacheConfig
cors?: CorsConfig
}
/** Edge context passed to handlers */
export declare interface EdgeContext {
runtime: RuntimeInfo
platform: Record<string, unknown>
requestId: string
geo?: GeoInfo
timing: {
start: number
getElapsed: () => number
}
kv?: KVNamespace
cache?: EdgeCache
env: EnvAccessor
}
/** Geographic information */
export declare interface GeoInfo {
city?: string
country?: string
countryCode?: string
region?: string
latitude?: number
longitude?: number
timezone?: string
}
/** Cache configuration */
export declare interface EdgeCacheConfig {
enabled: boolean
ttl?: number
keyGenerator?: (request: Request) => string
includePaths?: RegExp[]
excludePaths?: RegExp[]
staleWhileRevalidate?: number
}
/** CORS configuration */
export declare interface CorsConfig {
origins?: string[] | '*'
methods?: string[]
headers?: string[]
exposeHeaders?: string[]
credentials?: boolean
maxAge?: number
}
/** KV namespace interface (platform-agnostic) */
export declare interface KVNamespace {
get: (key: string) => Promise<string | null>
getWithMetadata: <T>(key: string) => Promise<{ value: string | null, metadata: T | null }>
put: (key: string, value: string, options?: KVPutOptions) => Promise<void>
delete: (key: string) => Promise<void>
list: (options?: KVListOptions) => Promise<KVListResult>
}
/** KV put options */
export declare interface KVPutOptions {
expiration?: number
expirationTtl?: number
metadata?: Record<string, unknown>
}
/** KV list options */
export declare interface KVListOptions {
prefix?: string
limit?: number
cursor?: string
}
/** KV list result */
export declare interface KVListResult {
keys: { name: string, expiration?: number, metadata?: Record<string, unknown> }[]
cursor?: string
list_complete: boolean
}
/** Edge cache interface */
export declare interface EdgeCache {
match: (request: Request) => Promise<Response | undefined>
put: (request: Request, response: Response) => Promise<void>
delete: (request: Request) => Promise<boolean>
}
/** Environment variable accessor */
export declare interface EnvAccessor {
get: (key: string) => string | undefined
getOrThrow: (key: string) => string
has: (key: string) => boolean
all: () => Record<string, string>
}
/** Supported edge runtime platforms */
export type EdgePlatform = | 'cloudflare'
| 'deno'
| 'vercel'
| 'netlify'
| 'fastly'
| 'node'
| 'bun'
| 'unknown'
/** Middleware function */
export type EdgeMiddleware = (
request: Request,
context: EdgeContext,
next: () => Promise<Response>
) => Promise<Response>