Skip to content

Commit 9d546e5

Browse files
authored
feat: typescript docs update (#681)
1 parent aad5b78 commit 9d546e5

File tree

25 files changed

+386
-333
lines changed

25 files changed

+386
-333
lines changed

packages/ragbits-chat/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
- Add support for live updates and followup messages (#654)
1212
- Fix invalid context structure in requests from FE (#663)
1313
- Arrow Up and Arrow Down now cycle through sent messages in a terminal-like style (#667)
14-
- Fix followup messages not sending (#670)
14+
- Fix followup messages not sending (#680)
15+
- Improve typing of TypeScript libraries (#681)
1516

1617
## 1.0.0 (2025-06-04)
1718

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/FeedbackForm-DGKRWbmP.js renamed to packages/ragbits-chat/src/ragbits/chat/ui-build/assets/FeedbackForm-DMnDoAZy.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/index-2dLwwTR5.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import{aF as a,aG as e,aH as t}from"./index-cNuF2g19.js";const n={renderer:t,...e,...a};var o=n;export{o as default};

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/index-CHdih0Fl.js renamed to packages/ragbits-chat/src/ragbits/chat/ui-build/assets/index-cNuF2g19.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ragbits-chat/src/ragbits/chat/ui-build/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<link rel="icon" type="image/svg+xml" href="/assets/ragbits-9U4hpuUb.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Ragbits</title>
8-
<script type="module" crossorigin src="/assets/index-CHdih0Fl.js"></script>
8+
<script type="module" crossorigin src="/assets/index-cNuF2g19.js"></script>
99
<link rel="stylesheet" crossorigin href="/assets/index-BPwdy74I.css">
1010
</head>
1111

typescript/@ragbits/api-client-react/README.md

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,56 @@ function ChatComponent() {
128128
}
129129
```
130130

131+
#### Custom Route Call Example
132+
133+
```tsx
134+
import { useRagbitsCall } from '@ragbits/api-client-react'
135+
136+
type MyEndpoints = {
137+
'/api/my-endpoint': {
138+
method: 'GET'
139+
request: never
140+
response: string
141+
}
142+
}
143+
144+
function MyComponent() {
145+
// In case of usage of custom Endpoints, we have to specify the URL as generic parameter
146+
const custom = useRagbitsCall<MyEndpoints, '/api/my-endpoint'>(
147+
'/api/my-endpoint'
148+
)
149+
150+
const handleLoadCustom = async () => {
151+
try {
152+
await custom.call()
153+
console.log('Custom loaded:', custom.data)
154+
} catch (error) {
155+
console.error('Failed to load custom:', error)
156+
}
157+
}
158+
159+
return (
160+
<div>
161+
<button onClick={handleLoadCustom} disabled={custom.isLoading}>
162+
{custom.isLoading
163+
? 'Loading...'
164+
: custom.data
165+
? 'Reload custom'
166+
: 'Load custom'}
167+
</button>
168+
169+
{custom.data && (
170+
<div>
171+
<h3>Custom loaded successfully</h3>
172+
</div>
173+
)}
174+
175+
{custom.error && <p>Error: {custom.error.message}</p>}
176+
</div>
177+
)
178+
}
179+
```
180+
131181
## API Reference
132182

133183
### `RagbitsProvider`
@@ -143,13 +193,14 @@ interface RagbitsProviderProps {
143193
}
144194
```
145195

146-
### `useRagbitsCall<T>(endpoint, defaultOptions?)`
196+
### `useRagbitsCall<Endpoints, URL>(endpoint, defaultOptions?)`
147197

148-
React hook for making type-safe API calls with automatic state management.
198+
React hook for making type-safe API calls with automatic state management. `endpoint` must be one of the key of `Endpoints` type
199+
that define schema of the available endpoints. All default Ragbits routes are supported out of the box.
149200

150201
**Parameters:**
151202

152-
- `endpoint`: Predefined API endpoint path (e.g., '/api/config', '/api/feedback')
203+
- `endpoint`: API endpoint path (e.g., '/api/config', '/api/feedback')
153204
- `defaultOptions` (optional): Default request options
154205

155206
**Returns:**
@@ -165,13 +216,14 @@ interface RagbitsCallResult<T, E = Error> {
165216
}
166217
```
167218

168-
### `useRagbitsStream<T>(endpoint)`
219+
### `useRagbitsStream<Endpoints, URL>(endpoint)`
169220

170-
React hook for handling streaming responses with automatic state management.
221+
React hook for handling streaming responses with automatic state management. `endpoint` must be one of the key of `Endpoints` type
222+
that define schema of the available endpoints. All default Ragbits routes are supported out of the box.
171223

172224
**Parameters:**
173225

174-
- `endpoint`: Predefined streaming endpoint path (e.g., '/api/chat')
226+
- `endpoint`: Streaming endpoint path (e.g., '/api/chat')
175227

176228
**Returns:**
177229

@@ -196,43 +248,6 @@ interface RagbitsContextValue {
196248
}
197249
```
198250

199-
## Types
200-
201-
All types from `@ragbits/api-client` are re-exported:
202-
203-
```typescript
204-
import type {
205-
// Core types
206-
RagbitsClient,
207-
ClientConfig,
208-
209-
// Request/Response types
210-
ChatRequest,
211-
FeedbackRequest,
212-
ConfigResponse,
213-
FeedbackResponse,
214-
215-
// Message types
216-
Message,
217-
MessageRole,
218-
TypedChatResponse,
219-
ChatResponseType,
220-
221-
// Feedback types
222-
FeedbackType,
223-
224-
// Stream types
225-
StreamCallbacks,
226-
227-
// Endpoint types
228-
ApiEndpointPath,
229-
StreamingEndpointPath,
230-
ApiEndpointResponse,
231-
StreamingEndpointStream,
232-
TypedApiRequestOptions,
233-
} from '@ragbits/api-client-react'
234-
```
235-
236251
## Browser Support
237252

238253
This package supports all modern browsers with React 16.8+ (for hooks):

typescript/@ragbits/api-client-react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
}
1515
},
1616
"scripts": {
17-
"build": "npm run clean && tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --declarationDir dist --project tsconfig.json",
17+
"build": "npm run clean && tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --project tsconfig.json",
1818
"dev": "tsc -b . --watch",
1919
"test": "vitest",
2020
"test:run": "vitest run",
2121
"test:coverage": "vitest run --coverage",
2222
"lint": "eslint .",
2323
"format": "prettier --write .",
2424
"format:check": "prettier --check .",
25-
"clean": "rm -rf ./dist"
25+
"clean": "rm -rf ./dist && rm -f ./tsconfig.tsbuildinfo"
2626
},
2727
"peerDependencies": {
2828
"react": ">=16.8.0",

typescript/@ragbits/api-client-react/src/hooks.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import { useState, useCallback, useRef } from 'react'
22
import type {
33
StreamCallbacks,
4-
ApiEndpointPath,
5-
ApiEndpointResponse,
6-
TypedApiRequestOptions,
7-
StreamingEndpointPath,
8-
StreamingEndpointStream,
9-
StreamingEndpointRequest,
4+
EndpointResponse,
5+
RequestOptions,
6+
EndpointDefinition,
7+
BaseApiEndpoints,
8+
EndpointRequest,
9+
BaseStreamingEndpoints,
1010
} from '@ragbits/api-client'
1111
import type { RagbitsCallResult, RagbitsStreamResult } from './types'
1212
import { useRagbitsContext } from './RagbitsProvider'
1313

1414
/**
1515
* Hook for making API calls to Ragbits endpoints
16-
* - Only predefined routes are allowed
17-
* - Response type can be overridden with explicit type parameter
16+
* - Supports any endpoints by providing `Endpoints` generic argument
1817
* @param endpoint - The predefined API endpoint
1918
* @param defaultOptions - Default options for the API call
2019
*/
2120
export function useRagbitsCall<
22-
TEndpoint extends ApiEndpointPath,
23-
TResponse = ApiEndpointResponse<TEndpoint>,
21+
Endpoints extends {
22+
[K in keyof Endpoints]: EndpointDefinition
23+
} = BaseApiEndpoints,
24+
URL extends keyof Endpoints = keyof Endpoints,
2425
>(
25-
endpoint: TEndpoint,
26-
defaultOptions?: TypedApiRequestOptions<TEndpoint>
27-
): RagbitsCallResult<TResponse, Error, TEndpoint> {
26+
endpoint: URL,
27+
defaultOptions?: RequestOptions<URL, Endpoints>
28+
): RagbitsCallResult<URL, Endpoints, Error> {
2829
const { client } = useRagbitsContext()
29-
const [data, setData] = useState<TResponse | null>(null)
30+
const [data, setData] = useState<EndpointResponse<URL, Endpoints> | null>(
31+
null
32+
)
3033
const [error, setError] = useState<Error | null>(null)
3134
const [isLoading, setIsLoading] = useState(false)
3235
const abortControllerRef = useRef<AbortController | null>(null)
@@ -43,8 +46,8 @@ export function useRagbitsCall<
4346

4447
const call = useCallback(
4548
async (
46-
options: TypedApiRequestOptions<TEndpoint> = {}
47-
): Promise<TResponse> => {
49+
options: RequestOptions<URL, Endpoints> = {}
50+
): Promise<EndpointResponse<URL, Endpoints>> => {
4851
// Abort any existing request only if there's one in progress
4952
if (abortControllerRef.current && isLoading) {
5053
abortControllerRef.current.abort()
@@ -73,18 +76,18 @@ export function useRagbitsCall<
7376
}
7477

7578
// Now we can use the properly typed makeRequest without casting
76-
const result = await client.makeRequest(
79+
const result = await client.makeRequest<Endpoints>(
7780
endpoint,
7881
requestOptions
7982
)
8083

8184
// Only update state if request wasn't aborted
8285
if (!abortController.signal.aborted) {
83-
setData(result as TResponse)
86+
setData(result)
8487
abortControllerRef.current = null
8588
}
8689

87-
return result as TResponse
90+
return result
8891
} catch (err) {
8992
// Only update error state if request wasn't aborted
9093
if (!abortController.signal.aborted) {
@@ -126,13 +129,15 @@ export function useRagbitsCall<
126129

127130
/**
128131
* Hook for handling streaming responses from Ragbits endpoints
129-
* - Only predefined streaming routes are allowed
130-
* - Response type can be overridden with explicit type parameter
132+
* - Supports any streaming endpoints by providing `Endpoints` generic argument
131133
* @param endpoint - The predefined streaming endpoint
132134
*/
133-
export function useRagbitsStream<TEndpoint extends StreamingEndpointPath>(
134-
endpoint: TEndpoint
135-
): RagbitsStreamResult<Error, TEndpoint> {
135+
export function useRagbitsStream<
136+
Endpoints extends {
137+
[K in keyof Endpoints]: EndpointDefinition
138+
} = BaseStreamingEndpoints,
139+
URL extends keyof Endpoints = keyof Endpoints,
140+
>(endpoint: URL): RagbitsStreamResult<URL, Endpoints, Error> {
136141
const { client } = useRagbitsContext()
137142
const [isStreaming, setIsStreaming] = useState(false)
138143
const [error, setError] = useState<Error | null>(null)
@@ -150,11 +155,8 @@ export function useRagbitsStream<TEndpoint extends StreamingEndpointPath>(
150155

151156
const stream = useCallback(
152157
(
153-
data: StreamingEndpointRequest<TEndpoint>,
154-
callbacks: StreamCallbacks<
155-
StreamingEndpointStream<TEndpoint>,
156-
string
157-
>
158+
data: EndpointRequest<URL, Endpoints>,
159+
callbacks: StreamCallbacks<EndpointResponse<URL, Endpoints>, string>
158160
): (() => void) => {
159161
// Abort any existing stream only if there's one in progress
160162
if (abortControllerRef.current && isStreaming) {

typescript/@ragbits/api-client-react/src/types.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
import type {
22
StreamCallbacks,
33
RagbitsClient,
4-
StreamingEndpointPath,
5-
StreamingEndpointRequest,
6-
TypedApiRequestOptions,
7-
ApiEndpointPath,
8-
StreamingEndpointStream,
4+
RequestOptions,
5+
EndpointDefinition,
6+
BaseApiEndpoints,
7+
EndpointRequest,
8+
EndpointResponse,
9+
BaseStreamingEndpoints,
910
} from '@ragbits/api-client'
1011

1112
// React-specific hook result types
1213
export interface RagbitsCallResult<
13-
T,
14-
E = Error,
15-
TEndpoint extends ApiEndpointPath = ApiEndpointPath,
14+
URL extends keyof Endpoints,
15+
Endpoints extends {
16+
[K in keyof Endpoints]: EndpointDefinition
17+
} = BaseApiEndpoints,
18+
Err = Error,
1619
> {
17-
data: T | null
18-
error: E | null
20+
data: EndpointResponse<URL, Endpoints> | null
21+
error: Err | null
1922
isLoading: boolean
20-
call: (options?: TypedApiRequestOptions<TEndpoint>) => Promise<T>
23+
call: (
24+
options?: RequestOptions<URL, Endpoints>
25+
) => Promise<EndpointResponse<URL, Endpoints>>
2126
reset: () => void
2227
abort: () => void
2328
}
2429

2530
export interface RagbitsStreamResult<
26-
E = Error,
27-
TEndpoint extends StreamingEndpointPath = StreamingEndpointPath,
31+
URL extends keyof Endpoints,
32+
Endpoints extends {
33+
[K in keyof Endpoints]: EndpointDefinition
34+
} = BaseStreamingEndpoints,
35+
Err = Error,
2836
> {
2937
isStreaming: boolean
30-
error: E | null
38+
error: Err | null
3139
stream: (
32-
data: StreamingEndpointRequest<TEndpoint>,
33-
callbacks: StreamCallbacks<StreamingEndpointStream<TEndpoint>, string>
40+
data: EndpointRequest<URL, Endpoints>,
41+
callbacks: StreamCallbacks<EndpointResponse<URL, Endpoints>, string>
3442
) => () => void
3543
cancel: () => void
3644
}

0 commit comments

Comments
 (0)