Skip to content

Commit

Permalink
fix: prettier and CI setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed May 1, 2024
1 parent 16eb340 commit ff6adbc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Linting
on: [push]

jobs:
eslint:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ jobs:
- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn build

- name: Install latest npm
run: npm install -g npm@latest

Expand All @@ -39,4 +36,4 @@ jobs:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
NPM_CONFIG_PROVENANCE: true
run: yarn release
run: yarn release
10 changes: 3 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v4
- name: Use Node ${{ matrix.node-version }}
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
node-version: 20
cache: yarn
- run: yarn
- run: yarn test
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ yarn add @includable/serverless-middleware
## Example usage

```js
import { middleware, auth } from '@includable/serverless-middleware';
import { middleware, auth } from "@includable/serverless-middleware";

const dependencies = {
// dependencies for the dependency injector
// dependencies for the dependency injector
};

export const app = async ({ query, path, body }, { currentUser, /* dependences */ }) => {
// if `auth` is included in the second param of `middleware`, currentUser
// will be an object in the form of `{ id, groups, email, ... }`
export const app = async (
{ query, path, body },
{ currentUser /* dependences */ },
) => {
// if `auth` is included in the second param of `middleware`, currentUser
// will be an object in the form of `{ id, groups, email, ... }`

// your business logic goes here
// your business logic goes here

return {
success: true,
text: 'Hello, world!'
};
}
return {
success: true,
text: "Hello, world!",
};
};

export const handler = middleware(app, [auth]).register(dependencies);
```
Expand All @@ -38,14 +41,12 @@ export const handler = middleware(app, [auth]).register(dependencies);

### Warmup support

Out of the box this middleware setup supports the [serverless-plugin-warmup](https://github.com/FidelLimited/serverless-plugin-warmup)
serverless plugin.
Out of the box this middleware setup supports the [serverless-plugin-warmup](https://github.com/FidelLimited/serverless-plugin-warmup)
serverless plugin.

Simply install the serverless plugin, no other changes to your code necessary.
Simply install the serverless plugin, no other changes to your code necessary.
The middleware will automatically prevent code execution on warmup requests.



<br /><br />

---
Expand Down
42 changes: 23 additions & 19 deletions src/policies/currentUser.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const currentUser = ({ event }) => {
const { requestContext: { authorizer } } = event;
const { claims } = authorizer.jwt || authorizer; // Dealing with difference between production and sls-offline
const {
requestContext: { authorizer },
} = event;
const { claims } = authorizer.jwt || authorizer; // Dealing with difference between production and sls-offline

if (!claims || !claims.sub || !claims.sub.length) {
throw new Error('Unauthorized');
}
if (!claims || !claims.sub || !claims.sub.length) {
throw new Error("Unauthorized");
}

const claimGroups = claims['cognito:groups'] ? claims['cognito:groups'] : '[]';
const groups = claimGroups
.substring(1, claimGroups.length - 1)
.split(' ')
.filter(Boolean);
const claimGroups = claims["cognito:groups"]
? claims["cognito:groups"]
: "[]";
const groups = claimGroups
.substring(1, claimGroups.length - 1)
.split(" ")
.filter(Boolean);

function hasGroup(group) {
return groups.includes(group);
}
function hasGroup(group) {
return groups.includes(group);
}

return {
...claims,
id: claims.sub,
groups,
hasGroup
};
return {
...claims,
id: claims.sub,
groups,
hasGroup,
};
};

module.exports = currentUser;
54 changes: 27 additions & 27 deletions tests/eventbridge.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
const middleware = require('../src');
const middleware = require("../src");

it('eventbridge events are passed through', async() => {
const payload = {
'version': '0',
'id': '03f6b260-9ee8-af5f-4a02-95ebf5fc0925',
'detail-type': 'ArtworkUpdate',
'source': 'some-other-system',
'account': '493638157050',
'time': '2020-11-10T10:36:28Z',
'region': 'eu-west-1',
'resources': [],
'detail': {
'id': 31329,
'action': 'create',
}
};
it("eventbridge events are passed through", async () => {
const payload = {
version: "0",
id: "03f6b260-9ee8-af5f-4a02-95ebf5fc0925",
"detail-type": "ArtworkUpdate",
source: "some-other-system",
account: "493638157050",
time: "2020-11-10T10:36:28Z",
region: "eu-west-1",
resources: [],
detail: {
id: 31329,
action: "create",
},
};

const exampleApp = middleware(async(event) => {
return event;
}).register(() => ({}));
const exampleApp = middleware(async (event) => {
return event;
}).register(() => ({}));

expect(JSON.parse(JSON.stringify(await exampleApp(payload)))).toEqual({
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
isBase64Encoded: false,
statusCode: 200,
});
expect(JSON.parse(JSON.stringify(await exampleApp(payload)))).toEqual({
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json; charset=utf-8",
},
isBase64Encoded: false,
statusCode: 200,
});
});
18 changes: 10 additions & 8 deletions tests/warmup.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const middleware = require('../src');
const middleware = require("../src");

it('responds correctly to serverless-plugin-warmup', async() => {
const exampleApp = middleware(async() => {
return 'Hello, world!';
}).register(() => ({}));
it("responds correctly to serverless-plugin-warmup", async () => {
const exampleApp = middleware(async () => {
return "Hello, world!";
}).register(() => ({}));

expect(await exampleApp({
source: 'serverless-plugin-warmup'
})).toEqual('Lambda is warm');
expect(
await exampleApp({
source: "serverless-plugin-warmup",
}),
).toEqual("Lambda is warm");
});

0 comments on commit ff6adbc

Please sign in to comment.