Skip to content

Commit

Permalink
feat: bind consume & publish to client class & remove singleton patterns
Browse files Browse the repository at this point in the history
BREAKING CHANGE: refactor public api
  • Loading branch information
tada5hi committed Feb 28, 2024
1 parent 0405339 commit 9160408
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 626 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ jobs:
run: |
npm run build
- name: Test package
run: |
npm run test
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
236 changes: 20 additions & 216 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
[![Known Vulnerabilities](https://snyk.io/test/github/Tada5hi/amqp-extension/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Tada5hi/amqp-extension?targetFile=package.json)
[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)

This is a library on top of the famous [amqplib](https://www.npmjs.com/package/amqplib) library and defines a [message format](#message-types) for queue messages through a message broker across multiple standalone services.
All utility functions support the usage of multiple registered connections.
This is a library on top of the [amqplib](https://www.npmjs.com/package/amqplib) library to simplify the process of consuming & publishing queue messages.

**Table of Contents**

Expand Down Expand Up @@ -35,18 +34,13 @@ npm install amqp-extension --save

### Publish to Queue

The `publish` function allows you to send messages quickly.
The `publish` method allows you to send messages quickly.
Existing options can be added or overwritten

```typescript
import {
useConnection,
publish,
setConfig
} from "amqp-extension";
import { Client } from "amqp-extension";

// This will set the default connection :)
setConfig({
const client = new Client({
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
Expand All @@ -55,7 +49,7 @@ setConfig({
});

(async () => {
await publish({
await client.publish({
content: {
type: 'resourceCreated',
name: 'foo'
Expand All @@ -71,14 +65,12 @@ and as second argument and object to specify an async callback function handler

```typescript
import {
consume,
Client,
ConsumeMessage,
ConsumeOptions,
setConfig
} from "amqp-extension";

// This will set the default connection :)
setConfig({
const client = new Client({
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
Expand All @@ -93,7 +85,7 @@ const options: ConsumeOptions = {
}

(async () => {
await consume(options, {
await client.consume(options, {
resourceCreated: async (message: ConsumeMessage) => {
const content = message.content.toString('utf-8');
const payload = JSON.parse(content);
Expand All @@ -104,201 +96,8 @@ const options: ConsumeOptions = {
})();
```

### Multiple Connections

To define multiple concurrent connections just specify a different `alias` in the configuration object, so it does not
get overwritten.

```typescript
import {
ConsumeOptions,
publish,
PublishOptionsExtended,
setConfig
} from "amqp-extension";

// This will set the default connection :)
setConfig({
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});

setConfig({
alias: 'foo',
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});
(async () => {
await consume(
{
routingKey: '<routing-key>',
alias: 'foo' // <--- use another connection :)
},
{
// ... handlers
}
);

await publish({
routingKey: '<routing-key>',
alias: 'foo', // <--- use another connection :)
content: {
foo: 'bar'
}
});
})();

```

## Functions

### setConfig

`function` **setConfig**(`key?: string | ConfigInput`, `value?: ConfigInput`): `Config`

Register a connection as `default` alias or specify an `<alias>` as config property.

#### Example
**`Simple`**

Define the default connection config.

```typescript
import {setConfig, useConnection} from "amqp-extension";

(async () => {
setConfig({
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});

const connection = await useConnection();
})();

```

Define a non default connection.

```typescript
import { setConfig, useConnection } from "amqp-extension";

(async () => {
setConfig({
alias: '<alias>',
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});

setConfig('foo', {
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});

const connection = await useConnection('<alias>');
const fooConnection = await useConnection('foo');
})();

```


#### Type parameters

| Name | Description |
|:------|:------------|



#### Parameters

| Name | Type | Description |
|:--------|:--------------------------|:-----------------------------------------------------------|
| `key` | `string` or `ConfigInput` | Config object or alias of config. [more](#config-types) |
| `value` | `Config` | Config object. [more](#config-types) |

#### Returns

`Config`

The function returns the Config object.

### useConnection

`function` **useConnection**(`key?: string | ConfigInput`): `Promise<Connection>`

Either register a connection as `default` alias or specify an `alias` as config property.
If you have registered a connection you can receive the connection by specifying no arguments or provide an alias name if specified.


#### Example
**`Simple`**

Receive underlying driver connection.

```typescript
import {useConnection} from "amqp-extension";

(async () => {
const connection = await useConnection();
})();

```

**`Advanced`**

Use a none `default` connection.

```typescript
import {setConfig, useConnection} from "amqp-extension";

(async () => {
setConfig({
alias: '<alias>',
connection: 'amqp://<user>:<password>@<host>',
exchange: {
name: '<name>',
type: 'topic'
}
});

const connection = await useConnection('<alias>');
})();

```

#### Type parameters

| Name | Description |
|:------|:--------------|



#### Parameters

| Name | Type | Description |
|:------|:---------------------|:-------------------------------------------------|
| `key` | `string` or `Config` | Config or alias of config. [more](#config-types) |

#### Returns

`Promise<Connection>`

The function returns the Connection object of the `amqplib`.

### publish

`function` **publish**(`message: Message`, `options?: PublishOptions`): `Promise<void>`
Expand All @@ -309,12 +108,14 @@ As second parameter a registered config can be used by specifying the alias or p
**`Simple`**

```typescript
import {
publish
} from "amqp-extension";
import { Client } from "amqp-extension";

const client = new Client({
// ...
});

(async () => {
await publish({
await client.publish({
content: {
type: 'resourceCreated'
}
Expand Down Expand Up @@ -351,17 +152,21 @@ As second parameter a registered config can be used by specifying the alias or p

```typescript
import {
consume,
Client,
ConsumeOptions,
ConsumeMessage,
} from "amqp-extension";

const client = new Client({
// ...
});

const options: ConsumeOptions = {
routingKey: '<routing-key>'
}

(async () => {
await consume(options, {
await client.consume(options, {
'<type>': async (message: ConsumeMessage) => {
// do some async action :)
}
Expand Down Expand Up @@ -400,7 +205,6 @@ import { ExchangeOptions } from '../exchange';
import { ConsumeOptions, PublishOptions } from '../type';

export type Config = {
alias: string,
connection: Options.Connect | string,
exchange: ExchangeOptions,
publish: PublishOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

export * from './type';
export * from './module';
export * from './utils';
export * from './module';
Loading

0 comments on commit 9160408

Please sign in to comment.