Skip to content

Commit

Permalink
fix: sns template, tests and add tests run part of master
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaz committed Feb 12, 2021
1 parent 9c62053 commit 9aa4501
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions packages/sls-aws/src/cognito/user-pool-handler.ts
Original file line number Diff line number Diff line change
@@ -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<PreSignUpTriggerEvent, Context, Promise<PreSignUpTriggerEvent>>

const responseOrError = <T>({ result }: SuccessParams<T>): T | never => {
if (result) {
return result
}
throw Error("expected event result to be present but it wasn't")
}

const cognitoUserPoolPreSignUp = <D, C = never>(
config?: EnvironmentConfig<PreSignUpHandler>
): SlsEnvironment<PreSignUpHandler, C, D> => environment<PreSignUpHandler, C, D>(config).successHandler(responseOrError)
Expand Down
9 changes: 9 additions & 0 deletions packages/sls-aws/src/reponse/response-or-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SuccessParams } from '@cabiri-io/sls-env'

const responseOrError = <T>({ result }: SuccessParams<T>): T | never => {
if (result) {
return result
}
throw Error("expected event result to be present but it wasn't")
}
export { responseOrError }
8 changes: 5 additions & 3 deletions packages/sls-aws/src/sns/index.ts
Original file line number Diff line number Diff line change
@@ -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<SNSEvent, Context, void>
type SNSHandler = Handler<SNSEvent, Context, Promise<void>>

export const snsMessage = <D, P, C = never>(
config?: EnvironmentConfig<SNSHandler>
): SlsEnvironment<SNSHandler, C, D, P> => environment<SNSHandler, C, D, P>(config).payload(jsonSNSMessage)
): SlsEnvironment<SNSHandler, C, D, P> =>
environment<SNSHandler, C, D, P>(config).payload(jsonSNSMessage).successHandler(responseOrError)

export const snsMessages = <D, P, C = never>(
config?: EnvironmentConfig<SNSHandler>
): SlsEnvironment<SNSHandler, C, D, Array<P>> =>
environment<SNSHandler, C, D, Array<P>>(config).payload(jsonSNSMessages)
environment<SNSHandler, C, D, Array<P>>(config).payload(jsonSNSMessages).successHandler(responseOrError)
4 changes: 2 additions & 2 deletions packages/sls-env/src/__tests__/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ describe('serverless environment', () => {
it('uses application to return response', async () =>
environment<Handler<EmptyEvent, EmptyContext, string>, 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<Handler<EmptyEvent, EmptyContext, string>, never, void, void>()
.app(async () => 'hello world!')
.successHandler(({ result }) => result)
.successHandler(({ result }) => result as string)
.start({}, {})
.then(result => expect(result).toBe('hello world!')))

Expand Down
6 changes: 3 additions & 3 deletions packages/sls-env/src/__tests__/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'))
})
Expand All @@ -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!'))
})
Expand All @@ -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!'))
})
Expand Down
2 changes: 1 addition & 1 deletion packages/sls-env/src/__tests__/error-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('serverless environment', () => {
type AppResult = string
type HandlerResult = { message: string }
const result = await environment<Handler<EmptyEvent, EmptyContext, HandlerResult>, void, never, void, AppResult>()
.successHandler(({ result }) => ({ message: result }))
.successHandler(({ result }) => ({ message: result as string }))
.app(() => 'app result')
.start({}, {})

Expand Down
2 changes: 1 addition & 1 deletion packages/sls-env/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ xdescribe('serverless environment', () => {
type AppResult = string
type HandlerResult = { message: string }
const result = await environment<Handler<EmptyEvent, EmptyContext, HandlerResult>, void, never, void, AppResult>()
.successHandler(({ result }) => ({ message: result }))
.successHandler(({ result }) => ({ message: result as string }))
.app(() => 'app result')
.start({}, {})

Expand Down
2 changes: 1 addition & 1 deletion packages/sls-env/src/__tests__/payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'))
Expand Down
2 changes: 1 addition & 1 deletion packages/sls-env/src/__tests__/success-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('serverless environment', () => {
type AppResult = string
type HandlerResult = { message: string }
const result = await environment<Handler<EmptyEvent, EmptyContext, HandlerResult>, void, never, void, AppResult>()
.successHandler(({ result }) => ({ message: result }))
.successHandler(({ result }) => ({ message: result as string }))
.app(() => 'app result')
.start({}, {})

Expand Down

0 comments on commit 9aa4501

Please sign in to comment.