How to use the @marblejs/core.r.matchPath function in @marblejs/core

To help you get started, we’ve selected a few @marblejs/core 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 marblejs / marble / packages / @integration / src / effects / user.effects.ts View on Github external
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$),
    mergeMap(req => of(req).pipe(
      map(req => req.params.id),
      switchMap(Dao.getUserById),
      map(user => ({ body: user })),
      catchError(() => throwError(
        new HttpError('User does not exist', HttpStatus.NOT_FOUND)
      ))
    )),
  )));

const postUser$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
github marblejs / marble / packages / @integration / src / websockets / http.server.ts View on Github external
matchEvent,
  ServerEvent,
  httpListener,
  HttpServerEffect,
  useContext,
} from '@marblejs/core';
import { mapToServer, MarbleWebSocketServer } from '@marblejs/websockets';
import { logger$ } from '@marblejs/middleware-logger';
import { merge } from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { websocketsServer } from './websockets.server';

export const WsServerToken = createContextToken('MarbleWebSocketServer');

const root$ = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect((req$, { ask }) => {
    const wsServer = useContext(WsServerToken)(ask);

    return req$.pipe(
      tap(() => wsServer.sendBroadcastResponse({ type: 'ROOT', payload: 'Hello' })),
      map(body => ({ body })),
    );
  }));

const upgrade$: HttpServerEffect = (event$, { ask }) =>
  event$.pipe(
    matchEvent(ServerEvent.upgrade),
    mapToServer({
      path: '/api/:version/ws',
      server: ask(WsServerToken),
github marblejs / marble / packages / @integration / src / effects / user.effects.ts View on Github external
r.matchPath('/:id'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    use(getUserValidator$),
    mergeMap(req => of(req).pipe(
      map(req => req.params.id),
      switchMap(Dao.getUserById),
      map(user => ({ body: user })),
      catchError(() => throwError(
        new HttpError('User does not exist', HttpStatus.NOT_FOUND)
      ))
    )),
  )));

const postUser$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    use(postUserValidator$),
    map(req => req.body),
    mergeMap(Dao.postUser),
    map(body => ({ body })),
  )));

export const user$ = combineRoutes('/user', {
  middlewares: [authorize$],
  effects: [getUserList$, getUser$, postUser$],
});
github marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
server.sendBroadcastResponse({ type: 'ROOT', payload: message })),
    ),
    map(message => ({ body: message })),
  )));

const notImplemented$ = r.pipe(
  r.matchPath('/error'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    mergeMap(() => throwError(
      new HttpError('Route not implemented', HttpStatus.NOT_IMPLEMENTED, { reason: 'Not implemented' })
    )),
  )));

const webSockets$ = r.pipe(
  r.matchPath('/ws'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    switchToProtocol('websocket')
  )));

const notFound$ = r.pipe(
  r.matchPath('*'),
  r.matchType('*'),
  r.useEffect(req$ => req$.pipe(
    mergeMap(() => throwError(
      new HttpError('Route not found', HttpStatus.NOT_FOUND)
    )),
  )));

export const api$ = combineRoutes(
  '/api/:version',
github marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
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 })),
  )));

const notImplemented$ = r.pipe(
  r.matchPath('/error'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    mergeMap(() => throwError(
      new HttpError('Route not implemented', HttpStatus.NOT_IMPLEMENTED, { reason: 'Not implemented' })
    )),
  )));

const webSockets$ = r.pipe(
  r.matchPath('/ws'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    switchToProtocol('websocket')
  )));

const notFound$ = r.pipe(
  r.matchPath('*'),
github marblejs / marble / packages / middleware-multipart / src / specs / multipart.middleware.spec.ts View on Github external
import * as fs from 'fs';
import * as path from 'path';
import * as request from 'supertest';
import { map } from 'rxjs/operators';
import { r, use, httpListener, createContext } from '@marblejs/core';
import { multipart$ } from '../multipart.middleware';
import { streamFileTo } from '../multipart.util';

const TMP_PATH = path.resolve(__dirname, '../../../../tmp');

const memoryMultipart$ = r.pipe(
  r.matchPath('/memory'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    use(multipart$()),
    map(req => ({ body: {
      files: req.files,
      fields: req.body,
    }}))
  )));

const memoryWithOptionsMultipart$ = r.pipe(
  r.matchPath('/memory-with-options'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    use(multipart$({
      maxFileCount: 1,
      maxFieldCount: 2,
github marblejs / marble / packages / @integration / src / effects / static.effects.ts View on Github external
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,
      },
    }))
  )));

const getFileStream$ = r.pipe(
  r.matchPath('/stream/:dir*'),
github marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
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 })),
  )));

const notImplemented$ = r.pipe(
  r.matchPath('/error'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    mergeMap(() => throwError(
github marblejs / marble / packages / serverless / src / +awsLambda / spec / awsLambda.spec-setup.ts View on Github external
import { HttpEffectResponse, httpListener, r } from '@marblejs/core';
import { map, mapTo } from 'rxjs/operators';
import { createLambda, ProxyType } from '../../';
import { awsApiGatewayMiddleware$ } from '../awsLambda.middleware';
import { bodyParser$ } from '@marblejs/middleware-body';
import { Readable } from 'stream';

const effect$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    map((req): HttpEffectResponse => ({
      status: 200,
      body: {
        path: req.path,
        body: req.body,
        event: req.apiGatewayEvent,
        context: req.apiGatewayContext,
      },
    }))
  )),
);

const chunkedTransferEncodingEffect$ = r.pipe(
  r.matchPath('/chunked-transfer'),
github marblejs / marble / packages / @integration / src / effects / static.effects.ts View on Github external
}))
  )));

const getFileStream$ = r.pipe(
  r.matchPath('/stream/:dir*'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    use(getFileValidator$),
    map(req => req.params.dir),
    map(dir => fs.createReadStream(path.resolve(STATIC_PATH, dir))),
    map(body => ({ body })),
  )),
);

const getFile$ = r.pipe(
  r.matchPath('/:dir*'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
    use(getFileValidator$),
    map(req => req.params.dir),
    mergeMap(readFile(STATIC_PATH)),
    map(body => ({ body }))
  )));

export const static$ = combineRoutes(
  '/static',
  [ postFile$, getFileStream$, getFile$ ],
);