Skip to content

Commit

Permalink
feat: api gateway v2 with config (#13)
Browse files Browse the repository at this point in the history
* feat: api gateway with config

* feat: support value config

* feat: clean-up dependencies and pass config and logger to global

* chore: remove unused files

* chore: add success handler and update package version to beta

* chore(release): publish v0.0.3

* chore: add canary publishing

* chore: version package using canary

* chore: update package for carary script

* chore: update dependencies between packages to use file

* chore: force canary on env and aws packages

* chore: describe additional use cases

* feat: introduce mapping for success handler

* feat: add success response mapper

* fix: export create success response

* fix: linting issue for no unresolved aws-lambda module

* chore: enable confirmation before publishing packages

* fix: build before releasing change

* chore: publish ci published minor version
  • Loading branch information
kamaz authored Jan 5, 2021
1 parent 394005e commit 944af10
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
devDependencies: ['**/__tests__/**']
}
],
'import/no-unresolved': [2, { ignore: ['fsevents'] }],
'import/no-unresolved': [2, { ignore: ['fsevents', 'aws-lambda'] }],
'import/order': 2,
'no-console': 2,
'no-restricted-imports': [
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.3](https://github.com/cabiri-io/sls-pipe/compare/v0.0.2...v0.0.3) (2020-11-17)


### Features

* api gateway with config ([c8e49c0](https://github.com/cabiri-io/sls-pipe/commit/c8e49c019ed4830bb43b2d13bd56dbafa3c5aed3))
* clean-up dependencies and pass config and logger to global ([ffd5aa1](https://github.com/cabiri-io/sls-pipe/commit/ffd5aa1ca132207cd012f72008f1712fe7a1bb38))
* support value config ([0013bff](https://github.com/cabiri-io/sls-pipe/commit/0013bff60b2c29d66257128a4a02ba4820992d71))





## 0.0.2 (2020-07-29)


Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.0.2",
"version": "0.0.3",
"command": {
"version": {
"includeMergedTags": true,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"pretty-quick": "pretty-quick",
"prettify:fix": "prettier --write 'packages/**/*.{js,ts}'",
"prettify": "prettier --check 'packages/**/*.{js,ts}'",
"publish:ci": "lerna publish patch --yes --create-release github --conventional-commits"
"publish:canary": "yarn build && lerna publish --canary --force-publish=@cabiri-io/sls-env,@cabiri-io/sls-aws",
"publish:ci": "yarn build && lerna publish minor --create-release github --conventional-commits"
},
"workspaces": [
"packages/*"
Expand Down
8 changes: 8 additions & 0 deletions packages/sls-app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.3](https://github.com/cabiri-io/sls-pipe/compare/v0.0.2...v0.0.3) (2020-11-17)

**Note:** Version bump only for package @cabiri-io/sls-app





## 0.0.2 (2020-07-29)


Expand Down
2 changes: 1 addition & 1 deletion packages/sls-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cabiri-io/sls-app",
"version": "0.0.2",
"version": "0.0.3-alpha.1",
"author": "Cabiri <[email protected]>",
"license": "UNLICENSED",
"repository": {
Expand Down
11 changes: 11 additions & 0 deletions packages/sls-aws/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.3](https://github.com/cabiri-io/sls-pipe/compare/v0.0.2...v0.0.3) (2020-11-17)


### Features

* api gateway with config ([c8e49c0](https://github.com/cabiri-io/sls-pipe/commit/c8e49c019ed4830bb43b2d13bd56dbafa3c5aed3))





## 0.0.2 (2020-07-29)


Expand Down
8 changes: 5 additions & 3 deletions packages/sls-aws/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cabiri-io/sls-aws",
"version": "0.0.2",
"version": "0.0.3-alpha.1",
"author": "Cabiri <[email protected]>",
"license": "UNLICENSED",
"repository": {
Expand All @@ -16,9 +16,11 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"@cabiri-io/sls-env": "^0.0.2"
"@cabiri-io/sls-env": "file:../sls-env"
},
"devDependencies": {
"optionalDependencies": {
"@types/aws-lambda": "^8.10.59"
},
"devDependencies": {
}
}
48 changes: 48 additions & 0 deletions packages/sls-aws/src/api-gateway/__tests__/response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createSuccessResponse } from '../index'

describe('structure response', () => {
it('creates response with sensible defaults', () => {
const successResponse = createSuccessResponse()

const result = successResponse({ message: 'value' })

expect(result).toMatchInlineSnapshot(`
Object {
"body": "{\\"message\\":\\"value\\"}",
"statusCode": 200,
}
`)
})

it('allows override of status code', () => {
type Message = { message: string }
const successResponse = createSuccessResponse<Message, never>({ statusCode: 201 })

const result = successResponse({ message: 'value' })

expect(result).toMatchInlineSnapshot(`
Object {
"body": "{\\"message\\":\\"value\\"}",
"statusCode": 201,
}
`)
})

it('allows mapping between different types', () => {
type Message = { message: string }
type Echo = { value: string }

const message2Echo = (m: Message): Echo => ({ value: m.message })

const successResponse = createSuccessResponse<Message, Echo>({ statusCode: 201, mapper: message2Echo })

const result = successResponse({ message: 'hello' })

expect(result).toMatchInlineSnapshot(`
Object {
"body": "{\\"value\\":\\"hello\\"}",
"statusCode": 201,
}
`)
})
})
14 changes: 14 additions & 0 deletions packages/sls-aws/src/api-gateway/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EnvConfig, Handler, SlsEnvironment, environment } from '@cabiri-io/sls-env'
import type { APIGatewayProxyEventV2, APIGatewayProxyResultV2, Context } from 'aws-lambda'
import { createSuccessResponse } from './response'

type APIGatewayV2Handler<T = never> = Handler<APIGatewayProxyEventV2, Context, APIGatewayProxyResultV2<T>>

// fixme: or maybe we create a different versions of apiGatewayV2 for example
// apiGatewayV2<D, P, R>
// apiGatewayV2Config<D, P, R, C>
// apiGatewayV2CustomResponse<D, P, R, HR, C = never>
export const apiGatewayV2 = <D, P, R, C = never>(
config?: EnvConfig
): SlsEnvironment<APIGatewayV2Handler<never>, C, D, P, R> =>
environment<APIGatewayV2Handler<never>, C, D, P, R>(config).successHandler(createSuccessResponse())
2 changes: 2 additions & 0 deletions packages/sls-aws/src/api-gateway/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createSuccessResponse } from './response'
export { apiGatewayV2 } from './handler'
14 changes: 14 additions & 0 deletions packages/sls-aws/src/api-gateway/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'

type StructuredResult<T, R> = APIGatewayProxyStructuredResultV2 & { mapper?: (t: T) => R }

export const createSuccessResponse = <T, R = never>(config?: StructuredResult<T, R extends never ? T : R>) => (
t: T
): APIGatewayProxyStructuredResultV2 => {
const { mapper, statusCode, ...defaultValues } = config ?? {}
return {
statusCode: statusCode ?? 200,
...defaultValues,
body: JSON.stringify(mapper?.(t) ?? t)
}
}
13 changes: 2 additions & 11 deletions packages/sls-aws/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
import { EnvConfig, SlsEnvironment, environment } from '@cabiri-io/sls-env'
// eslint-disable-next-line import/no-unresolved
import type { Context, SNSEvent } from 'aws-lambda'
import { jsonSNSMessage } from './sns/json-sns-message'
import { jsonSNSMessages } from './sns/json-sns-messages'

export const snsMessage = <D, P>(config?: EnvConfig): SlsEnvironment<SNSEvent, Context, D, P, void> =>
environment<SNSEvent, Context, D, P, void>(config).payload(jsonSNSMessage)

export const snsMessages = <D, P>(config?: EnvConfig): SlsEnvironment<SNSEvent, Context, D, Array<P>, void> =>
environment<SNSEvent, Context, D, Array<P>, void>(config).payload(jsonSNSMessages)
export { snsMessage, snsMessages } from './sns'
export { apiGatewayV2, createSuccessResponse } from './api-gateway'
12 changes: 12 additions & 0 deletions packages/sls-aws/src/sns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EnvConfig, Handler, SlsEnvironment, environment } from '@cabiri-io/sls-env'
import type { Context, SNSEvent } from 'aws-lambda'
import { jsonSNSMessage } from './json-sns-message'
import { jsonSNSMessages } from './json-sns-messages'

type SNSHandler = Handler<SNSEvent, Context, void>

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

export const snsMessages = <D, P, C = never>(config?: EnvConfig): SlsEnvironment<SNSHandler, C, D, Array<P>> =>
environment<SNSHandler, C, D, Array<P>>(config).payload(jsonSNSMessages)
1 change: 0 additions & 1 deletion packages/sls-aws/src/sns/json-sns-message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line import/no-unresolved
import type { Context, SNSEvent } from 'aws-lambda'

import { PayloadError, PayloadParseError } from '../error/parse'
Expand Down
1 change: 0 additions & 1 deletion packages/sls-aws/src/sns/json-sns-messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line import/no-unresolved
import type { Context, SNSEvent } from 'aws-lambda'

import { PayloadParseError } from '../error/parse'
Expand Down
2 changes: 1 addition & 1 deletion packages/sls-aws/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"outDir": "dist"
}
}
13 changes: 13 additions & 0 deletions packages/sls-env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.3](https://github.com/cabiri-io/sls-pipe/compare/v0.0.2...v0.0.3) (2020-11-17)


### Features

* api gateway with config ([c8e49c0](https://github.com/cabiri-io/sls-pipe/commit/c8e49c019ed4830bb43b2d13bd56dbafa3c5aed3))
* clean-up dependencies and pass config and logger to global ([ffd5aa1](https://github.com/cabiri-io/sls-pipe/commit/ffd5aa1ca132207cd012f72008f1712fe7a1bb38))
* support value config ([0013bff](https://github.com/cabiri-io/sls-pipe/commit/0013bff60b2c29d66257128a4a02ba4820992d71))





## 0.0.2 (2020-07-29)


Expand Down
5 changes: 1 addition & 4 deletions packages/sls-env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cabiri-io/sls-env",
"version": "0.0.2",
"version": "0.0.3-alpha.1",
"author": "Cabiri <[email protected]>",
"license": "UNLICENSED",
"description": "Environment to build your serverless applications",
Expand All @@ -16,9 +16,6 @@
"homepage": "https://github.com/cabiri-io/sls-pipe#readme",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"lodash.camelcase": "^4.3.0"
},
"devDependencies": {
"@types/lodash.camelcase": "^4.3.6",
"@types/lodash.snakecase": "^4.1.6",
Expand Down
Loading

0 comments on commit 944af10

Please sign in to comment.