@richie-rpc/client 1.2.3 → 1.2.4

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.
@@ -1,4 +1,5 @@
1
- import type { Contract, EndpointDefinition, ExtractBody, ExtractHeaders, ExtractParams, ExtractQuery } from '@richie-rpc/core';
1
+ import type { Contract, DownloadEndpointDefinition, DownloadProgressEvent, EndpointDefinition, ExtractBody, ExtractChunk, ExtractFinalResponse, ExtractHeaders, ExtractParams, ExtractQuery, ExtractSSEEventData, SSEEndpointDefinition, StandardEndpointDefinition, StreamingEndpointDefinition, UploadProgressEvent } from '@richie-rpc/core';
2
+ export type { UploadProgressEvent, DownloadProgressEvent };
2
3
  import type { z } from 'zod';
3
4
  export interface ClientConfig {
4
5
  baseUrl: string;
@@ -12,16 +13,82 @@ export type EndpointRequestOptions<T extends EndpointDefinition> = {
12
13
  headers?: ExtractHeaders<T> extends never ? never : ExtractHeaders<T>;
13
14
  body?: ExtractBody<T> extends never ? never : ExtractBody<T>;
14
15
  abortSignal?: AbortSignal;
16
+ /** Upload progress callback (uses XHR for progress tracking) */
17
+ onUploadProgress?: (event: UploadProgressEvent) => void;
15
18
  };
16
- export type EndpointResponse<T extends EndpointDefinition> = {
19
+ export type EndpointResponse<T extends StandardEndpointDefinition> = {
17
20
  [Status in keyof T['responses']]: {
18
21
  status: Status;
19
22
  data: T['responses'][Status] extends z.ZodTypeAny ? z.infer<T['responses'][Status]> : never;
20
23
  };
21
24
  }[keyof T['responses']];
22
- export type ClientMethod<T extends EndpointDefinition> = (options: EndpointRequestOptions<T>) => Promise<EndpointResponse<T>>;
25
+ export type ClientMethod<T extends StandardEndpointDefinition> = (options: EndpointRequestOptions<T>) => Promise<EndpointResponse<T>>;
26
+ /**
27
+ * Result object for streaming endpoints - event-based API
28
+ */
29
+ export interface StreamingResult<T extends StreamingEndpointDefinition> {
30
+ /** Subscribe to chunks */
31
+ on(event: 'chunk', handler: (chunk: ExtractChunk<T>) => void): () => void;
32
+ /** Subscribe to stream close (with optional final response) */
33
+ on(event: 'close', handler: (final?: ExtractFinalResponse<T>) => void): () => void;
34
+ /** Subscribe to errors */
35
+ on(event: 'error', handler: (error: Error) => void): () => void;
36
+ /** Abort the stream */
37
+ abort(): void;
38
+ /** Check if aborted */
39
+ readonly aborted: boolean;
40
+ }
41
+ /**
42
+ * Client method type for streaming endpoints
43
+ */
44
+ export type StreamingClientMethod<T extends StreamingEndpointDefinition> = (options: EndpointRequestOptions<T>) => Promise<StreamingResult<T>>;
45
+ /**
46
+ * Connection object for SSE endpoints - event-based API
47
+ */
48
+ export interface SSEConnection<T extends SSEEndpointDefinition> {
49
+ /** Subscribe to a specific event type */
50
+ on<K extends keyof T['events']>(event: K, handler: (data: ExtractSSEEventData<T, K>, id?: string) => void): () => void;
51
+ /** Subscribe to errors */
52
+ on(event: 'error', handler: (error: Error) => void): () => void;
53
+ /** Close the connection */
54
+ close(): void;
55
+ /** Current connection state */
56
+ readonly state: 'connecting' | 'open' | 'closed';
57
+ }
58
+ /**
59
+ * Client method type for SSE endpoints
60
+ */
61
+ export type SSEClientMethod<T extends SSEEndpointDefinition> = (options?: Omit<EndpointRequestOptions<T>, 'body' | 'onUploadProgress'>) => SSEConnection<T>;
62
+ /**
63
+ * Request options for download endpoints
64
+ */
65
+ export type DownloadRequestOptions<T extends DownloadEndpointDefinition> = {
66
+ params?: ExtractParams<T> extends never ? never : ExtractParams<T>;
67
+ query?: ExtractQuery<T> extends never ? never : ExtractQuery<T>;
68
+ headers?: ExtractHeaders<T> extends never ? never : ExtractHeaders<T>;
69
+ abortSignal?: AbortSignal;
70
+ /** Download progress callback */
71
+ onDownloadProgress?: (event: DownloadProgressEvent) => void;
72
+ };
73
+ /**
74
+ * Response type for download endpoints
75
+ * Success (200) returns File, errors return typed error response
76
+ */
77
+ export type DownloadResponse<T extends DownloadEndpointDefinition> = {
78
+ status: 200;
79
+ data: File;
80
+ } | (T['errorResponses'] extends Record<number, z.ZodTypeAny> ? {
81
+ [S in keyof T['errorResponses']]: {
82
+ status: S;
83
+ data: T['errorResponses'][S] extends z.ZodTypeAny ? z.infer<T['errorResponses'][S]> : never;
84
+ };
85
+ }[keyof T['errorResponses']] : never);
86
+ /**
87
+ * Client method type for download endpoints
88
+ */
89
+ export type DownloadClientMethod<T extends DownloadEndpointDefinition> = (options?: DownloadRequestOptions<T>) => Promise<DownloadResponse<T>>;
23
90
  export type Client<T extends Contract> = {
24
- [K in keyof T]: ClientMethod<T[K]>;
91
+ [K in keyof T]: T[K] extends StandardEndpointDefinition ? ClientMethod<T[K]> : T[K] extends StreamingEndpointDefinition ? StreamingClientMethod<T[K]> : T[K] extends SSEEndpointDefinition ? SSEClientMethod<T[K]> : T[K] extends DownloadEndpointDefinition ? DownloadClientMethod<T[K]> : never;
25
92
  };
26
93
  export declare class ClientValidationError extends Error {
27
94
  field: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@richie-rpc/client",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "main": "./dist/cjs/index.cjs",
5
5
  "exports": {
6
6
  ".": {
@@ -10,7 +10,7 @@
10
10
  }
11
11
  },
12
12
  "peerDependencies": {
13
- "@richie-rpc/core": "^1.2.2",
13
+ "@richie-rpc/core": "^1.2.3",
14
14
  "typescript": "^5",
15
15
  "zod": "^4.1.12"
16
16
  },