@celerity-sdk/topic 0.3.0 → 0.4.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/dist/index.d.cts CHANGED
@@ -1,2 +1,215 @@
1
+ import { Closeable, CelerityTracer, ServiceContainer, CelerityLayer, BaseHandlerContext } from '@celerity-sdk/types';
1
2
 
2
- export { }
3
+ declare const TopicClient: unique symbol;
4
+ /**
5
+ * A topic client abstraction for pub/sub topics. Provides access to named
6
+ * topics, each representing a logical destination for published messages.
7
+ */
8
+ interface TopicClient extends Closeable {
9
+ /**
10
+ * Retrieves a topic instance by its name. The returned topic is a lightweight
11
+ * handle — no network calls are made until an operation is invoked.
12
+ *
13
+ * @param name The topic identifier (SNS topic ARN, Redis channel name, etc.).
14
+ */
15
+ topic(name: string): Topic$1;
16
+ }
17
+ /**
18
+ * A topic represents a logical pub/sub destination. It provides methods for
19
+ * publishing messages individually or in batches. The Celerity runtime handles
20
+ * all subscription and consumption — this interface is producer-only.
21
+ */
22
+ interface Topic$1 {
23
+ /**
24
+ * Publish a single message to the topic. The body is serialized to JSON.
25
+ *
26
+ * @param body The message body (serialized to JSON string for transport).
27
+ * @param options Optional publish parameters such as FIFO ordering or subject.
28
+ * @returns A promise that resolves to the publish result with the provider-assigned message ID.
29
+ */
30
+ publish<T = Record<string, unknown>>(body: T, options?: PublishOptions): Promise<PublishResult>;
31
+ /**
32
+ * Publish multiple messages in a single batch. The provider may impose batch
33
+ * size limits (e.g. SNS limits to 10 per request) — the implementation
34
+ * handles chunking transparently.
35
+ *
36
+ * @param entries The batch of messages to publish, each with a caller-assigned ID for correlation.
37
+ * @returns A promise that resolves to the batch result with successful and failed entries.
38
+ */
39
+ publishBatch<T = Record<string, unknown>>(entries: BatchPublishEntry<T>[]): Promise<BatchPublishResult>;
40
+ }
41
+ /**
42
+ * Options for the publish and publishBatch operations.
43
+ */
44
+ type PublishOptions = {
45
+ /** Message group ID for FIFO topics (SNS: MessageGroupId, Pub/Sub: ordering key). */
46
+ groupId?: string;
47
+ /** Deduplication ID for FIFO topics. */
48
+ deduplicationId?: string;
49
+ /** Message subject (SNS: Subject). */
50
+ subject?: string;
51
+ /** String key-value message attributes (metadata sent alongside the body). */
52
+ attributes?: Record<string, string>;
53
+ };
54
+ /**
55
+ * The result of a single publish call.
56
+ */
57
+ type PublishResult = {
58
+ /** The provider-assigned message identifier. */
59
+ messageId: string;
60
+ };
61
+ /**
62
+ * A single entry in a batch publish request.
63
+ */
64
+ type BatchPublishEntry<T = Record<string, unknown>> = {
65
+ /** Caller-assigned ID for correlating results within the batch. */
66
+ id: string;
67
+ /** The message body (serialized to JSON). */
68
+ body: T;
69
+ /** Per-message publish options. */
70
+ options?: PublishOptions;
71
+ };
72
+ /**
73
+ * The result of a batch publish operation.
74
+ */
75
+ type BatchPublishResult = {
76
+ /** Entries that were published successfully. */
77
+ successful: BatchPublishSuccess[];
78
+ /** Entries that failed. */
79
+ failed: BatchPublishFailure[];
80
+ };
81
+ /**
82
+ * A successfully published entry from a batch operation.
83
+ */
84
+ type BatchPublishSuccess = {
85
+ /** The caller-assigned entry ID. */
86
+ id: string;
87
+ /** The provider-assigned message ID. */
88
+ messageId: string;
89
+ };
90
+ /**
91
+ * A failed entry from a batch operation.
92
+ */
93
+ type BatchPublishFailure = {
94
+ /** The caller-assigned entry ID. */
95
+ id: string;
96
+ /** Provider error code. */
97
+ code: string;
98
+ /** Human-readable error message. */
99
+ message: string;
100
+ };
101
+
102
+ type SNSTopicConfig = {
103
+ /** AWS region (e.g. "us-east-1"). */
104
+ region?: string;
105
+ /** Override endpoint URL for LocalStack or other SNS-compatible services. */
106
+ endpoint?: string;
107
+ /** Explicit AWS credentials. When omitted, the SDK's default credential chain is used. */
108
+ credentials?: {
109
+ accessKeyId: string;
110
+ secretAccessKey: string;
111
+ };
112
+ };
113
+
114
+ declare class SNSTopicClient implements TopicClient {
115
+ private readonly tracer?;
116
+ private client;
117
+ private readonly config;
118
+ constructor(config?: SNSTopicConfig, tracer?: CelerityTracer | undefined);
119
+ topic(name: string): Topic$1;
120
+ close(): void;
121
+ private getClient;
122
+ }
123
+
124
+ type RedisTopicConfig = {
125
+ /** Redis/Valkey connection URL. Defaults to "redis://localhost:6379". */
126
+ url?: string;
127
+ };
128
+
129
+ declare class RedisTopicClient implements TopicClient {
130
+ private readonly tracer?;
131
+ private client;
132
+ private readonly config;
133
+ constructor(config?: RedisTopicConfig, tracer?: CelerityTracer | undefined);
134
+ topic(name: string): Topic$1;
135
+ close(): Promise<void>;
136
+ private getClient;
137
+ }
138
+
139
+ type CreateTopicClientOptions = {
140
+ /** Override provider selection. If omitted, derived from platform config. */
141
+ provider?: "aws" | "local" | "gcp" | "azure";
142
+ /** SNS-specific configuration overrides. */
143
+ aws?: SNSTopicConfig;
144
+ /** Redis-specific configuration overrides (local environment). */
145
+ local?: RedisTopicConfig;
146
+ /** Optional tracer for Celerity-level span instrumentation. */
147
+ tracer?: CelerityTracer;
148
+ };
149
+ declare function createTopicClient(options?: CreateTopicClientOptions): TopicClient;
150
+
151
+ declare function topicToken(resourceName: string): symbol;
152
+ declare const DEFAULT_TOPIC_TOKEN: unique symbol;
153
+ interface Topic extends Topic$1 {
154
+ }
155
+ /**
156
+ * Parameter decorator that injects a {@link Topic} instance for the given
157
+ * blueprint resource. Writes both DI injection metadata and CLI resource-ref
158
+ * metadata using well-known `Symbol.for()` keys (no dependency on core).
159
+ *
160
+ * When `resourceName` is omitted, the default topic token is used — this
161
+ * auto-resolves when exactly one topic resource exists.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * @Controller("/orders")
166
+ * class OrderController {
167
+ * constructor(@Topic("orderEvents") private events: Topic) {}
168
+ * }
169
+ * ```
170
+ */
171
+ declare function Topic(resourceName?: string): ParameterDecorator;
172
+
173
+ /**
174
+ * Resolves a {@link Topic} instance from the DI container.
175
+ * For function-based handlers where parameter decorators aren't available.
176
+ *
177
+ * @param container - The service container (typically `context.container`).
178
+ * @param resourceName - The blueprint resource name. Omit when exactly one
179
+ * topic resource exists to use the default.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * const handler = createHttpHandler(async (req, ctx) => {
184
+ * const events = await getTopic(ctx.container, "orderEvents");
185
+ * await events.publish({ orderId: "123", action: "created" });
186
+ * });
187
+ * ```
188
+ */
189
+ declare function getTopic(container: ServiceContainer, resourceName?: string): Promise<Topic$1>;
190
+
191
+ /**
192
+ * System layer that auto-registers {@link TopicClient} and per-resource
193
+ * {@link Topic} handles in the DI container.
194
+ *
195
+ * Reads resource link topology from `CELERITY_RESOURCE_LINKS` and resolves
196
+ * actual topic ARNs / channel names from the ConfigService "resources" namespace.
197
+ * Must run after ConfigLayer in the layer pipeline.
198
+ */
199
+ declare class TopicLayer implements CelerityLayer<BaseHandlerContext> {
200
+ private initialized;
201
+ handle(context: BaseHandlerContext, next: () => Promise<unknown>): Promise<unknown>;
202
+ }
203
+
204
+ /**
205
+ * Base error for topic operations. Wraps provider SDK errors and includes
206
+ * the topic identifier for context.
207
+ */
208
+ declare class TopicError extends Error {
209
+ readonly topic: string;
210
+ constructor(message: string, topic: string, options?: {
211
+ cause?: unknown;
212
+ });
213
+ }
214
+
215
+ export { type BatchPublishEntry, type BatchPublishFailure, type BatchPublishResult, type BatchPublishSuccess, type CreateTopicClientOptions, DEFAULT_TOPIC_TOKEN, type PublishOptions, type PublishResult, RedisTopicClient, type RedisTopicConfig, SNSTopicClient, type SNSTopicConfig, Topic, TopicClient, TopicError, TopicLayer, createTopicClient, getTopic, topicToken };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,215 @@
1
+ import { Closeable, CelerityTracer, ServiceContainer, CelerityLayer, BaseHandlerContext } from '@celerity-sdk/types';
1
2
 
