Skip to content

Commit

Permalink
feat(sqs): expose utility to get sqs queue url from task name
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Aug 1, 2024
1 parent c6ef64c commit 034a3b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getSqsQueueUrlForTask as getQueueUrlForTask } from './logic/sqs/getSqsQueueUrlForTask';
export { extractTaskParcelFromSqsEvent } from './logic/extractTaskParcelFromSqsEvent';
export {
withAsyncTaskExecutionLifecycleEnqueue,
Expand Down
40 changes: 40 additions & 0 deletions src/logic/sqs/getSqsQueueUrlForTask.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DomainEntity } from 'domain-objects';

import { AsyncTask, AsyncTaskStatus } from '../../domain/objects/AsyncTask';
import { getSqsQueueUrlForTask } from './getSqsQueueUrlForTask';

/**
* an async task for emitting some data to remote persistance
*
* e.g., for @mhetrics/app-usage-events-react
*/
interface AsyncTaskEmitToRemote extends AsyncTask {
uuid?: string;
updatedAt?: string;
status: AsyncTaskStatus;
endpoint: string;
bytes: number;
payload: string;
}
class AsyncTaskEmitToRemote
extends DomainEntity<AsyncTaskEmitToRemote>
implements AsyncTaskEmitToRemote {}

describe('getSqsQueueUrlForTask', () => {
it('should define queue url accurately based on config and task name', async () => {
const url = await getSqsQueueUrlForTask(AsyncTaskEmitToRemote, {
config: {
aws: {
account: '__account_id__',
},
project: 'svc-doitall',
environment: {
access: 'prod',
},
},
});
expect(url).toEqual(
'https://sqs.us-east-1.amazonaws.com/__account_id__/svc-doitall-prod-async-task-emit-to-remote-llq',
);
});
});
30 changes: 30 additions & 0 deletions src/logic/sqs/getSqsQueueUrlForTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { paramCase } from 'change-case';
import type { DomainObject } from 'domain-objects';

import { AsyncTask } from '../../domain/objects/AsyncTask';

export type ClassOf<T> = new (...args: any[]) => T;

export const getSqsQueueUrlForTask = async (
input: ClassOf<DomainObject<AsyncTask>>,
context: {
config: {
aws: {
account: string;
};
project: string;
environment: {
access: 'prod' | 'dev' | 'test';
};
};
},
) => {
const taskName = input.name;
return [
'https://sqs.us-east-1.amazonaws.com',
`/${context.config.aws.account}/`,
`${context.config.project}-${context.config.environment.access}-${paramCase(
taskName,
)}-llq`,
].join('');
};

0 comments on commit 034a3b4

Please sign in to comment.