@spikard/node 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,400 @@
1
+ interface StreamingResponseInit {
2
+ statusCode?: number;
3
+ headers?: Record<string, string>;
4
+ }
5
+ declare const STREAM_HANDLE_PROP: "__spikard_stream_handle";
6
+ type StreamChunk = JsonValue | string | Buffer | Uint8Array | ArrayBuffer | ArrayBufferView | null | undefined;
7
+ type ChunkIterator = AsyncIterator<StreamChunk> & AsyncIterable<StreamChunk>;
8
+ type StreamingHandle = {
9
+ kind: "native";
10
+ handle: number;
11
+ init: StreamingResponseInit;
12
+ } | {
13
+ kind: "js";
14
+ iterator: ChunkIterator;
15
+ init: StreamingResponseInit;
16
+ };
17
+ declare class StreamingResponse {
18
+ readonly [STREAM_HANDLE_PROP]: StreamingHandle;
19
+ constructor(stream: AsyncIterable<StreamChunk> | AsyncIterator<StreamChunk>, init?: StreamingResponseInit);
20
+ }
21
+
22
+ type JsonPrimitive = string | number | boolean | null;
23
+ type JsonValue = JsonPrimitive | JsonValue[] | {
24
+ [Key in string]: JsonValue;
25
+ };
26
+ type JsonRecord = Record<string, JsonValue>;
27
+ type MaybePromise<T> = T | Promise<T>;
28
+ interface Base64EncodedBody {
29
+ __spikard_base64__: string;
30
+ }
31
+ type HandlerBody = JsonValue | Base64EncodedBody | null;
32
+ interface StructuredHandlerResponse {
33
+ status?: number;
34
+ statusCode?: number;
35
+ headers?: Record<string, string>;
36
+ body?: HandlerBody;
37
+ }
38
+ type HandlerResult = StructuredHandlerResponse | JsonValue | StreamingResponse | undefined;
39
+ type HandlerFunction<TReturn extends HandlerResult = HandlerResult> = (request: Request) => MaybePromise<TReturn>;
40
+ type NativeHandlerFunction<TReturn extends HandlerResult = HandlerResult> = (requestJson: string) => MaybePromise<TReturn | string>;
41
+ type WebSocketHandler = (message: unknown) => MaybePromise<unknown>;
42
+ interface WebSocketOptions {
43
+ onConnect?: () => MaybePromise<void>;
44
+ onDisconnect?: () => MaybePromise<void>;
45
+ messageSchema?: unknown;
46
+ responseSchema?: unknown;
47
+ handlerName?: string;
48
+ }
49
+
50
+ interface Request {
51
+ method: string;
52
+ path: string;
53
+ params: Record<string, string>;
54
+ pathParams: Record<string, string>;
55
+ query: Record<string, string>;
56
+ queryParams: Record<string, string>;
57
+ headers: Record<string, string>;
58
+ cookies: Record<string, string>;
59
+ body: Buffer | null;
60
+ dependencies: Record<string, unknown> | undefined;
61
+ json<T = JsonValue>(): T;
62
+ form(): Record<string, string>;
63
+ }
64
+
65
+ interface CompressionConfig {
66
+ gzip?: boolean;
67
+ brotli?: boolean;
68
+ minSize?: number;
69
+ quality?: number;
70
+ }
71
+ interface RateLimitConfig {
72
+ perSecond: number;
73
+ burst: number;
74
+ ipBased?: boolean;
75
+ }
76
+ interface JwtConfig {
77
+ secret: string;
78
+ algorithm?: string;
79
+ audience?: string[];
80
+ issuer?: string;
81
+ leeway?: number;
82
+ }
83
+ interface ApiKeyConfig {
84
+ keys: string[];
85
+ headerName?: string;
86
+ }
87
+ interface ContactInfo {
88
+ name?: string;
89
+ email?: string;
90
+ url?: string;
91
+ }
92
+ interface LicenseInfo {
93
+ name: string;
94
+ url?: string;
95
+ }
96
+ interface ServerInfo {
97
+ url: string;
98
+ description?: string;
99
+ }
100
+ interface SecuritySchemeInfo {
101
+ type: "http" | "apiKey";
102
+ scheme?: string;
103
+ bearerFormat?: string;
104
+ location?: "header" | "query" | "cookie";
105
+ name?: string;
106
+ }
107
+ interface OpenApiConfig {
108
+ enabled?: boolean;
109
+ title?: string;
110
+ version?: string;
111
+ description?: string;
112
+ swaggerUiPath?: string;
113
+ redocPath?: string;
114
+ openapiJsonPath?: string;
115
+ contact?: ContactInfo;
116
+ license?: LicenseInfo;
117
+ servers?: ServerInfo[];
118
+ securitySchemes?: Record<string, SecuritySchemeInfo>;
119
+ }
120
+ interface StaticFilesConfig {
121
+ directory: string;
122
+ routePrefix: string;
123
+ indexFile?: boolean;
124
+ cacheControl?: string;
125
+ }
126
+ interface ServerConfig {
127
+ host?: string;
128
+ port?: number;
129
+ workers?: number;
130
+ enableRequestId?: boolean;
131
+ maxBodySize?: number | null;
132
+ requestTimeout?: number | null;
133
+ compression?: CompressionConfig | null;
134
+ rateLimit?: RateLimitConfig | null;
135
+ jwtAuth?: JwtConfig | null;
136
+ apiKeyAuth?: ApiKeyConfig | null;
137
+ staticFiles?: StaticFilesConfig[];
138
+ gracefulShutdown?: boolean;
139
+ shutdownTimeout?: number;
140
+ openapi?: OpenApiConfig | null;
141
+ }
142
+
143
+ interface ServerOptions {
144
+ host?: string;
145
+ port?: number;
146
+ }
147
+ declare function runServer(app: SpikardApp, config?: ServerConfig | ServerOptions): void;
148
+
149
+ type DependencyValue = unknown;
150
+ type DependencyFactory = (dependencies: Record<string, DependencyValue>) => MaybePromise<DependencyValue>;
151
+ interface DependencyOptions {
152
+ dependsOn?: string[];
153
+ singleton?: boolean;
154
+ cacheable?: boolean;
155
+ }
156
+ interface DependencyDescriptor {
157
+ isFactory: boolean;
158
+ value?: DependencyValue | undefined;
159
+ factory?: DependencyFactory | undefined;
160
+ dependsOn: string[];
161
+ singleton: boolean;
162
+ cacheable: boolean;
163
+ }
164
+ type LifecycleHookPayload = Request | StructuredHandlerResponse;
165
+ type LifecycleHookFunction = (payload: LifecycleHookPayload) => MaybePromise<LifecycleHookPayload>;
166
+ interface LifecycleHooks {
167
+ onRequest: LifecycleHookFunction[];
168
+ preValidation: LifecycleHookFunction[];
169
+ preHandler: LifecycleHookFunction[];
170
+ onResponse: LifecycleHookFunction[];
171
+ onError: LifecycleHookFunction[];
172
+ }
173
+ declare class Spikard implements SpikardApp {
174
+ routes: RouteMetadata[];
175
+ handlers: Record<string, HandlerFunction | NativeHandlerFunction>;
176
+ websocketRoutes: RouteMetadata[];
177
+ websocketHandlers: Record<string, Record<string, unknown>>;
178
+ lifecycleHooks: LifecycleHooks;
179
+ dependencies: Record<string, DependencyDescriptor>;
180
+ addRoute(metadata: RouteMetadata, handler: HandlerFunction | NativeHandlerFunction): void;
181
+ websocket(path: string, handler: WebSocketHandler, options?: WebSocketOptions): void;
182
+ run(options?: ServerOptions): void;
183
+ onRequest(hook: LifecycleHookFunction): LifecycleHookFunction;
184
+ preValidation(hook: LifecycleHookFunction): LifecycleHookFunction;
185
+ preHandler(hook: LifecycleHookFunction): LifecycleHookFunction;
186
+ onResponse(hook: LifecycleHookFunction): LifecycleHookFunction;
187
+ onError(hook: LifecycleHookFunction): LifecycleHookFunction;
188
+ provide(key: string, valueOrFactory: DependencyValue | DependencyFactory, options?: DependencyOptions): this;
189
+ getLifecycleHooks(): LifecycleHooks;
190
+ }
191
+
192
+ declare function run(work: () => void | Promise<void>): void;
193
+
194
+ declare const background_run: typeof run;
195
+ declare namespace background {
196
+ export { background_run as run };
197
+ }
198
+
199
+ type GrpcMetadata = Record<string, string>;
200
+ interface GrpcRequest {
201
+ serviceName: string;
202
+ methodName: string;
203
+ payload: Buffer;
204
+ metadata: GrpcMetadata;
205
+ }
206
+ interface GrpcResponse {
207
+ payload: Buffer;
208
+ metadata?: GrpcMetadata;
209
+ }
210
+ interface GrpcHandler {
211
+ handleRequest(request: GrpcRequest): Promise<GrpcResponse>;
212
+ }
213
+ declare enum GrpcStatusCode {
214
+ OK = 0,
215
+ CANCELLED = 1,
216
+ UNKNOWN = 2,
217
+ INVALID_ARGUMENT = 3,
218
+ DEADLINE_EXCEEDED = 4,
219
+ NOT_FOUND = 5,
220
+ ALREADY_EXISTS = 6,
221
+ PERMISSION_DENIED = 7,
222
+ RESOURCE_EXHAUSTED = 8,
223
+ FAILED_PRECONDITION = 9,
224
+ ABORTED = 10,
225
+ OUT_OF_RANGE = 11,
226
+ UNIMPLEMENTED = 12,
227
+ INTERNAL = 13,
228
+ UNAVAILABLE = 14,
229
+ DATA_LOSS = 15,
230
+ UNAUTHENTICATED = 16
231
+ }
232
+ declare class GrpcError extends Error {
233
+ readonly code: GrpcStatusCode;
234
+ constructor(code: GrpcStatusCode, message: string);
235
+ }
236
+ interface GrpcServiceConfig {
237
+ serviceName: string;
238
+ handler: GrpcHandler;
239
+ }
240
+ type UnaryHandlerResult<TResponse> = TResponse | {
241
+ response: TResponse;
242
+ metadata?: Record<string, string>;
243
+ };
244
+ declare function createUnaryHandler<TRequest, TResponse>(methodName: string, handler: (request: TRequest, metadata: Record<string, string>) => Promise<UnaryHandlerResult<TResponse>>, requestType: {
245
+ decode(buffer: Uint8Array): TRequest;
246
+ }, responseType: {
247
+ encode(message: TResponse): {
248
+ finish(): Uint8Array;
249
+ };
250
+ }): GrpcHandler;
251
+ declare function createServiceHandler(methods: Record<string, GrpcHandler>): GrpcHandler;
252
+
253
+ declare function wrapHandler(handler: HandlerFunction): NativeHandlerFunction;
254
+ declare function wrapBodyHandler<TBody = unknown>(handler: (body: TBody, request: Request) => MaybePromise<HandlerResult>): NativeHandlerFunction;
255
+
256
+ type Query<T> = T;
257
+ type Path<T> = T;
258
+ type Body<T> = T;
259
+ declare function QueryDefault<T>(value: T): T;
260
+
261
+ interface RouteOptions {
262
+ methods?: string | string[];
263
+ bodySchema?: JsonSchema;
264
+ responseSchema?: JsonSchema;
265
+ parameterSchema?: JsonSchema;
266
+ cors?: CorsConfig;
267
+ }
268
+ type RouteHandler = (request: Request) => MaybePromise<HandlerResult>;
269
+ declare function route(path: string, options?: RouteOptions): (handler: RouteHandler) => RouteHandler;
270
+ declare function get(path: string, options?: Omit<RouteOptions, "methods">): (handler: RouteHandler) => RouteHandler;
271
+ declare function post(path: string, options?: Omit<RouteOptions, "methods">): (handler: RouteHandler) => RouteHandler;
272
+ declare function put(path: string, options?: Omit<RouteOptions, "methods">): (handler: RouteHandler) => RouteHandler;
273
+ declare function del(path: string, options?: Omit<RouteOptions, "methods">): (handler: RouteHandler) => RouteHandler;
274
+ declare function patch(path: string, options?: Omit<RouteOptions, "methods">): (handler: RouteHandler) => RouteHandler;
275
+
276
+ interface NativeTestResponse {
277
+ statusCode: number;
278
+ headers(): Record<string, string>;
279
+ text(): string;
280
+ json<T>(): T;
281
+ bytes(): Buffer;
282
+ graphqlData(): unknown;
283
+ graphqlErrors(): Array<Record<string, unknown>>;
284
+ }
285
+ interface WebSocketTestConnection {
286
+ sendText(text: string): Promise<void>;
287
+ sendJson(obj: unknown): Promise<void>;
288
+ receiveText(): Promise<string>;
289
+ receiveJson(): Promise<unknown>;
290
+ receiveBytes(): Promise<Buffer>;
291
+ receiveMessage(): Promise<unknown>;
292
+ close(): Promise<void>;
293
+ }
294
+ type TestResponse = NativeTestResponse;
295
+ interface MultipartFile {
296
+ name: string;
297
+ filename?: string;
298
+ content: string;
299
+ contentType?: string;
300
+ }
301
+ interface RequestOptions {
302
+ headers?: Record<string, string>;
303
+ json?: JsonValue;
304
+ form?: Record<string, JsonValue>;
305
+ multipart?: {
306
+ fields?: Record<string, JsonValue>;
307
+ files?: MultipartFile[];
308
+ };
309
+ }
310
+ declare class TestClient {
311
+ readonly app: SpikardApp;
312
+ private nativeClient;
313
+ private looksLikeStringHandler;
314
+ constructor(app: SpikardApp);
315
+ get(path: string, headers?: Record<string, string>): Promise<TestResponse>;
316
+ post(path: string, options?: RequestOptions): Promise<TestResponse>;
317
+ put(path: string, options?: RequestOptions): Promise<TestResponse>;
318
+ delete(path: string, headers?: Record<string, string>): Promise<TestResponse>;
319
+ patch(path: string, options?: RequestOptions): Promise<TestResponse>;
320
+ head(path: string, headers?: Record<string, string>): Promise<TestResponse>;
321
+ options(path: string, options?: RequestOptions): Promise<TestResponse>;
322
+ trace(path: string, options?: RequestOptions): Promise<TestResponse>;
323
+ private buildHeaders;
324
+ private buildBody;
325
+ websocketConnect(path: string): Promise<WebSocketTestConnection>;
326
+ graphql(query: string, variables?: Record<string, unknown> | null, operationName?: string | null): Promise<TestResponse>;
327
+ graphqlWithStatus(query: string, variables?: Record<string, unknown> | null, operationName?: string | null): Promise<{
328
+ status: number;
329
+ statusCode: number;
330
+ headers: string;
331
+ bodyText: string;
332
+ }>;
333
+ cleanup(): Promise<void>;
334
+ }
335
+
336
+ declare class UploadFile {
337
+ readonly filename: string;
338
+ readonly contentType: string;
339
+ readonly size: number;
340
+ readonly headers: Record<string, string>;
341
+ private readonly _content;
342
+ private _position;
343
+ constructor(filename: string, content: Buffer, contentType?: string | null, size?: number | null, headers?: Record<string, string> | null);
344
+ read(size?: number): Buffer;
345
+ readAsync(size?: number): Promise<Buffer>;
346
+ text(): string;
347
+ textAsync(): Promise<string>;
348
+ seek(offset: number, whence?: number): number;
349
+ seekAsync(offset: number, whence?: number): Promise<number>;
350
+ tell(): number;
351
+ getBuffer(): Buffer;
352
+ close(): void;
353
+ closeAsync(): Promise<void>;
354
+ toString(): string;
355
+ toJSON(): Record<string, unknown>;
356
+ }
357
+
358
+ interface JsonSchema {
359
+ type?: string | string[];
360
+ properties?: Record<string, JsonSchema>;
361
+ required?: string[];
362
+ items?: JsonSchema | JsonSchema[];
363
+ enum?: JsonValue[];
364
+ [key: string]: JsonSchema | JsonSchema[] | JsonValue | JsonValue[] | string | number | boolean | undefined;
365
+ }
366
+ interface CorsConfig {
367
+ allow_origin?: string | string[];
368
+ allow_methods?: string[];
369
+ allow_headers?: string[];
370
+ allow_credentials?: boolean;
371
+ max_age?: number;
372
+ }
373
+ interface FileParam {
374
+ name: string;
375
+ required?: boolean;
376
+ max_size?: number;
377
+ allowed_types?: string[];
378
+ }
379
+ interface RouteMetadata {
380
+ method: string;
381
+ path: string;
382
+ handler_name: string;
383
+ request_schema?: JsonSchema | undefined;
384
+ response_schema?: JsonSchema | undefined;
385
+ parameter_schema?: JsonSchema | undefined;
386
+ file_params?: FileParam[] | undefined;
387
+ is_async: boolean;
388
+ cors?: CorsConfig | undefined;
389
+ }
390
+ interface SpikardApp {
391
+ routes: RouteMetadata[];
392
+ handlers: Record<string, HandlerFunction | NativeHandlerFunction>;
393
+ websocketRoutes?: RouteMetadata[];
394
+ websocketHandlers?: Record<string, Record<string, unknown>>;
395
+ config?: ServerConfig;
396
+ lifecycleHooks?: Partial<LifecycleHooks>;
397
+ dependencies?: Record<string, unknown>;
398
+ }
399
+
400
+ export { type ApiKeyConfig, type Base64EncodedBody, type Body, type CompressionConfig, type ContactInfo, type CorsConfig, type DependencyFactory, type DependencyOptions, type DependencyValue, type FileParam, GrpcError, type GrpcHandler, type GrpcMetadata, type GrpcRequest, type GrpcResponse, type GrpcServiceConfig, GrpcStatusCode, type HandlerFunction, type HandlerResult, type JsonPrimitive, type JsonRecord, type JsonSchema, type JsonValue, type JwtConfig, type LicenseInfo, type LifecycleHookFunction, type LifecycleHooks, type MaybePromise, type NativeHandlerFunction, type OpenApiConfig, type Path, type Query, QueryDefault, type RateLimitConfig, type Request, type RouteMetadata, type RouteOptions, type SecuritySchemeInfo, type ServerConfig, type ServerInfo, type ServerOptions, Spikard, type SpikardApp, type StaticFilesConfig, StreamingResponse, type StreamingResponseInit, type StructuredHandlerResponse, TestClient, type TestResponse, UploadFile, type WebSocketHandler, type WebSocketOptions, background, createServiceHandler, createUnaryHandler, del, get, patch, post, put, route, runServer, wrapBodyHandler, wrapHandler };