Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { EffectFactory, HttpError, HttpStatus, combineRoutes } from '@marblejs/core';
import { throwError } from 'rxjs';
import { mapTo, switchMap } from 'rxjs/operators';
import { auth$ } from './auth/auth.controller';
import { user$ } from './user/user.controller';
const root$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(req$ => req$.pipe(
mapTo({ body: `API version: v1` }),
));
const notFound$ = EffectFactory
.matchPath('*')
.matchType('*')
.use(req$ => req$.pipe(
switchMap(() =>
throwError(new HttpError('Route not found', HttpStatus.NOT_FOUND))
)
));
export const api$ = combineRoutes(
import { combineRoutes, EffectFactory } from '@marblejs/core';
import { authorize$ } from '../authentication/middlewares/auth.middleware';
import { getMeEffect$ } from './effects/get-me.effect';
import { getUsersEffect$ } from './effects/get-users.effect';
export const getUsers$ = EffectFactory.matchPath('/')
.matchType('GET')
.use(getUsersEffect$);
export const getMe$ = EffectFactory.matchPath('/me')
.matchType('GET')
.use(getMeEffect$);
export const user$ = combineRoutes('/user', {
effects: [getUsers$, getMe$],
middlewares: [authorize$],
});
import { EffectFactory, HttpError, HttpStatus } from '@marblejs/core';
import { switchMap } from 'rxjs/operators';
import { throwError } from 'rxjs';
export const notFound$ = EffectFactory
.matchPath('*')
.matchType('*')
.use(req$ => req$.pipe(
switchMap(() =>
throwError(new HttpError('Route not found', HttpStatus.NOT_FOUND))
)
));
const STATIC_PATH = path.resolve(__dirname, '../../../assets');
const authorize$: Effect = request$ =>
request$.pipe(
filter(req => req.headers.authorization === 'Bearer test'),
);
const root$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(req$ => req$
.pipe(
mapTo({ status: 200, body: 'root' }),
));
const getUserList$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(req$ => req$
.pipe(
switchMap(() => of(MOCKED_USER_LIST)),
map(users => ({ body: users }))
));
const getUserSingle$ = EffectFactory
.matchPath('/:id')
.matchType('GET')
.use(req$ => req$
.pipe(
map(({ params, query }) => ({ body: { params, query } }))
));
import { combineRoutes, EffectFactory } from '@marblejs/core';
import { versionEffect$, preflightEffect$, getFileEffect$, notFoundEffect$ } from './common/effects';
import { auth$ } from './auth';
import { users$ } from './users';
import { actors$ } from './actors';
import { movies$ } from './movies';
const root$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(versionEffect$);
const preflight$ = EffectFactory
.matchPath('*')
.matchType('OPTIONS')
.use(preflightEffect$);
const getFile$ = EffectFactory
.matchPath('/assets/:dir*')
.matchType('GET')
.use(getFileEffect$);
const notFound$ = EffectFactory
.matchPath('*')
import { auth$ } from './auth';
import { users$ } from './users';
import { actors$ } from './actors';
import { movies$ } from './movies';
const root$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(versionEffect$);
const preflight$ = EffectFactory
.matchPath('*')
.matchType('OPTIONS')
.use(preflightEffect$);
const getFile$ = EffectFactory
.matchPath('/assets/:dir*')
.matchType('GET')
.use(getFileEffect$);
const notFound$ = EffectFactory
.matchPath('*')
.matchType('*')
.use(notFoundEffect$);
export const api$ = combineRoutes('/api/v1', [
root$,
auth$,
users$,
actors$,
movies$,
getFile$,
.matchType('GET')
.use(getArticleByIdEffect$);
const getArticleBySlug$ = EffectFactory.matchPath('/slug/:slug')
.matchType('GET')
.use(getArticleBySlugEffect$);
const removeArticle$ = EffectFactory.matchPath('/:id')
.matchType('DELETE')
.use(removeArticleEffect$);
const updateArticle$ = EffectFactory.matchPath('/:id')
.matchType('PUT')
.use(updateArticleEffect$);
const updateArticleReaction$ = EffectFactory.matchPath('/:id/reaction')
.matchType('POST')
.use(updateArticleReactionEffect$);
const postArticle$ = EffectFactory.matchPath('/')
.matchType('POST')
.use(postArticleEffect$);
export const article$ = combineRoutes('/article', {
effects: [getArticleList$, getArticleById$, getArticleBySlug$, updateArticleReaction$],
});
export const authorizedArticle$ = combineRoutes('/article', {
effects: [postArticle$, removeArticle$, updateArticle$],
middlewares: [authorize$],
});
.matchType('GET')
.use(req$ => req$
.pipe(
mapTo({ status: 200, body: 'root' }),
));
const getUserList$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(req$ => req$
.pipe(
switchMap(() => of(MOCKED_USER_LIST)),
map(users => ({ body: users }))
));
const getUserSingle$ = EffectFactory
.matchPath('/:id')
.matchType('GET')
.use(req$ => req$
.pipe(
map(({ params, query }) => ({ body: { params, query } }))
));
const postUser$ = EffectFactory
.matchPath('/')
.matchType('POST')
.use(req$ => req$
.pipe(
use(authorize$),
map(req => req.body),
map(response => ({ body: response }))
));
.matchType('GET')
.use(getArticleBySlugEffect$);
const removeArticle$ = EffectFactory.matchPath('/:id')
.matchType('DELETE')
.use(removeArticleEffect$);
const updateArticle$ = EffectFactory.matchPath('/:id')
.matchType('PUT')
.use(updateArticleEffect$);
const updateArticleReaction$ = EffectFactory.matchPath('/:id/reaction')
.matchType('POST')
.use(updateArticleReactionEffect$);
const postArticle$ = EffectFactory.matchPath('/')
.matchType('POST')
.use(postArticleEffect$);
export const article$ = combineRoutes('/article', {
effects: [getArticleList$, getArticleById$, getArticleBySlug$, updateArticleReaction$],
});
export const authorizedArticle$ = combineRoutes('/article', {
effects: [postArticle$, removeArticle$, updateArticle$],
middlewares: [authorize$],
});
export const authorizedDraft$ = combineRoutes('/draft', {
effects: [getDraftList$],
middlewares: [authorize$],
});
.pipe(
use(authorize$),
map(req => req.body),
map(response => ({ body: response }))
));
const error$ = EffectFactory
.matchPath('/error')
.matchType('GET')
.use(req$ => req$
.pipe(
switchMap(() => throwError(new HttpError('test', HttpStatus.METHOD_NOT_ALLOWED, { test: 'test' }))),
)
);
const file$ = EffectFactory
.matchPath('/static/:dir')
.matchType('GET')
.use(req$ => req$
.pipe(
map(req => req.params!.dir as string),
switchMap(readFile(STATIC_PATH)),
map(body => ({ body }))
));
const user$ = combineRoutes('/user', [getUserList$, getUserSingle$, postUser$]);
const api$ = combineRoutes('/api/:version', [root$, file$, error$, user$]);
const app = httpListener({
middlewares: [bodyParser$],
effects: [api$],