How to use the @marblejs/middleware-io.t.type function in @marblejs/middleware-io

To help you get started, we’ve selected a few @marblejs/middleware-io examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github edbzn / reactive-blog / packages / server / src / api / article / helpers / article-body.validator.ts View on Github external
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;

export const articleValidator$ = requestValidator$({ body: articleSchema });
github edbzn / reactive-blog / packages / server / src / api / article / effects / get-article-by-slug.effect.ts View on Github external
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)))
      )
    )
  );
github edbzn / reactive-blog / packages / server / src / api / article / helpers / article-body.validator.ts View on Github external
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;

export const articleValidator$ = requestValidator$({ body: articleSchema });
github edbzn / reactive-blog / packages / server / src / api / article / effects / post-article-reaction.effect.ts View on Github external
import { requestValidator$, t } from '@marblejs/middleware-io';
import { of, throwError } from 'rxjs';
import { catchError, map, mapTo, mergeMap } from 'rxjs/operators';
import { InstanceType } from 'typegoose';

import { neverNullable } from '../../../utils/never-nullable';
import { ReactionType } from '../model/article-reactions';
import { ArticleDao } from '../model/article.dao';
import { Article } from '../model/article.model';

export const articleReactionSchema = t.type({
  reaction: t.union([t.literal('unicorn'), t.literal('mark'), t.literal('heart')]),
});

const validator$ = requestValidator$({
  params: t.type({
    id: t.string,
  }),
  body: articleReactionSchema,
});

const applyReaction = (reaction: ReactionType) =&gt; (article: InstanceType<article>) =&gt; {
  const count = ++article.reactions.types[reaction].count;
  article.reactions.types[reaction].count = count;

  return article;
};

export const updateArticleReactionEffect$: HttpEffect = req$ =&gt;
  req$.pipe(
    use(validator$),
    mergeMap(req =&gt;</article>
github marblejs / marble / packages / @integration / src / effects / static.effects.ts View on Github external
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,
      },
    }))
github marblejs / marble / packages / @integration / src / effects / user.effects.ts View on Github external
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 })),
  )));

const getUser$ = r.pipe(
  r.matchPath('/:id'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    use(getUserValidator$),
github edbzn / reactive-blog / packages / server / src / api / article / helpers / article-collection-query.validator.ts View on Github external
export const articleCollectionQuery = (options: ArticleCollectionQueryOpts) =>
  t.intersection([
    createQuery(options),
    t.type({
      tags: t.union([t.string, t.array(t.string), t.undefined]),
    }),
  ]);