Commit 243e885
authored
fix(medusa): API workflow subscription (#15134)
## Summary
**What** —
1) Change req.query to req.params in appropriate api routes
+ move /api/admin/workflows-executions/[workflow_id]/[transaction_id]/[step_id]/subscribe/route.ts
to /api/admin/workflows-executions/[workflow_id]/[transaction_id]/subscribe/route.ts, because it is not possible to subscribe to individual steps, only to individual workflows.
+ modify API reference in documentation to mirror changes
2) Fix SSE Streaming in JS-SDK
**Why** —
1) Current implementation relies on **req.query** to get the id of workflow/transaction, but it should get them from **req.params** instead. It is also impossible to subscribe to specific steps.
2) When setting up SSE streams with JS-SDK, if an event has yet to be streamed, it is impossible to abort, because nothing has returned from the JS-SDK yet.
**How** —
1) Change req.query to req.params
2) fetchStream now always returns (non-null generator), so that it is possible to abort before receiving any events
**Testing** —
https://docs.medusajs.com/api/admin#workflows-executions_getworkflowsexecutionsworkflow_idsubscribe
---
## Examples
https://docs.medusajs.com/resources/js-sdk#stream-server-sent-events
```ts
const StreamTestPage = () => {
const [messages, setMessages] = useState<string[]>([])
const [isStreaming, setIsStreaming] = useState(false)
const [abortStream, setAbortStream] = useState<(() => void) | null>(null)
const startStream = async () => {
setIsStreaming(true)
setMessages([])
const { stream, abort } = await sdk.client.fetchStream("/admin/stream")
// Store the abort function for the abort button
setAbortStream(() => abort)
try {
for await (const chunk of stream) {
// Since the server sends plain text, convert to string
const message = typeof chunk === "string" ? chunk : (chunk.data || String(chunk))
setMessages((prev) => [...prev, message.trim()])
}
} catch (error) {
// Don't log abort errors as they're expected when user clicks abort
if (error instanceof Error && error.name !== "AbortError") {
console.error("Stream error:", error)
if (error.name === "HttpError") {
abort()
}
}
} finally {
setIsStreaming(false)
setAbortStream(null)
}
}
const handleAbort = () => {
if (abortStream) {
abortStream()
setIsStreaming(false)
setAbortStream(null)
}
}
return (
<Container className="p-6">
<Heading level="h1" className="mb-6">
fetchStream Example
</Heading>
<div className="space-y-4">
<div className="flex gap-2">
<Button
onClick={startStream}
disabled={isStreaming}
variant="primary"
>
{isStreaming ? "Streaming..." : "Start Stream"}
</Button>
<Button
onClick={handleAbort}
disabled={!isStreaming}
variant="secondary"
>
Abort Stream
</Button>
</div>
<div className="border rounded p-4 h-64 overflow-y-auto bg-ui-bg-subtle">
{messages.length === 0 ? (
<Text className="text-ui-fg-muted">No messages yet...</Text>
) : (
messages.map((msg, index) => (
<div key={index} className="mb-2 text-sm">
{msg}
</div>
))
)}
</div>
</div>
</Container>
)
}
```
---
## Checklist
Please ensure the following before requesting a review:
- [ X] I have added a **changeset** for this PR
- Every non-breaking change should be marked as a **patch**
- To add a changeset, run `yarn changeset` and follow the prompts
- [ X] The changes are covered by relevant **tests**
- [ X] I have verified the code works as intended locally
- [ X] I have linked the related issue(s) if applicable
---
## Additional Context
This is a new PR based on [13886](#13886).
Unfortunately I synced the fork and deleted my commits, which then automatically closed the PR.
Closes #15135
Closes #151361 parent 10bd6eb commit 243e885
6 files changed
Lines changed: 71 additions & 91 deletions
File tree
- .changeset
- integration-tests/http/__tests__/workflow-engine/admin
- packages
- core/js-sdk/src
- medusa/src/api/admin/workflows-executions/[workflow_id]
- [transaction_id]/subscribe
- subscribe
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 48 additions & 83 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
| 107 | + | |
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| |||
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
115 | | - | |
| 116 | + | |
| 117 | + | |
116 | 118 | | |
117 | 119 | | |
118 | 120 | | |
| |||
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
165 | 155 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
173 | 165 | | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
183 | 173 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
| 174 | + | |
188 | 175 | | |
189 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
190 | 180 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
201 | 185 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
| 186 | + | |
222 | 187 | | |
223 | | - | |
224 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
225 | 195 | | |
| 196 | + | |
226 | 197 | | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
| 198 | + | |
| 199 | + | |
235 | 200 | | |
236 | 201 | | |
237 | 202 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
| 197 | + | |
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
203 | | - | |
204 | | - | |
205 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
208 | 217 | | |
209 | 218 | | |
210 | 219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
0 commit comments