@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
|
@@ -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 };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/client/SagaBusProvider.tsx
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var SagaBusContext = createContext({
|
|
5
|
+
apiEndpoint: "/api/saga"
|
|
6
|
+
});
|
|
7
|
+
function SagaBusProvider({
|
|
8
|
+
children,
|
|
9
|
+
apiEndpoint = "/api/saga"
|
|
10
|
+
}) {
|
|
11
|
+
return /* @__PURE__ */ jsx(SagaBusContext.Provider, { value: { apiEndpoint }, children });
|
|
12
|
+
}
|
|
13
|
+
function useSagaBusConfig() {
|
|
14
|
+
return useContext(SagaBusContext);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/client/useSaga.ts
|
|
18
|
+
import { useState, useCallback, useEffect } from "react";
|
|
19
|
+
function useSagaState(sagaName, sagaId) {
|
|
20
|
+
const { apiEndpoint } = useSagaBusConfig();
|
|
21
|
+
const [state, setState] = useState(null);
|
|
22
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
23
|
+
const [error, setError] = useState(null);
|
|
24
|
+
const refresh = useCallback(async () => {
|
|
25
|
+
setIsLoading(true);
|
|
26
|
+
setError(null);
|
|
27
|
+
try {
|
|
28
|
+
const response = await fetch(apiEndpoint, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: { "Content-Type": "application/json" },
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
action: "getState",
|
|
33
|
+
sagaName,
|
|
34
|
+
id: sagaId
|
|
35
|
+
})
|
|
36
|
+
});
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
throw new Error(`Failed to fetch saga state: ${response.statusText}`);
|
|
39
|
+
}
|
|
40
|
+
const data = await response.json();
|
|
41
|
+
setState(data.state);
|
|
42
|
+
} catch (err) {
|
|
43
|
+
setError(err instanceof Error ? err : new Error("Unknown error"));
|
|
44
|
+
} finally {
|
|
45
|
+
setIsLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [apiEndpoint, sagaName, sagaId]);
|
|
48
|
+
const publish = useCallback(
|
|
49
|
+
async (type, payload) => {
|
|
50
|
+
const response = await fetch(apiEndpoint, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: { "Content-Type": "application/json" },
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
action: "publish",
|
|
55
|
+
message: {
|
|
56
|
+
type,
|
|
57
|
+
payload,
|
|
58
|
+
correlationId: sagaId
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(`Failed to publish message: ${response.statusText}`);
|
|
64
|
+
}
|
|
65
|
+
await refresh();
|
|
66
|
+
},
|
|
67
|
+
[apiEndpoint, sagaId, refresh]
|
|
68
|
+
);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
void refresh();
|
|
71
|
+
}, [refresh]);
|
|
72
|
+
return { state, isLoading, error, refresh, publish };
|
|
73
|
+
}
|
|
74
|
+
function usePublish() {
|
|
75
|
+
const { apiEndpoint } = useSagaBusConfig();
|
|
76
|
+
return useCallback(
|
|
77
|
+
async (type, payload, correlationId) => {
|
|
78
|
+
const response = await fetch(apiEndpoint, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: { "Content-Type": "application/json" },
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
action: "publish",
|
|
83
|
+
message: { type, payload, correlationId }
|
|
84
|
+
})
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
throw new Error(`Failed to publish message: ${response.statusText}`);
|
|
88
|
+
}
|
|
89
|
+
const data = await response.json();
|
|
90
|
+
return data.messageId;
|
|
91
|
+
},
|
|
92
|
+
[apiEndpoint]
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
export {
|
|
96
|
+
SagaBusProvider,
|
|
97
|
+
usePublish,
|
|
98
|
+
useSagaBusConfig,
|
|
99
|
+
useSagaState
|
|
100
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Transport, SagaStore, SagaState, SagaMiddleware, BaseMessage, TransportPublishOptions } from '@saga-bus/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Saga bus configuration options.
|
|
5
|
+
*/
|
|
6
|
+
interface SagaBusConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Transport implementation.
|
|
9
|
+
*/
|
|
10
|
+
transport: Transport;
|
|
11
|
+
/**
|
|
12
|
+
* Saga store implementation.
|
|
13
|
+
*/
|
|
14
|
+
store: SagaStore<SagaState>;
|
|
15
|
+
/**
|
|
16
|
+
* Middleware pipeline.
|
|
17
|
+
*/
|
|
18
|
+
middleware?: SagaMiddleware[];
|
|
19
|
+
/**
|
|
20
|
+
* Default endpoint for publishing messages.
|
|
21
|
+
* @default "saga-bus"
|
|
22
|
+
*/
|
|
23
|
+
defaultEndpoint?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Saga bus instance for Next.js.
|
|
27
|
+
*/
|
|
28
|
+
interface SagaBus {
|
|
29
|
+
/**
|
|
30
|
+
* Publish a message.
|
|
31
|
+
*/
|
|
32
|
+
publish<TMessage extends BaseMessage>(message: TMessage, options?: Partial<TransportPublishOptions>): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Get saga state by ID.
|
|
35
|
+
*/
|
|
36
|
+
getState<T extends SagaState>(sagaName: string, sagaId: string): Promise<T | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Get saga state by correlation ID.
|
|
39
|
+
*/
|
|
40
|
+
getStateByCorrelation<T extends SagaState>(sagaName: string, correlationId: string): Promise<T | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Start the transport.
|
|
43
|
+
*/
|
|
44
|
+
start(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Stop the transport.
|
|
47
|
+
*/
|
|
48
|
+
stop(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Get the underlying transport.
|
|
51
|
+
*/
|
|
52
|
+
getTransport(): Transport;
|
|
53
|
+
/**
|
|
54
|
+
* Get the underlying store.
|
|
55
|
+
*/
|
|
56
|
+
getStore(): SagaStore<SagaState>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a saga bus instance for Next.js.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // lib/saga-bus.ts
|
|
64
|
+
* import { createSagaBus } from "@saga-bus/nextjs/server";
|
|
65
|
+
*
|
|
66
|
+
* export const sagaBus = createSagaBus({
|
|
67
|
+
* transport: new InMemoryTransport(),
|
|
68
|
+
* store: new InMemorySagaStore(),
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function createSagaBus(config: SagaBusConfig): SagaBus;
|
|
73
|
+
/**
|
|
74
|
+
* Get saga state helper for server components.
|
|
75
|
+
*/
|
|
76
|
+
declare function getSagaState<T extends SagaState>(bus: SagaBus, sagaName: string, sagaId: string): Promise<T | null>;
|
|
77
|
+
|
|
78
|
+
export { type SagaBusConfig as S, type SagaBus as a, createSagaBus as c, getSagaState as g };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Transport, SagaStore, SagaState, SagaMiddleware, BaseMessage, TransportPublishOptions } from '@saga-bus/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Saga bus configuration options.
|
|
5
|
+
*/
|
|
6
|
+
interface SagaBusConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Transport implementation.
|
|
9
|
+
*/
|
|
10
|
+
transport: Transport;
|
|
11
|
+
/**
|
|
12
|
+
* Saga store implementation.
|
|
13
|
+
*/
|
|
14
|
+
store: SagaStore<SagaState>;
|
|
15
|
+
/**
|
|
16
|
+
* Middleware pipeline.
|
|
17
|
+
*/
|
|
18
|
+
middleware?: SagaMiddleware[];
|
|
19
|
+
/**
|
|
20
|
+
* Default endpoint for publishing messages.
|
|
21
|
+
* @default "saga-bus"
|
|
22
|
+
*/
|
|
23
|
+
defaultEndpoint?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Saga bus instance for Next.js.
|
|
27
|
+
*/
|
|
28
|
+
interface SagaBus {
|
|
29
|
+
/**
|
|
30
|
+
* Publish a message.
|
|
31
|
+
*/
|
|
32
|
+
publish<TMessage extends BaseMessage>(message: TMessage, options?: Partial<TransportPublishOptions>): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Get saga state by ID.
|
|
35
|
+
*/
|
|
36
|
+
getState<T extends SagaState>(sagaName: string, sagaId: string): Promise<T | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Get saga state by correlation ID.
|
|
39
|
+
*/
|
|
40
|
+
getStateByCorrelation<T extends SagaState>(sagaName: string, correlationId: string): Promise<T | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Start the transport.
|
|
43
|
+
*/
|
|
44
|
+
start(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Stop the transport.
|
|
47
|
+
*/
|
|
48
|
+
stop(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Get the underlying transport.
|
|
51
|
+
*/
|
|
52
|
+
getTransport(): Transport;
|
|
53
|
+
/**
|
|
54
|
+
* Get the underlying store.
|
|
55
|
+
*/
|
|
56
|
+
getStore(): SagaStore<SagaState>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a saga bus instance for Next.js.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // lib/saga-bus.ts
|
|
64
|
+
* import { createSagaBus } from "@saga-bus/nextjs/server";
|
|
65
|
+
*
|
|
66
|
+
* export const sagaBus = createSagaBus({
|
|
67
|
+
* transport: new InMemoryTransport(),
|
|
68
|
+
* store: new InMemorySagaStore(),
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
declare function createSagaBus(config: SagaBusConfig): SagaBus;
|
|
73
|
+
/**
|
|
74
|
+
* Get saga state helper for server components.
|
|
75
|
+
*/
|
|
76
|
+
declare function getSagaState<T extends SagaState>(bus: SagaBus, sagaName: string, sagaId: string): Promise<T | null>;
|
|
77
|
+
|
|
78
|
+
export { type SagaBusConfig as S, type SagaBus as a, createSagaBus as c, getSagaState as g };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
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/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createSagaBus: () => createSagaBus,
|
|
24
|
+
createSagaHandler: () => createSagaHandler,
|
|
25
|
+
getSagaState: () => getSagaState,
|
|
26
|
+
withSagaBus: () => withSagaBus
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/server/createSagaBus.ts
|
|
31
|
+
var initialized = false;
|
|
32
|
+
function createSagaBus(config) {
|
|
33
|
+
const { transport, store, defaultEndpoint = "saga-bus" } = config;
|
|
34
|
+
if (typeof globalThis !== "undefined" && !("EdgeRuntime" in globalThis) && !initialized) {
|
|
35
|
+
initialized = true;
|
|
36
|
+
transport.start().catch(console.error);
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
async publish(message, options) {
|
|
40
|
+
await transport.publish(message, {
|
|
41
|
+
endpoint: defaultEndpoint,
|
|
42
|
+
...options
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
async getState(sagaName, sagaId) {
|
|
46
|
+
return store.getById(sagaName, sagaId);
|
|
47
|
+
},
|
|
48
|
+
async getStateByCorrelation(sagaName, correlationId) {
|
|
49
|
+
return store.getByCorrelationId(sagaName, correlationId);
|
|
50
|
+
},
|
|
51
|
+
async start() {
|
|
52
|
+
await transport.start();
|
|
53
|
+
},
|
|
54
|
+
async stop() {
|
|
55
|
+
await transport.stop();
|
|
56
|
+
},
|
|
57
|
+
getTransport() {
|
|
58
|
+
return transport;
|
|
59
|
+
},
|
|
60
|
+
getStore() {
|
|
61
|
+
return store;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function getSagaState(bus, sagaName, sagaId) {
|
|
66
|
+
return bus.getState(sagaName, sagaId);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/server/withSagaBus.ts
|
|
70
|
+
function withSagaBus(bus, handler) {
|
|
71
|
+
return async (request) => {
|
|
72
|
+
return handler(request, { sagaBus: bus });
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/api/createHandler.ts
|
|
77
|
+
function createSagaHandler(bus) {
|
|
78
|
+
return async (request) => {
|
|
79
|
+
try {
|
|
80
|
+
const body = await request.json();
|
|
81
|
+
switch (body.action) {
|
|
82
|
+
case "publish": {
|
|
83
|
+
if (!body.message) {
|
|
84
|
+
return Response.json(
|
|
85
|
+
{ error: "Message is required for publish action" },
|
|
86
|
+
{ status: 400 }
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const messageId = crypto.randomUUID();
|
|
90
|
+
const message = {
|
|
91
|
+
type: body.message.type,
|
|
92
|
+
...body.message.payload
|
|
93
|
+
};
|
|
94
|
+
await bus.publish(message, {
|
|
95
|
+
endpoint: "saga-bus",
|
|
96
|
+
headers: body.message.correlationId ? { "x-correlation-id": body.message.correlationId } : void 0
|
|
97
|
+
});
|
|
98
|
+
return Response.json({ success: true, messageId });
|
|
99
|
+
}
|
|
100
|
+
case "getState": {
|
|
101
|
+
if (!body.sagaName || !body.id) {
|
|
102
|
+
return Response.json(
|
|
103
|
+
{ error: "sagaName and id are required for getState action" },
|
|
104
|
+
{ status: 400 }
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const state = await bus.getState(body.sagaName, body.id);
|
|
108
|
+
return Response.json({ state });
|
|
109
|
+
}
|
|
110
|
+
case "getStateByCorrelation": {
|
|
111
|
+
if (!body.sagaName || !body.id) {
|
|
112
|
+
return Response.json(
|
|
113
|
+
{
|
|
114
|
+
error: "sagaName and id are required for getStateByCorrelation action"
|
|
115
|
+
},
|
|
116
|
+
{ status: 400 }
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const state = await bus.getStateByCorrelation(body.sagaName, body.id);
|
|
120
|
+
return Response.json({ state });
|
|
121
|
+
}
|
|
122
|
+
default:
|
|
123
|
+
return Response.json(
|
|
124
|
+
{ error: `Unknown action: ${body.action}` },
|
|
125
|
+
{ status: 400 }
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error("Saga API error:", error);
|
|
130
|
+
return Response.json(
|
|
131
|
+
{ error: error instanceof Error ? error.message : "Unknown error" },
|
|
132
|
+
{ status: 500 }
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
139
|
+
createSagaBus,
|
|
140
|
+
createSagaHandler,
|
|
141
|
+
getSagaState,
|
|
142
|
+
withSagaBus
|
|
143
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { a as SagaBus, S as SagaBusConfig, c as createSagaBus, g as getSagaState } from './createSagaBus-DvszRb3a.cjs';
|
|
2
|
+
export { SagaBusContext, SagaBusHandler, withSagaBus } from './server/index.cjs';
|
|
3
|
+
export { SagaApiRequest, createSagaHandler } from './api/index.cjs';
|
|
4
|
+
import '@saga-bus/core';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { a as SagaBus, S as SagaBusConfig, c as createSagaBus, g as getSagaState } from './createSagaBus-DvszRb3a.js';
|
|
2
|
+
export { SagaBusContext, SagaBusHandler, withSagaBus } from './server/index.js';
|
|
3
|
+
export { SagaApiRequest, createSagaHandler } from './api/index.js';
|
|
4
|
+
import '@saga-bus/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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/server/index.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
createSagaBus: () => createSagaBus,
|
|
24
|
+
getSagaState: () => getSagaState,
|
|
25
|
+
withSagaBus: () => withSagaBus
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(server_exports);
|
|
28
|
+
|
|
29
|
+
// src/server/createSagaBus.ts
|
|
30
|
+
var initialized = false;
|
|
31
|
+
function createSagaBus(config) {
|
|
32
|
+
const { transport, store, defaultEndpoint = "saga-bus" } = config;
|
|
33
|
+
if (typeof globalThis !== "undefined" && !("EdgeRuntime" in globalThis) && !initialized) {
|
|
34
|
+
initialized = true;
|
|
35
|
+
transport.start().catch(console.error);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
async publish(message, options) {
|
|
39
|
+
await transport.publish(message, {
|
|
40
|
+
endpoint: defaultEndpoint,
|
|
41
|
+
...options
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
async getState(sagaName, sagaId) {
|
|
45
|
+
return store.getById(sagaName, sagaId);
|
|
46
|
+
},
|
|
47
|
+
async getStateByCorrelation(sagaName, correlationId) {
|
|
48
|
+
return store.getByCorrelationId(sagaName, correlationId);
|
|
49
|
+
},
|
|
50
|
+
async start() {
|
|
51
|
+
await transport.start();
|
|
52
|
+
},
|
|
53
|
+
async stop() {
|
|
54
|
+
await transport.stop();
|
|
55
|
+
},
|
|
56
|
+
getTransport() {
|
|
57
|
+
return transport;
|
|
58
|
+
},
|
|
59
|
+
getStore() {
|
|
60
|
+
return store;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
async function getSagaState(bus, sagaName, sagaId) {
|
|
65
|
+
return bus.getState(sagaName, sagaId);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/server/withSagaBus.ts
|
|
69
|
+
function withSagaBus(bus, handler) {
|
|
70
|
+
return async (request) => {
|
|
71
|
+
return handler(request, { sagaBus: bus });
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
75
|
+
0 && (module.exports = {
|
|
76
|
+
createSagaBus,
|
|
77
|
+
getSagaState,
|
|
78
|
+
withSagaBus
|
|
79
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { a as SagaBus } from '../createSagaBus-DvszRb3a.cjs';
|
|
2
|
+
export { S as SagaBusConfig, c as createSagaBus, g as getSagaState } from '../createSagaBus-DvszRb3a.cjs';
|
|
3
|
+
import '@saga-bus/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Context provided to saga-enabled API handlers.
|
|
7
|
+
*/
|
|
8
|
+
interface SagaBusContext {
|
|
9
|
+
sagaBus: SagaBus;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Handler function with saga bus context.
|
|
13
|
+
*/
|
|
14
|
+
type SagaBusHandler = (request: Request, context: SagaBusContext) => Promise<Response>;
|
|
15
|
+
/**
|
|
16
|
+
* Wrap an API handler with saga bus context.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // app/api/orders/route.ts
|
|
21
|
+
* import { withSagaBus } from "@saga-bus/nextjs/server";
|
|
22
|
+
* import { sagaBus } from "@/lib/saga-bus";
|
|
23
|
+
*
|
|
24
|
+
* export const POST = withSagaBus(sagaBus, async (req, { sagaBus }) => {
|
|
25
|
+
* const body = await req.json();
|
|
26
|
+
* await sagaBus.publish({
|
|
27
|
+
* type: "OrderCreated",
|
|
28
|
+
* ...body,
|
|
29
|
+
* });
|
|
30
|
+
* return new Response("OK");
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function withSagaBus(bus: SagaBus, handler: SagaBusHandler): (request: Request) => Promise<Response>;
|
|
35
|
+
|
|
36
|
+
export { SagaBus, type SagaBusContext, type SagaBusHandler, withSagaBus };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { a as SagaBus } from '../createSagaBus-DvszRb3a.js';
|
|
2
|
+
export { S as SagaBusConfig, c as createSagaBus, g as getSagaState } from '../createSagaBus-DvszRb3a.js';
|
|
3
|
+
import '@saga-bus/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Context provided to saga-enabled API handlers.
|
|
7
|
+
*/
|
|
8
|
+
interface SagaBusContext {
|
|
9
|
+
sagaBus: SagaBus;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Handler function with saga bus context.
|
|
13
|
+
*/
|
|
14
|
+
type SagaBusHandler = (request: Request, context: SagaBusContext) => Promise<Response>;
|
|
15
|
+
/**
|
|
16
|
+
* Wrap an API handler with saga bus context.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // app/api/orders/route.ts
|
|
21
|
+
* import { withSagaBus } from "@saga-bus/nextjs/server";
|
|
22
|
+
* import { sagaBus } from "@/lib/saga-bus";
|
|
23
|
+
*
|
|
24
|
+
* export const POST = withSagaBus(sagaBus, async (req, { sagaBus }) => {
|
|
25
|
+
* const body = await req.json();
|
|
26
|
+
* await sagaBus.publish({
|
|
27
|
+
* type: "OrderCreated",
|
|
28
|
+
* ...body,
|
|
29
|
+
* });
|
|
30
|
+
* return new Response("OK");
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function withSagaBus(bus: SagaBus, handler: SagaBusHandler): (request: Request) => Promise<Response>;
|
|
35
|
+
|
|
36
|
+
export { SagaBus, type SagaBusContext, type SagaBusHandler, withSagaBus };
|