@uploadista/expo 0.0.15-beta.3 → 0.0.15-beta.5
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/dist/index.d.mts +252 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
- package/src/components/uploadista-provider.tsx +20 -29
- package/src/hooks/event-utils.ts +44 -0
- package/src/hooks/use-flow-events.ts +141 -0
- package/src/hooks/use-upload-events.ts +218 -0
- package/src/hooks/use-uploadista-events.ts +41 -0
- package/src/index.ts +14 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FlowEventFlowCancel,
|
|
3
|
+
FlowEventFlowEnd,
|
|
4
|
+
FlowEventFlowError,
|
|
5
|
+
FlowEventFlowPause,
|
|
6
|
+
FlowEventFlowStart,
|
|
7
|
+
FlowEventJobEnd,
|
|
8
|
+
FlowEventJobStart,
|
|
9
|
+
FlowEventNodeEnd,
|
|
10
|
+
FlowEventNodeError,
|
|
11
|
+
FlowEventNodePause,
|
|
12
|
+
FlowEventNodeResume,
|
|
13
|
+
FlowEventNodeStart,
|
|
14
|
+
} from "@uploadista/core/flow";
|
|
15
|
+
import { EventType } from "@uploadista/core/flow";
|
|
16
|
+
import { useEffect } from "react";
|
|
17
|
+
import { useUploadistaContext } from "../components/uploadista-provider";
|
|
18
|
+
import { isFlowEvent } from "./event-utils";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Options for handling flow execution events.
|
|
22
|
+
*
|
|
23
|
+
* All callbacks are optional - only provide handlers for events you care about.
|
|
24
|
+
*/
|
|
25
|
+
export interface UseFlowEventsOptions {
|
|
26
|
+
/** Called when a job starts execution */
|
|
27
|
+
onJobStart?: (event: FlowEventJobStart) => void;
|
|
28
|
+
/** Called when a job completes (success or failure) */
|
|
29
|
+
onJobEnd?: (event: FlowEventJobEnd) => void;
|
|
30
|
+
/** Called when a flow begins execution */
|
|
31
|
+
onFlowStart?: (event: FlowEventFlowStart) => void;
|
|
32
|
+
/** Called when a flow completes successfully */
|
|
33
|
+
onFlowEnd?: (event: FlowEventFlowEnd) => void;
|
|
34
|
+
/** Called when a flow encounters an error */
|
|
35
|
+
onFlowError?: (event: FlowEventFlowError) => void;
|
|
36
|
+
/** Called when a flow is paused by user request */
|
|
37
|
+
onFlowPause?: (event: FlowEventFlowPause) => void;
|
|
38
|
+
/** Called when a flow is cancelled by user request */
|
|
39
|
+
onFlowCancel?: (event: FlowEventFlowCancel) => void;
|
|
40
|
+
/** Called when a node starts processing */
|
|
41
|
+
onNodeStart?: (event: FlowEventNodeStart) => void;
|
|
42
|
+
/** Called when a node completes successfully */
|
|
43
|
+
onNodeEnd?: (event: FlowEventNodeEnd) => void;
|
|
44
|
+
/** Called when a node pauses (waiting for additional data) */
|
|
45
|
+
onNodePause?: (event: FlowEventNodePause) => void;
|
|
46
|
+
/** Called when a paused node resumes execution */
|
|
47
|
+
onNodeResume?: (event: FlowEventNodeResume) => void;
|
|
48
|
+
/** Called when a node encounters an error */
|
|
49
|
+
onNodeError?: (event: FlowEventNodeError) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Structured hook for handling flow execution events with type-safe callbacks.
|
|
54
|
+
*
|
|
55
|
+
* This hook provides a clean API for listening to specific flow events without
|
|
56
|
+
* needing to manually filter events or use type guards.
|
|
57
|
+
*
|
|
58
|
+
* Must be used within UploadistaProvider.
|
|
59
|
+
*
|
|
60
|
+
* @param options - Object with optional callbacks for each flow event type
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useFlowEvents } from '@uploadista/expo';
|
|
65
|
+
* import { View, Text } from 'react-native';
|
|
66
|
+
*
|
|
67
|
+
* function FlowMonitor() {
|
|
68
|
+
* useFlowEvents({
|
|
69
|
+
* onFlowStart: (event) => {
|
|
70
|
+
* console.log('Flow started:', event.flowId);
|
|
71
|
+
* },
|
|
72
|
+
* onNodeStart: (event) => {
|
|
73
|
+
* console.log('Node started:', event.nodeName);
|
|
74
|
+
* },
|
|
75
|
+
* onNodeEnd: (event) => {
|
|
76
|
+
* console.log('Node completed:', event.nodeName, event.result);
|
|
77
|
+
* },
|
|
78
|
+
* onFlowEnd: (event) => {
|
|
79
|
+
* console.log('Flow completed with outputs:', event.outputs);
|
|
80
|
+
* },
|
|
81
|
+
* onFlowError: (event) => {
|
|
82
|
+
* console.error('Flow failed:', event.error);
|
|
83
|
+
* },
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* return <View><Text>Monitoring flow execution...</Text></View>;
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function useFlowEvents(options: UseFlowEventsOptions): void {
|
|
91
|
+
const { subscribeToEvents } = useUploadistaContext();
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
const unsubscribe = subscribeToEvents((event) => {
|
|
95
|
+
// Only handle flow events
|
|
96
|
+
if (!isFlowEvent(event)) return;
|
|
97
|
+
|
|
98
|
+
// Route to appropriate callback based on event type
|
|
99
|
+
switch (event.eventType) {
|
|
100
|
+
case EventType.JobStart:
|
|
101
|
+
options.onJobStart?.(event);
|
|
102
|
+
break;
|
|
103
|
+
case EventType.JobEnd:
|
|
104
|
+
options.onJobEnd?.(event);
|
|
105
|
+
break;
|
|
106
|
+
case EventType.FlowStart:
|
|
107
|
+
options.onFlowStart?.(event);
|
|
108
|
+
break;
|
|
109
|
+
case EventType.FlowEnd:
|
|
110
|
+
options.onFlowEnd?.(event);
|
|
111
|
+
break;
|
|
112
|
+
case EventType.FlowError:
|
|
113
|
+
options.onFlowError?.(event);
|
|
114
|
+
break;
|
|
115
|
+
case EventType.FlowPause:
|
|
116
|
+
options.onFlowPause?.(event);
|
|
117
|
+
break;
|
|
118
|
+
case EventType.FlowCancel:
|
|
119
|
+
options.onFlowCancel?.(event);
|
|
120
|
+
break;
|
|
121
|
+
case EventType.NodeStart:
|
|
122
|
+
options.onNodeStart?.(event);
|
|
123
|
+
break;
|
|
124
|
+
case EventType.NodeEnd:
|
|
125
|
+
options.onNodeEnd?.(event);
|
|
126
|
+
break;
|
|
127
|
+
case EventType.NodePause:
|
|
128
|
+
options.onNodePause?.(event);
|
|
129
|
+
break;
|
|
130
|
+
case EventType.NodeResume:
|
|
131
|
+
options.onNodeResume?.(event);
|
|
132
|
+
break;
|
|
133
|
+
case EventType.NodeError:
|
|
134
|
+
options.onNodeError?.(event);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return unsubscribe;
|
|
140
|
+
}, [subscribeToEvents, options]);
|
|
141
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { UploadEventType } from "@uploadista/core/types";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { useUploadistaContext } from "../components/uploadista-provider";
|
|
4
|
+
import { isUploadEvent } from "./event-utils";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Upload progress event data
|
|
8
|
+
*/
|
|
9
|
+
export interface UploadProgressEventData {
|
|
10
|
+
id: string;
|
|
11
|
+
progress: number;
|
|
12
|
+
total: number;
|
|
13
|
+
flow?: {
|
|
14
|
+
flowId: string;
|
|
15
|
+
nodeId: string;
|
|
16
|
+
jobId: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Upload started/complete event data (contains full UploadFile)
|
|
22
|
+
*/
|
|
23
|
+
export interface UploadFileEventData {
|
|
24
|
+
// This will contain the full UploadFile schema
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
flow?: {
|
|
27
|
+
flowId: string;
|
|
28
|
+
nodeId: string;
|
|
29
|
+
jobId: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Upload failed event data
|
|
35
|
+
*/
|
|
36
|
+
export interface UploadFailedEventData {
|
|
37
|
+
id: string;
|
|
38
|
+
error: string;
|
|
39
|
+
flow?: {
|
|
40
|
+
flowId: string;
|
|
41
|
+
nodeId: string;
|
|
42
|
+
jobId: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Upload validation success event data
|
|
48
|
+
*/
|
|
49
|
+
export interface UploadValidationSuccessEventData {
|
|
50
|
+
id: string;
|
|
51
|
+
validationType: "checksum" | "mimetype";
|
|
52
|
+
algorithm?: string;
|
|
53
|
+
flow?: {
|
|
54
|
+
flowId: string;
|
|
55
|
+
nodeId: string;
|
|
56
|
+
jobId: string;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Upload validation failed event data
|
|
62
|
+
*/
|
|
63
|
+
export interface UploadValidationFailedEventData {
|
|
64
|
+
id: string;
|
|
65
|
+
reason: string;
|
|
66
|
+
expected: string;
|
|
67
|
+
actual: string;
|
|
68
|
+
flow?: {
|
|
69
|
+
flowId: string;
|
|
70
|
+
nodeId: string;
|
|
71
|
+
jobId: string;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Upload validation warning event data
|
|
77
|
+
*/
|
|
78
|
+
export interface UploadValidationWarningEventData {
|
|
79
|
+
id: string;
|
|
80
|
+
message: string;
|
|
81
|
+
flow?: {
|
|
82
|
+
flowId: string;
|
|
83
|
+
nodeId: string;
|
|
84
|
+
jobId: string;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Options for handling upload events.
|
|
90
|
+
*
|
|
91
|
+
* All callbacks are optional - only provide handlers for events you care about.
|
|
92
|
+
*/
|
|
93
|
+
export interface UseUploadEventsOptions {
|
|
94
|
+
/** Called when an upload starts */
|
|
95
|
+
onUploadStarted?: (data: UploadFileEventData) => void;
|
|
96
|
+
/** Called with upload progress updates */
|
|
97
|
+
onUploadProgress?: (data: UploadProgressEventData) => void;
|
|
98
|
+
/** Called when an upload completes successfully */
|
|
99
|
+
onUploadComplete?: (data: UploadFileEventData) => void;
|
|
100
|
+
/** Called when an upload fails */
|
|
101
|
+
onUploadFailed?: (data: UploadFailedEventData) => void;
|
|
102
|
+
/** Called when upload validation succeeds */
|
|
103
|
+
onUploadValidationSuccess?: (data: UploadValidationSuccessEventData) => void;
|
|
104
|
+
/** Called when upload validation fails */
|
|
105
|
+
onUploadValidationFailed?: (data: UploadValidationFailedEventData) => void;
|
|
106
|
+
/** Called when upload validation produces a warning */
|
|
107
|
+
onUploadValidationWarning?: (data: UploadValidationWarningEventData) => void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Structured hook for handling upload events with type-safe callbacks.
|
|
112
|
+
*
|
|
113
|
+
* This hook provides a clean API for listening to specific upload events without
|
|
114
|
+
* needing to manually filter events or use type guards.
|
|
115
|
+
*
|
|
116
|
+
* Must be used within UploadistaProvider.
|
|
117
|
+
*
|
|
118
|
+
* @param options - Object with optional callbacks for each upload event type
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```tsx
|
|
122
|
+
* import { useUploadEvents } from '@uploadista/expo';
|
|
123
|
+
* import { View, Text } from 'react-native';
|
|
124
|
+
*
|
|
125
|
+
* function UploadMonitor() {
|
|
126
|
+
* useUploadEvents({
|
|
127
|
+
* onUploadStarted: (data) => {
|
|
128
|
+
* console.log('Upload started:', data.id);
|
|
129
|
+
* },
|
|
130
|
+
* onUploadProgress: (data) => {
|
|
131
|
+
* const percent = (data.progress / data.total) * 100;
|
|
132
|
+
* console.log(`Upload progress: ${percent}%`);
|
|
133
|
+
* },
|
|
134
|
+
* onUploadComplete: (data) => {
|
|
135
|
+
* console.log('Upload completed:', data);
|
|
136
|
+
* },
|
|
137
|
+
* onUploadFailed: (data) => {
|
|
138
|
+
* console.error('Upload failed:', data.error);
|
|
139
|
+
* },
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* return <View><Text>Monitoring uploads...</Text></View>;
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function useUploadEvents(options: UseUploadEventsOptions): void {
|
|
147
|
+
const { subscribeToEvents } = useUploadistaContext();
|
|
148
|
+
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
const unsubscribe = subscribeToEvents((event) => {
|
|
151
|
+
// Only handle upload events
|
|
152
|
+
if (!isUploadEvent(event)) return;
|
|
153
|
+
|
|
154
|
+
// Route to appropriate callback based on event type
|
|
155
|
+
// Note: flow context is at the top level of the event, not inside data
|
|
156
|
+
const flowContext = "flow" in event ? event.flow : undefined;
|
|
157
|
+
|
|
158
|
+
switch (event.type) {
|
|
159
|
+
case UploadEventType.UPLOAD_STARTED:
|
|
160
|
+
options.onUploadStarted?.({
|
|
161
|
+
...(event.data as unknown as Omit<UploadFileEventData, "flow">),
|
|
162
|
+
flow: flowContext,
|
|
163
|
+
});
|
|
164
|
+
break;
|
|
165
|
+
case UploadEventType.UPLOAD_PROGRESS:
|
|
166
|
+
options.onUploadProgress?.({
|
|
167
|
+
...(event.data as unknown as Omit<
|
|
168
|
+
UploadProgressEventData,
|
|
169
|
+
"flow"
|
|
170
|
+
>),
|
|
171
|
+
flow: flowContext,
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
case UploadEventType.UPLOAD_COMPLETE:
|
|
175
|
+
options.onUploadComplete?.({
|
|
176
|
+
...(event.data as unknown as Omit<UploadFileEventData, "flow">),
|
|
177
|
+
flow: flowContext,
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
case UploadEventType.UPLOAD_FAILED:
|
|
181
|
+
options.onUploadFailed?.({
|
|
182
|
+
...(event.data as unknown as Omit<UploadFailedEventData, "flow">),
|
|
183
|
+
flow: flowContext,
|
|
184
|
+
});
|
|
185
|
+
break;
|
|
186
|
+
case UploadEventType.UPLOAD_VALIDATION_SUCCESS:
|
|
187
|
+
options.onUploadValidationSuccess?.({
|
|
188
|
+
...(event.data as unknown as Omit<
|
|
189
|
+
UploadValidationSuccessEventData,
|
|
190
|
+
"flow"
|
|
191
|
+
>),
|
|
192
|
+
flow: flowContext,
|
|
193
|
+
});
|
|
194
|
+
break;
|
|
195
|
+
case UploadEventType.UPLOAD_VALIDATION_FAILED:
|
|
196
|
+
options.onUploadValidationFailed?.({
|
|
197
|
+
...(event.data as unknown as Omit<
|
|
198
|
+
UploadValidationFailedEventData,
|
|
199
|
+
"flow"
|
|
200
|
+
>),
|
|
201
|
+
flow: flowContext,
|
|
202
|
+
});
|
|
203
|
+
break;
|
|
204
|
+
case UploadEventType.UPLOAD_VALIDATION_WARNING:
|
|
205
|
+
options.onUploadValidationWarning?.({
|
|
206
|
+
...(event.data as unknown as Omit<
|
|
207
|
+
UploadValidationWarningEventData,
|
|
208
|
+
"flow"
|
|
209
|
+
>),
|
|
210
|
+
flow: flowContext,
|
|
211
|
+
});
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return unsubscribe;
|
|
217
|
+
}, [subscribeToEvents, options]);
|
|
218
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { UploadistaEvent } from "@uploadista/client-browser";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { useUploadistaContext } from "../components/uploadista-provider";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Simple hook that subscribes to all Uploadista events (both flow and upload events).
|
|
7
|
+
*
|
|
8
|
+
* This is a low-level hook that provides access to all events. For more structured
|
|
9
|
+
* event handling, consider using `useFlowEvents` or `useUploadEvents` instead.
|
|
10
|
+
*
|
|
11
|
+
* Must be used within UploadistaProvider.
|
|
12
|
+
*
|
|
13
|
+
* @param callback - Function called for every event emitted by the Uploadista client
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* import { useUploadistaEvents, isFlowEvent, isUploadEvent } from '@uploadista/expo';
|
|
18
|
+
*
|
|
19
|
+
* function MyComponent() {
|
|
20
|
+
* useUploadistaEvents((event) => {
|
|
21
|
+
* if (isFlowEvent(event)) {
|
|
22
|
+
* console.log('Flow event:', event.eventType);
|
|
23
|
+
* } else if (isUploadEvent(event)) {
|
|
24
|
+
* console.log('Upload event:', event.type);
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* return <View><Text>Listening to all events...</Text></View>;
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function useUploadistaEvents(
|
|
33
|
+
callback: (event: UploadistaEvent) => void,
|
|
34
|
+
): void {
|
|
35
|
+
const { subscribeToEvents } = useUploadistaContext();
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const unsubscribe = subscribeToEvents(callback);
|
|
39
|
+
return unsubscribe;
|
|
40
|
+
}, [subscribeToEvents, callback]);
|
|
41
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -74,6 +74,20 @@ export {
|
|
|
74
74
|
type UseUploadistaClientReturn,
|
|
75
75
|
useUploadistaClient,
|
|
76
76
|
} from "./hooks/use-uploadista-client";
|
|
77
|
+
// Export event hooks and utilities
|
|
78
|
+
export { isFlowEvent, isUploadEvent } from "./hooks/event-utils";
|
|
79
|
+
export { useUploadistaEvents } from "./hooks/use-uploadista-events";
|
|
80
|
+
export { useFlowEvents, type UseFlowEventsOptions } from "./hooks/use-flow-events";
|
|
81
|
+
export {
|
|
82
|
+
useUploadEvents,
|
|
83
|
+
type UseUploadEventsOptions,
|
|
84
|
+
type UploadProgressEventData,
|
|
85
|
+
type UploadFileEventData,
|
|
86
|
+
type UploadFailedEventData,
|
|
87
|
+
type UploadValidationSuccessEventData,
|
|
88
|
+
type UploadValidationFailedEventData,
|
|
89
|
+
type UploadValidationWarningEventData,
|
|
90
|
+
} from "./hooks/use-upload-events";
|
|
77
91
|
// Re-export service implementations and factories
|
|
78
92
|
export {
|
|
79
93
|
createAsyncStorageService,
|