Skip to content

Commit

Permalink
fix: pr reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Jan 24, 2024
1 parent 5f83825 commit a825493
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/spacecat-shared-slack-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A library utility to manage to interact with different Slack organizations.
## Installation

```
npm install my-slack-client
npm install @adobe/spacecat-shared-slack-client
```

## Usage
Expand Down Expand Up @@ -60,6 +60,7 @@ const threadId = 'thread-id'; // thread id to send the message under (optional)
// initializations...

await slackClient.postMessage(ADOBE_INTERNAL, {
text: 'HELLO WORLD!',
channel: 'channel-id',
thread_ts: threadId, // (optional)
});
Expand Down
15 changes: 10 additions & 5 deletions packages/spacecat-shared-slack-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ const DEFAULT_PARAMS = {
unfurl_media: false,
};

function getEnvironmentVariableNameForTarget(target) {
return `${ENV_PREFIX}${target}`;
}

export class SlackClient {
static createFrom(context) {
if (context.slackClient) return context.slackClient;

const expected = Object.values(SLACK_TARGETS).map((target) => `${ENV_PREFIX}${target}`);
const expected = Object.values(SLACK_TARGETS)
.map((target) => getEnvironmentVariableNameForTarget(target));

const targetTokenPairs = Object.keys(context.env)
.filter((variable) => expected.includes(variable))
Expand All @@ -51,16 +56,16 @@ export class SlackClient {
}

this.clients = targetTokenPairs
.reduce((acc, pair) => {
acc[pair.target] = new WebClient(pair.token);
.reduce((acc, { target, token }) => {
acc[target] = new WebClient(token);
return acc;
}, {});
this.log = log;
}

#getClient(target) {
if (!this.clients[target]) {
throw new Error(`Environment variable '${ENV_PREFIX}${target}' does not exist. Slack Client could not initialized.`);
throw new Error(`Environment variable '${getEnvironmentVariableNameForTarget(target)}' does not exist. Slack Client could not be initialized.`);
}
return this.clients[target];
}
Expand All @@ -78,7 +83,7 @@ export class SlackClient {
threadId: result.ts,
};
} catch (error) {
this.log.error(`Slack message failed to send to ${target}`);
this.log.error(`Slack message failed to send to ${target}`, error);
throw error;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-slack-client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Slack Client', () => {
},
});
await expect(slackClient.postMessage('unknown-target', {}))
.to.be.rejectedWith('Environment variable \'SLACK_TOKEN_unknown-target\' does not exist. Slack Client could not initialized.');
.to.be.rejectedWith('Environment variable \'SLACK_TOKEN_unknown-target\' does not exist. Slack Client could not be initialized.');
});

it('returns channel-id and thread-id when message posted', async () => {
Expand Down

0 comments on commit a825493

Please sign in to comment.