diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 74efc44..92da053 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -8,7 +8,7 @@ on: types: [created] jobs: - build: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -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 @@ -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}} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5d1d503 --- /dev/null +++ b/CHANGELOG.md @@ -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) + diff --git a/README.md b/README.md index e4a0786..b602f8b 100644 --- a/README.md +++ b/README.md @@ -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') } ``` @@ -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' diff --git a/package.json b/package.json index 421bb1f..17ed49b 100644 --- a/package.json +++ b/package.json @@ -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": "git@github.com:CodeForBaltimore/lambda-restful-util.git", "author": "Jason Anton ", diff --git a/src/index.spec.ts b/src/index.spec.ts index c083f0f..97116d6 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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', } @@ -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) }) }) diff --git a/src/index.ts b/src/index.ts index 15a8221..50fb6a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,9 @@ +// index.ts +/** + * A AWS Lambda helper package + * + * @module + */ import HttpStatusCode from './HttpStatusCode' import { APIGatewayProxyEvent } from 'aws-lambda' @@ -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 | Array | 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 | Array | void, origin = '*'): HttpResponse => { const response: HttpResponse = { statusCode: statusCode, headers: { @@ -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 | null => { if (apiGatewayProxyEvent !== null && apiGatewayProxyEvent.headers !== null && apiGatewayProxyEvent.headers !== undefined) { @@ -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) {