LogoLogo
ChangelogGitHubTwitterGitter
v4.x
v4.x
  • Marble.js
  • Getting started
    • Installation
    • Quick setup
  • HTTP
    • Effects
    • Middlewares
    • Routing
    • Errors
    • Output
    • Context
    • Advanced
      • Logging
      • Validation
      • Server Events
      • Streaming
      • Continuous mode
  • Messaging
    • Core concepts
      • Events
      • Effects
    • Microservices
      • AMQP (RabbitMQ)
      • Redis Pub/Sub
    • CQRS
    • WebSockets
  • Testing
    • HTTP routes testing
  • Other
    • How does it glue together?
    • Migration guides
      • Migration from version 3.x
      • Migration from version 2.x
      • Migration from version 1.x
    • API reference
      • @marblejs/core
        • bindTo
        • bindEagerlyTo
        • createEvent
        • createContextToken
        • operator: matchEvent
        • operator: use
        • operator: act
      • @marblejs/http
        • httpListener
        • r.pipe
        • combineRoutes
        • createServer
      • @marblejs/messaging
        • eventBus
        • messagingClient
        • createMicroservice
        • reply
      • @marblejs/websockets
        • webSocketListener
        • operator: broadcast
        • operator: mapToServer
      • @marblejs/middleware-multipart
      • @marblejs/middleware-cors
      • @marblejs/middleware-io
      • @marblejs/middleware-logger
      • @marblejs/middleware-body
      • @marblejs-contrib/middleware-jwt
        • Token signing
      • @marblejs-contrib/middleware-joi
    • Style Guide
    • FAQ
Powered by GitBook
On this page
  • @marblejs/core
  • Type declarations
  • Effects - body, params, query
  • Effects - third argument
  • httpListener error effect
  • httpListener server bootstrapping
  • @marblejs/middleware-body
  • @marblejs/middleware-logger
  1. Other
  2. Migration guides

Migration from version 1.x

This chapter provides a set of guidelines to help you migrate from Marble.js version 1.x to version 2.x.

If you are new to Marble.js, starting with 2.x is easy-peasy. The biggest question for current 1.x users though, is how to migrate to the new version. You can ask, how many of the API has changed? Just relaxβ€Šβ€”β€Šabout 90% of the API is the same and the core concepts haven’t changed.

@marblejs/core

Type declarations

# Effect πŸ‘‰ HttpEffect

# EffectResponse πŸ‘‰ HttpEffectResponse

# Middleware πŸ‘‰ HttpMiddlewareEffect

# ErrorEffect πŸ‘‰HttpErrorEffect

Effects - body, params, query

In order to improve request type inference HTTP Effect req.params, req.body, req.query are by default of unknown type instead of any.

❌ Old:

const example$: Effect = (req$) =>
  req$.pipe(
    map(req => req.params.version),        // (typeof req.params) = any
    map(version => `Version: ${version}`), // (typeof version) = any
    map(message => ({ body: message })),
  );

βœ… New:

const example$: HttpEffect = (req$) =>
  req$.pipe(
    map(req => req.params.version),        // (typeof req.params) = unknown
    map(version => `Version: ${version}`), // ❌ type error!
    map(message => ({ body: message })),
  );
  
πŸ‘‡

const example$: HttpEffect = (req$) =>
  req$.pipe(
    map(req => req.params.version),        // (typeof req.params) = unknown
    map(version => version as string),     // or use validation middleware
    map(version => `Version: ${version}`), // βœ… looks fine!
    map(message => ({ body: message })),
  );

Effects - third argument

From version 2.0 the effect function third argument is a common EffectMetadata object which can contain eventual error object or contextual injector.

❌ Old:

const error$: ErrorEffect = (req$, _, error) =>
  req$.pipe(
    mapTo(error),
    // ...
  );

βœ… New:

const error$: HttpErrorEffect = (req$, _, meta) =>
  req$.pipe(
    mapTo(meta.error),
    // ...
  );

httpListener error effect

HTTP error effect is registered inside httpListener configuration object via error$ instead of errorEffect.

❌ Old:

export default httpListener({
  // ...
  errorEffect: // ...
});

βœ… New:

export default httpListener({
  // ...
  error$: // ...
});

httpListener server bootstrapping

In order to use httpListener directly connected to Node.js http.createServer you have to run and apply Reader context first.

❌ Old:

import { createContext } from '@marblejs/core';
import * as http from 'http';
import httpListener from './http.listener';
  
const server = http
  .createServer(httpListener)
  .listen(1337);

βœ… New:

  • direct usage

import { createContext } from '@marblejs/core';
import * as http from 'http';
import httpListener from './http.listener';

const httpListenerWithContext = httpListener
  .run(createContext());
  
const server = http
  .createServer(httpListenerWithContext)
  .listen(1337);
  • using createServer

import { createContext, createServer } from '@marblejs/core';
import httpListener from './http.listener';

const server = createServer({
  port: 1337,
  httpListener,
});

server.run();

@marblejs/middleware-body

Every body parsing middleware should be registered via function invocation. It allows you to pass an optional middleware configuration object.

❌ Old:

import { bodyParser$ } '@marblejs/middleware-body';

httpListener({
  middlewares: [bodyParser$],
  // ...
});

βœ… New:

import { bodyParser$ } '@marblejs/middleware-body';

httpListener({
  middlewares: [bodyParser$()],
  // ...
});

@marblejs/middleware-logger

Because previous logger$ wasn't exposed as a function, it was very hard to extend it. Version 1.2.0 deprecated it in favor of loggerWithOpts$ entry point which was more maintainable and could be extended easier. From the version v2.x the logger$ entry point is be swapped with loggerWithOpts$ implementation.

❌ Old:

import { logger$, loggerWithOpts$ } '@marblejs/middleware-logger';

httpListener({
  middlewares: [
    logger$,
    // or
    loggerWithOpts$(),
  ],
  // ...
});

βœ… New:

import { bodyParser$ } '@marblejs/middleware-body';

httpListener({
  middlewares: [logger$()],
  // ...
});
PreviousMigration from version 2.xNextAPI reference

Last updated 5 years ago