@saga-bus/nextjs 0.1.0
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/LICENSE +21 -0
- package/README.md +140 -0
- package/dist/api/index.cjs +91 -0
- package/dist/api/index.d.cts +43 -0
- package/dist/api/index.d.ts +43 -0
- package/dist/api/index.js +6 -0
- package/dist/chunk-4PDKHF26.js +65 -0
- package/dist/chunk-SYXWECVX.js +51 -0
- package/dist/client/index.cjs +130 -0
- package/dist/client/index.d.cts +93 -0
- package/dist/client/index.d.ts +93 -0
- package/dist/client/index.js +100 -0
- package/dist/createSagaBus-DvszRb3a.d.cts +78 -0
- package/dist/createSagaBus-DvszRb3a.d.ts +78 -0
- package/dist/index.cjs +143 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +14 -0
- package/dist/server/index.cjs +79 -0
- package/dist/server/index.d.cts +36 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +10 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dean Foran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# @saga-bus/nextjs
|
|
2
|
+
|
|
3
|
+
Next.js helpers for saga-bus integration.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @saga-bus/nextjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Server Setup
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// lib/saga-bus.ts
|
|
17
|
+
import { createSagaBus } from "@saga-bus/nextjs/server";
|
|
18
|
+
import { InMemoryTransport } from "@saga-bus/transport-inmemory";
|
|
19
|
+
import { InMemorySagaStore } from "@saga-bus/store-inmemory";
|
|
20
|
+
|
|
21
|
+
export const sagaBus = createSagaBus({
|
|
22
|
+
transport: new InMemoryTransport(),
|
|
23
|
+
store: new InMemorySagaStore(),
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### API Route (App Router)
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// app/api/saga/route.ts
|
|
31
|
+
import { createSagaHandler } from "@saga-bus/nextjs/api";
|
|
32
|
+
import { sagaBus } from "@/lib/saga-bus";
|
|
33
|
+
|
|
34
|
+
export const POST = createSagaHandler(sagaBus);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Server Components
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// app/orders/[id]/page.tsx
|
|
41
|
+
import { getSagaState } from "@saga-bus/nextjs/server";
|
|
42
|
+
import { sagaBus } from "@/lib/saga-bus";
|
|
43
|
+
|
|
44
|
+
export default async function OrderPage({ params }: { params: { id: string } }) {
|
|
45
|
+
const state = await getSagaState(sagaBus, "OrderSaga", params.id);
|
|
46
|
+
return <div>Status: {state?.status}</div>;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Client Components
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// components/OrderStatus.tsx
|
|
54
|
+
"use client";
|
|
55
|
+
|
|
56
|
+
import { useSagaState } from "@saga-bus/nextjs/client";
|
|
57
|
+
|
|
58
|
+
export function OrderStatus({ orderId }: { orderId: string }) {
|
|
59
|
+
const { state, isLoading, publish } = useSagaState("OrderSaga", orderId);
|
|
60
|
+
|
|
61
|
+
if (isLoading) return <div>Loading...</div>;
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div>
|
|
65
|
+
<p>Status: {state?.status}</p>
|
|
66
|
+
<button onClick={() => publish("OrderCancelled", { orderId })}>
|
|
67
|
+
Cancel
|
|
68
|
+
</button>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Provider Setup
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// app/layout.tsx
|
|
78
|
+
import { SagaBusProvider } from "@saga-bus/nextjs/client";
|
|
79
|
+
|
|
80
|
+
export default function RootLayout({ children }) {
|
|
81
|
+
return (
|
|
82
|
+
<html>
|
|
83
|
+
<body>
|
|
84
|
+
<SagaBusProvider apiEndpoint="/api/saga">
|
|
85
|
+
{children}
|
|
86
|
+
</SagaBusProvider>
|
|
87
|
+
</body>
|
|
88
|
+
</html>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Exports
|
|
94
|
+
|
|
95
|
+
### Server (`@saga-bus/nextjs/server`)
|
|
96
|
+
|
|
97
|
+
- `createSagaBus(config)` - Create saga bus instance
|
|
98
|
+
- `getSagaState(bus, sagaName, id)` - Get saga state
|
|
99
|
+
- `withSagaBus(bus, handler)` - Wrap API handler with context
|
|
100
|
+
|
|
101
|
+
### API (`@saga-bus/nextjs/api`)
|
|
102
|
+
|
|
103
|
+
- `createSagaHandler(bus)` - Create API route handler
|
|
104
|
+
|
|
105
|
+
### Client (`@saga-bus/nextjs/client`)
|
|
106
|
+
|
|
107
|
+
- `SagaBusProvider` - React context provider
|
|
108
|
+
- `useSagaBusConfig()` - Access provider config
|
|
109
|
+
- `useSagaState(sagaName, id)` - Fetch and interact with saga
|
|
110
|
+
- `usePublish()` - Publish messages
|
|
111
|
+
|
|
112
|
+
## API Actions
|
|
113
|
+
|
|
114
|
+
The API handler supports these actions:
|
|
115
|
+
|
|
116
|
+
| Action | Description |
|
|
117
|
+
|--------|-------------|
|
|
118
|
+
| `publish` | Publish a message |
|
|
119
|
+
| `getState` | Get saga state by ID |
|
|
120
|
+
| `getStateByCorrelation` | Get state by correlation ID |
|
|
121
|
+
|
|
122
|
+
## Configuration
|
|
123
|
+
|
|
124
|
+
### Server Config
|
|
125
|
+
|
|
126
|
+
| Option | Type | Default | Description |
|
|
127
|
+
|--------|------|---------|-------------|
|
|
128
|
+
| `transport` | `Transport` | required | Transport implementation |
|
|
129
|
+
| `store` | `SagaStore` | required | Saga store implementation |
|
|
130
|
+
| `defaultEndpoint` | `string` | `"saga-bus"` | Default publish endpoint |
|
|
131
|
+
|
|
132
|
+
### Client Config
|
|
133
|
+
|
|
134
|
+
| Option | Type | Default | Description |
|
|
135
|
+
|--------|------|---------|-------------|
|
|
136
|
+
| `apiEndpoint` | `string` | `"/api/saga"` | API endpoint URL |
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/api/index.ts
|
|
21
|
+
var api_exports = {};
|
|
22
|
+
__export(api_exports, {
|
|
23
|
+
createSagaHandler: () => createSagaHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(api_exports);
|
|
26
|
+
|
|
27
|
+
// src/api/createHandler.ts
|
|
28
|
+
function createSagaHandler(bus) {
|
|
29
|
+
return async (request) => {
|
|
30
|
+
try {
|
|
31
|
+
const body = await request.json();
|
|
32
|
+
switch (body.action) {
|
|
33
|
+
case "publish": {
|
|
34
|
+
if (!body.message) {
|
|
35
|
+
return Response.json(
|
|
36
|
+
{ error: "Message is required for publish action" },
|
|
37
|
+
{ status: 400 }
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const messageId = crypto.randomUUID();
|
|
41
|
+
const message = {
|
|
42
|
+
type: body.message.type,
|
|
43
|
+
...body.message.payload
|
|
44
|
+
};
|
|
45
|
+
await bus.publish(message, {
|
|
46
|
+
endpoint: "saga-bus",
|
|
47
|
+
headers: body.message.correlationId ? { "x-correlation-id": body.message.correlationId } : void 0
|
|
48
|
+
});
|
|
49
|
+
return Response.json({ success: true, messageId });
|
|
50
|
+
}
|
|
51
|
+
case "getState": {
|
|
52
|
+
if (!body.sagaName || !body.id) {
|
|
53
|
+
return Response.json(
|
|
54
|
+
{ error: "sagaName and id are required for getState action" },
|
|
55
|
+
{ status: 400 }
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const state = await bus.getState(body.sagaName, body.id);
|
|
59
|
+
return Response.json({ state });
|
|
60
|
+
}
|
|
61
|
+
case "getStateByCorrelation": {
|
|
62
|
+
if (!body.sagaName || !body.id) {
|
|
63
|
+
return Response.json(
|
|
64
|
+
{
|
|
65
|
+
error: "sagaName and id are required for getStateByCorrelation action"
|
|
66
|
+
},
|
|
67
|
+
{ status: 400 }
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const state = await bus.getStateByCorrelation(body.sagaName, body.id);
|
|
71
|
+
return Response.json({ state });
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
return Response.json(
|
|
75
|
+
{ error: `Unknown action: ${body.action}` },
|
|
76
|
+
{ status: 400 }
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error("Saga API error:", error);
|
|
81
|
+
return Response.json(
|
|
82
|
+
{ error: error instanceof Error ? error.message : "Unknown error" },
|
|
83
|
+
{ status: 500 }
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
createSagaHandler
|
|
91
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { a as SagaBus } from '../createSagaBus-DvszRb3a.cjs';
|
|
2
|
+
import '@saga-bus/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Request body for saga API endpoints.
|
|
6
|
+
*/
|
|
7
|
+
interface SagaApiRequest {
|
|
8
|
+
/**
|
|
9
|
+
* Action to perform.
|
|
10
|
+
*/
|
|
11
|
+
action: "publish" | "getState" | "getStateByCorrelation";
|
|
12
|
+
/**
|
|
13
|
+
* Saga name (required for getState actions).
|
|
14
|
+
*/
|
|
15
|
+
sagaName?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Saga or correlation ID.
|
|
18
|
+
*/
|
|
19
|
+
id?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Message to publish.
|
|
22
|
+
*/
|
|
23
|
+
message?: {
|
|
24
|
+
type: string;
|
|
25
|
+
payload?: Record<string, unknown>;
|
|
26
|
+
correlationId?: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create an API route handler for saga operations.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // app/api/saga/route.ts
|
|
35
|
+
* import { createSagaHandler } from "@saga-bus/nextjs/api";
|
|
36
|
+
* import { sagaBus } from "@/lib/saga-bus";
|
|
37
|
+
*
|
|
38
|
+
* export const POST = createSagaHandler(sagaBus);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function createSagaHandler(bus: SagaBus): (request: Request) => Promise<Response>;
|
|
42
|
+
|
|
43
|
+
export { type SagaApiRequest, createSagaHandler };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { a as SagaBus } from '../createSagaBus-DvszRb3a.js';
|
|
2
|
+
import '@saga-bus/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Request body for saga API endpoints.
|
|
6
|
+
*/
|
|
7
|
+
interface SagaApiRequest {
|
|
8
|
+
/**
|
|
9
|
+
* Action to perform.
|
|
10
|
+
*/
|
|
11
|
+
action: "publish" | "getState" | "getStateByCorrelation";
|
|
12
|
+
/**
|
|
13
|
+
* Saga name (required for getState actions).
|
|
14
|
+
*/
|
|
15
|
+
sagaName?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Saga or correlation ID.
|
|
18
|
+
*/
|
|
19
|
+
id?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Message to publish.
|
|
22
|
+
*/
|
|
23
|
+
message?: {
|
|
24
|
+
type: string;
|
|
25
|
+
payload?: Record<string, unknown>;
|
|
26
|
+
correlationId?: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create an API route handler for saga operations.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // app/api/saga/route.ts
|
|
35
|
+
* import { createSagaHandler } from "@saga-bus/nextjs/api";
|
|
36
|
+
* import { sagaBus } from "@/lib/saga-bus";
|
|
37
|
+
*
|
|
38
|
+
* export const POST = createSagaHandler(sagaBus);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function createSagaHandler(bus: SagaBus): (request: Request) => Promise<Response>;
|
|
42
|
+
|
|
43
|
+
export { type SagaApiRequest, createSagaHandler };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/api/createHandler.ts
|
|
2
|
+
function createSagaHandler(bus) {
|
|
3
|
+
return async (request) => {
|
|
4
|
+
try {
|
|
5
|
+
const body = await request.json();
|
|
6
|
+
switch (body.action) {
|
|
7
|
+
case "publish": {
|
|
8
|
+
if (!body.message) {
|
|
9
|
+
return Response.json(
|
|
10
|
+
{ error: "Message is required for publish action" },
|
|
11
|
+
{ status: 400 }
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
const messageId = crypto.randomUUID();
|
|
15
|
+
const message = {
|
|
16
|
+
type: body.message.type,
|
|
17
|
+
...body.message.payload
|
|
18
|
+
};
|
|
19
|
+
await bus.publish(message, {
|
|
20
|
+
endpoint: "saga-bus",
|
|
21
|
+
headers: body.message.correlationId ? { "x-correlation-id": body.message.correlationId } : void 0
|
|
22
|
+
});
|
|
23
|
+
return Response.json({ success: true, messageId });
|
|
24
|
+
}
|
|
25
|
+
case "getState": {
|
|
26
|
+
if (!body.sagaName || !body.id) {
|
|
27
|
+
return Response.json(
|
|
28
|
+
{ error: "sagaName and id are required for getState action" },
|
|
29
|
+
{ status: 400 }
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
const state = await bus.getState(body.sagaName, body.id);
|
|
33
|
+
return Response.json({ state });
|
|
34
|
+
}
|
|
35
|
+
case "getStateByCorrelation": {
|
|
36
|
+
if (!body.sagaName || !body.id) {
|
|
37
|
+
return Response.json(
|
|
38
|
+
{
|
|
39
|
+
error: "sagaName and id are required for getStateByCorrelation action"
|
|
40
|
+
},
|
|
41
|
+
{ status: 400 }
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const state = await bus.getStateByCorrelation(body.sagaName, body.id);
|
|
45
|
+
return Response.json({ state });
|
|
46
|
+
}
|
|
47
|
+
default:
|
|
48
|
+
return Response.json(
|
|
49
|
+
{ error: `Unknown action: ${body.action}` },
|
|
50
|
+
{ status: 400 }
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error("Saga API error:", error);
|
|
55
|
+
return Response.json(
|
|
56
|
+
{ error: error instanceof Error ? error.message : "Unknown error" },
|
|
57
|
+
{ status: 500 }
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
createSagaHandler
|
|
65
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/server/createSagaBus.ts
|
|
2
|
+
var initialized = false;
|
|
3
|
+
function createSagaBus(config) {
|
|
4
|
+
const { transport, store, defaultEndpoint = "saga-bus" } = config;
|
|
5
|
+
if (typeof globalThis !== "undefined" && !("EdgeRuntime" in globalThis) && !initialized) {
|
|
6
|
+
initialized = true;
|
|
7
|
+
transport.start().catch(console.error);
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
async publish(message, options) {
|
|
11
|
+
await transport.publish(message, {
|
|
12
|
+
endpoint: defaultEndpoint,
|
|
13
|
+
...options
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
async getState(sagaName, sagaId) {
|
|
17
|
+
return store.getById(sagaName, sagaId);
|
|
18
|
+
},
|
|
19
|
+
async getStateByCorrelation(sagaName, correlationId) {
|
|
20
|
+
return store.getByCorrelationId(sagaName, correlationId);
|
|
21
|
+
},
|
|
22
|
+
async start() {
|
|
23
|
+
await transport.start();
|
|
24
|
+
},
|
|
25
|
+
async stop() {
|
|
26
|
+
await transport.stop();
|
|
27
|
+
},
|
|
28
|
+
getTransport() {
|
|
29
|
+
return transport;
|
|
30
|
+
},
|
|
31
|
+
getStore() {
|
|
32
|
+
return store;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async function getSagaState(bus, sagaName, sagaId) {
|
|
37
|
+
return bus.getState(sagaName, sagaId);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/server/withSagaBus.ts
|
|
41
|
+
function withSagaBus(bus, handler) {
|
|
42
|
+
return async (request) => {
|
|
43
|
+
return handler(request, { sagaBus: bus });
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
createSagaBus,
|
|
49
|
+
getSagaState,
|
|
50
|
+
withSagaBus
|
|
51
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
SagaBusProvider: () => SagaBusProvider,
|
|
24
|
+
usePublish: () => usePublish,
|
|
25
|
+
useSagaBusConfig: () => useSagaBusConfig,
|
|
26
|
+
useSagaState: () => useSagaState
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(client_exports);
|
|
29
|
+
|
|
30
|
+
// src/client/SagaBusProvider.tsx
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
33
|
+
var SagaBusContext = (0, import_react.createContext)({
|
|
34
|
+
apiEndpoint: "/api/saga"
|
|
35
|
+
});
|
|
36
|
+
function SagaBusProvider({
|
|
37
|
+
children,
|
|
38
|
+
apiEndpoint = "/api/saga"
|
|
39
|
+
}) {
|
|
40
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SagaBusContext.Provider, { value: { apiEndpoint }, children });
|
|
41
|
+
}
|
|
42
|
+
function useSagaBusConfig() {
|
|
43
|
+
return (0, import_react.useContext)(SagaBusContext);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/client/useSaga.ts
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
function useSagaState(sagaName, sagaId) {
|
|
49
|
+
const { apiEndpoint } = useSagaBusConfig();
|
|
50
|
+
const [state, setState] = (0, import_react2.useState)(null);
|
|
51
|
+
const [isLoading, setIsLoading] = (0, import_react2.useState)(true);
|
|
52
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
53
|
+
const refresh = (0, import_react2.useCallback)(async () => {
|
|
54
|
+
setIsLoading(true);
|
|
55
|
+
setError(null);
|
|
56
|
+
try {
|
|
57
|
+
const response = await fetch(apiEndpoint, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: { "Content-Type": "application/json" },
|
|
60
|
+
body: JSON.stringify({
|
|
61
|
+
action: "getState",
|
|
62
|
+
sagaName,
|
|
63
|
+
id: sagaId
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw new Error(`Failed to fetch saga state: ${response.statusText}`);
|
|
68
|
+
}
|
|
69
|
+
const data = await response.json();
|
|
70
|
+
setState(data.state);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
setError(err instanceof Error ? err : new Error("Unknown error"));
|
|
73
|
+
} finally {
|
|
74
|
+
setIsLoading(false);
|
|
75
|
+
}
|
|
76
|
+
}, [apiEndpoint, sagaName, sagaId]);
|
|
77
|
+
const publish = (0, import_react2.useCallback)(
|
|
78
|
+
async (type, payload) => {
|
|
79
|
+
const response = await fetch(apiEndpoint, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: { "Content-Type": "application/json" },
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
action: "publish",
|
|
84
|
+
message: {
|
|
85
|
+
type,
|
|
86
|
+
payload,
|
|
87
|
+
correlationId: sagaId
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
});
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new Error(`Failed to publish message: ${response.statusText}`);
|
|
93
|
+
}
|
|
94
|
+
await refresh();
|
|
95
|
+
},
|
|
96
|
+
[apiEndpoint, sagaId, refresh]
|
|
97
|
+
);
|
|
98
|
+
(0, import_react2.useEffect)(() => {
|
|
99
|
+
void refresh();
|
|
100
|
+
}, [refresh]);
|
|
101
|
+
return { state, isLoading, error, refresh, publish };
|
|
102
|
+
}
|
|
103
|
+
function usePublish() {
|
|
104
|
+
const { apiEndpoint } = useSagaBusConfig();
|
|
105
|
+
return (0, import_react2.useCallback)(
|
|
106
|
+
async (type, payload, correlationId) => {
|
|
107
|
+
const response = await fetch(apiEndpoint, {
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: { "Content-Type": "application/json" },
|
|
110
|
+
body: JSON.stringify({
|
|
111
|
+
action: "publish",
|
|
112
|
+
message: { type, payload, correlationId }
|
|
113
|
+
})
|
|
114
|
+
});
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
throw new Error(`Failed to publish message: ${response.statusText}`);
|
|
117
|
+
}
|
|
118
|
+
const data = await response.json();
|
|
119
|
+
return data.messageId;
|
|
120
|
+
},
|
|
121
|
+
[apiEndpoint]
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
SagaBusProvider,
|
|
127
|
+
usePublish,
|
|
128
|
+
useSagaBusConfig,
|
|
129
|
+
useSagaState
|
|
130
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { SagaState } from '@saga-bus/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Client-side saga bus configuration.
|
|
6
|
+
*/
|
|
7
|
+
interface SagaBusClientConfig {
|
|
8
|
+
/**
|
|
9
|
+
* API endpoint for saga operations.
|
|
10
|
+
* @default "/api/saga"
|
|
11
|
+
*/
|
|
12
|
+
apiEndpoint: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Provider for saga bus client configuration.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* // app/layout.tsx
|
|
20
|
+
* import { SagaBusProvider } from "@saga-bus/nextjs/client";
|
|
21
|
+
*
|
|
22
|
+
* export default function RootLayout({ children }) {
|
|
23
|
+
* return (
|
|
24
|
+
* <html>
|
|
25
|
+
* <body>
|
|
26
|
+
* <SagaBusProvider apiEndpoint="/api/saga">
|
|
27
|
+
* {children}
|
|
28
|
+
* </SagaBusProvider>
|
|
29
|
+
* </body>
|
|
30
|
+
* </html>
|
|
31
|
+
* );
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function SagaBusProvider({ children, apiEndpoint, }: {
|
|
36
|
+
children: ReactNode;
|
|
37
|
+
apiEndpoint?: string;
|
|
38
|
+
}): ReactNode;
|
|
39
|
+
declare function useSagaBusConfig(): SagaBusClientConfig;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Hook result for saga state.
|
|
43
|
+
*/
|
|
44
|
+
interface UseSagaStateResult<T extends SagaState> {
|
|
45
|
+
/**
|
|
46
|
+
* Current saga state.
|
|
47
|
+
*/
|
|
48
|
+
state: T | null;
|
|
49
|
+
/**
|
|
50
|
+
* Whether the state is loading.
|
|
51
|
+
*/
|
|
52
|
+
isLoading: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Error if fetch failed.
|
|
55
|
+
*/
|
|
56
|
+
error: Error | null;
|
|
57
|
+
/**
|
|
58
|
+
* Refresh the saga state.
|
|
59
|
+
*/
|
|
60
|
+
refresh: () => Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Publish a message to the saga.
|
|
63
|
+
*/
|
|
64
|
+
publish: (type: string, payload: unknown) => Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Hook for interacting with saga state from client components.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* function OrderStatus({ orderId }: { orderId: string }) {
|
|
72
|
+
* const { state, isLoading, publish } = useSagaState<OrderState>("OrderSaga", orderId);
|
|
73
|
+
*
|
|
74
|
+
* if (isLoading) return <div>Loading...</div>;
|
|
75
|
+
*
|
|
76
|
+
* return (
|
|
77
|
+
* <div>
|
|
78
|
+
* <p>Status: {state?.status}</p>
|
|
79
|
+
* <button onClick={() => publish("OrderCancelled", { orderId })}>
|
|
80
|
+
* Cancel
|
|
81
|
+
* </button>
|
|
82
|
+
* </div>
|
|
83
|
+
* );
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function useSagaState<T extends SagaState>(sagaName: string, sagaId: string): UseSagaStateResult<T>;
|
|
88
|
+
/**
|
|
89
|
+
* Hook for publishing messages without fetching state.
|
|
90
|
+
*/
|
|
91
|
+
declare function usePublish(): (type: string, payload: unknown, correlationId?: string) => Promise<string>;
|
|
92
|
+
|
|
93
|
+
export { type SagaBusClientConfig, SagaBusProvider, type UseSagaStateResult, usePublish, useSagaBusConfig, useSagaState };
|