@ragbits/api-client-react 0.0.3 → 1.4.0-dev.202512021005
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/README.md +58 -43
- package/__tests__/mocks/handlers.ts +57 -0
- package/__tests__/setup.ts +23 -0
- package/__tests__/utils.tsx +120 -0
- package/dist/RagbitsContextProvider.d.ts +7 -0
- package/dist/hooks.d.ts +9 -7
- package/dist/index.cjs +14 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +13 -7
- package/dist/types.d.ts +12 -8
- package/package.json +10 -6
- package/dist/RagbitsProvider.d.ts +0 -7
package/README.md
CHANGED
|
@@ -128,6 +128,56 @@ function ChatComponent() {
|
|
|
128
128
|
}
|
|
129
129
|
```
|
|
130
130
|
|
|
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
|
+
|
|
131
181
|
## API Reference
|
|
132
182
|
|
|
133
183
|
### `RagbitsProvider`
|
|
@@ -143,13 +193,14 @@ interface RagbitsProviderProps {
|
|
|
143
193
|
}
|
|
144
194
|
```
|
|
145
195
|
|
|
146
|
-
### `useRagbitsCall<
|
|
196
|
+
### `useRagbitsCall<Endpoints, URL>(endpoint, defaultOptions?)`
|
|
147
197
|
|
|
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.
|
|
149
200
|
|
|
150
201
|
**Parameters:**
|
|
151
202
|
|
|
152
|
-
- `endpoint`:
|
|
203
|
+
- `endpoint`: API endpoint path (e.g., '/api/config', '/api/feedback')
|
|
153
204
|
- `defaultOptions` (optional): Default request options
|
|
154
205
|
|
|
155
206
|
**Returns:**
|
|
@@ -165,13 +216,14 @@ interface RagbitsCallResult<T, E = Error> {
|
|
|
165
216
|
}
|
|
166
217
|
```
|
|
167
218
|
|
|
168
|
-
### `useRagbitsStream<
|
|
219
|
+
### `useRagbitsStream<Endpoints, URL>(endpoint)`
|
|
169
220
|
|
|
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.
|
|
171
223
|
|
|
172
224
|
**Parameters:**
|
|
173
225
|
|
|
174
|
-
- `endpoint`:
|
|
226
|
+
- `endpoint`: Streaming endpoint path (e.g., '/api/chat')
|
|
175
227
|
|
|
176
228
|
**Returns:**
|
|
177
229
|
|
|
@@ -196,43 +248,6 @@ interface RagbitsContextValue {
|
|
|
196
248
|
}
|
|
197
249
|
```
|
|
198
250
|
|
|
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
|
-
|
|
236
251
|
## Browser Support
|
|
237
252
|
|
|
238
253
|
This package supports all modern browsers with React 16.8+ (for hooks):
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { http, HttpResponse } from 'msw'
|
|
2
|
+
import type { FeedbackResponse, ChatResponse } from '@ragbits/api-client'
|
|
3
|
+
import { defaultConfigResponse } from '../utils'
|
|
4
|
+
|
|
5
|
+
export const handlers = [
|
|
6
|
+
// Mock /api/config endpoint (GET)
|
|
7
|
+
http.get('http://127.0.0.1:8000/api/config', () => {
|
|
8
|
+
return HttpResponse.json(defaultConfigResponse)
|
|
9
|
+
}),
|
|
10
|
+
|
|
11
|
+
// Mock /api/feedback endpoint (POST)
|
|
12
|
+
http.post('http://127.0.0.1:8000/api/feedback', async ({ request }) => {
|
|
13
|
+
const _body = await request.json()
|
|
14
|
+
const response: FeedbackResponse = {
|
|
15
|
+
status: 'success',
|
|
16
|
+
}
|
|
17
|
+
return HttpResponse.json(response)
|
|
18
|
+
}),
|
|
19
|
+
|
|
20
|
+
// Mock /api/chat endpoint for streaming
|
|
21
|
+
http.post('http://127.0.0.1:8000/api/chat', () => {
|
|
22
|
+
const encoder = new TextEncoder()
|
|
23
|
+
|
|
24
|
+
const stream = new ReadableStream({
|
|
25
|
+
start(controller) {
|
|
26
|
+
// Send multiple chunks to simulate streaming with proper TypedChatResponse format
|
|
27
|
+
const chunks: string[] = [
|
|
28
|
+
`data: ${JSON.stringify({ type: 'text', content: 'Hello' } as ChatResponse)}\n\n`,
|
|
29
|
+
`data: ${JSON.stringify({ type: 'text', content: ' there!' } as ChatResponse)}\n\n`,
|
|
30
|
+
`data: ${JSON.stringify({ type: 'message_id', content: 'msg-123' } as ChatResponse)}\n\n`,
|
|
31
|
+
`data: ${JSON.stringify({ type: 'conversation_id', content: 'conv-456' } as ChatResponse)}\n\n`,
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
let index = 0
|
|
35
|
+
const sendChunk = () => {
|
|
36
|
+
if (index < chunks.length) {
|
|
37
|
+
controller.enqueue(encoder.encode(chunks[index]))
|
|
38
|
+
index++
|
|
39
|
+
setTimeout(sendChunk, 10)
|
|
40
|
+
} else {
|
|
41
|
+
controller.close()
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
sendChunk()
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
return new HttpResponse(stream, {
|
|
50
|
+
headers: {
|
|
51
|
+
'Content-Type': 'text/event-stream',
|
|
52
|
+
'Cache-Control': 'no-cache',
|
|
53
|
+
Connection: 'keep-alive',
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
}),
|
|
57
|
+
]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
import { beforeAll, afterEach, afterAll } from 'vitest'
|
|
3
|
+
import { setupServer } from 'msw/node'
|
|
4
|
+
import { handlers } from './mocks/handlers'
|
|
5
|
+
|
|
6
|
+
// Setup MSW server
|
|
7
|
+
export const server = setupServer(...handlers)
|
|
8
|
+
|
|
9
|
+
beforeAll(() => {
|
|
10
|
+
// Start the interception on the client side before all tests run.
|
|
11
|
+
server.listen({ onUnhandledRequest: 'error' })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
// Reset any request handlers that we may add during the tests,
|
|
16
|
+
// so they don't affect other tests.
|
|
17
|
+
server.resetHandlers()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
// Clean up after the tests are finished.
|
|
22
|
+
server.close()
|
|
23
|
+
})
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {
|
|
3
|
+
render,
|
|
4
|
+
renderHook,
|
|
5
|
+
RenderOptions,
|
|
6
|
+
RenderHookOptions,
|
|
7
|
+
RenderResult,
|
|
8
|
+
} from '@testing-library/react'
|
|
9
|
+
import { RagbitsContextProvider } from '../src'
|
|
10
|
+
import type { ConfigResponse } from '@ragbits/api-client'
|
|
11
|
+
|
|
12
|
+
interface CustomRenderOptions extends Omit<RenderOptions, 'wrapper'> {
|
|
13
|
+
baseUrl?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface CustomRenderHookOptions<TProps>
|
|
17
|
+
extends Omit<RenderHookOptions<TProps>, 'wrapper'> {
|
|
18
|
+
baseUrl?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function createWrapper(baseUrl?: string) {
|
|
22
|
+
return function Wrapper({ children }: { children: React.ReactNode }) {
|
|
23
|
+
return (
|
|
24
|
+
<RagbitsContextProvider baseUrl={baseUrl}>
|
|
25
|
+
{children}
|
|
26
|
+
</RagbitsContextProvider>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function renderWithProvider(
|
|
32
|
+
ui: React.ReactElement,
|
|
33
|
+
options: CustomRenderOptions = {}
|
|
34
|
+
): RenderResult {
|
|
35
|
+
const { baseUrl, ...renderOptions } = options
|
|
36
|
+
|
|
37
|
+
return render(ui, {
|
|
38
|
+
wrapper: createWrapper(baseUrl),
|
|
39
|
+
...renderOptions,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function renderHookWithProvider<TResult, TProps>(
|
|
44
|
+
hook: (props: TProps) => TResult,
|
|
45
|
+
options: CustomRenderHookOptions<TProps> = {}
|
|
46
|
+
) {
|
|
47
|
+
const { baseUrl, ...renderOptions } = options
|
|
48
|
+
|
|
49
|
+
return renderHook(hook, {
|
|
50
|
+
wrapper: createWrapper(baseUrl),
|
|
51
|
+
...renderOptions,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Default config response used in most tests - matches Python classes
|
|
56
|
+
export const defaultConfigResponse: ConfigResponse = {
|
|
57
|
+
feedback: {
|
|
58
|
+
like: {
|
|
59
|
+
enabled: true,
|
|
60
|
+
form: {
|
|
61
|
+
title: 'Like Form',
|
|
62
|
+
type: 'object',
|
|
63
|
+
required: ['like_reason'],
|
|
64
|
+
properties: {
|
|
65
|
+
like_reason: {
|
|
66
|
+
title: 'Like Reason',
|
|
67
|
+
description: 'Why do you like this?',
|
|
68
|
+
type: 'string',
|
|
69
|
+
minLength: 1,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
dislike: {
|
|
75
|
+
enabled: true,
|
|
76
|
+
form: {
|
|
77
|
+
title: 'Dislike Form',
|
|
78
|
+
type: 'object',
|
|
79
|
+
required: ['issue_type', 'feedback'],
|
|
80
|
+
properties: {
|
|
81
|
+
issue_type: {
|
|
82
|
+
title: 'Issue Type',
|
|
83
|
+
description: 'What was the issue?',
|
|
84
|
+
type: 'string',
|
|
85
|
+
enum: [
|
|
86
|
+
'Incorrect information',
|
|
87
|
+
'Not helpful',
|
|
88
|
+
'Unclear',
|
|
89
|
+
'Other',
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
feedback: {
|
|
93
|
+
title: 'Feedback',
|
|
94
|
+
description: 'Please provide more details',
|
|
95
|
+
type: 'string',
|
|
96
|
+
minLength: 1,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
customization: null,
|
|
103
|
+
user_settings: {
|
|
104
|
+
form: {
|
|
105
|
+
title: 'Chat Form',
|
|
106
|
+
type: 'object',
|
|
107
|
+
required: ['language'],
|
|
108
|
+
properties: {
|
|
109
|
+
language: {
|
|
110
|
+
title: 'Language',
|
|
111
|
+
description: 'Please select the language',
|
|
112
|
+
type: 'string',
|
|
113
|
+
enum: ['English', 'Polish'],
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
debug_mode: false,
|
|
119
|
+
conversation_history: false,
|
|
120
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { ClientConfig, RagbitsContextValue } from './index';
|
|
3
|
+
export interface RagbitsContextProviderProps extends ClientConfig {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export declare function RagbitsContextProvider({ children, ...config }: RagbitsContextProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare function useRagbitsContext(): RagbitsContextValue;
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RequestOptions, EndpointDefinition, BaseApiEndpoints, BaseStreamingEndpoints } from '@ragbits/api-client';
|
|
2
2
|
import type { RagbitsCallResult, RagbitsStreamResult } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Hook for making API calls to Ragbits endpoints
|
|
5
|
-
* -
|
|
6
|
-
* - Response type can be overridden with explicit type parameter
|
|
5
|
+
* - Supports any endpoints by providing `Endpoints` generic argument
|
|
7
6
|
* @param endpoint - The predefined API endpoint
|
|
8
7
|
* @param defaultOptions - Default options for the API call
|
|
9
8
|
*/
|
|
10
|
-
export declare function useRagbitsCall<
|
|
9
|
+
export declare function useRagbitsCall<Endpoints extends {
|
|
10
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
11
|
+
} = BaseApiEndpoints, URL extends keyof Endpoints = keyof Endpoints>(endpoint: URL, defaultOptions?: RequestOptions<URL, Endpoints>): RagbitsCallResult<URL, Endpoints, Error>;
|
|
11
12
|
/**
|
|
12
13
|
* Hook for handling streaming responses from Ragbits endpoints
|
|
13
|
-
* -
|
|
14
|
-
* - Response type can be overridden with explicit type parameter
|
|
14
|
+
* - Supports any streaming endpoints by providing `Endpoints` generic argument
|
|
15
15
|
* @param endpoint - The predefined streaming endpoint
|
|
16
16
|
*/
|
|
17
|
-
export declare function useRagbitsStream<
|
|
17
|
+
export declare function useRagbitsStream<Endpoints extends {
|
|
18
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
19
|
+
} = BaseStreamingEndpoints, URL extends keyof Endpoints = keyof Endpoints>(endpoint: URL, customHeaders?: Record<string, string>): RagbitsStreamResult<URL, Endpoints, Error>;
|
package/dist/index.cjs
CHANGED
|
@@ -21,7 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/index.ts
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
|
-
|
|
24
|
+
RagbitsContextProvider: () => RagbitsContextProvider,
|
|
25
25
|
useRagbitsCall: () => useRagbitsCall,
|
|
26
26
|
useRagbitsContext: () => useRagbitsContext,
|
|
27
27
|
useRagbitsStream: () => useRagbitsStream
|
|
@@ -32,12 +32,15 @@ __reExport(index_exports, require("@ragbits/api-client"), module.exports);
|
|
|
32
32
|
// src/hooks.ts
|
|
33
33
|
var import_react2 = require("react");
|
|
34
34
|
|
|
35
|
-
// src/
|
|
35
|
+
// src/RagbitsContextProvider.tsx
|
|
36
36
|
var import_react = require("react");
|
|
37
37
|
var import_api_client = require("@ragbits/api-client");
|
|
38
38
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
39
39
|
var RagbitsContext = (0, import_react.createContext)(null);
|
|
40
|
-
function
|
|
40
|
+
function RagbitsContextProvider({
|
|
41
|
+
children,
|
|
42
|
+
...config
|
|
43
|
+
}) {
|
|
41
44
|
const client = (0, import_react.useMemo)(() => new import_api_client.RagbitsClient(config), [config]);
|
|
42
45
|
const contextValue = (0, import_react.useMemo)(
|
|
43
46
|
() => ({
|
|
@@ -60,7 +63,9 @@ function useRagbitsContext() {
|
|
|
60
63
|
// src/hooks.ts
|
|
61
64
|
function useRagbitsCall(endpoint, defaultOptions) {
|
|
62
65
|
const { client } = useRagbitsContext();
|
|
63
|
-
const [data, setData] = (0, import_react2.useState)(
|
|
66
|
+
const [data, setData] = (0, import_react2.useState)(
|
|
67
|
+
null
|
|
68
|
+
);
|
|
64
69
|
const [error, setError] = (0, import_react2.useState)(null);
|
|
65
70
|
const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
|
|
66
71
|
const abortControllerRef = (0, import_react2.useRef)(null);
|
|
@@ -134,7 +139,7 @@ function useRagbitsCall(endpoint, defaultOptions) {
|
|
|
134
139
|
abort
|
|
135
140
|
};
|
|
136
141
|
}
|
|
137
|
-
function useRagbitsStream(endpoint) {
|
|
142
|
+
function useRagbitsStream(endpoint, customHeaders) {
|
|
138
143
|
const { client } = useRagbitsContext();
|
|
139
144
|
const [isStreaming, setIsStreaming] = (0, import_react2.useState)(false);
|
|
140
145
|
const [error, setError] = (0, import_react2.useState)(null);
|
|
@@ -177,14 +182,15 @@ function useRagbitsStream(endpoint) {
|
|
|
177
182
|
}
|
|
178
183
|
}
|
|
179
184
|
},
|
|
180
|
-
abortController.signal
|
|
185
|
+
abortController.signal,
|
|
186
|
+
customHeaders
|
|
181
187
|
);
|
|
182
188
|
return () => {
|
|
183
189
|
cancel();
|
|
184
190
|
cancelFn();
|
|
185
191
|
};
|
|
186
192
|
},
|
|
187
|
-
[
|
|
193
|
+
[isStreaming, client, endpoint, customHeaders, cancel]
|
|
188
194
|
);
|
|
189
195
|
return {
|
|
190
196
|
isStreaming,
|
|
@@ -195,7 +201,7 @@ function useRagbitsStream(endpoint) {
|
|
|
195
201
|
}
|
|
196
202
|
// Annotate the CommonJS export names for ESM import in node:
|
|
197
203
|
0 && (module.exports = {
|
|
198
|
-
|
|
204
|
+
RagbitsContextProvider,
|
|
199
205
|
useRagbitsCall,
|
|
200
206
|
useRagbitsContext,
|
|
201
207
|
useRagbitsStream,
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -4,12 +4,15 @@ export * from "@ragbits/api-client";
|
|
|
4
4
|
// src/hooks.ts
|
|
5
5
|
import { useState, useCallback, useRef } from "react";
|
|
6
6
|
|
|
7
|
-
// src/
|
|
7
|
+
// src/RagbitsContextProvider.tsx
|
|
8
8
|
import { createContext, useContext, useMemo } from "react";
|
|
9
9
|
import { RagbitsClient } from "@ragbits/api-client";
|
|
10
10
|
import { jsx } from "react/jsx-runtime";
|
|
11
11
|
var RagbitsContext = createContext(null);
|
|
12
|
-
function
|
|
12
|
+
function RagbitsContextProvider({
|
|
13
|
+
children,
|
|
14
|
+
...config
|
|
15
|
+
}) {
|
|
13
16
|
const client = useMemo(() => new RagbitsClient(config), [config]);
|
|
14
17
|
const contextValue = useMemo(
|
|
15
18
|
() => ({
|
|
@@ -32,7 +35,9 @@ function useRagbitsContext() {
|
|
|
32
35
|
// src/hooks.ts
|
|
33
36
|
function useRagbitsCall(endpoint, defaultOptions) {
|
|
34
37
|
const { client } = useRagbitsContext();
|
|
35
|
-
const [data, setData] = useState(
|
|
38
|
+
const [data, setData] = useState(
|
|
39
|
+
null
|
|
40
|
+
);
|
|
36
41
|
const [error, setError] = useState(null);
|
|
37
42
|
const [isLoading, setIsLoading] = useState(false);
|
|
38
43
|
const abortControllerRef = useRef(null);
|
|
@@ -106,7 +111,7 @@ function useRagbitsCall(endpoint, defaultOptions) {
|
|
|
106
111
|
abort
|
|
107
112
|
};
|
|
108
113
|
}
|
|
109
|
-
function useRagbitsStream(endpoint) {
|
|
114
|
+
function useRagbitsStream(endpoint, customHeaders) {
|
|
110
115
|
const { client } = useRagbitsContext();
|
|
111
116
|
const [isStreaming, setIsStreaming] = useState(false);
|
|
112
117
|
const [error, setError] = useState(null);
|
|
@@ -149,14 +154,15 @@ function useRagbitsStream(endpoint) {
|
|
|
149
154
|
}
|
|
150
155
|
}
|
|
151
156
|
},
|
|
152
|
-
abortController.signal
|
|
157
|
+
abortController.signal,
|
|
158
|
+
customHeaders
|
|
153
159
|
);
|
|
154
160
|
return () => {
|
|
155
161
|
cancel();
|
|
156
162
|
cancelFn();
|
|
157
163
|
};
|
|
158
164
|
},
|
|
159
|
-
[
|
|
165
|
+
[isStreaming, client, endpoint, customHeaders, cancel]
|
|
160
166
|
);
|
|
161
167
|
return {
|
|
162
168
|
isStreaming,
|
|
@@ -166,7 +172,7 @@ function useRagbitsStream(endpoint) {
|
|
|
166
172
|
};
|
|
167
173
|
}
|
|
168
174
|
export {
|
|
169
|
-
|
|
175
|
+
RagbitsContextProvider,
|
|
170
176
|
useRagbitsCall,
|
|
171
177
|
useRagbitsContext,
|
|
172
178
|
useRagbitsStream
|
package/dist/types.d.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import type { StreamCallbacks, RagbitsClient,
|
|
2
|
-
export interface RagbitsCallResult<
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type { StreamCallbacks, RagbitsClient, RequestOptions, EndpointDefinition, BaseApiEndpoints, EndpointRequest, EndpointResponse, BaseStreamingEndpoints } from '@ragbits/api-client';
|
|
2
|
+
export interface RagbitsCallResult<URL extends keyof Endpoints, Endpoints extends {
|
|
3
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
4
|
+
} = BaseApiEndpoints, Err = Error> {
|
|
5
|
+
data: EndpointResponse<URL, Endpoints> | null;
|
|
6
|
+
error: Err | null;
|
|
5
7
|
isLoading: boolean;
|
|
6
|
-
call: (options?:
|
|
8
|
+
call: (options?: RequestOptions<URL, Endpoints>) => Promise<EndpointResponse<URL, Endpoints>>;
|
|
7
9
|
reset: () => void;
|
|
8
10
|
abort: () => void;
|
|
9
11
|
}
|
|
10
|
-
export interface RagbitsStreamResult<
|
|
12
|
+
export interface RagbitsStreamResult<URL extends keyof Endpoints, Endpoints extends {
|
|
13
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
14
|
+
} = BaseStreamingEndpoints, Err = Error> {
|
|
11
15
|
isStreaming: boolean;
|
|
12
|
-
error:
|
|
13
|
-
stream: (data:
|
|
16
|
+
error: Err | null;
|
|
17
|
+
stream: (data: EndpointRequest<URL, Endpoints>, callbacks: StreamCallbacks<EndpointResponse<URL, Endpoints>, string>) => () => void;
|
|
14
18
|
cancel: () => void;
|
|
15
19
|
}
|
|
16
20
|
export interface RagbitsContextValue {
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ragbits/api-client-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.4.0-dev.202512021005",
|
|
4
4
|
"description": "React hooks for the Ragbits API client",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/deepsense-ai/ragbits"
|
|
8
|
+
},
|
|
5
9
|
"main": "dist/index.cjs",
|
|
6
10
|
"module": "dist/index.js",
|
|
7
11
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +18,7 @@
|
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
20
|
"scripts": {
|
|
17
|
-
"build": "npm run clean && tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --
|
|
21
|
+
"build": "npm run clean && tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --project tsconfig.json",
|
|
18
22
|
"dev": "tsc -b . --watch",
|
|
19
23
|
"test": "vitest",
|
|
20
24
|
"test:run": "vitest run",
|
|
@@ -22,14 +26,14 @@
|
|
|
22
26
|
"lint": "eslint .",
|
|
23
27
|
"format": "prettier --write .",
|
|
24
28
|
"format:check": "prettier --check .",
|
|
25
|
-
"clean": "rm -rf ./dist"
|
|
29
|
+
"clean": "rm -rf ./dist && rm -f ./tsconfig.tsbuildinfo"
|
|
26
30
|
},
|
|
27
31
|
"peerDependencies": {
|
|
28
32
|
"react": ">=16.8.0",
|
|
29
33
|
"react-dom": ">=16.8.0"
|
|
30
34
|
},
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"@ragbits/api-client": "
|
|
36
|
+
"@ragbits/api-client": "1.4.0-dev.202512021005"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
39
|
"@eslint/js": "^9.17.0",
|
|
@@ -38,7 +42,7 @@
|
|
|
38
42
|
"@testing-library/user-event": "^14.5.0",
|
|
39
43
|
"@types/react": "^18.2.0",
|
|
40
44
|
"@types/react-dom": "^18.2.0",
|
|
41
|
-
"@vitest/coverage-v8": "^
|
|
45
|
+
"@vitest/coverage-v8": "^4.0.7",
|
|
42
46
|
"eslint": "^9.17.0",
|
|
43
47
|
"eslint-plugin-react-hooks": "^5.0.0",
|
|
44
48
|
"eslint-plugin-react-refresh": "^0.4.16",
|
|
@@ -49,7 +53,7 @@
|
|
|
49
53
|
"tsup": "^8.0.0",
|
|
50
54
|
"typescript": "^5.0.0",
|
|
51
55
|
"typescript-eslint": "^8.18.2",
|
|
52
|
-
"vitest": "^
|
|
56
|
+
"vitest": "^4.0.7"
|
|
53
57
|
},
|
|
54
58
|
"keywords": [
|
|
55
59
|
"ragbits",
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { type ReactNode } from 'react';
|
|
2
|
-
import type { ClientConfig, RagbitsContextValue } from './index';
|
|
3
|
-
export interface RagbitsProviderProps extends ClientConfig {
|
|
4
|
-
children: ReactNode;
|
|
5
|
-
}
|
|
6
|
-
export declare function RagbitsProvider({ children, ...config }: RagbitsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
7
|
-
export declare function useRagbitsContext(): RagbitsContextValue;
|