@amqp-contract/core 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Benoit Travers
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,90 @@
1
+ # @amqp-contract/core
2
+
3
+ Core utilities for AMQP setup and management in amqp-contract. This package provides centralized functionality for establishing AMQP topology (exchanges, queues, and bindings) from contract definitions.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @amqp-contract/core
9
+ # or
10
+ pnpm add @amqp-contract/core
11
+ # or
12
+ yarn add @amqp-contract/core
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ The core package exports a `setupInfra` function that handles the creation of all AMQP resources defined in a contract.
18
+
19
+ ```typescript
20
+ import { connect } from "amqplib";
21
+ import { setupInfra } from "@amqp-contract/core";
22
+ import {
23
+ defineContract,
24
+ defineExchange,
25
+ defineQueue,
26
+ defineQueueBinding,
27
+ } from "@amqp-contract/contract";
28
+
29
+ // Define resources
30
+ const ordersExchange = defineExchange("orders", "topic", { durable: true });
31
+ const orderProcessingQueue = defineQueue("order-processing", { durable: true });
32
+
33
+ // Define your contract
34
+ const contract = defineContract({
35
+ exchanges: {
36
+ orders: ordersExchange,
37
+ },
38
+ queues: {
39
+ orderProcessing: orderProcessingQueue,
40
+ },
41
+ bindings: {
42
+ orderBinding: defineQueueBinding(orderProcessingQueue, ordersExchange, {
43
+ routingKey: "order.created",
44
+ }),
45
+ },
46
+ });
47
+
48
+ // Setup AMQP resources
49
+ const connection = await connect("amqp://localhost");
50
+ const channel = await connection.createChannel();
51
+
52
+ await setupInfra(channel, contract);
53
+ ```
54
+
55
+ ## API
56
+
57
+ ### `setupInfra(channel: Channel, contract: ContractDefinition): Promise<void>`
58
+
59
+ Sets up all AMQP resources defined in the contract:
60
+
61
+ - **Exchanges**: Creates all exchanges with their configurations
62
+ - **Queues**: Creates all queues with their configurations
63
+ - **Bindings**: Creates all bindings (queue-to-exchange and exchange-to-exchange)
64
+
65
+ #### Parameters
66
+
67
+ - `channel`: AMQP channel to use for setup
68
+ - `contract`: Contract definition containing exchanges, queues, and bindings
69
+
70
+ #### Returns
71
+
72
+ A Promise that resolves when all resources are created.
73
+
74
+ ## Features
75
+
76
+ - ✅ Type-safe contract setup
77
+ - ✅ Supports all exchange types (topic, direct, fanout)
78
+ - ✅ Handles both queue-to-exchange and exchange-to-exchange bindings
79
+ - ✅ Passes custom arguments to AMQP resources
80
+ - ✅ Used internally by `@amqp-contract/client` and `@amqp-contract/worker`
81
+
82
+ ## Related Packages
83
+
84
+ - [@amqp-contract/contract](../contract) - Contract definition builders
85
+ - [@amqp-contract/client](../client) - Type-safe AMQP client
86
+ - [@amqp-contract/worker](../worker) - Type-safe AMQP worker
87
+
88
+ ## License
89
+
90
+ MIT © Benoit TRAVERS
package/dist/index.cjs ADDED
@@ -0,0 +1,30 @@
1
+
2
+ //#region src/setup.ts
3
+ /**
4
+ * Setup AMQP resources (exchanges, queues, bindings) for a contract
5
+ * This function is used by both client and worker to establish the AMQP topology
6
+ *
7
+ * @param channel - AMQP channel to use for setup
8
+ * @param contract - Contract definition containing exchanges, queues, and bindings
9
+ */
10
+ async function setupInfra(channel, contract) {
11
+ if (contract.exchanges) await Promise.all(Object.values(contract.exchanges).map((exchange) => channel.assertExchange(exchange.name, exchange.type, {
12
+ durable: exchange.durable,
13
+ autoDelete: exchange.autoDelete,
14
+ internal: exchange.internal,
15
+ arguments: exchange.arguments
16
+ })));
17
+ if (contract.queues) await Promise.all(Object.values(contract.queues).map((queue) => channel.assertQueue(queue.name, {
18
+ durable: queue.durable,
19
+ exclusive: queue.exclusive,
20
+ autoDelete: queue.autoDelete,
21
+ arguments: queue.arguments
22
+ })));
23
+ if (contract.bindings) await Promise.all(Object.values(contract.bindings).map((binding) => {
24
+ if (binding.type === "queue") return channel.bindQueue(binding.queue.name, binding.exchange.name, binding.routingKey ?? "", binding.arguments);
25
+ return channel.bindExchange(binding.destination.name, binding.source.name, binding.routingKey ?? "", binding.arguments);
26
+ }));
27
+ }
28
+
29
+ //#endregion
30
+ exports.setupInfra = setupInfra;
@@ -0,0 +1,16 @@
1
+ import { Channel } from "amqplib";
2
+ import { ContractDefinition } from "@amqp-contract/contract";
3
+
4
+ //#region src/setup.d.ts
5
+
6
+ /**
7
+ * Setup AMQP resources (exchanges, queues, bindings) for a contract
8
+ * This function is used by both client and worker to establish the AMQP topology
9
+ *
10
+ * @param channel - AMQP channel to use for setup
11
+ * @param contract - Contract definition containing exchanges, queues, and bindings
12
+ */
13
+ declare function setupInfra(channel: Channel, contract: ContractDefinition): Promise<void>;
14
+ //#endregion
15
+ export { setupInfra };
16
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/setup.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;;;;;iBAAsB,UAAA,UAAoB,mBAAmB,qBAAqB"}
@@ -0,0 +1,16 @@
1
+ import { Channel } from "amqplib";
2
+ import { ContractDefinition } from "@amqp-contract/contract";
3
+
4
+ //#region src/setup.d.ts
5
+
6
+ /**
7
+ * Setup AMQP resources (exchanges, queues, bindings) for a contract
8
+ * This function is used by both client and worker to establish the AMQP topology
9
+ *
10
+ * @param channel - AMQP channel to use for setup
11
+ * @param contract - Contract definition containing exchanges, queues, and bindings
12
+ */
13
+ declare function setupInfra(channel: Channel, contract: ContractDefinition): Promise<void>;
14
+ //#endregion
15
+ export { setupInfra };
16
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/setup.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;;;;;iBAAsB,UAAA,UAAoB,mBAAmB,qBAAqB"}
package/dist/index.mjs ADDED
@@ -0,0 +1,30 @@
1
+ //#region src/setup.ts
2
+ /**
3
+ * Setup AMQP resources (exchanges, queues, bindings) for a contract
4
+ * This function is used by both client and worker to establish the AMQP topology
5
+ *
6
+ * @param channel - AMQP channel to use for setup
7
+ * @param contract - Contract definition containing exchanges, queues, and bindings
8
+ */
9
+ async function setupInfra(channel, contract) {
10
+ if (contract.exchanges) await Promise.all(Object.values(contract.exchanges).map((exchange) => channel.assertExchange(exchange.name, exchange.type, {
11
+ durable: exchange.durable,
12
+ autoDelete: exchange.autoDelete,
13
+ internal: exchange.internal,
14
+ arguments: exchange.arguments
15
+ })));
16
+ if (contract.queues) await Promise.all(Object.values(contract.queues).map((queue) => channel.assertQueue(queue.name, {
17
+ durable: queue.durable,
18
+ exclusive: queue.exclusive,
19
+ autoDelete: queue.autoDelete,
20
+ arguments: queue.arguments
21
+ })));
22
+ if (contract.bindings) await Promise.all(Object.values(contract.bindings).map((binding) => {
23
+ if (binding.type === "queue") return channel.bindQueue(binding.queue.name, binding.exchange.name, binding.routingKey ?? "", binding.arguments);
24
+ return channel.bindExchange(binding.destination.name, binding.source.name, binding.routingKey ?? "", binding.arguments);
25
+ }));
26
+ }
27
+
28
+ //#endregion
29
+ export { setupInfra };
30
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/setup.ts"],"sourcesContent":["import type { Channel } from \"amqplib\";\nimport type { ContractDefinition } from \"@amqp-contract/contract\";\n\n/**\n * Setup AMQP resources (exchanges, queues, bindings) for a contract\n * This function is used by both client and worker to establish the AMQP topology\n *\n * @param channel - AMQP channel to use for setup\n * @param contract - Contract definition containing exchanges, queues, and bindings\n */\nexport async function setupInfra(channel: Channel, contract: ContractDefinition): Promise<void> {\n // Setup exchanges\n if (contract.exchanges) {\n await Promise.all(\n Object.values(contract.exchanges).map((exchange) =>\n 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\n // Setup queues\n if (contract.queues) {\n await Promise.all(\n Object.values(contract.queues).map((queue) =>\n 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\n // Setup bindings\n if (contract.bindings) {\n await Promise.all(\n Object.values(contract.bindings).map((binding) => {\n if (binding.type === \"queue\") {\n return channel.bindQueue(\n binding.queue.name,\n binding.exchange.name,\n binding.routingKey ?? \"\",\n binding.arguments,\n );\n }\n\n return channel.bindExchange(\n binding.destination.name,\n binding.source.name,\n binding.routingKey ?? \"\",\n binding.arguments,\n );\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;AAUA,eAAsB,WAAW,SAAkB,UAA6C;AAE9F,KAAI,SAAS,UACX,OAAM,QAAQ,IACZ,OAAO,OAAO,SAAS,UAAU,CAAC,KAAK,aACrC,QAAQ,eAAe,SAAS,MAAM,SAAS,MAAM;EACnD,SAAS,SAAS;EAClB,YAAY,SAAS;EACrB,UAAU,SAAS;EACnB,WAAW,SAAS;EACrB,CAAC,CACH,CACF;AAIH,KAAI,SAAS,OACX,OAAM,QAAQ,IACZ,OAAO,OAAO,SAAS,OAAO,CAAC,KAAK,UAClC,QAAQ,YAAY,MAAM,MAAM;EAC9B,SAAS,MAAM;EACf,WAAW,MAAM;EACjB,YAAY,MAAM;EAClB,WAAW,MAAM;EAClB,CAAC,CACH,CACF;AAIH,KAAI,SAAS,SACX,OAAM,QAAQ,IACZ,OAAO,OAAO,SAAS,SAAS,CAAC,KAAK,YAAY;AAChD,MAAI,QAAQ,SAAS,QACnB,QAAO,QAAQ,UACb,QAAQ,MAAM,MACd,QAAQ,SAAS,MACjB,QAAQ,cAAc,IACtB,QAAQ,UACT;AAGH,SAAO,QAAQ,aACb,QAAQ,YAAY,MACpB,QAAQ,OAAO,MACf,QAAQ,cAAc,IACtB,QAAQ,UACT;GACD,CACH"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@amqp-contract/core",
3
+ "version": "0.2.0",
4
+ "description": "Core utilities for AMQP setup and management in amqp-contract",
5
+ "keywords": [
6
+ "amqp",
7
+ "contract",
8
+ "core",
9
+ "rabbitmq",
10
+ "typescript"
11
+ ],
12
+ "homepage": "https://github.com/btravers/amqp-contract#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/btravers/amqp-contract/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/btravers/amqp-contract.git",
19
+ "directory": "packages/core"
20
+ },
21
+ "license": "MIT",
22
+ "author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./dist/index.d.mts",
28
+ "default": "./dist/index.mjs"
29
+ },
30
+ "require": {
31
+ "types": "./dist/index.d.cts",
32
+ "default": "./dist/index.cjs"
33
+ }
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "main": "./dist/index.cjs",
38
+ "module": "./dist/index.mjs",
39
+ "types": "./dist/index.d.mts",
40
+ "files": [
41
+ "dist"
42
+ ],
43
+ "dependencies": {
44
+ "@amqp-contract/contract": "0.2.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/amqplib": "0.10.8",
48
+ "@vitest/coverage-v8": "4.0.16",
49
+ "amqplib": "0.10.9",
50
+ "tsdown": "0.18.2",
51
+ "typescript": "5.9.3",
52
+ "vitest": "4.0.16",
53
+ "@amqp-contract/tsconfig": "0.0.0"
54
+ },
55
+ "peerDependencies": {
56
+ "amqplib": ">=0.10.0"
57
+ },
58
+ "scripts": {
59
+ "build": "tsdown src/index.ts --format cjs,esm --dts --clean",
60
+ "dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
61
+ "test": "vitest run",
62
+ "test:watch": "vitest",
63
+ "typecheck": "tsc --noEmit"
64
+ }
65
+ }