Skip to content

Commit

Permalink
feat: express middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Ottun committed Nov 15, 2021
1 parent 25e2dd8 commit 3978734
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 16 deletions.
14 changes: 0 additions & 14 deletions examples/docker-compose.secure.yml

This file was deleted.

53 changes: 53 additions & 0 deletions examples/with-express-middleware/mocks/mountain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"request": {
"path": "/api/weather",
"method": "GET"
},
"response": {
"statusCode": 200,
"body": {
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 350
},
"clouds": {
"all": 1
},
"dt": 1560350645,
"sys": {
"type": 1,
"id": 5122,
"message": 0.0139,
"country": "US",
"sunrise": 1560343627,
"sunset": 1560396563
},
"timezone": -25200,
"id": 420006353,
"name": "Mountain View",
"cod": 200
}
}
}
6 changes: 6 additions & 0 deletions examples/with-express-middleware/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
18 changes: 18 additions & 0 deletions examples/with-express-middleware/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "deputy-with-express-middleware",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"@sayjava/deputy": "../../",
"next": "^12.0.3",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/react": "^17.0.34"
}
}
15 changes: 15 additions & 0 deletions examples/with-express-middleware/pages/api/weather.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createExpressMiddleware } from '@sayjava/deputy';

const mockMiddleware = createExpressMiddleware({ mocksDirectory: 'mocks' });
const callRemoteApi = (req, res) => {
console.log('Call some remote api');
res.send('result from remote api');
};

export default (req, res) => {
if (process.env.NODE_ENV !== 'production') {
return mockMiddleware(req, res);
}

return callRemoteApi(req, res);
};
42 changes: 42 additions & 0 deletions examples/with-express-middleware/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {useEffect, useState} from 'react'
export default () => {
const [state, setState] = useState(null)

useEffect(() => {
fetch('/api/weather')
.then(res => res.json())
.then(data => setState({error: null, data}))
.catch(error => setState({error, data: null}))
}, [])


if(!state) {
return <div>
Loading up weather
</div>
}

if(state.error) {
return <div style={{color: 'red'}}>
{state.error.message}
</div>
}

console.log(state.data)

return <div>
<h2>{state.data.name}'s Weather</h2>
<div>
<h4>Feels Like</h4>
<ul>
<li>{state.data.main.feels_like}</li>
<li>{state.data.main.humidity}</li>
</ul>
<h4>Weather</h4>
<ul>
<li>{state.data.weather[0].description}</li>
<li>{state.data.weather[0].main}</li>
</ul>
</div>
</div>
}
30 changes: 30 additions & 0 deletions examples/with-express-middleware/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const createExpressMiddleware = () => console.info('Coming soon');
import { createMiddleware } from './server';
import { MiddlewareConfig } from './types';
export const createExpressMiddleware = (config: MiddlewareConfig) => createMiddleware(config);
11 changes: 10 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { loadSSLCerts } from './ssl';
import { createAPIRouter } from './routes/api';
import { createMocksRouter } from './routes/mocks';
import { errorHandler, responseHandler, parseBodyHandler } from './routes/middleware';
import { DeputyConfig } from '../types';
import { DeputyConfig, MiddlewareConfig } from '../types';
import logger from './logger';

const defaultConfig: DeputyConfig = {
Expand Down Expand Up @@ -104,3 +104,12 @@ export const createServer = async (argConfig: DeputyConfig): Promise<App> => {
},
};
};

export const createMiddleware = (argConfig: MiddlewareConfig) => {
const config = Object.assign({}, defaultConfig, argConfig);
const engine = createEngine(config);
const apiRouter = createMocksRouter({ engine });
apiRouter.use(responseHandler);
apiRouter.use(errorHandler);
return apiRouter;
};
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ export interface DeputyConfig {
proxy?: boolean;
tslDomains?: Array<string>;
}

export interface MiddlewareConfig extends DeputyConfig {
enableAPI?: boolean;
}

0 comments on commit 3978734

Please sign in to comment.