Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
const Positive = t.brand(
t.number,
(n): n is t.Branded => n >= 0,
'Positive'
);
type Positive = t.TypeOf;
export const articleSchema = t.type({
title: t.string,
markdown: t.string,
html: t.string,
tags: t.array(t.string),
slug: t.string,
published: t.boolean,
publishedAt: t.union([t.string, t.null]),
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;import { HttpEffect, HttpError, HttpStatus, use } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { forkJoin, of, throwError } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { UserDao } from '../../user/model/user.dao';
import { createHash$ } from '../helpers/hash';
import { generateTokenFromUser } from '../helpers/token.helper';
import { User } from '../../user/model/user.model';
import { InstanceType } from 'typegoose';
const userSchema = t.type({
email: t.string,
password: t.string,
firstName: t.string,
lastName: t.string,
});
export type UserPayload = t.TypeOf;
const throwIfUserAlreadyExists = (body: UserPayload) =>
UserDao.findByEmail(body.email).pipe(
mergeMap(user =>
null != user
? throwError(new HttpError('This email already exists', HttpStatus.CONFLICT))
: of(body)
)
);
const createUser = (body: UserPayload) =>
forkJoin(of(body), createHash$(body.password)).pipe(import { HttpEffect, HttpError, HttpStatus, use } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { forkJoin, of, throwError } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { UserDao } from '../../user/model/user.dao';
import { createHash$ } from '../helpers/hash';
import { generateTokenFromUser } from '../helpers/token.helper';
import { User } from '../../user/model/user.model';
import { InstanceType } from 'typegoose';
const userSchema = t.type({
email: t.string,
password: t.string,
firstName: t.string,
lastName: t.string,
});
export type UserPayload = t.TypeOf;
const throwIfUserAlreadyExists = (body: UserPayload) =>
UserDao.findByEmail(body.email).pipe(
mergeMap(user =>
null != user
? throwError(new HttpError('This email already exists', HttpStatus.CONFLICT))
: of(body)
)
);
const createUser = (body: UserPayload) =>
forkJoin(of(body), createHash$(body.password)).pipe(
mergeMap(([payload, password]) =>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 { 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 { neverNullable } from '../../../utils/never-nullable';
import { ArticleDao } from '../model/article.dao';
const validator$ = requestValidator$({
params: t.type({
slug: t.string,
}),
});
export const getArticleBySlugEffect$: HttpEffect = req$ =>
req$.pipe(
use(validator$),
mergeMap(req =>
of(req.params.slug).pipe(
mergeMap(ArticleDao.findByTitle),
mergeMap(neverNullable),
map(article => ({ body: article })),
catchError(() => throwError(new HttpError('Article does not exist', HttpStatus.NOT_FOUND)))
)
)
);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 { throwIfNotAdmin } from '../../user/helpers/throw-if-not-admin';
import { checkPassword } from '../helpers/check-password';
import { getUser } from '../helpers/get-user-by-email';
import { generateTokenFromUser } from '../helpers/token.helper';
const credentialsSchema = t.type({
email: t.string,
password: t.string,
});
export type CredentialsPayload = t.TypeOf;
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)))