Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { combineRoutes, HttpError, HttpStatus, use, r } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { throwError, of } from 'rxjs';
import { map, switchMap, catchError, mergeMap } from 'rxjs/operators';
import { Dao } from '../fakes/dao.fake';
import { authorize$ } from '../middlewares/auth.middleware';
const getUserValidator$ = requestValidator$({
params: t.type({ id: t.string }),
});
const postUserValidator$ = requestValidator$({
body: t.type({
user: t.type({ id: t.string })
}),
});
const getUserList$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mergeMap(Dao.getUsers),
map(users => ({ body: users })),
)));import { r, HttpError, HttpStatus, combineRoutes, use, switchToProtocol } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { throwError } from 'rxjs';
import { map, mergeMap, tap } from 'rxjs/operators';
import { user$ } from './user.effects';
import { static$ } from './static.effects';
import { WsServerToken } from '../tokens';
const rootValiadtor$ = requestValidator$({
params: t.type({
version: t.union([
t.literal('v1'),
t.literal('v2'),
]),
}),
});
const root$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect((req$, _, { ask }) => req$.pipe(
use(rootValiadtor$),
map(req => req.params.version),
map(version => `API version: ${version}`),
tap(message => ask(WsServerToken).map(server =>posterUrl: t.union([t.string, t.null]),
metaTitle: t.union([t.string, t.null]),
metaDescription: t.union([t.string, t.null]),
reactions: t.type({
types: t.type({
unicorn: t.type({ count: Positive }),
heart: t.type({ count: Positive }),
mark: t.type({ count: Positive }),
}),
}),
lang: t.union([t.literal(ArticleLanguage.FR), t.literal(ArticleLanguage.EN)]),
});
export type ArticlePayload = t.TypeOf;
export const articleValidator$ = requestValidator$({ body: articleSchema });export const getCommentByArticleEffect$: HttpEffect = req$ =>
req$.pipe(
use(requestValidator$(commentQuerySchema)),
mergeMap(req =>
of(req).pipe(
map(req => req.query),
mergeMap(() =>
CommentDao.findAllByArticle(req.params.articleId, {
sortBy: '_id',
sortDir: SortDir.DESC,
limit: 5,
page: 1,
})
),
map(commentCollection => ({ body: commentCollection })),
catchError(err => throwError(new HttpError(err, HttpStatus.INTERNAL_SERVER_ERROR)))
)
)
);export const loginEffect$: HttpEffect = req$ =>
req$.pipe(
use(requestValidator$({ body: credentialsSchema })),
mergeMap(req =>
of(req).pipe(
map(req => req.body),
mergeMap(checkPassword),
mergeMap(getUser),
mergeMap(throwIfNotAdmin),
mergeMap(generateTokenFromUser),
map(token => ({ body: { token } })),
catchError(() => throwError(new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED)))
)
)
);export const postCommentByArticleEffect$: HttpEffect = req$ =>
req$.pipe(
use(requestValidator$({ body: commentSchema })),
map(req => req.body),
mergeMap(CommentDao.create),
map(article => ({ body: article }))
);import { HttpEffect, use } from '@marblejs/core';
import { requestValidator$ } from '@marblejs/middleware-io';
import { of, throwError } from 'rxjs';
import { catchError, map, mapTo, mergeMap } from 'rxjs/operators';
import { articleSchema } from '../helpers/article-body.validator';
import { ArticleDao } from '../model/article.dao';
const validator$ = requestValidator$({
body: articleSchema,
});
export const postArticleEffect$: HttpEffect = req$ =>
req$.pipe(
use(validator$),
mergeMap(req =>
of(req).pipe(
mapTo(req.body),
mergeMap(ArticleDao.create),
map(article => ({ body: article })),
catchError(err => throwError(err))
)
)
);import { HttpEffect, HttpError, HttpStatus, use } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { of, throwError } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { ArticleDao } from '../model/article.dao';
const validator$ = requestValidator$({
params: t.type({
id: t.string,
}),
});
export const removeArticleEffect$: HttpEffect = req$ =>
req$.pipe(
use(validator$),
mergeMap(req =>
of(req.params.id).pipe(
mergeMap(ArticleDao.removeById),
map(() => ({ body: null })),
catchError(err => throwError(new HttpError(err, HttpStatus.INTERNAL_SERVER_ERROR)))
)
)
);import * as fs from 'fs';
import * as path from 'path';
import { r, combineRoutes, use } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { multipart$ } from '@marblejs/middleware-multipart';
import { streamFileTo } from '@marblejs/middleware-multipart/dist/multipart.util';
import { readFile } from '@marblejs/core/dist/+internal';
import { map, mergeMap } from 'rxjs/operators';
const STATIC_PATH = path.resolve(__dirname, '../../../../assets');
const TMP_PATH = path.resolve(__dirname, '../../../../tmp');
const getFileValidator$ = requestValidator$({
params: t.type({ dir: t.string })
});
const postFile$ = r.pipe(
r.matchPath('/upload'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$({
files: ['image_1'],
stream: streamFileTo(TMP_PATH),
})),
map(req => ({
body: {
file: req.files,
body: req.body,
},