How to use the @marblejs/core.r.useEffect 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 / messaging / client.ts View on Github external
host: 'amqp://localhost:5672',
    queue: 'test_queue',
    queueOptions: { durable: false },
  },
});

const rootValiadtor$ = requestValidator$({
  params: t.type({
    number: t.string,
  }),
});

const test$ = r.pipe(
  r.matchPath('/test'),
  r.matchType('GET'),
  r.useEffect((req$, { ask }) => req$.pipe(
    mergeMap(() => useContext(ClientToken)(ask).emit({ type: 'TEST' })),
    mapTo(({ body: 'OK' })),
  )),
);

const fib$ = r.pipe(
  r.matchPath('/fib/:number'),
  r.matchType('GET'),
  r.useEffect((req$, ctx) => {
    const client = useContext(ClientToken)(ctx.ask);

    return req$.pipe(
      use(rootValiadtor$),
      map(req => Number(req.params.number)),
      mergeMap(number => forkJoin(
        client.send({ type: 'FIB', payload: number + 0 }),
github marblejs / marble / packages / middleware-multipart / src / specs / multipart.middleware.spec.ts View on Github external
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,
      maxFileSize: 20,
    })),
    map(req => ({ body: {
      files: req.files,
      fields: req.body,
    }}))
  )));

const filesystemMultipart$ = r.pipe(
  r.matchPath('/filesystem'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    use(multipart$({
github marblejs / marble / packages / @integration / src / effects / api.effects.ts View on Github external
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',
  [ root$, user$, static$, notImplemented$, webSockets$, notFound$ ],
);
github marblejs / marble / packages / @integration / src / websockets / http.server.ts View on Github external
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 / static.effects.ts View on Github external
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*'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
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'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
github marblejs / marble / packages / @integration / src / effects / user.effects.ts View on Github external
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$],
});