Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: filled out more documentation and tests #9

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
types: [created]

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -17,10 +17,9 @@ jobs:
node-version: 12
- run: npm ci
- run: npm test
- run: npm run build

publish-npm:
needs: build
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -29,22 +28,7 @@ jobs:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm build
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#### 1.0.2 (2021-03-08)

##### Bug Fixes

* removing github publish. updating README. (1ea94d4a)

##### Other Changes

* filled out more documentation and tests (71961ba7)

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ exports.handler = async (event: APIGatewayProxyEvent) => {

### Using the `withStatusCode` function

To use the `withStatusCode` you only _need_ to specify the response code and the request origin (for CORS). An example of a simple 200 response is as follows:
To use the `withStatusCode` you only _need_ to specify the response code when declaring the type of response. It is recommended to pass an approved origin for the request if applicable when calling that function. An example of a simple 200 response is as follows:

```
import util from 'lambda-restful-util'
...
const ok = util.withStatusCode(200, 'http://localhost:8080')
const ok = util.withStatusCode(200)

exports.handler = async (event: APIGatewayProxyEvent) => {
...
return ok('Hey Buddy!')
return ok('Hey Buddy!', 'http://localhost:8080')
}
```

Expand All @@ -77,7 +77,7 @@ If you know your response is going to be JSON this will simplify converting your

```
...
const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify)
const ok = util.withStatusCode(util.HttpStatusCode.OK, JSON.stringify)
...
const res = {
name: 'Homer Simpson'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-restful-util",
"version": "1.0.1",
"version": "1.0.2",
"description": "A lightweight utility for Lambda API development",
"repository": "[email protected]:CodeForBaltimore/lambda-restful-util.git",
"author": "Jason Anton <[email protected]>",
Expand Down
34 changes: 30 additions & 4 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ describe('Validate package', function () {
it('withStatusCode invalid HTTP code', async function () {
expect(function () {
const httpCode = 42
app.withStatusCode(httpCode, 'http://localhost:8080')
app.withStatusCode(httpCode)
}).to.throw('status code out of range')
})
it('withStatusCode 200 HTTP code', async function () {
const ok = app.withStatusCode(app.HttpStatusCode.OK, 'http://localhost:8080', JSON.stringify)
it('withStatusCode 200 secure', async function () {
const ok = app.withStatusCode(app.HttpStatusCode.OK, JSON.stringify)
const bad = app.withStatusCode(app.HttpStatusCode.BAD_REQUEST, JSON.stringify)
const example = {
name: 'Homer Simpson',
}
Expand All @@ -26,6 +27,31 @@ describe('Validate package', function () {
body: '{"name":"Homer Simpson"}'
}

expect(ok(example)).to.deep.equal(goodOutput)
const insecureOutput: HttpResponse = {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: '{"name":"Homer Simpson"}'
}

expect(ok(example, 'http://localhost:8080')).to.deep.equal(goodOutput)
expect(bad(example)).to.deep.equal(insecureOutput)
})
it('withStatusCode 400 insecure', async function () {
const bad = app.withStatusCode(app.HttpStatusCode.BAD_REQUEST)
const example = 'test'

const insecureOutput: HttpResponse = {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: 'test'
}

expect(bad(example)).to.deep.equal(insecureOutput)
})
})
52 changes: 42 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// index.ts
/**
* A AWS Lambda helper package
*
* @module
*/
import HttpStatusCode from './HttpStatusCode'
import { APIGatewayProxyEvent } from 'aws-lambda'

Expand All @@ -12,19 +18,40 @@ export interface HttpResponse {
}

/**
* Will return fully formatted and ready
* HTTP response for Lambda delivery
* @param statusCode
* @param origin
* @param format
* Will return fully formatted and ready HTTP response for Lambda delivery
*
* @param statusCode - An HTTP response code
* @param format - If you need to parse your body send the parser here
*
* @example
* Sets a function to return a 200 OK response
* ```ts
* const ok = util.withStatusCode(200, JSON.stringify)
* const bad = util.withStatusCode(400)
* ```
*
* @returns A function that can be called to send an HTTP response
*/
const withStatusCode = (statusCode: number, origin: string, format?: Function): Function => {
const withStatusCode = (statusCode: number, format?: Function): Function => {
if (100 > statusCode || statusCode > 599) {
throw new Error('status code out of range')
}

// return a function that will take some data and formats a response with a status code
return (data: string | Record<string, unknown> | Array<any> | void): HttpResponse => {
/**
* The function that sends the HTTP response
*
* @param data - The information you are sending
* @param origin - What domain can receive this response
*
* @example
* Returns a JSON stringified var body to a localhost domain
* ```ts
* return ok(body, 'http://localhost')
* ```
*
* @returns Formatted and parsed response
*/
return (data: string | Record<string, unknown> | Array<any> | void, origin = '*'): HttpResponse => {
const response: HttpResponse = {
statusCode: statusCode,
headers: {
Expand All @@ -44,8 +71,11 @@ const withStatusCode = (statusCode: number, origin: string, format?: Function):
}

/**
* Ensuring the header exists in the API request and then parses it
*
* @param apiGatewayProxyEvent - The event coming from the API Gateway request
*
* @param apiGatewayProxyEvent
* @returns The headers parsed into a Object
*/
const validateAndParseRequestHeaders = (apiGatewayProxyEvent: APIGatewayProxyEvent): Record<string, unknown> | null => {
if (apiGatewayProxyEvent !== null && apiGatewayProxyEvent.headers !== null && apiGatewayProxyEvent.headers !== undefined) {
Expand All @@ -58,8 +88,10 @@ const validateAndParseRequestHeaders = (apiGatewayProxyEvent: APIGatewayProxyEve
}

/**
* Ensuring the body eixists in the API request and then parses it
* @param apiGatewayProxyEvent - The event coming from the API Gateway request
*
* @param apiGatewayProxyEvent
* @returns The body parsed into an object
*/
const validateAndParseRequestBody = (apiGatewayProxyEvent: APIGatewayProxyEvent): string | null => {
if (apiGatewayProxyEvent !== null && apiGatewayProxyEvent.body !== null && apiGatewayProxyEvent.body !== undefined) {
Expand Down