https://github.com/neki-dev/moccu
📟 Simple typescript mock server with memory context
https://github.com/neki-dev/moccu
api context memory mock proxy response serve server
Last synced: 4 months ago
JSON representation
📟 Simple typescript mock server with memory context
- Host: GitHub
- URL: https://github.com/neki-dev/moccu
- Owner: neki-dev
- License: mit
- Created: 2024-12-16T23:25:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-30T08:44:04.000Z (over 1 year ago)
- Last Synced: 2025-10-06T03:45:06.822Z (9 months ago)
- Topics: api, context, memory, mock, proxy, response, serve, server
- Language: TypeScript
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 📟 Moccu
[](https://npmjs.com/package/moccu)
Simple typescript mock server with memory context
.
* ## Install
```sh
npm i moccu --save-dev
```
.
* ## Run
Run the server by command
```sh
moccu
```
.
* ## Configuration
Create and configure `moccu.config.ts` file at project root. Or it will created automatically on the first start.
```ts
import type { Config } from 'moccu';
const config: Config = {
/**
* Server port
*/
port: 3000,
/**
* API url prefix
*/
base: '',
/**
* List of mocked routes
*/
routes: [],
/**
* Logger level
*/
logger: 'main',
};
export default config;
```
.
* ## Mock route
#### 1. Create mocked route
`./__mocks__/get-user.ts`
```ts
import type { Route, Request } from 'moccu';
const route: Route = {
/**
* Request path
*/
path: '/user/:userId',
/**
* Request method
*/
method: 'get',
/**
* Response status
*/
status: 200,
/**
* Response body
*/
response: (req: Request) => {
return {
text: `Hello, ${req.params.userId}`,
};
},
/**
* Response delay
*/
delay: 100,
};
export default route;
```
#### 2. Import mocked route to global config
`./moccu.config.ts`
```ts
import type { Config } from 'moccu';
import getUser from './__mocks__/get-user';
const config: Config = {
port: 3000,
base: '/api',
routes: [
getUser,
],
};
export default config;
```
.
* ## Context example
We can change mock responses based on the global context between requests.
`./moccu.config.ts`
```ts
import type { Config, Request } from 'moccu';
import { Context } from 'moccu';
type MockContext = {
name: string;
};
const config: Config = {
port: 3000,
base: '/api',
routes: [
{
method: 'get',
path: '/greet',
response: () => {
const ctx = Context.use('testContext');
return {
message: `Hello, ${ctx.name ?? 'Noname'}!`,
};
},
},
{
method: 'put',
path: '/rename',
response: (req: Request) => {
const ctx = Context.use('testContext');
ctx.name = req.body.value ?? 'Unnamed';
},
},
],
};
export default config;
```