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

Return types #44

Merged
merged 3 commits into from
Nov 2, 2023
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
57 changes: 6 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ and type safe.
- [`decoreParameters`](#decodeParameters)
- [`verifyJwt`](#verifyjwt)
- [`verifyXsrfToken`](#verifyxsrftoken)
- [Routing](#routing)

## Install

Expand All @@ -35,28 +34,31 @@ npm install thirty
import { APIGatewayProxyEvent } from 'aws-lambda';
import { compose, eventType } from 'thirty/core';
import { parseJson } from 'thirty/parseJson';
import { serializeJson } from 'thirty/serializeJson';
import { verifyJwt, tokenFromHeaderFactory } from 'thirty/verifyJwt';
import { handleHttpErrors } from 'thirty/handleHttpErrors';
import { inject } from 'thirty/inject';
import { APIGatewayProxyResult } from 'thrirty/types/APIGatewayProxyResult';

export const handler = compose(
eventType<APIGatewayProxyEvent>(),
types<APIGatewayProxyEvent, Promise<APIGatewayProxyResult>>(),
inject({
authService: authServiceFactory,
userService: userServiceFactory,
}),
handleHttpErrors(),
parseJson(),
verifyJwt({
getToken: tokenFromHeaderFactory(),
getSecretOrPublic: ({ deps }) => deps.authService.getSecret(),
}),
parseJson(),
serializeJson(),
)(async event => {
const { userService } = event.deps;
const user = await userService.createUser(event.jsonObject);
return {
statusCode: 201,
body: JSON.stringify(user),
body: user,
};
});
```
Expand Down Expand Up @@ -361,50 +363,3 @@ export const handler = compose(
// ...
});
```

## Routing

`routing` is a wrapper for the actual handler function to define multiple routes and their corresponding handlers:

```typescript
import { createRoutes } from 'thirty/createRoutes';

export const handler = compose(
eventType<APIGatewayProxyEvent>(),
inject({
userService: () => ({
/*...*/
}),
}),
parseJson(),
)(
createRoutes(router => {
router.get('/users', ({ deps }) => {
return {
statusCode: 200,
body: JSON.stringify(deps.userService.getUsers()),
};
});

router.post('/users', async ({ deps, jsonBody }) => {
return {
statusCode: 201,
body: JSON.stringify(deps.userService.createUser(jsonBody)),
};
});

router.get('/users/:id', async ({ deps, params }) => {
const user = deps.userService.getUserById(params.id);
if (user) {
return {
statusCode: 200,
body: JSON.stringify(user),
};
}
return {
statusCode: 404,
};
});
}),
);
```
Loading
Loading