Hi there.
You'll need pnpm
installed. If you don't have it already you can enable corepack and it will automagically appear in proper version in this project.
corepack enable
After that install dependencies and you're good to go:
pnpm i
pnpm test
perform the jest unit testspnpm cdk deploy
deploy this stack to your default AWS account/regionpnpm cdk deploy --watch
deploy this stack to your default AWS account/region and watch for changespnpm cdk diff
compare deployed stack with current statepnpm cdk destroy
remove stack
API accepts and returns application/json
content-type.
All requests have to be authenticated by sending access_token in Authentication
header.
type Note = {
id: string | undefined;
title: string;
body: string;
};
type Error = {
message: string;
details: any | undefined;
};
-
POST /notes
- create a noteAccepts
Note
in body. Fieldid
should be left empty, as it's generated on the server.Returns
201
on success.
Returns422
on malformed request and sendsError
in response. -
GET /notes
- list all notesReturns
200
on success and sends aNote[]
in response. -
GET /notes/:id
- retrieve a noteReturns
200
on success and sends aNote
in response.
Returns404
on request to non-existent resource. -
POST /notes/:id
- update a noteAccepts
Note
in body. Fieldid
is overridden with with:id
from request parameter.Returns
204
on success.
Returns404
on request to non-existent resource.
Returns422
on malformed request and sendsError
in response. -
DELETE /notes/:id
- delete a noteReturns
204
on success.
Deleting non-existent resource is also treated as success and returns204
.
After deployment stack outputs signin-url. You can go there and register an user.
You'll be redirected to a demo callback function that returns oauth2 response - grab access_token
from there.
Alternatively: Change callback parameter in signin url to https://localhost
and fetch token manualy with your favourite API Client or if you preffer CLI with command like one bellow:
curl --request POST \
--url ${COGNITO_DOMAIN}/oauth2/token \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=authorization_code \
--data redirect_uri=https://localhost \
--data client_id=${CLIENT_ID} \
--data code=${CODE}
Although it's possible to run an API Gateway stack defined by AWS-CDK locally thanks to AWS-SAM, it's far from perfect. API Gateway Local will silently ignore the existence of any authorizers. That breaks the contract with Lambda integration.
That's why for local development I recommend relaying on unit / integration tests and than jump to remote execution on a development cloud deployment.
You need to have DynamoDB Local running. You can use provided docker-compose.yaml
:
docker compose up
Then run the test suite:
pnpm test -- --collectCoverage --verbose
Run tests with --maxWorkers=1
to prevent concurrent tests from working on the same DynamoDB Table (optional, tests are safe to run concurrently).
NOTE: You need to have AWS credentials
Run command below to deploy to your active AWS account and watch for changes.
pnpm cdk deploy --watch