-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api gateway v2 with config (#13)
* 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
Showing
22 changed files
with
386 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
|
@@ -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
48
packages/sls-aws/src/api-gateway/__tests__/response.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { createSuccessResponse } from './response' | ||
export { apiGatewayV2 } from './handler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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", | ||
|
Oops, something went wrong.