Skip to content
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
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ npm run dev

The framework is written in TypeScript and can :

- Serve data from static files (JSON or text)
- Serve media : Images and Videos
- Serve markdown files
- Be used to write and test AWS Lambda Functions
- Use custom middleware to transform input/output
- Serve random Database seed data
- Serve persisted mock data to the database
- Perform CRUD operations on the local database via a REST endpoint
- Serve data from static files (JSON or text)
- Serve media : Images and Videos
- Serve markdown files
- Be used to write and test AWS Lambda Functions
- Use custom middleware to transform input/output
- Serve random Database seed data
- Serve persisted mock data to the database
- Perform CRUD operations on the local database via a REST endpoint

### Setting up a new API route

Expand All @@ -111,7 +111,7 @@ mkdir src/api/users

#### IMPORTANT

Rember to use 'npm run dev' to start a local dev server when developing new API endpoints.
Remember to use 'npm run dev' to start a local dev server when developing new API endpoints.

Once endpoints have been developed and tested then use 'npm run start' to build the docker image and start a container to serve the endpoints on localhost (or use 'npm run rebuild' or 'npm run nuke' if changes need to be made to an existing docker image).

Expand Down Expand Up @@ -271,6 +271,31 @@ http://localhost:8000/api/json/demo

The templates directory contains some templates for different type of handlers, models and seeders but the demo api endpoints can just as easily be copied and modified for individual use cases.

### 13. Error Route

It can be useful to mock api errors in order to test frontend error handling logic.

To do this redirect frontend fetch requests to the api/error route.

```
http://localhost:8000/api/error
```

The default error for this route is a '404: not found' error but if specific errors are required, then this can be customised by passing 'status' to the endpoint and also a custom 'message' if required.

E.g to mimic a 500 'Internal Server Error'

```
http://localhost:8000/api/error?status=500&message=Internal%20Server%20Error
```

this will return a 500 error code and the JSON response below:

```
{"error":"500: Internal Server Error"}

```

## Customisation

### Changing api url prefix
Expand Down
24 changes: 24 additions & 0 deletions cypress/e2e/error-endpoint-spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe('default mock error endpoint works as expected', () => {
it('checks default error endpoint is running', () => {
cy.request({ url: '/api/error', failOnStatusCode: false }).then(
(response) => {
expect(response.status).to.eq(404);
expect(response.body).to.be.jsonSchema({
error: '404: Not Found',
});
},
);
});

it('checks default error endpoint is running', () => {
cy.request({
url: '/api/error?status=500&message=Internal%20Server%20Error',
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(500);
expect(response.body).to.be.jsonSchema({
error: '500: Internal Server Error',
});
});
});
});
1 change: 1 addition & 0 deletions cypress/e2e/server-page-spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Server page contains expected information', () => {
'/api/posts',
'/api/users',
'/api/videos',
'/api/error',
];
cy.visit('/');
cy.get('h1').contains('Running');
Expand Down
83 changes: 41 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@
"validate-branch-name": "bash validate-branch-name.sh"
},
"dependencies": {
"@faker-js/faker": "^9.6.0",
"@faker-js/faker": "^9.7.0",
"@mswjs/data": "^0.16.2",
"@mswjs/http-middleware": "^0.10.3",
"@types/chai-json-schema": "^1.4.10",
"@types/express": "^5.0.1",
"@types/markdown-it": "^14.1.2",
"chai-json-schema": "^1.5.1",
"dotenv": "^16.4.7",
"dotenv": "^16.5.0",
"highlight.js": "^11.11.1",
"markdown-it": "^14.1.0",
"msw": "^2.7.3",
"msw": "^2.7.4",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"typescript": "^5.8.3",
"zod": "^3.24.2"
},
"devDependencies": {
"@commitlint/cli": "^19.8.0",
"@commitlint/config-conventional": "^19.8.0",
"@types/aws-lambda": "^8.10.148",
"@types/node": "^22.13.13",
"cypress": "^14.2.0",
"@types/aws-lambda": "^8.10.149",
"@types/node": "^22.14.1",
"cypress": "^14.3.0",
"husky": "^9.1.7",
"lint-staged": "^15.5.0",
"lint-staged": "^15.5.1",
"prettier": "3.5.3",
"start-server-and-test": "^2.0.11",
"xo": "^0.60.0"
Expand Down
25 changes: 25 additions & 0 deletions src/api/error/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'node:fs';
import path from 'node:path';
import { http, HttpResponse } from 'msw';

function handler(pathName: string) {
return [
http.get(`/${pathName}`, ({ request }) => {
const url = new URL(request.url);

const statusCode = Number.parseInt(
url.searchParams.get('status') ?? '404',
10,
);

const errorMessage = url.searchParams.get('message') ?? 'Not Found';

return HttpResponse.json(
{ error: statusCode + ': ' + errorMessage },
{ status: statusCode },
);
}),
];
}

export default handler;