2
- export { }
3
+ declare const TopicClient: unique symbol;
4
+ /**
5
+ * A topic client abstraction for pub/sub topics. Provides access to named
6
+ * topics, each representing a logical destination for published messages.
7
+ */
8
+ interface TopicClient extends Closeable {
9
+ /**
10
+ * Retrieves a topic instance by its name. The returned topic is a lightweight
11
+ * handle — no network calls are made until an operation is invoked.
12
+ *
13
+ * @param name The topic identifier (SNS topic ARN, Redis channel name, etc.).
14
+ */
15
+ topic(name: string): Topic$1;
16
+ }
17
+ /**
18
+ * A topic represents a logical pub/sub destination. It provides methods for
19
+ * publishing messages individually or in batches. The Celerity runtime handles
20
+ * all subscription and consumption — this interface is producer-only.
21
+ */
22
+ interface Topic$1 {
23
+ /**
24
+ * Publish a single message to the topic. The body is serialized to JSON.
25
+ *
26
+ * @param body The message body (serialized to JSON string for transport).
27
+ * @param options Optional publish parameters such as FIFO ordering or subject.
28
+ * @returns A promise that resolves to the publish result with the provider-assigned message ID.
29
+ */
30
+ publish<T = Record<string, unknown>>(body: T, options?: PublishOptions): Promise<PublishResult>;
31
+ /**
32
+ * Publish multiple messages in a single batch. The provider may impose batch
33
+ * size limits (e.g. SNS limits to 10 per request) — the implementation
34
+ * handles chunking transparently.
35
+ *
36
+ * @param entries The batch of messages to publish, each with a caller-assigned ID for correlation.
37
+ * @returns A promise that resolves to the batch result with successful and failed entries.
38
+ */
39
+ publishBatch<T = Record<string, unknown>>(entries: BatchPublishEntry<T>[]): Promise<BatchPublishResult>;
40
+ }
41
+ /**
42
+ * Options for the publish and publishBatch operations.
43
+ */
44
+ type PublishOptions = {
45
+ /** Message group ID for FIFO topics (SNS: MessageGroupId, Pub/Sub: ordering key). */
46
+ groupId?: string;
47
+ /** Deduplication ID for FIFO topics. */
48
+ deduplicationId?: string;
49
+ /** Message subject (SNS: Subject). */
50
+ subject?: string;
51
+ /** String key-value message attributes (metadata sent alongside the body). */
52
+ attributes?: Record<string, string>;
53
+ };
54
+ /**
55
+ * The result of a single publish call.
56
+ */
57
+ type PublishResult = {
58
+ /** The provider-assigned message identifier. */
59
+ messageId: string;
60
+ };
61
+ /**
62
+ * A single entry in a batch publish request.
63
+ */
64
+ type BatchPublishEntry<T = Record<string, unknown>> = {
65
+ /** Caller-assigned ID for correlating results within the batch. */
66
+ id: string;
67
+ /** The message body (serialized to JSON). */
68
+ body: T;
69
+ /** Per-message publish options. */
70
+ options?: PublishOptions;
71
+ };
72
+ /**
73
+ * The result of a batch publish operation.
74
+ */
75
+ type BatchPublishResult = {
76
+ /** Entries that were published successfully. */
77
+ successful: BatchPublishSuccess[];
78
+ /** Entries that failed. */
79
+ failed: BatchPublishFailure[];
80
+ };
81
+ /**
82
+ * A successfully published entry from a batch operation.
83
+ */
84
+ type BatchPublishSuccess = {
85
+ /** The caller-assigned entry ID. */
86
+ id: string;
87
+ /** The provider-assigned message ID. */
88
+ messageId: string;
89
+ };
90
+ /**
91
+ * A failed entry from a batch operation.
92
+ */
93
+ type BatchPublishFailure = {
94
+ /** The caller-assigned entry ID. */
95
+ id: string;
96
+ /** Provider error code. */
97
+ code: string;
98
+ /** Human-readable error message. */
99
+ message: string;
100
+ };
101
+
102
+ type SNSTopicConfig = {
103
+ /** AWS region (e.g. "us-east-1"). */
104
+ region?: string;
105
+ /** Override endpoint URL for LocalStack or other SNS-compatible services. */
106
+ endpoint?: string;
107
+ /** Explicit AWS credentials. When omitted, the SDK's default credential chain is used. */
108
+ credentials?: {
109
+ accessKeyId: string;
110
+ secretAccessKey: string;
111
+ };
112
+ };
113
+
114
+ declare class SNSTopicClient implements TopicClient {
115
+ private readonly tracer?;
116
+ private client;
117
+ private readonly config;
118
+ constructor(config?: SNSTopicConfig, tracer?: CelerityTracer | undefined);
119
+ topic(name: string): Topic$1;
120
+ close(): void;
121
+ private getClient;
122
+ }
123
+
124
+ type RedisTopicConfig = {
125
+ /** Redis/Valkey connection URL. Defaults to "redis://localhost:6379". */
126
+ url?: string;
127
+ };
128
+
129
+ declare class RedisTopicClient implements TopicClient {
130
+ private readonly tracer?;
131
+ private client;
132
+ private readonly config;
133
+ constructor(config?: RedisTopicConfig, tracer?: CelerityTracer | undefined);
134
+ topic(name: string): Topic$1;
135
+ close(): Promise<void>;
136
+ private getClient;
137
+ }
138
+
139
+ type CreateTopicClientOptions = {
140
+ /** Override provider selection. If omitted, derived from platform config. */
141
+ provider?: "aws" | "local" | "gcp" | "azure";
142
+ /** SNS-specific configuration overrides. */
143
+ aws?: SNSTopicConfig;
144
+ /** Redis-specific configuration overrides (local environment). */
145
+ local?: RedisTopicConfig;
146
+ /** Optional tracer for Celerity-level span instrumentation. */
147
+ tracer?: CelerityTracer;
148
+ };
149
+ declare function createTopicClient(options?: CreateTopicClientOptions): TopicClient;
150
+
151
+ declare function topicToken(resourceName: string): symbol;
152
+ declare const DEFAULT_TOPIC_TOKEN: unique symbol;
153
+ interface Topic extends Topic$1 {
154
+ }
155
+ /**
156
+ * Parameter decorator that injects a {@link Topic} instance for the given
157
+ * blueprint resource. Writes both DI injection metadata and CLI resource-ref
158
+ * metadata using well-known `Symbol.for()` keys (no dependency on core).
159
+ *
160
+ * When `resourceName` is omitted, the default topic token is used — this
161
+ * auto-resolves when exactly one topic resource exists.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * @Controller("/orders")
166
+ * class OrderController {
167
+ * constructor(@Topic("orderEvents") private events: Topic) {}
168
+ * }
169
+ * ```
170
+ */
171
+ declare function Topic(resourceName?: string): ParameterDecorator;
172
+
173
+ /**
174
+ * Resolves a {@link Topic} instance from the DI container.
175
+ * For function-based handlers where parameter decorators aren't available.
176
+ *
177
+ * @param container - The service container (typically `context.container`).
178
+ * @param resourceName - The blueprint resource name. Omit when exactly one
179
+ * topic resource exists to use the default.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * const handler = createHttpHandler(async (req, ctx) => {
184
+ * const events = await getTopic(ctx.container, "orderEvents");
185
+ * await events.publish({ orderId: "123", action: "created" });
186
+ * });
187
+ * ```
188
+ */
189
+ declare function getTopic(container: ServiceContainer, resourceName?: string): Promise<Topic$1>;
190
+
191
+ /**
192
+ * System layer that auto-registers {@link TopicClient} and per-resource
193
+ * {@link Topic} handles in the DI container.
194
+ *
195
+ * Reads resource link topology from `CELERITY_RESOURCE_LINKS` and resolves
196
+ * actual topic ARNs / channel names from the ConfigService "resources" namespace.
197
+ * Must run after ConfigLayer in the layer pipeline.
198
+ */
199
+ declare class TopicLayer implements CelerityLayer<BaseHandlerContext> {
200
+ private initialized;
201
+ handle(context: BaseHandlerContext, next: () => Promise<unknown>): Promise<unknown>;
202
+ }
203
+
204
+ /**
205
+ * Base error for topic operations. Wraps provider SDK errors and includes
206
+ * the topic identifier for context.
207
+ */
208
+ declare class TopicError extends Error {
209
+ readonly topic: string;
210
+ constructor(message: string, topic: string, options?: {
211
+ cause?: unknown;
212
+ });
213
+ }
214
+
215
+ export { type BatchPublishEntry, type BatchPublishFailure, type BatchPublishResult, type BatchPublishSuccess, type CreateTopicClientOptions, DEFAULT_TOPIC_TOKEN, type PublishOptions, type PublishResult, RedisTopicClient, type RedisTopicConfig, SNSTopicClient, type SNSTopicConfig, Topic, TopicClient, TopicError, TopicLayer, createTopicClient, getTopic, topicToken };