From 9aa4501efe1aef50ce3ed6bad9aa40033d75aafa Mon Sep 17 00:00:00 2001 From: Kamaz Date: Fri, 12 Feb 2021 19:45:53 +0000 Subject: [PATCH] fix: sns template, tests and add tests run part of master --- .github/workflows/master.yml | 5 +++++ packages/sls-aws/src/cognito/user-pool-handler.ts | 10 ++-------- packages/sls-aws/src/reponse/response-or-error.ts | 9 +++++++++ packages/sls-aws/src/sns/index.ts | 8 +++++--- packages/sls-env/src/__tests__/application.test.ts | 4 ++-- packages/sls-env/src/__tests__/dependencies.test.ts | 6 +++--- packages/sls-env/src/__tests__/error-handler.test.ts | 2 +- packages/sls-env/src/__tests__/index.test.ts | 2 +- packages/sls-env/src/__tests__/payload.test.ts | 2 +- packages/sls-env/src/__tests__/success-handler.test.ts | 2 +- 10 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 packages/sls-aws/src/reponse/response-or-error.ts diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index c1cbf29..40f05d4 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -49,6 +49,11 @@ jobs: - name: build packages run: yarn build + + - name: test + run: yarn test + env: + CI: true # at the moment it is being release manually # - name: publish packages # run: yarn publish:ci diff --git a/packages/sls-aws/src/cognito/user-pool-handler.ts b/packages/sls-aws/src/cognito/user-pool-handler.ts index cd6b4a9..220c755 100644 --- a/packages/sls-aws/src/cognito/user-pool-handler.ts +++ b/packages/sls-aws/src/cognito/user-pool-handler.ts @@ -1,15 +1,9 @@ -import { EnvironmentConfig, Handler, SlsEnvironment, SuccessParams, environment } from '@cabiri-io/sls-env' +import { EnvironmentConfig, Handler, SlsEnvironment, environment } from '@cabiri-io/sls-env' import type { Context, PreSignUpTriggerEvent, PreTokenGenerationTriggerEvent } from 'aws-lambda' +import { responseOrError } from '../reponse/response-or-error' type PreSignUpHandler = Handler> -const responseOrError = ({ result }: SuccessParams): T | never => { - if (result) { - return result - } - throw Error("expected event result to be present but it wasn't") -} - const cognitoUserPoolPreSignUp = ( config?: EnvironmentConfig ): SlsEnvironment => environment(config).successHandler(responseOrError) diff --git a/packages/sls-aws/src/reponse/response-or-error.ts b/packages/sls-aws/src/reponse/response-or-error.ts new file mode 100644 index 0000000..0e42708 --- /dev/null +++ b/packages/sls-aws/src/reponse/response-or-error.ts @@ -0,0 +1,9 @@ +import { SuccessParams } from '@cabiri-io/sls-env' + +const responseOrError = ({ result }: SuccessParams): T | never => { + if (result) { + return result + } + throw Error("expected event result to be present but it wasn't") +} +export { responseOrError } diff --git a/packages/sls-aws/src/sns/index.ts b/packages/sls-aws/src/sns/index.ts index a8b5cd8..ea7b6fa 100644 --- a/packages/sls-aws/src/sns/index.ts +++ b/packages/sls-aws/src/sns/index.ts @@ -1,15 +1,17 @@ import { EnvironmentConfig, Handler, SlsEnvironment, environment } from '@cabiri-io/sls-env' import type { Context, SNSEvent } from 'aws-lambda' +import { responseOrError } from '../reponse/response-or-error' import { jsonSNSMessage } from './json-sns-message' import { jsonSNSMessages } from './json-sns-messages' -type SNSHandler = Handler +type SNSHandler = Handler> export const snsMessage = ( config?: EnvironmentConfig -): SlsEnvironment => environment(config).payload(jsonSNSMessage) +): SlsEnvironment => + environment(config).payload(jsonSNSMessage).successHandler(responseOrError) export const snsMessages = ( config?: EnvironmentConfig ): SlsEnvironment> => - environment>(config).payload(jsonSNSMessages) + environment>(config).payload(jsonSNSMessages).successHandler(responseOrError) diff --git a/packages/sls-env/src/__tests__/application.test.ts b/packages/sls-env/src/__tests__/application.test.ts index b107558..dfaa174 100644 --- a/packages/sls-env/src/__tests__/application.test.ts +++ b/packages/sls-env/src/__tests__/application.test.ts @@ -6,14 +6,14 @@ describe('serverless environment', () => { it('uses application to return response', async () => environment, never, void, void>() .app(() => 'hello world!') - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .start({}, {}) .then(result => expect(result).toBe('hello world!'))) it('uses application to return promise based response', async () => environment, never, void, void>() .app(async () => 'hello world!') - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .start({}, {}) .then(result => expect(result).toBe('hello world!'))) diff --git a/packages/sls-env/src/__tests__/dependencies.test.ts b/packages/sls-env/src/__tests__/dependencies.test.ts index 9cbdf51..0b268e3 100644 --- a/packages/sls-env/src/__tests__/dependencies.test.ts +++ b/packages/sls-env/src/__tests__/dependencies.test.ts @@ -17,7 +17,7 @@ describe('serverless environment', () => { .app(({ payload: { event, context }, dependencies: { buildMessage } }) => buildMessage(event.message, context.name) ) - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .start({ message: 'hello' }, { name: 'world' }) .then(result => expect(result).toBe('hello world!')) }) @@ -35,7 +35,7 @@ describe('serverless environment', () => { .app(({ payload: { event, context }, dependencies: { buildMessage } }) => buildMessage(event.message, context.name) ) - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .start({ message: 'hello' }, { name: 'world' }) .then(result => expect(result).toBe('hello world!')) }) @@ -55,7 +55,7 @@ describe('serverless environment', () => { .app(({ payload: { event, context }, dependencies: { buildMessage } }) => buildMessage(event.message, context.name) ) - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .start({ message: 'hello' }, { name: 'world' }) .then(result => expect(result).toBe('hello world of config!')) }) diff --git a/packages/sls-env/src/__tests__/error-handler.test.ts b/packages/sls-env/src/__tests__/error-handler.test.ts index ca946f5..a670c2b 100644 --- a/packages/sls-env/src/__tests__/error-handler.test.ts +++ b/packages/sls-env/src/__tests__/error-handler.test.ts @@ -7,7 +7,7 @@ describe('serverless environment', () => { type AppResult = string type HandlerResult = { message: string } const result = await environment, void, never, void, AppResult>() - .successHandler(({ result }) => ({ message: result })) + .successHandler(({ result }) => ({ message: result as string })) .app(() => 'app result') .start({}, {}) diff --git a/packages/sls-env/src/__tests__/index.test.ts b/packages/sls-env/src/__tests__/index.test.ts index 735f939..4f143e0 100644 --- a/packages/sls-env/src/__tests__/index.test.ts +++ b/packages/sls-env/src/__tests__/index.test.ts @@ -6,7 +6,7 @@ xdescribe('serverless environment', () => { type AppResult = string type HandlerResult = { message: string } const result = await environment, void, never, void, AppResult>() - .successHandler(({ result }) => ({ message: result })) + .successHandler(({ result }) => ({ message: result as string })) .app(() => 'app result') .start({}, {}) diff --git a/packages/sls-env/src/__tests__/payload.test.ts b/packages/sls-env/src/__tests__/payload.test.ts index 4005ba2..5f9e7fa 100644 --- a/packages/sls-env/src/__tests__/payload.test.ts +++ b/packages/sls-env/src/__tests__/payload.test.ts @@ -31,7 +31,7 @@ describe('serverless environment', () => { hello: event.message, world: context.name })) - .successHandler(({ result }) => result) + .successHandler(({ result }) => result as string) .app(({ payload, dependencies: { buildMessage } }) => buildMessage(payload)) .start({ message: 'hello' }, { name: 'world' }) .then(result => expect(result).toBe('hello world!')) diff --git a/packages/sls-env/src/__tests__/success-handler.test.ts b/packages/sls-env/src/__tests__/success-handler.test.ts index 1afddcb..527d095 100644 --- a/packages/sls-env/src/__tests__/success-handler.test.ts +++ b/packages/sls-env/src/__tests__/success-handler.test.ts @@ -7,7 +7,7 @@ describe('serverless environment', () => { type AppResult = string type HandlerResult = { message: string } const result = await environment, void, never, void, AppResult>() - .successHandler(({ result }) => ({ message: result })) + .successHandler(({ result }) => ({ message: result as string })) .app(() => 'app result') .start({}, {})