How to use the @marblejs/middleware-io.t.literal 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 / effects / post-article-reaction.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, 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;
};
</article>
github edbzn / reactive-blog / packages / server / src / utils / collection-query.validator.ts View on Github external
export const createQuery = (opts: CollectionQueryValidatorOpts) =>
  t.partial({
    sortBy: t.union(opts.sortBy.map(s => t.literal(s)) as any),
    sortDir: t.union([
      t.refinement(t.union([t.string, t.number]), n => Number(n) === SortDir.ASC, 'SortDir.ASC'),
      t.refinement(t.union([t.string, t.number]), n => Number(n) === SortDir.DESC, 'SortDir.DESC'),
    ]),
    limit: t.refinement(t.union([t.string, t.number]), n => Number(n) >= 0, 'number.0+'),
    page: t.refinement(t.union([t.string, t.number]), n => Number(n) >= 1, 'number.1+'),
  });
github edbzn / reactive-blog / packages / server / src / api / article / helpers / article-body.validator.ts View on Github external
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 marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
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 =>
      server.sendBroadcastResponse({ type: 'ROOT', payload: message })),
    ),
    map(message => ({ body: message })),
github marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
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 =>
      server.sendBroadcastResponse({ type: 'ROOT', payload: message })),
    ),
    map(message => ({ body: message })),
  )));