@amqp-contract/client 0.0.5 → 0.0.6
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 +6 -7
- package/dist/index.cjs +14 -5
- package/dist/index.d.cts +5 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +5 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +15 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -7,21 +7,20 @@ Type-safe AMQP client for publishing messages using amqp-contract.
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
pnpm add @amqp-contract/client
|
|
10
|
+
pnpm add @amqp-contract/client
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
import { TypedAmqpClient } from '@amqp-contract/client';
|
|
17
|
-
import { connect } from 'amqplib';
|
|
18
17
|
import { contract } from './contract';
|
|
19
18
|
|
|
20
|
-
// Connect to RabbitMQ
|
|
21
|
-
const connection = await connect('amqp://localhost');
|
|
22
|
-
|
|
23
19
|
// Create client from contract (automatically connects)
|
|
24
|
-
const client = await TypedAmqpClient.create({
|
|
20
|
+
const client = await TypedAmqpClient.create({
|
|
21
|
+
contract,
|
|
22
|
+
connection: 'amqp://localhost'
|
|
23
|
+
});
|
|
25
24
|
|
|
26
25
|
// Publish message with type safety
|
|
27
26
|
await client.publish('orderCreated', {
|
|
@@ -42,7 +41,7 @@ Create a type-safe AMQP client from a contract. Automatically connects to Rabbit
|
|
|
42
41
|
**Parameters:**
|
|
43
42
|
|
|
44
43
|
- `options.contract` - Contract definition
|
|
45
|
-
- `options.connection` -
|
|
44
|
+
- `options.connection` - AMQP connection URL (string) or connection options (Options.Connect)
|
|
46
45
|
|
|
47
46
|
### `TypedAmqpClient.publish(publisherName, message, options?)`
|
|
48
47
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
let amqplib = require("amqplib");
|
|
1
2
|
|
|
2
3
|
//#region src/client.ts
|
|
3
4
|
/**
|
|
@@ -5,9 +6,10 @@
|
|
|
5
6
|
*/
|
|
6
7
|
var TypedAmqpClient = class TypedAmqpClient {
|
|
7
8
|
channel = null;
|
|
8
|
-
|
|
9
|
+
connection = null;
|
|
10
|
+
constructor(contract, connectionOptions) {
|
|
9
11
|
this.contract = contract;
|
|
10
|
-
this.
|
|
12
|
+
this.connectionOptions = connectionOptions;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
15
|
* Create a type-safe AMQP client from a contract
|
|
@@ -36,16 +38,23 @@ var TypedAmqpClient = class TypedAmqpClient {
|
|
|
36
38
|
return this.channel.publish(publisherDef.exchange, routingKey, content, options?.options);
|
|
37
39
|
}
|
|
38
40
|
/**
|
|
39
|
-
* Close the connection
|
|
41
|
+
* Close the channel and connection
|
|
40
42
|
*/
|
|
41
43
|
async close() {
|
|
42
|
-
if (this.channel)
|
|
43
|
-
|
|
44
|
+
if (this.channel) {
|
|
45
|
+
await this.channel.close();
|
|
46
|
+
this.channel = null;
|
|
47
|
+
}
|
|
48
|
+
if (this.connection) {
|
|
49
|
+
await this.connection.close();
|
|
50
|
+
this.connection = null;
|
|
51
|
+
}
|
|
44
52
|
}
|
|
45
53
|
/**
|
|
46
54
|
* Connect to AMQP broker
|
|
47
55
|
*/
|
|
48
56
|
async init() {
|
|
57
|
+
this.connection = await (0, amqplib.connect)(this.connectionOptions);
|
|
49
58
|
this.channel = await this.connection.createChannel();
|
|
50
59
|
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
51
60
|
durable: exchange.durable,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Options } from "amqplib";
|
|
2
2
|
import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } from "@amqp-contract/contract";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
@@ -8,7 +8,7 @@ import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } fr
|
|
|
8
8
|
*/
|
|
9
9
|
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
10
10
|
contract: TContract;
|
|
11
|
-
connection:
|
|
11
|
+
connection: string | Options.Connect;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Options for publishing a message
|
|
@@ -22,8 +22,9 @@ interface PublishOptions {
|
|
|
22
22
|
*/
|
|
23
23
|
declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
24
24
|
private readonly contract;
|
|
25
|
-
private readonly
|
|
25
|
+
private readonly connectionOptions;
|
|
26
26
|
private channel;
|
|
27
|
+
private connection;
|
|
27
28
|
private constructor();
|
|
28
29
|
/**
|
|
29
30
|
* Create a type-safe AMQP client from a contract
|
|
@@ -35,7 +36,7 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
|
35
36
|
*/
|
|
36
37
|
publish<TName extends InferPublisherNames<TContract>>(publisherName: TName, message: ClientInferPublisherInput<TContract, TName>, options?: PublishOptions): Promise<boolean>;
|
|
37
38
|
/**
|
|
38
|
-
* Close the connection
|
|
39
|
+
* Close the channel and connection
|
|
39
40
|
*/
|
|
40
41
|
close(): Promise<void>;
|
|
41
42
|
/**
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAWA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EACW,UAAQ,EAAA,MAAA,GAAR,OAAA,CAAQ,OAAA;;AAM/B;AAQA;;AAawC,UArBvB,cAAA,CAqBuB;EACP,UAAA,CAAA,EAAA,MAAA;EAApB,OAAA,CAAA,EApBD,OAAA,CAAQ,OAoBP;;;;;AAUiB,cAxBjB,eAwBiB,CAAA,kBAxBiB,kBAwBjB,CAAA,CAAA;EACX,iBAAA,QAAA;EACoB,iBAAA,iBAAA;EAAW,QAAA,OAAA;EAArC,QAAA,UAAA;EACC,QAAA,WAAA,CAAA;EACT;;;;kCAfmC,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;wBASC,oBAAoB,2BAC/B,gBACN,0BAA0B,WAAW,kBACpC,iBACT;;;;WAiDY"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Options } from "amqplib";
|
|
2
2
|
import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } from "@amqp-contract/contract";
|
|
3
3
|
|
|
4
4
|
//#region src/client.d.ts
|
|
@@ -8,7 +8,7 @@ import { ClientInferPublisherInput, ContractDefinition, InferPublisherNames } fr
|
|
|
8
8
|
*/
|
|
9
9
|
interface CreateClientOptions<TContract extends ContractDefinition> {
|
|
10
10
|
contract: TContract;
|
|
11
|
-
connection:
|
|
11
|
+
connection: string | Options.Connect;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Options for publishing a message
|
|
@@ -22,8 +22,9 @@ interface PublishOptions {
|
|
|
22
22
|
*/
|
|
23
23
|
declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
24
24
|
private readonly contract;
|
|
25
|
-
private readonly
|
|
25
|
+
private readonly connectionOptions;
|
|
26
26
|
private channel;
|
|
27
|
+
private connection;
|
|
27
28
|
private constructor();
|
|
28
29
|
/**
|
|
29
30
|
* Create a type-safe AMQP client from a contract
|
|
@@ -35,7 +36,7 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
|
35
36
|
*/
|
|
36
37
|
publish<TName extends InferPublisherNames<TContract>>(publisherName: TName, message: ClientInferPublisherInput<TContract, TName>, options?: PublishOptions): Promise<boolean>;
|
|
37
38
|
/**
|
|
38
|
-
* Close the connection
|
|
39
|
+
* Close the channel and connection
|
|
39
40
|
*/
|
|
40
41
|
close(): Promise<void>;
|
|
41
42
|
/**
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/client.ts"],"sourcesContent":[],"mappings":";;;;;;;AAWA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EACW,UAAQ,EAAA,MAAA,GAAR,OAAA,CAAQ,OAAA;;AAM/B;AAQA;;AAawC,UArBvB,cAAA,CAqBuB;EACP,UAAA,CAAA,EAAA,MAAA;EAApB,OAAA,CAAA,EApBD,OAAA,CAAQ,OAoBP;;;;;AAUiB,cAxBjB,eAwBiB,CAAA,kBAxBiB,kBAwBjB,CAAA,CAAA;EACX,iBAAA,QAAA;EACoB,iBAAA,iBAAA;EAAW,QAAA,OAAA;EAArC,QAAA,UAAA;EACC,QAAA,WAAA,CAAA;EACT;;;;kCAfmC,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;wBASC,oBAAoB,2BAC/B,gBACN,0BAA0B,WAAW,kBACpC,iBACT;;;;WAiDY"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { connect } from "amqplib";
|
|
2
|
+
|
|
1
3
|
//#region src/client.ts
|
|
2
4
|
/**
|
|
3
5
|
* Type-safe AMQP client for publishing messages
|
|
4
6
|
*/
|
|
5
7
|
var TypedAmqpClient = class TypedAmqpClient {
|
|
6
8
|
channel = null;
|
|
7
|
-
|
|
9
|
+
connection = null;
|
|
10
|
+
constructor(contract, connectionOptions) {
|
|
8
11
|
this.contract = contract;
|
|
9
|
-
this.
|
|
12
|
+
this.connectionOptions = connectionOptions;
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
15
|
* Create a type-safe AMQP client from a contract
|
|
@@ -35,16 +38,23 @@ var TypedAmqpClient = class TypedAmqpClient {
|
|
|
35
38
|
return this.channel.publish(publisherDef.exchange, routingKey, content, options?.options);
|
|
36
39
|
}
|
|
37
40
|
/**
|
|
38
|
-
* Close the connection
|
|
41
|
+
* Close the channel and connection
|
|
39
42
|
*/
|
|
40
43
|
async close() {
|
|
41
|
-
if (this.channel)
|
|
42
|
-
|
|
44
|
+
if (this.channel) {
|
|
45
|
+
await this.channel.close();
|
|
46
|
+
this.channel = null;
|
|
47
|
+
}
|
|
48
|
+
if (this.connection) {
|
|
49
|
+
await this.connection.close();
|
|
50
|
+
this.connection = null;
|
|
51
|
+
}
|
|
43
52
|
}
|
|
44
53
|
/**
|
|
45
54
|
* Connect to AMQP broker
|
|
46
55
|
*/
|
|
47
56
|
async init() {
|
|
57
|
+
this.connection = await connect(this.connectionOptions);
|
|
48
58
|
this.channel = await this.connection.createChannel();
|
|
49
59
|
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
50
60
|
durable: exchange.durable,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["contract: TContract","
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["contract: TContract","connectionOptions: string | Options.Connect"],"sources":["../src/client.ts"],"sourcesContent":["import { connect } from \"amqplib\";\nimport 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: string | Options.Connect;\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 private connection: ChannelModel | null = null;\n\n private constructor(\n private readonly contract: TContract,\n private readonly connectionOptions: string | Options.Connect,\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 channel and connection\n */\n async close(): Promise<void> {\n if (this.channel) {\n await this.channel.close();\n this.channel = null;\n }\n if (this.connection) {\n await this.connection.close();\n this.connection = null;\n }\n }\n\n /**\n * Connect to AMQP broker\n */\n private async init(): Promise<void> {\n this.connection = await connect(this.connectionOptions);\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":";;;;;;AA2BA,IAAa,kBAAb,MAAa,gBAAsD;CACjE,AAAQ,UAA0B;CAClC,AAAQ,aAAkC;CAE1C,AAAQ,YACN,AAAiBA,UACjB,AAAiBC,mBACjB;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,MAA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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.6",
|
|
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.6"
|
|
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.6",
|
|
56
56
|
"@amqp-contract/tsconfig": "0.0.0"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|