@amqp-contract/client 0.0.4 → 0.0.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/README.md +5 -11
- package/dist/index.cjs +32 -40
- package/dist/index.d.cts +18 -18
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +18 -18
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +32 -39
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ pnpm add @amqp-contract/client amqplib
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import {
|
|
16
|
+
import { TypedAmqpClient } from '@amqp-contract/client';
|
|
17
17
|
import { connect } from 'amqplib';
|
|
18
18
|
import { contract } from './contract';
|
|
19
19
|
|
|
@@ -21,7 +21,7 @@ import { contract } from './contract';
|
|
|
21
21
|
const connection = await connect('amqp://localhost');
|
|
22
22
|
|
|
23
23
|
// Create client from contract (automatically connects)
|
|
24
|
-
const client = await
|
|
24
|
+
const client = await TypedAmqpClient.create({ contract, connection });
|
|
25
25
|
|
|
26
26
|
// Publish message with type safety
|
|
27
27
|
await client.publish('orderCreated', {
|
|
@@ -35,7 +35,7 @@ await client.close();
|
|
|
35
35
|
|
|
36
36
|
## API
|
|
37
37
|
|
|
38
|
-
### `
|
|
38
|
+
### `TypedAmqpClient.create(options)`
|
|
39
39
|
|
|
40
40
|
Create a type-safe AMQP client from a contract. Automatically connects to RabbitMQ.
|
|
41
41
|
|
|
@@ -44,17 +44,11 @@ Create a type-safe AMQP client from a contract. Automatically connects to Rabbit
|
|
|
44
44
|
- `options.contract` - Contract definition
|
|
45
45
|
- `options.connection` - amqplib Connection object
|
|
46
46
|
|
|
47
|
-
### `
|
|
48
|
-
|
|
49
|
-
Connect to an AMQP broker and set up all exchanges, queues, and bindings defined in the contract.
|
|
50
|
-
|
|
51
|
-
**Note:** When using `createClient()`, this is called automatically.
|
|
52
|
-
|
|
53
|
-
### `AmqpClient.publish(publisherName, message, options?)`
|
|
47
|
+
### `TypedAmqpClient.publish(publisherName, message, options?)`
|
|
54
48
|
|
|
55
49
|
Publish a message using a defined publisher. The message will be validated against the schema and type-checked at compile time.
|
|
56
50
|
|
|
57
|
-
### `
|
|
51
|
+
### `TypedAmqpClient.close()`
|
|
58
52
|
|
|
59
53
|
Close the channel and connection.
|
|
60
54
|
|
package/dist/index.cjs
CHANGED
|
@@ -3,37 +3,26 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Type-safe AMQP client for publishing messages
|
|
5
5
|
*/
|
|
6
|
-
var
|
|
6
|
+
var TypedAmqpClient = class TypedAmqpClient {
|
|
7
7
|
channel = null;
|
|
8
|
-
connection
|
|
9
|
-
constructor(contract) {
|
|
8
|
+
constructor(contract, connection) {
|
|
10
9
|
this.contract = contract;
|
|
10
|
+
this.connection = connection;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Create a type-safe AMQP client from a contract
|
|
14
|
+
* The client will automatically connect to the AMQP broker
|
|
14
15
|
*/
|
|
15
|
-
async
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
durable: exchange.durable,
|
|
20
|
-
autoDelete: exchange.autoDelete,
|
|
21
|
-
internal: exchange.internal,
|
|
22
|
-
arguments: exchange.arguments
|
|
23
|
-
});
|
|
24
|
-
if (this.contract.queues && this.channel) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
25
|
-
durable: queue.durable,
|
|
26
|
-
exclusive: queue.exclusive,
|
|
27
|
-
autoDelete: queue.autoDelete,
|
|
28
|
-
arguments: queue.arguments
|
|
29
|
-
});
|
|
30
|
-
if (this.contract.bindings && this.channel) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
16
|
+
static async create(options) {
|
|
17
|
+
const client = new TypedAmqpClient(options.contract, options.connection);
|
|
18
|
+
await client.init();
|
|
19
|
+
return client;
|
|
31
20
|
}
|
|
32
21
|
/**
|
|
33
22
|
* Publish a message using a defined publisher
|
|
34
23
|
*/
|
|
35
24
|
async publish(publisherName, message, options) {
|
|
36
|
-
if (!this.channel) throw new Error("Client not
|
|
25
|
+
if (!this.channel) throw new Error("Client not initialized. Create the client using TypedAmqpClient.create() to establish a connection.");
|
|
37
26
|
const publishers = this.contract.publishers;
|
|
38
27
|
if (!publishers) throw new Error("No publishers defined in contract");
|
|
39
28
|
const publisher = publishers[publisherName];
|
|
@@ -50,26 +39,29 @@ var AmqpClient = class {
|
|
|
50
39
|
* Close the connection
|
|
51
40
|
*/
|
|
52
41
|
async close() {
|
|
53
|
-
if (this.channel)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
42
|
+
if (this.channel) await this.channel.close();
|
|
43
|
+
await this.connection.close();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Connect to AMQP broker
|
|
47
|
+
*/
|
|
48
|
+
async init() {
|
|
49
|
+
this.channel = await this.connection.createChannel();
|
|
50
|
+
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
51
|
+
durable: exchange.durable,
|
|
52
|
+
autoDelete: exchange.autoDelete,
|
|
53
|
+
internal: exchange.internal,
|
|
54
|
+
arguments: exchange.arguments
|
|
55
|
+
});
|
|
56
|
+
if (this.contract.queues) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
57
|
+
durable: queue.durable,
|
|
58
|
+
exclusive: queue.exclusive,
|
|
59
|
+
autoDelete: queue.autoDelete,
|
|
60
|
+
arguments: queue.arguments
|
|
61
|
+
});
|
|
62
|
+
if (this.contract.bindings) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
61
63
|
}
|
|
62
64
|
};
|
|
63
|
-
/**
|
|
64
|
-
* Create a type-safe AMQP client from a contract
|
|
65
|
-
* The client will automatically connect to the AMQP broker
|
|
66
|
-
*/
|
|
67
|
-
async function createClient(options) {
|
|
68
|
-
const client = new AmqpClient(options.contract);
|
|
69
|
-
await client.connect(options.connection);
|
|
70
|
-
return client;
|
|
71
|
-
}
|
|
72
65
|
|
|
73
66
|
//#endregion
|
|
74
|
-
exports.
|
|
75
|
-
exports.createClient = createClient;
|
|
67
|
+
exports.TypedAmqpClient = TypedAmqpClient;
|
package/dist/index.d.cts
CHANGED
|
@@ -3,6 +3,13 @@ import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } fr
|
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a client
|
|
8
|
+
*/
|
|
9
|
+
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
10
|
+
contract: TContract;
|
|
11
|
+
connection: ChannelModel;
|
|
12
|
+
}
|
|
6
13
|
/**
|
|
7
14
|
* Options for publishing a message
|
|
8
15
|
*/
|
|
@@ -13,15 +20,16 @@ interface PublishOptions {
|
|
|
13
20
|
/**
|
|
14
21
|
* Type-safe AMQP client for publishing messages
|
|
15
22
|
*/
|
|
16
|
-
declare class
|
|
23
|
+
declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
17
24
|
private readonly contract;
|
|
25
|
+
private readonly connection;
|
|
18
26
|
private channel;
|
|
19
|
-
private
|
|
20
|
-
constructor(contract: TContract);
|
|
27
|
+
private constructor();
|
|
21
28
|
/**
|
|
22
|
-
*
|
|
29
|
+
* Create a type-safe AMQP client from a contract
|
|
30
|
+
* The client will automatically connect to the AMQP broker
|
|
23
31
|
*/
|
|
24
|
-
|
|
32
|
+
static create<TContract extends ContractDefinition>(options: CreateClientOptions<TContract>): Promise<TypedAmqpClient<TContract>>;
|
|
25
33
|
/**
|
|
26
34
|
* Publish a message using a defined publisher
|
|
27
35
|
*/
|
|
@@ -30,19 +38,11 @@ declare class AmqpClient<TContract extends ContractDefinition> {
|
|
|
30
38
|
* Close the connection
|
|
31
39
|
*/
|
|
32
40
|
close(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Connect to AMQP broker
|
|
43
|
+
*/
|
|
44
|
+
private init;
|
|
33
45
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Options for creating a client
|
|
36
|
-
*/
|
|
37
|
-
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
38
|
-
contract: TContract;
|
|
39
|
-
connection: ChannelModel;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a type-safe AMQP client from a contract
|
|
43
|
-
* The client will automatically connect to the AMQP broker
|
|
44
|
-
*/
|
|
45
|
-
declare function createClient<TContract extends ContractDefinition>(options: CreateClientOptions<TContract>): Promise<AmqpClient<TContract>>;
|
|
46
46
|
//#endregion
|
|
47
|
-
export {
|
|
47
|
+
export { type CreateClientOptions, type PublishOptions, TypedAmqpClient };
|
|
48
48
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EACE,UAAA,EAAA,YAAA;;AAMd;AAQA;;AAYwC,UApBvB,cAAA,CAoBuB;EACP,UAAA,CAAA,EAAA,MAAA;EAApB,OAAA,CAAA,EAnBD,OAAA,CAAQ,OAmBP;;;;;AAUiB,cAvBjB,eAuBiB,CAAA,kBAvBiB,kBAuBjB,CAAA,CAAA;EACX,iBAAA,QAAA;EACoB,iBAAA,UAAA;EAAW,QAAA,OAAA;EAArC,QAAA,WAAA,CAAA;EACC;;;;kCAd0B,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;wBASC,oBAAoB,2BAC/B,gBACN,0BAA0B,WAAW,kBACpC,iBACT;;;;WAiDY"}
|
package/dist/index.d.mts
CHANGED
|
@@ -3,6 +3,13 @@ import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } fr
|
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a client
|
|
8
|
+
*/
|
|
9
|
+
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
10
|
+
contract: TContract;
|
|
11
|
+
connection: ChannelModel;
|
|
12
|
+
}
|
|
6
13
|
/**
|
|
7
14
|
* Options for publishing a message
|
|
8
15
|
*/
|
|
@@ -13,15 +20,16 @@ interface PublishOptions {
|
|
|
13
20
|
/**
|
|
14
21
|
* Type-safe AMQP client for publishing messages
|
|
15
22
|
*/
|
|
16
|
-
declare class
|
|
23
|
+
declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
17
24
|
private readonly contract;
|
|
25
|
+
private readonly connection;
|
|
18
26
|
private channel;
|
|
19
|
-
private
|
|
20
|
-
constructor(contract: TContract);
|
|
27
|
+
private constructor();
|
|
21
28
|
/**
|
|
22
|
-
*
|
|
29
|
+
* Create a type-safe AMQP client from a contract
|
|
30
|
+
* The client will automatically connect to the AMQP broker
|
|
23
31
|
*/
|
|
24
|
-
|
|
32
|
+
static create<TContract extends ContractDefinition>(options: CreateClientOptions<TContract>): Promise<TypedAmqpClient<TContract>>;
|
|
25
33
|
/**
|
|
26
34
|
* Publish a message using a defined publisher
|
|
27
35
|
*/
|
|
@@ -30,19 +38,11 @@ declare class AmqpClient<TContract extends ContractDefinition> {
|
|
|
30
38
|
* Close the connection
|
|
31
39
|
*/
|
|
32
40
|
close(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Connect to AMQP broker
|
|
43
|
+
*/
|
|
44
|
+
private init;
|
|
33
45
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Options for creating a client
|
|
36
|
-
*/
|
|
37
|
-
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
38
|
-
contract: TContract;
|
|
39
|
-
connection: ChannelModel;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a type-safe AMQP client from a contract
|
|
43
|
-
* The client will automatically connect to the AMQP broker
|
|
44
|
-
*/
|
|
45
|
-
declare function createClient<TContract extends ContractDefinition>(options: CreateClientOptions<TContract>): Promise<AmqpClient<TContract>>;
|
|
46
46
|
//#endregion
|
|
47
|
-
export {
|
|
47
|
+
export { type CreateClientOptions, type PublishOptions, TypedAmqpClient };
|
|
48
48
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EACE,UAAA,EAAA,YAAA;;AAMd;AAQA;;AAYwC,UApBvB,cAAA,CAoBuB;EACP,UAAA,CAAA,EAAA,MAAA;EAApB,OAAA,CAAA,EAnBD,OAAA,CAAQ,OAmBP;;;;;AAUiB,cAvBjB,eAuBiB,CAAA,kBAvBiB,kBAuBjB,CAAA,CAAA;EACX,iBAAA,QAAA;EACoB,iBAAA,UAAA;EAAW,QAAA,OAAA;EAArC,QAAA,WAAA,CAAA;EACC;;;;kCAd0B,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;wBASC,oBAAoB,2BAC/B,gBACN,0BAA0B,WAAW,kBACpC,iBACT;;;;WAiDY"}
|
package/dist/index.mjs
CHANGED
|
@@ -2,37 +2,26 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Type-safe AMQP client for publishing messages
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var TypedAmqpClient = class TypedAmqpClient {
|
|
6
6
|
channel = null;
|
|
7
|
-
connection
|
|
8
|
-
constructor(contract) {
|
|
7
|
+
constructor(contract, connection) {
|
|
9
8
|
this.contract = contract;
|
|
9
|
+
this.connection = connection;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Create a type-safe AMQP client from a contract
|
|
13
|
+
* The client will automatically connect to the AMQP broker
|
|
13
14
|
*/
|
|
14
|
-
async
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
durable: exchange.durable,
|
|
19
|
-
autoDelete: exchange.autoDelete,
|
|
20
|
-
internal: exchange.internal,
|
|
21
|
-
arguments: exchange.arguments
|
|
22
|
-
});
|
|
23
|
-
if (this.contract.queues && this.channel) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
24
|
-
durable: queue.durable,
|
|
25
|
-
exclusive: queue.exclusive,
|
|
26
|
-
autoDelete: queue.autoDelete,
|
|
27
|
-
arguments: queue.arguments
|
|
28
|
-
});
|
|
29
|
-
if (this.contract.bindings && this.channel) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
15
|
+
static async create(options) {
|
|
16
|
+
const client = new TypedAmqpClient(options.contract, options.connection);
|
|
17
|
+
await client.init();
|
|
18
|
+
return client;
|
|
30
19
|
}
|
|
31
20
|
/**
|
|
32
21
|
* Publish a message using a defined publisher
|
|
33
22
|
*/
|
|
34
23
|
async publish(publisherName, message, options) {
|
|
35
|
-
if (!this.channel) throw new Error("Client not
|
|
24
|
+
if (!this.channel) throw new Error("Client not initialized. Create the client using TypedAmqpClient.create() to establish a connection.");
|
|
36
25
|
const publishers = this.contract.publishers;
|
|
37
26
|
if (!publishers) throw new Error("No publishers defined in contract");
|
|
38
27
|
const publisher = publishers[publisherName];
|
|
@@ -49,26 +38,30 @@ var AmqpClient = class {
|
|
|
49
38
|
* Close the connection
|
|
50
39
|
*/
|
|
51
40
|
async close() {
|
|
52
|
-
if (this.channel)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
41
|
+
if (this.channel) await this.channel.close();
|
|
42
|
+
await this.connection.close();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Connect to AMQP broker
|
|
46
|
+
*/
|
|
47
|
+
async init() {
|
|
48
|
+
this.channel = await this.connection.createChannel();
|
|
49
|
+
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
50
|
+
durable: exchange.durable,
|
|
51
|
+
autoDelete: exchange.autoDelete,
|
|
52
|
+
internal: exchange.internal,
|
|
53
|
+
arguments: exchange.arguments
|
|
54
|
+
});
|
|
55
|
+
if (this.contract.queues) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
56
|
+
durable: queue.durable,
|
|
57
|
+
exclusive: queue.exclusive,
|
|
58
|
+
autoDelete: queue.autoDelete,
|
|
59
|
+
arguments: queue.arguments
|
|
60
|
+
});
|
|
61
|
+
if (this.contract.bindings) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
60
62
|
}
|
|
61
63
|
};
|
|
62
|
-
/**
|
|
63
|
-
* Create a type-safe AMQP client from a contract
|
|
64
|
-
* The client will automatically connect to the AMQP broker
|
|
65
|
-
*/
|
|
66
|
-
async function createClient(options) {
|
|
67
|
-
const client = new AmqpClient(options.contract);
|
|
68
|
-
await client.connect(options.connection);
|
|
69
|
-
return client;
|
|
70
|
-
}
|
|
71
64
|
|
|
72
65
|
//#endregion
|
|
73
|
-
export {
|
|
66
|
+
export { TypedAmqpClient };
|
|
74
67
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["contract: TContract"],"sources":["../src/client.ts"],"sourcesContent":["import type { Channel, ChannelModel, Options } from \"amqplib\";\nimport type {\n ClientInferPublisherInput,\n ContractDefinition,\n InferPublisherNames,\n} from \"@amqp-contract/contract\";\n\n/**\n * Options for publishing a message\n */\nexport interface PublishOptions {\n routingKey?: string;\n options?: Options.Publish;\n}\n\n/**\n * Type-safe AMQP client for publishing messages\n */\nexport class
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["contract: TContract","connection: ChannelModel"],"sources":["../src/client.ts"],"sourcesContent":["import type { Channel, ChannelModel, Options } from \"amqplib\";\nimport type {\n ClientInferPublisherInput,\n ContractDefinition,\n InferPublisherNames,\n} from \"@amqp-contract/contract\";\n\n/**\n * Options for creating a client\n */\nexport interface CreateClientOptions<TContract extends ContractDefinition> {\n contract: TContract;\n connection: ChannelModel;\n}\n\n/**\n * Options for publishing a message\n */\nexport interface PublishOptions {\n routingKey?: string;\n options?: Options.Publish;\n}\n\n/**\n * Type-safe AMQP client for publishing messages\n */\nexport class TypedAmqpClient<TContract extends ContractDefinition> {\n private channel: Channel | null = null;\n\n private constructor(\n private readonly contract: TContract,\n private readonly connection: ChannelModel,\n ) {}\n\n /**\n * Create a type-safe AMQP client from a contract\n * The client will automatically connect to the AMQP broker\n */\n static async create<TContract extends ContractDefinition>(\n options: CreateClientOptions<TContract>,\n ): Promise<TypedAmqpClient<TContract>> {\n const client = new TypedAmqpClient(options.contract, options.connection);\n await client.init();\n return client;\n }\n\n /**\n * Publish a message using a defined publisher\n */\n async publish<TName extends InferPublisherNames<TContract>>(\n publisherName: TName,\n message: ClientInferPublisherInput<TContract, TName>,\n options?: PublishOptions,\n ): Promise<boolean> {\n if (!this.channel) {\n throw new Error(\n \"Client not initialized. Create the client using TypedAmqpClient.create() to establish a connection.\",\n );\n }\n\n const publishers = this.contract.publishers as Record<string, unknown>;\n if (!publishers) {\n throw new Error(\"No publishers defined in contract\");\n }\n\n const publisher = publishers[publisherName as string];\n if (!publisher || typeof publisher !== \"object\") {\n throw new Error(`Publisher \"${String(publisherName)}\" not found in contract`);\n }\n\n const publisherDef = publisher as {\n exchange: string;\n routingKey?: string;\n message: { \"~standard\": { validate: (value: unknown) => unknown } };\n };\n\n // Validate message using schema\n const validation = publisherDef.message[\"~standard\"].validate(message);\n if (\n typeof validation === \"object\" &&\n validation !== null &&\n \"issues\" in validation &&\n validation.issues\n ) {\n throw new Error(`Message validation failed: ${JSON.stringify(validation.issues)}`);\n }\n\n const validatedMessage =\n typeof validation === \"object\" && validation !== null && \"value\" in validation\n ? validation.value\n : message;\n\n // Publish message\n const routingKey = options?.routingKey ?? publisherDef.routingKey ?? \"\";\n const content = Buffer.from(JSON.stringify(validatedMessage));\n\n return this.channel.publish(publisherDef.exchange, routingKey, content, options?.options);\n }\n\n /**\n * Close the connection\n */\n async close(): Promise<void> {\n if (this.channel) {\n await this.channel.close();\n }\n\n await this.connection.close();\n }\n\n /**\n * Connect to AMQP broker\n */\n private async init(): Promise<void> {\n this.channel = await this.connection.createChannel();\n\n // Setup exchanges\n if (this.contract.exchanges) {\n for (const exchange of Object.values(this.contract.exchanges)) {\n await this.channel.assertExchange(exchange.name, exchange.type, {\n durable: exchange.durable,\n autoDelete: exchange.autoDelete,\n internal: exchange.internal,\n arguments: exchange.arguments,\n });\n }\n }\n\n // Setup queues\n if (this.contract.queues) {\n for (const queue of Object.values(this.contract.queues)) {\n await this.channel.assertQueue(queue.name, {\n durable: queue.durable,\n exclusive: queue.exclusive,\n autoDelete: queue.autoDelete,\n arguments: queue.arguments,\n });\n }\n }\n\n // Setup bindings\n if (this.contract.bindings) {\n for (const binding of Object.values(this.contract.bindings)) {\n await this.channel.bindQueue(\n binding.queue,\n binding.exchange,\n binding.routingKey ?? \"\",\n binding.arguments,\n );\n }\n }\n }\n}\n"],"mappings":";;;;AA0BA,IAAa,kBAAb,MAAa,gBAAsD;CACjE,AAAQ,UAA0B;CAElC,AAAQ,YACN,AAAiBA,UACjB,AAAiBC,YACjB;EAFiB;EACA;;;;;;CAOnB,aAAa,OACX,SACqC;EACrC,MAAM,SAAS,IAAI,gBAAgB,QAAQ,UAAU,QAAQ,WAAW;AACxE,QAAM,OAAO,MAAM;AACnB,SAAO;;;;;CAMT,MAAM,QACJ,eACA,SACA,SACkB;AAClB,MAAI,CAAC,KAAK,QACR,OAAM,IAAI,MACR,sGACD;EAGH,MAAM,aAAa,KAAK,SAAS;AACjC,MAAI,CAAC,WACH,OAAM,IAAI,MAAM,oCAAoC;EAGtD,MAAM,YAAY,WAAW;AAC7B,MAAI,CAAC,aAAa,OAAO,cAAc,SACrC,OAAM,IAAI,MAAM,cAAc,OAAO,cAAc,CAAC,yBAAyB;EAG/E,MAAM,eAAe;EAOrB,MAAM,aAAa,aAAa,QAAQ,aAAa,SAAS,QAAQ;AACtE,MACE,OAAO,eAAe,YACtB,eAAe,QACf,YAAY,cACZ,WAAW,OAEX,OAAM,IAAI,MAAM,8BAA8B,KAAK,UAAU,WAAW,OAAO,GAAG;EAGpF,MAAM,mBACJ,OAAO,eAAe,YAAY,eAAe,QAAQ,WAAW,aAChE,WAAW,QACX;EAGN,MAAM,aAAa,SAAS,cAAc,aAAa,cAAc;EACrE,MAAM,UAAU,OAAO,KAAK,KAAK,UAAU,iBAAiB,CAAC;AAE7D,SAAO,KAAK,QAAQ,QAAQ,aAAa,UAAU,YAAY,SAAS,SAAS,QAAQ;;;;;CAM3F,MAAM,QAAuB;AAC3B,MAAI,KAAK,QACP,OAAM,KAAK,QAAQ,OAAO;AAG5B,QAAM,KAAK,WAAW,OAAO;;;;;CAM/B,MAAc,OAAsB;AAClC,OAAK,UAAU,MAAM,KAAK,WAAW,eAAe;AAGpD,MAAI,KAAK,SAAS,UAChB,MAAK,MAAM,YAAY,OAAO,OAAO,KAAK,SAAS,UAAU,CAC3D,OAAM,KAAK,QAAQ,eAAe,SAAS,MAAM,SAAS,MAAM;GAC9D,SAAS,SAAS;GAClB,YAAY,SAAS;GACrB,UAAU,SAAS;GACnB,WAAW,SAAS;GACrB,CAAC;AAKN,MAAI,KAAK,SAAS,OAChB,MAAK,MAAM,SAAS,OAAO,OAAO,KAAK,SAAS,OAAO,CACrD,OAAM,KAAK,QAAQ,YAAY,MAAM,MAAM;GACzC,SAAS,MAAM;GACf,WAAW,MAAM;GACjB,YAAY,MAAM;GAClB,WAAW,MAAM;GAClB,CAAC;AAKN,MAAI,KAAK,SAAS,SAChB,MAAK,MAAM,WAAW,OAAO,OAAO,KAAK,SAAS,SAAS,CACzD,OAAM,KAAK,QAAQ,UACjB,QAAQ,OACR,QAAQ,UACR,QAAQ,cAAc,IACtB,QAAQ,UACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amqp-contract/client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Client utilities for publishing messages using amqp-contract",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"amqp",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@amqp-contract/contract": "0.0.
|
|
44
|
+
"@amqp-contract/contract": "0.0.5"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/amqplib": "0.10.8",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"typescript": "5.9.3",
|
|
53
53
|
"vitest": "4.0.16",
|
|
54
54
|
"zod": "4.2.1",
|
|
55
|
-
"@amqp-contract/testing": "0.0.
|
|
55
|
+
"@amqp-contract/testing": "0.0.5",
|
|
56
56
|
"@amqp-contract/tsconfig": "0.0.0"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|