How to use @marblejs/core - 10 common examples

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 / websockets / src / listener / specs / websocket.listener.spec.ts View on Github external
test('triggers connection error', done => {
      // given
      const error = new WebSocketConnectionError('Unauthorized', HttpStatus.UNAUTHORIZED);
      const connection$: WsConnectionEffect = req$ => req$.pipe(mergeMapTo(throwError(error)));
      const webSocketServer = webSocketListener({ connection$ });
      const targetClient1 = testBed.getClient(0);
      const targetClient2 = testBed.getClient(1);
      const server = testBed.getServer();
      const context = createContext();

      // when
      webSocketServer({ server }).run(context);

      // then
      merge(
        fromEvent(targetClient1, 'unexpected-response'),
        fromEvent(targetClient2, 'unexpected-response'),
      )
      .pipe(take(2), toArray())
      .subscribe(
        (data) => {
          expect(data[0][1].statusCode).toEqual(error.status);
          expect(data[1][1].statusCode).toEqual(error.status);
          expect(data[0][1].statusMessage).toEqual(error.message);
          expect(data[1][1].statusMessage).toEqual(error.message);
github marblejs / marble / packages / middleware-jwt / src / spec / jwt.middleware.spec.ts View on Github external
describe('JWT middleware', () => {
  let utilModule;
  let factoryModule;
  const context = createContext();
  const effectMeta = createEffectMetadata({ ask: lookup(context) });

  beforeEach(() => {
    jest.unmock('../jwt.util.ts');
    jest.unmock('../jwt.factory.ts');
    utilModule = require('../jwt.util.ts');
    factoryModule = require('../jwt.factory.ts');
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  test('authorize$ authorizes incoming request and saves JWT payload to "req.user"', done => {
    // given
    const mockedSecret = 'test_secret';
github marblejs / marble / packages / middleware-jwt / src / spec / jwt.middleware.spec.ts View on Github external
test('authorize$ throws error if incoming request is not authorized', done => {
    // given
    const mockedSecret = 'test_secret';
    const mockedToken = 'TEST_TOKEN';
    const mockedRequest = { headers: { authorization: `Bearer ${mockedToken}`} } as HttpRequest;
    const expectedError = new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED);

    const req$ = of(mockedRequest);
    const res = {} as HttpResponse;

    // when
    utilModule.parseAuthorizationHeader = jest.fn(() => mockedToken);
    factoryModule.verifyToken$ = jest.fn(() => () => throwError(expectedError));

    const middleware$ = authorize$({ secret: mockedSecret }, verifyPayload$)(req$, res, effectMeta);

    // then
    middleware$.subscribe(
      () => {
        fail(`Stream should throw an error`);
        done();
      },
github marblejs / marble / packages / middleware-jwt / src / spec / jwt.middleware.spec.ts View on Github external
test('authorize$ throws error if verifyPayload$ handler doesn\'t pass', done => {
    // given
    const mockedSecret = 'test_secret';
    const mockedToken = 'TEST_TOKEN';
    const mockedTokenPayload = { id: 'test_id_wrong' };
    const mockedRequest = { headers: { authorization: `Bearer ${mockedToken}`} } as HttpRequest;
    const expectedError = new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED);

    const req$ = of(mockedRequest);
    const res = {} as HttpResponse;

    // when
    utilModule.parseAuthorizationHeader = jest.fn(() => mockedToken);
    factoryModule.verifyToken$ = jest.fn(() => () => of(mockedTokenPayload));

    const middleware$ = authorize$({ secret: mockedSecret }, verifyPayload$)(req$, res, effectMeta);

    // then
    middleware$.subscribe(
      () => {
        fail(`Stream should throw an error`);
        done();
      },
github marblejs / marble / packages / websockets / src / listener / specs / websocket.listener.spec.ts View on Github external
test('triggers connection error', done => {
      // given
      const error = new WebSocketConnectionError('Unauthorized', HttpStatus.UNAUTHORIZED);
      const connection$: WsConnectionEffect = req$ => req$.pipe(mergeMapTo(throwError(error)));
      const webSocketServer = webSocketListener({ connection$ });
      const targetClient1 = testBed.getClient(0);
      const targetClient2 = testBed.getClient(1);
      const server = testBed.getServer();
      const context = createContext();

      // when
      webSocketServer({ server }).run(context);

      // then
      merge(
        fromEvent(targetClient1, 'unexpected-response'),
        fromEvent(targetClient2, 'unexpected-response'),
      )
      .pipe(take(2), toArray())
github marblejs / marble / packages / middleware-jwt / src / spec / jwt.middleware.spec.ts View on Github external
describe('JWT middleware', () => {
  let utilModule;
  let factoryModule;
  const context = createContext();
  const effectMeta = createEffectMetadata({ ask: lookup(context) });

  beforeEach(() => {
    jest.unmock('../jwt.util.ts');
    jest.unmock('../jwt.factory.ts');
    utilModule = require('../jwt.util.ts');
    factoryModule = require('../jwt.factory.ts');
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  test('authorize$ authorizes incoming request and saves JWT payload to "req.user"', done => {
    // given
    const mockedSecret = 'test_secret';
    const mockedToken = 'TEST_TOKEN';
github marblejs / marble / packages / websockets / src / error / specs / ws-error.handler.spec.ts View on Github external
test('handles error if error$ is defined', () => {
    // given
    const client = { sendResponse: jest.fn() } as any as MarbleWebSocketClient;
    const error = new EventError({ type: 'EVENT' }, '');
    const error$: WsErrorEffect = event$ => event$.pipe(
      mapTo({ type: error.event.type, error: {} }),
    );

    // when
    handleEffectsError(defaultMetadata, client, error$)(error);

    // then
    expect(client.sendResponse).toHaveBeenCalled();
  });
github marblejs / marble / packages / websockets / src / error / specs / ws-error.effect.spec.ts View on Github external
test('returns stream of error events for defined error object', () => {
    // given
    const incomingEvent = { type: 'TEST_EVENT' };
    const error = new EventError(incomingEvent, 'Test error message', { errorData: 'test_error_data' });
    const outgoingEvent = {
      type: incomingEvent.type,
      error: {
        message: error.message,
        data: error.data,
      },
    };

    // then
    Marbles.assertEffect(error$, [
      ['--a--', { a: incomingEvent }],
      ['--b--', { b: outgoingEvent }],
    ], { meta: { error } });
  });
github marblejs / marble / packages / websockets / src / error / specs / ws-error.handler.spec.ts View on Github external
test('does nothing if error$ is undefined', () => {
    // given
    const client = { sendResponse: jest.fn() } as any as MarbleWebSocketClient;
    const error = new EventError({ type: 'EVENT' }, '');

    // when
    handleEffectsError(defaultMetadata, client, undefined)(error);

    // then
    expect(client.sendResponse).not.toHaveBeenCalled();
  });
});
github edbzn / reactive-blog / packages / server / src / api / comment / index.ts View on Github external
import { getCommentByArticleEffect$ } from './effects/get-comment-by-article.effect';
import { postCommentByArticleEffect$ } from './effects/post-comment-by-article.effect';

const getCommentByArticle$ = EffectFactory.matchPath('/')
  .matchType('GET')
  .use(getCommentByArticleEffect$);

const postCommentByArticle$ = EffectFactory.matchPath('/')
  .matchType('POST')
  .use(postCommentByArticleEffect$);

// const removeCommentByArticle$ = EffectFactory.matchPath("/:commentId")
//   .matchType("DELETE")
//   .use(removeCommentEffect$);

export const comment$ = combineRoutes('/article/:articleId/comment', {
  effects: [getCommentByArticle$, postCommentByArticle$],
});