From 034a3b4099aa5780aa088b00a3aa37480d86fbe7 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Thu, 1 Aug 2024 07:18:24 -0400 Subject: [PATCH] feat(sqs): expose utility to get sqs queue url from task name --- src/index.ts | 1 + src/logic/sqs/getSqsQueueUrlForTask.test.ts | 40 +++++++++++++++++++++ src/logic/sqs/getSqsQueueUrlForTask.ts | 30 ++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/logic/sqs/getSqsQueueUrlForTask.test.ts create mode 100644 src/logic/sqs/getSqsQueueUrlForTask.ts diff --git a/src/index.ts b/src/index.ts index 7cb9474..3371b59 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +export { getSqsQueueUrlForTask as getQueueUrlForTask } from './logic/sqs/getSqsQueueUrlForTask'; export { extractTaskParcelFromSqsEvent } from './logic/extractTaskParcelFromSqsEvent'; export { withAsyncTaskExecutionLifecycleEnqueue, diff --git a/src/logic/sqs/getSqsQueueUrlForTask.test.ts b/src/logic/sqs/getSqsQueueUrlForTask.test.ts new file mode 100644 index 0000000..269961d --- /dev/null +++ b/src/logic/sqs/getSqsQueueUrlForTask.test.ts @@ -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 + 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', + ); + }); +}); diff --git a/src/logic/sqs/getSqsQueueUrlForTask.ts b/src/logic/sqs/getSqsQueueUrlForTask.ts new file mode 100644 index 0000000..6b752b0 --- /dev/null +++ b/src/logic/sqs/getSqsQueueUrlForTask.ts @@ -0,0 +1,30 @@ +import { paramCase } from 'change-case'; +import type { DomainObject } from 'domain-objects'; + +import { AsyncTask } from '../../domain/objects/AsyncTask'; + +export type ClassOf = new (...args: any[]) => T; + +export const getSqsQueueUrlForTask = async ( + input: ClassOf>, + 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(''); +};