@bagelink/sdk 1.7.98 → 1.7.104
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.
- package/bin/index.ts +16 -0
- package/dist/index.cjs +578 -0
- package/dist/index.d.cts +171 -2
- package/dist/index.d.mts +171 -2
- package/dist/index.d.ts +171 -2
- package/dist/index.mjs +574 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/openAPITools/StreamController.ts +372 -0
- package/src/openAPITools/functionGenerator.ts +183 -1
- package/src/openAPITools/index.ts +2 -0
- package/src/openAPITools/streamClient.ts +247 -0
- package/src/openAPITools/streamDetector.ts +44 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StreamController - Elegant event-based SSE stream management
|
|
3
|
+
* Provides a beautiful, type-safe API for consuming Server-Sent Events
|
|
4
|
+
*/
|
|
5
|
+
interface SSEEvent<T = any> {
|
|
6
|
+
/** Event type (e.g., "token", "tool_call", "done") */
|
|
7
|
+
type: string;
|
|
8
|
+
/** Event data payload */
|
|
9
|
+
data: T;
|
|
10
|
+
/** Raw event string */
|
|
11
|
+
raw?: string;
|
|
12
|
+
}
|
|
13
|
+
type EventHandler<T = any> = (data: T) => void;
|
|
14
|
+
type ErrorHandler = (error: Error) => void;
|
|
15
|
+
type VoidHandler = () => void;
|
|
16
|
+
/**
|
|
17
|
+
* Type-safe event map for stream events
|
|
18
|
+
* Maps event names to their data types
|
|
19
|
+
*/
|
|
20
|
+
type StreamEventMap = Record<string, any>;
|
|
21
|
+
/**
|
|
22
|
+
* StreamController - Chainable event emitter for SSE streams with full type safety
|
|
23
|
+
* @template TEventMap - Map of event names to their data types
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* type ChatEvents = {
|
|
27
|
+
* token: { content: string }
|
|
28
|
+
* tool_call: { name: string, args: any }
|
|
29
|
+
* done: { message: string }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const stream: StreamController<ChatEvents> = ...
|
|
33
|
+
* stream.on('token', (data) => {
|
|
34
|
+
* // `data` is typed as { content: string }
|
|
35
|
+
* console.log(data.content)
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
declare class StreamController<TEventMap extends StreamEventMap = StreamEventMap> {
|
|
39
|
+
private streamFn;
|
|
40
|
+
private handlers;
|
|
41
|
+
private errorHandlers;
|
|
42
|
+
private completeHandlers;
|
|
43
|
+
private abortController;
|
|
44
|
+
private _closed;
|
|
45
|
+
private _promise;
|
|
46
|
+
private _resolvePromise;
|
|
47
|
+
private _rejectPromise;
|
|
48
|
+
constructor(streamFn: (onEvent: (event: SSEEvent) => void, onError: (error: Error) => void, onComplete: () => void) => () => void);
|
|
49
|
+
private start;
|
|
50
|
+
/**
|
|
51
|
+
* Register an event handler (fully typed!)
|
|
52
|
+
* @param event - Event type to listen for
|
|
53
|
+
* @param handler - Handler function (data type inferred from event)
|
|
54
|
+
* @returns this (for chaining)
|
|
55
|
+
*/
|
|
56
|
+
on<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
57
|
+
/**
|
|
58
|
+
* Register a one-time event handler (fully typed!)
|
|
59
|
+
* @param event - Event type to listen for
|
|
60
|
+
* @param handler - Handler function (called once then removed, data type inferred from event)
|
|
61
|
+
* @returns this (for chaining)
|
|
62
|
+
*/
|
|
63
|
+
once<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
64
|
+
/**
|
|
65
|
+
* Remove an event handler (fully typed!)
|
|
66
|
+
* @param event - Event type
|
|
67
|
+
* @param handler - Handler to remove
|
|
68
|
+
* @returns this (for chaining)
|
|
69
|
+
*/
|
|
70
|
+
off<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all handlers for an event (or all events if no event specified)
|
|
73
|
+
*/
|
|
74
|
+
removeAllListeners(event?: keyof TEventMap | 'error' | 'complete'): this;
|
|
75
|
+
private emit;
|
|
76
|
+
private emitError;
|
|
77
|
+
private emitComplete;
|
|
78
|
+
/**
|
|
79
|
+
* Close the stream
|
|
80
|
+
*/
|
|
81
|
+
close(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Check if stream is closed
|
|
84
|
+
*/
|
|
85
|
+
get closed(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Convert stream to a Promise that resolves when 'done' event fires
|
|
88
|
+
* @returns Promise that resolves with the 'done' event data
|
|
89
|
+
*/
|
|
90
|
+
toPromise<T = any>(): Promise<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Make the stream async iterable
|
|
93
|
+
* Usage: for await (const event of stream) { ... }
|
|
94
|
+
*/
|
|
95
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<SSEEvent>;
|
|
96
|
+
/**
|
|
97
|
+
* Collect all events into an array until stream completes
|
|
98
|
+
* @returns Promise<SSEEvent[]>
|
|
99
|
+
*/
|
|
100
|
+
toArray(): Promise<SSEEvent[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Pipe stream events through a transform function
|
|
103
|
+
*/
|
|
104
|
+
map<R>(transform: (event: SSEEvent) => R): StreamController<TEventMap>;
|
|
105
|
+
/**
|
|
106
|
+
* Filter events based on a predicate
|
|
107
|
+
*/
|
|
108
|
+
filter(predicate: (event: SSEEvent) => boolean): StreamController<TEventMap>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* SSE (Server-Sent Events) Client Utilities
|
|
113
|
+
* Provides utilities for consuming Server-Sent Events streams
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
interface SSEStreamOptions {
|
|
117
|
+
/** Additional headers to send with the request */
|
|
118
|
+
headers?: Record<string, string>;
|
|
119
|
+
/** Whether to include credentials (cookies) with the request */
|
|
120
|
+
withCredentials?: boolean;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates an SSE stream consumer with elegant event-based API
|
|
124
|
+
* @param url - The SSE endpoint URL
|
|
125
|
+
* @param options - Stream options
|
|
126
|
+
* @returns StreamController for chainable event handling
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const stream = createSSEStream(url)
|
|
131
|
+
* .on('token', data => console.log(data))
|
|
132
|
+
* .on('done', () => console.log('Complete!'))
|
|
133
|
+
* .on('error', err => console.error(err))
|
|
134
|
+
*
|
|
135
|
+
* // Close when needed
|
|
136
|
+
* stream.close()
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
declare function createSSEStream<TEventMap extends StreamEventMap = StreamEventMap>(url: string, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
140
|
+
/**
|
|
141
|
+
* Creates an SSE stream consumer for POST requests with elegant event-based API
|
|
142
|
+
* @param url - The SSE endpoint URL
|
|
143
|
+
* @param body - Request body
|
|
144
|
+
* @param options - Stream options
|
|
145
|
+
* @returns StreamController for chainable event handling
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const stream = createSSEStreamPost(url, { message: 'Hello' })
|
|
150
|
+
* .on('token', data => console.log(data))
|
|
151
|
+
* .on('done', () => console.log('Complete!'))
|
|
152
|
+
* .on('error', err => console.error(err))
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function createSSEStreamPost<TEventMap extends StreamEventMap = StreamEventMap, TBody = any>(url: string, body: TBody, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
156
|
+
|
|
1
157
|
declare function getPath(pathsObject: PathsObject | undefined, path: string): PathItemObject | undefined;
|
|
2
158
|
/**
|
|
3
159
|
* A type guard to check if the given value is a `ReferenceObject`.
|
|
@@ -350,6 +506,19 @@ interface SecurityRequirementObject {
|
|
|
350
506
|
[name: string]: string[];
|
|
351
507
|
}
|
|
352
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Detects if an operation is an SSE (Server-Sent Events) stream endpoint
|
|
511
|
+
* @param operation - The OpenAPI operation object
|
|
512
|
+
* @returns Whether the operation is an SSE stream
|
|
513
|
+
*/
|
|
514
|
+
declare function isSSEStream(operation: OperationObject): boolean;
|
|
515
|
+
/**
|
|
516
|
+
* Extracts SSE event types from operation description
|
|
517
|
+
* @param operation - The OpenAPI operation object
|
|
518
|
+
* @returns Array of event types or undefined
|
|
519
|
+
*/
|
|
520
|
+
declare function extractSSEEventTypes(operation: OperationObject): string[] | undefined;
|
|
521
|
+
|
|
353
522
|
interface OpenAPIResponse {
|
|
354
523
|
types: string;
|
|
355
524
|
code: string;
|
|
@@ -460,5 +629,5 @@ declare class Bagel {
|
|
|
460
629
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
461
630
|
}
|
|
462
631
|
|
|
463
|
-
export { Bagel, dereference, formatAPIErrorMessage, getPath, isReferenceObject, isSchemaObject, _default as openAPI };
|
|
464
|
-
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
632
|
+
export { Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventTypes, formatAPIErrorMessage, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI };
|
|
633
|
+
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SSEEvent, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StreamController - Elegant event-based SSE stream management
|
|
3
|
+
* Provides a beautiful, type-safe API for consuming Server-Sent Events
|
|
4
|
+
*/
|
|
5
|
+
interface SSEEvent<T = any> {
|
|
6
|
+
/** Event type (e.g., "token", "tool_call", "done") */
|
|
7
|
+
type: string;
|
|
8
|
+
/** Event data payload */
|
|
9
|
+
data: T;
|
|
10
|
+
/** Raw event string */
|
|
11
|
+
raw?: string;
|
|
12
|
+
}
|
|
13
|
+
type EventHandler<T = any> = (data: T) => void;
|
|
14
|
+
type ErrorHandler = (error: Error) => void;
|
|
15
|
+
type VoidHandler = () => void;
|
|
16
|
+
/**
|
|
17
|
+
* Type-safe event map for stream events
|
|
18
|
+
* Maps event names to their data types
|
|
19
|
+
*/
|
|
20
|
+
type StreamEventMap = Record<string, any>;
|
|
21
|
+
/**
|
|
22
|
+
* StreamController - Chainable event emitter for SSE streams with full type safety
|
|
23
|
+
* @template TEventMap - Map of event names to their data types
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* type ChatEvents = {
|
|
27
|
+
* token: { content: string }
|
|
28
|
+
* tool_call: { name: string, args: any }
|
|
29
|
+
* done: { message: string }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const stream: StreamController<ChatEvents> = ...
|
|
33
|
+
* stream.on('token', (data) => {
|
|
34
|
+
* // `data` is typed as { content: string }
|
|
35
|
+
* console.log(data.content)
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
declare class StreamController<TEventMap extends StreamEventMap = StreamEventMap> {
|
|
39
|
+
private streamFn;
|
|
40
|
+
private handlers;
|
|
41
|
+
private errorHandlers;
|
|
42
|
+
private completeHandlers;
|
|
43
|
+
private abortController;
|
|
44
|
+
private _closed;
|
|
45
|
+
private _promise;
|
|
46
|
+
private _resolvePromise;
|
|
47
|
+
private _rejectPromise;
|
|
48
|
+
constructor(streamFn: (onEvent: (event: SSEEvent) => void, onError: (error: Error) => void, onComplete: () => void) => () => void);
|
|
49
|
+
private start;
|
|
50
|
+
/**
|
|
51
|
+
* Register an event handler (fully typed!)
|
|
52
|
+
* @param event - Event type to listen for
|
|
53
|
+
* @param handler - Handler function (data type inferred from event)
|
|
54
|
+
* @returns this (for chaining)
|
|
55
|
+
*/
|
|
56
|
+
on<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
57
|
+
/**
|
|
58
|
+
* Register a one-time event handler (fully typed!)
|
|
59
|
+
* @param event - Event type to listen for
|
|
60
|
+
* @param handler - Handler function (called once then removed, data type inferred from event)
|
|
61
|
+
* @returns this (for chaining)
|
|
62
|
+
*/
|
|
63
|
+
once<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
64
|
+
/**
|
|
65
|
+
* Remove an event handler (fully typed!)
|
|
66
|
+
* @param event - Event type
|
|
67
|
+
* @param handler - Handler to remove
|
|
68
|
+
* @returns this (for chaining)
|
|
69
|
+
*/
|
|
70
|
+
off<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all handlers for an event (or all events if no event specified)
|
|
73
|
+
*/
|
|
74
|
+
removeAllListeners(event?: keyof TEventMap | 'error' | 'complete'): this;
|
|
75
|
+
private emit;
|
|
76
|
+
private emitError;
|
|
77
|
+
private emitComplete;
|
|
78
|
+
/**
|
|
79
|
+
* Close the stream
|
|
80
|
+
*/
|
|
81
|
+
close(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Check if stream is closed
|
|
84
|
+
*/
|
|
85
|
+
get closed(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Convert stream to a Promise that resolves when 'done' event fires
|
|
88
|
+
* @returns Promise that resolves with the 'done' event data
|
|
89
|
+
*/
|
|
90
|
+
toPromise<T = any>(): Promise<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Make the stream async iterable
|
|
93
|
+
* Usage: for await (const event of stream) { ... }
|
|
94
|
+
*/
|
|
95
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<SSEEvent>;
|
|
96
|
+
/**
|
|
97
|
+
* Collect all events into an array until stream completes
|
|
98
|
+
* @returns Promise<SSEEvent[]>
|
|
99
|
+
*/
|
|
100
|
+
toArray(): Promise<SSEEvent[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Pipe stream events through a transform function
|
|
103
|
+
*/
|
|
104
|
+
map<R>(transform: (event: SSEEvent) => R): StreamController<TEventMap>;
|
|
105
|
+
/**
|
|
106
|
+
* Filter events based on a predicate
|
|
107
|
+
*/
|
|
108
|
+
filter(predicate: (event: SSEEvent) => boolean): StreamController<TEventMap>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* SSE (Server-Sent Events) Client Utilities
|
|
113
|
+
* Provides utilities for consuming Server-Sent Events streams
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
interface SSEStreamOptions {
|
|
117
|
+
/** Additional headers to send with the request */
|
|
118
|
+
headers?: Record<string, string>;
|
|
119
|
+
/** Whether to include credentials (cookies) with the request */
|
|
120
|
+
withCredentials?: boolean;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates an SSE stream consumer with elegant event-based API
|
|
124
|
+
* @param url - The SSE endpoint URL
|
|
125
|
+
* @param options - Stream options
|
|
126
|
+
* @returns StreamController for chainable event handling
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const stream = createSSEStream(url)
|
|
131
|
+
* .on('token', data => console.log(data))
|
|
132
|
+
* .on('done', () => console.log('Complete!'))
|
|
133
|
+
* .on('error', err => console.error(err))
|
|
134
|
+
*
|
|
135
|
+
* // Close when needed
|
|
136
|
+
* stream.close()
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
declare function createSSEStream<TEventMap extends StreamEventMap = StreamEventMap>(url: string, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
140
|
+
/**
|
|
141
|
+
* Creates an SSE stream consumer for POST requests with elegant event-based API
|
|
142
|
+
* @param url - The SSE endpoint URL
|
|
143
|
+
* @param body - Request body
|
|
144
|
+
* @param options - Stream options
|
|
145
|
+
* @returns StreamController for chainable event handling
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const stream = createSSEStreamPost(url, { message: 'Hello' })
|
|
150
|
+
* .on('token', data => console.log(data))
|
|
151
|
+
* .on('done', () => console.log('Complete!'))
|
|
152
|
+
* .on('error', err => console.error(err))
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function createSSEStreamPost<TEventMap extends StreamEventMap = StreamEventMap, TBody = any>(url: string, body: TBody, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
156
|
+
|
|
1
157
|
declare function getPath(pathsObject: PathsObject | undefined, path: string): PathItemObject | undefined;
|
|
2
158
|
/**
|
|
3
159
|
* A type guard to check if the given value is a `ReferenceObject`.
|
|
@@ -350,6 +506,19 @@ interface SecurityRequirementObject {
|
|
|
350
506
|
[name: string]: string[];
|
|
351
507
|
}
|
|
352
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Detects if an operation is an SSE (Server-Sent Events) stream endpoint
|
|
511
|
+
* @param operation - The OpenAPI operation object
|
|
512
|
+
* @returns Whether the operation is an SSE stream
|
|
513
|
+
*/
|
|
514
|
+
declare function isSSEStream(operation: OperationObject): boolean;
|
|
515
|
+
/**
|
|
516
|
+
* Extracts SSE event types from operation description
|
|
517
|
+
* @param operation - The OpenAPI operation object
|
|
518
|
+
* @returns Array of event types or undefined
|
|
519
|
+
*/
|
|
520
|
+
declare function extractSSEEventTypes(operation: OperationObject): string[] | undefined;
|
|
521
|
+
|
|
353
522
|
interface OpenAPIResponse {
|
|
354
523
|
types: string;
|
|
355
524
|
code: string;
|
|
@@ -460,5 +629,5 @@ declare class Bagel {
|
|
|
460
629
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
461
630
|
}
|
|
462
631
|
|
|
463
|
-
export { Bagel, dereference, formatAPIErrorMessage, getPath, isReferenceObject, isSchemaObject, _default as openAPI };
|
|
464
|
-
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
632
|
+
export { Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventTypes, formatAPIErrorMessage, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI };
|
|
633
|
+
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SSEEvent, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StreamController - Elegant event-based SSE stream management
|
|
3
|
+
* Provides a beautiful, type-safe API for consuming Server-Sent Events
|
|
4
|
+
*/
|
|
5
|
+
interface SSEEvent<T = any> {
|
|
6
|
+
/** Event type (e.g., "token", "tool_call", "done") */
|
|
7
|
+
type: string;
|
|
8
|
+
/** Event data payload */
|
|
9
|
+
data: T;
|
|
10
|
+
/** Raw event string */
|
|
11
|
+
raw?: string;
|
|
12
|
+
}
|
|
13
|
+
type EventHandler<T = any> = (data: T) => void;
|
|
14
|
+
type ErrorHandler = (error: Error) => void;
|
|
15
|
+
type VoidHandler = () => void;
|
|
16
|
+
/**
|
|
17
|
+
* Type-safe event map for stream events
|
|
18
|
+
* Maps event names to their data types
|
|
19
|
+
*/
|
|
20
|
+
type StreamEventMap = Record<string, any>;
|
|
21
|
+
/**
|
|
22
|
+
* StreamController - Chainable event emitter for SSE streams with full type safety
|
|
23
|
+
* @template TEventMap - Map of event names to their data types
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* type ChatEvents = {
|
|
27
|
+
* token: { content: string }
|
|
28
|
+
* tool_call: { name: string, args: any }
|
|
29
|
+
* done: { message: string }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const stream: StreamController<ChatEvents> = ...
|
|
33
|
+
* stream.on('token', (data) => {
|
|
34
|
+
* // `data` is typed as { content: string }
|
|
35
|
+
* console.log(data.content)
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
declare class StreamController<TEventMap extends StreamEventMap = StreamEventMap> {
|
|
39
|
+
private streamFn;
|
|
40
|
+
private handlers;
|
|
41
|
+
private errorHandlers;
|
|
42
|
+
private completeHandlers;
|
|
43
|
+
private abortController;
|
|
44
|
+
private _closed;
|
|
45
|
+
private _promise;
|
|
46
|
+
private _resolvePromise;
|
|
47
|
+
private _rejectPromise;
|
|
48
|
+
constructor(streamFn: (onEvent: (event: SSEEvent) => void, onError: (error: Error) => void, onComplete: () => void) => () => void);
|
|
49
|
+
private start;
|
|
50
|
+
/**
|
|
51
|
+
* Register an event handler (fully typed!)
|
|
52
|
+
* @param event - Event type to listen for
|
|
53
|
+
* @param handler - Handler function (data type inferred from event)
|
|
54
|
+
* @returns this (for chaining)
|
|
55
|
+
*/
|
|
56
|
+
on<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
57
|
+
/**
|
|
58
|
+
* Register a one-time event handler (fully typed!)
|
|
59
|
+
* @param event - Event type to listen for
|
|
60
|
+
* @param handler - Handler function (called once then removed, data type inferred from event)
|
|
61
|
+
* @returns this (for chaining)
|
|
62
|
+
*/
|
|
63
|
+
once<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
64
|
+
/**
|
|
65
|
+
* Remove an event handler (fully typed!)
|
|
66
|
+
* @param event - Event type
|
|
67
|
+
* @param handler - Handler to remove
|
|
68
|
+
* @returns this (for chaining)
|
|
69
|
+
*/
|
|
70
|
+
off<K extends keyof TEventMap | 'error' | 'complete'>(event: K, handler: K extends 'error' ? ErrorHandler : K extends 'complete' ? VoidHandler : K extends keyof TEventMap ? (data: TEventMap[K]) => void : EventHandler): this;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all handlers for an event (or all events if no event specified)
|
|
73
|
+
*/
|
|
74
|
+
removeAllListeners(event?: keyof TEventMap | 'error' | 'complete'): this;
|
|
75
|
+
private emit;
|
|
76
|
+
private emitError;
|
|
77
|
+
private emitComplete;
|
|
78
|
+
/**
|
|
79
|
+
* Close the stream
|
|
80
|
+
*/
|
|
81
|
+
close(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Check if stream is closed
|
|
84
|
+
*/
|
|
85
|
+
get closed(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Convert stream to a Promise that resolves when 'done' event fires
|
|
88
|
+
* @returns Promise that resolves with the 'done' event data
|
|
89
|
+
*/
|
|
90
|
+
toPromise<T = any>(): Promise<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Make the stream async iterable
|
|
93
|
+
* Usage: for await (const event of stream) { ... }
|
|
94
|
+
*/
|
|
95
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<SSEEvent>;
|
|
96
|
+
/**
|
|
97
|
+
* Collect all events into an array until stream completes
|
|
98
|
+
* @returns Promise<SSEEvent[]>
|
|
99
|
+
*/
|
|
100
|
+
toArray(): Promise<SSEEvent[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Pipe stream events through a transform function
|
|
103
|
+
*/
|
|
104
|
+
map<R>(transform: (event: SSEEvent) => R): StreamController<TEventMap>;
|
|
105
|
+
/**
|
|
106
|
+
* Filter events based on a predicate
|
|
107
|
+
*/
|
|
108
|
+
filter(predicate: (event: SSEEvent) => boolean): StreamController<TEventMap>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* SSE (Server-Sent Events) Client Utilities
|
|
113
|
+
* Provides utilities for consuming Server-Sent Events streams
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
interface SSEStreamOptions {
|
|
117
|
+
/** Additional headers to send with the request */
|
|
118
|
+
headers?: Record<string, string>;
|
|
119
|
+
/** Whether to include credentials (cookies) with the request */
|
|
120
|
+
withCredentials?: boolean;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates an SSE stream consumer with elegant event-based API
|
|
124
|
+
* @param url - The SSE endpoint URL
|
|
125
|
+
* @param options - Stream options
|
|
126
|
+
* @returns StreamController for chainable event handling
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const stream = createSSEStream(url)
|
|
131
|
+
* .on('token', data => console.log(data))
|
|
132
|
+
* .on('done', () => console.log('Complete!'))
|
|
133
|
+
* .on('error', err => console.error(err))
|
|
134
|
+
*
|
|
135
|
+
* // Close when needed
|
|
136
|
+
* stream.close()
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
declare function createSSEStream<TEventMap extends StreamEventMap = StreamEventMap>(url: string, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
140
|
+
/**
|
|
141
|
+
* Creates an SSE stream consumer for POST requests with elegant event-based API
|
|
142
|
+
* @param url - The SSE endpoint URL
|
|
143
|
+
* @param body - Request body
|
|
144
|
+
* @param options - Stream options
|
|
145
|
+
* @returns StreamController for chainable event handling
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const stream = createSSEStreamPost(url, { message: 'Hello' })
|
|
150
|
+
* .on('token', data => console.log(data))
|
|
151
|
+
* .on('done', () => console.log('Complete!'))
|
|
152
|
+
* .on('error', err => console.error(err))
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function createSSEStreamPost<TEventMap extends StreamEventMap = StreamEventMap, TBody = any>(url: string, body: TBody, options?: SSEStreamOptions): StreamController<TEventMap>;
|
|
156
|
+
|
|
1
157
|
declare function getPath(pathsObject: PathsObject | undefined, path: string): PathItemObject | undefined;
|
|
2
158
|
/**
|
|
3
159
|
* A type guard to check if the given value is a `ReferenceObject`.
|
|
@@ -350,6 +506,19 @@ interface SecurityRequirementObject {
|
|
|
350
506
|
[name: string]: string[];
|
|
351
507
|
}
|
|
352
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Detects if an operation is an SSE (Server-Sent Events) stream endpoint
|
|
511
|
+
* @param operation - The OpenAPI operation object
|
|
512
|
+
* @returns Whether the operation is an SSE stream
|
|
513
|
+
*/
|
|
514
|
+
declare function isSSEStream(operation: OperationObject): boolean;
|
|
515
|
+
/**
|
|
516
|
+
* Extracts SSE event types from operation description
|
|
517
|
+
* @param operation - The OpenAPI operation object
|
|
518
|
+
* @returns Array of event types or undefined
|
|
519
|
+
*/
|
|
520
|
+
declare function extractSSEEventTypes(operation: OperationObject): string[] | undefined;
|
|
521
|
+
|
|
353
522
|
interface OpenAPIResponse {
|
|
354
523
|
types: string;
|
|
355
524
|
code: string;
|
|
@@ -460,5 +629,5 @@ declare class Bagel {
|
|
|
460
629
|
uploadFile<T>(file: File, options?: UploadOptions): Promise<T>;
|
|
461
630
|
}
|
|
462
631
|
|
|
463
|
-
export { Bagel, dereference, formatAPIErrorMessage, getPath, isReferenceObject, isSchemaObject, _default as openAPI };
|
|
464
|
-
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|
|
632
|
+
export { Bagel, StreamController, createSSEStream, createSSEStreamPost, dereference, extractSSEEventTypes, formatAPIErrorMessage, getPath, isReferenceObject, isSSEStream, isSchemaObject, _default as openAPI };
|
|
633
|
+
export type { BaseParameterObject, CallbackObject, CallbacksObject, ComponentsObject, ContactObject, ContentObject, DiscriminatorObject, EncodingObject, EncodingPropertyObject, ExampleObject, ExamplesObject, ExternalDocumentationObject, HeaderObject, HeadersObject, InfoObject, LicenseObject, LinkObject, LinkParametersObject, LinksObject, MediaTypeObject, OAuthFlowObject, OAuthFlowsObject, OpenAPIObject, OperationObject, ParameterLocation, ParameterObject, ParameterStyle, PathItemObject, PathsObject, ReferenceObject, RequestBodyObject, ResponseObject, ResponsesObject, SSEEvent, SSEStreamOptions, SchemaObject, SchemaObjectType, SchemasObject, ScopesObject, SecurityRequirementObject, SecuritySchemeObject, SecuritySchemeType, ServerObject, ServerVariableObject, StreamEventMap, TableToTypeMapping, Tables, TagObject, TypeFormatT, UploadOptions, User, XmlObject };
|