improvement - example code structure and route nesting
MODIFY
example/app.ts
example/app.ts
import { bodyParser$, httpListener, logger$ } from '../src';
-import { apiV1$, hello$, root$ } from './controllers';
+import { api$ } from './controllers/api.controller';
const middlewares = [
logger$,
@@ -7,9 +7,7 @@ const middlewares = [
];
const effects = [
- root$,
- hello$,
- apiV1$,
+ api$,
];
export const app = httpListener({ middlewares, effects });

DELETE
example/controllers.ts
-import { map } from 'rxjs/operators';
-import { Effect, combineRoutes, matchPath, matchType } from '../src';
-
-export const root$: Effect = request$ => request$
- .pipe(
- matchPath('/'),
- matchType('GET'),
- map(req => ({
- body: { data: `API root @ ${req.url}` },
- }))
- );
-
-export const hello$: Effect = request$ => request$
- .pipe(
- matchPath('/hello'),
- matchType('POST'),
- map(req => ({
- body: { data: `Hello, ${req.body.data}! @ ${req.url}` },
- }))
- );
-
-
-const route1$: Effect = request$ => request$
- .pipe(
- matchPath('/route1'),
- matchType('GET'),
- map(req => ({
- body: { data: `1 @ ${req.url}` },
- }))
- );
-
-export const apiV1$ = combineRoutes(
- '/api/v1',
- [ route1$ ],
-);

ADD
example/controllers/api.controller.ts
+import { map } from 'rxjs/operators';
+import { Effect, combineRoutes, matchPath, matchType } from '../../src';
+import { user$ } from './user.controller';
+
+const root$: Effect = request$ => request$
+ .pipe(
+ matchPath('/'),
+ matchType('GET'),
+ map(req => ({
+ body: { data: `API root @ ${req.url}` },
+ }))
+ );
+
+export const api$ = combineRoutes(
+ '/api/v1',
+ [ root$, user$ ],
+);

ADD
example/controllers/user.controller.ts
+import { map } from 'rxjs/operators';
+import { Effect, combineRoutes, matchPath, matchType } from '../../src';
+
+const getUser$: Effect = request$ => request$
+ .pipe(
+ matchPath('/'),
+ matchType('GET'),
+ map(() => ({ id: '123', firstName: 'Jozef', lastName: 'Flakus' })),
+ map(body => ({ body }))
+ );
+
+const postUser$: Effect = request$ => request$
+ .pipe(
+ matchPath('/'),
+ matchType('POST'),
+ map(req => ({ data: req.body })),
+ map(body => ({ body }))
+ );
+
+export const user$ = combineRoutes(
+ '/user',
+ [ getUser$, postUser$ ],
+);

improvement - example code structure and route nesting