Skip to content

Commit

Permalink
Merge pull request #88 from MathRobin/esm-only
Browse files Browse the repository at this point in the history
Esm only
  • Loading branch information
MathRobin authored Nov 17, 2023
2 parents a119eda + e296ab4 commit aa4d596
Show file tree
Hide file tree
Showing 31 changed files with 4,166 additions and 3,797 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# GENERATED FILE - Do not edit manually
root = true

[*]
Expand Down
14 changes: 10 additions & 4 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# GENERATED FILE - Do not edit manually
env:
node: true
es2021: true
es2023: true
jest/globals: true
plugins:
- jest
ignorePatterns:
- "**/*.test.js"
- json
extends:
- eslint:recommended
- prettier
- plugin:json/recommended
parserOptions:
ecmaVersion: 2021
ecmaVersion: 2022
sourceType: module
requireConfigFile: false
babelOptions:
plugins:
- '@babel/plugin-syntax-import-assertions'
parser: '@babel/eslint-parser'
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
30 changes: 26 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@ on: push
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
version: [18, 19, 20, 21]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.version }}
- uses: actions/checkout@v4
- name: System info
run: |
echo "node"
node -v
echo "npm"
npm -v
echo "yarn"
yarn -v
echo "yarn enableImmutableInstalls"
yarn config get enableImmutableInstalls
- name: Allow modify yarn.lock
run: yarn config set -H enableImmutableInstalls false
- name: Install modules
run: yarn
- name: Run lint
run: yarn lint
run: yarn install
- name: Run lint tasks
run: |
yarn lint
yarn prettier
- name: Run test
run: yarn test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Don't edit this part

.yarn/cache/*.zip
coverage_cjs

# Created by https://www.toptal.com/developers/gitignore/api/node,intellij+all,yarn
# Edit at https://www.toptal.com/developers/gitignore?templates=node,intellij+all,yarn
Expand Down
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint
yarn prettier:write
yarn test
git add index.esm.js index.cjs index.esm.d.ts
git add index.js index.d.ts
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.13.0
18.18.0
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# GENERATED FILE - Do not edit manually
# Ignore artifacts:
build
coverage
874 changes: 0 additions & 874 deletions .yarn/releases/yarn-3.6.4.cjs

This file was deleted.

893 changes: 893 additions & 0 deletions .yarn/releases/yarn-4.0.2.cjs

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defaultSemverRangePrefix: ""
compressionLevel: mixed

defaultSemverRangePrefix: ''

enableGlobalCache: false

nmMode: classic

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.6.4.cjs
yarnPath: .yarn/releases/yarn-4.0.2.cjs
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Static Badge](https://img.shields.io/badge/coverage-100-brightgreen)
![Static Badge](https://img.shields.io/badge/release-3.0.1-blue)
[![test](https://github.com/mathrobin/lambda-returns/actions/workflows/test.yml/badge.svg)](https://github.com/mathrobin/lambda-returns/actions/workflows/test.yml)

# lambda-returns

Provides shorthand to manage AWS lambda result. And provides test helper methods too!

ESM only since v3. Typings included.

## Usage

Deadly simple

```javascript
import { ok, internalServerError } from 'lambda-returns';

export default async () => {
try {
return ok({
status: 'success',
});
} catch (err) {
return internalServerError({
status: 'error',
error: err,
});
}
};
```

instead of that non-funny code:

```javascript
export default async () => {
try {
return {
statusCode: 200,
body: JSON.stringify({
status: 'success',
}),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({
status: 'error',
error: err,
}),
};
}
};
```

### Test helpers methods

Not enough for you? For me too. This package provides a simple way to test your return result from your AWS lambda
handler method.

```javascript
import { isOk, isBadRequest } from 'lambda-returns';

expect(isOk(result)).toBeTruthy();
expect(isBadRequest(result)).toBeTruthy();
```

## Pros

- No prod dependency
- Typings provided
- lower than 45kB unpacked

### Stop remember codes

You don't have to remember status code values. Just the name which is more "menaningful". Who really knows the code of:

- insufficientStorage
- partialContent
- imATeapot

You ? Me no. And don't want/need to.

### Stop polluting your business logic

Moreover, into vanilla AWS lambda way, you need to return a string as body. But just stringify your result is dangerous,
if you have a dynamic result, maybe is null, maybe is undefined, maybe you already have a string.

F\*ck! I don't want to care of it in my business logic! `lambda-returns` manages it for you.

## Cons

There is only one known pitfall. We can't technically export "continue" status due to it's a reserved word in
JavaScript.

```javascript
export const continue = {}; // or whatever;
```

This is just forbidden. If you knwon any way to go over this problem, tell me.

Despite to this problem, test helper method `isContinue` is available.
107 changes: 0 additions & 107 deletions build.js

This file was deleted.

58 changes: 58 additions & 0 deletions codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export default {
continue: 100,
switchingProtocols: 101,
processing: 102,
ok: 200,
created: 201,
accepted: 202,
nonAuthoritativeInformation: 203,
noContent: 204,
resetContent: 205,
partialContent: 206,
multiStatus: 207,
multipleChoices: 300,
movedPermanently: 301,
movedTemporarily: 302,
seeOther: 303,
notModified: 304,
useProxy: 305,
temporaryRedirect: 307,
badRequest: 400,
unauthorized: 401,
paymentRequired: 402,
forbidden: 403,
notFound: 404,
methodNotAllowed: 405,
notAcceptable: 406,
proxyAuthenticationRequired: 407,
requestTimeOut: 408,
conflict: 409,
gone: 410,
lengthRequired: 411,
preconditionFailed: 412,
requestEntityTooLarge: 413,
requestUriTooLarge: 414,
unsupportedMediaType: 415,
requestedRangeNotSatisfiable: 416,
expectationFailed: 417,
imATeapot: 418,
unprocessableEntity: 422,
locked: 423,
failedDependency: 424,
unorderedCollection: 425,
upgradeRequired: 426,
preconditionRequired: 428,
tooManyRequests: 429,
requestHeaderFieldsTooLarge: 431,
internalServerError: 500,
notImplemented: 501,
badGateway: 502,
serviceUnavailable: 503,
gatewayTimeOut: 504,
httpVersionNotSupported: 505,
variantAlsoNegotiates: 506,
insufficientStorage: 507,
bandwidthLimitExceeded: 509,
notExtended: 510,
networkAuthenticationRequired: 511,
};
Loading

0 comments on commit aa4d596

Please sign in to comment.