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:
1
const example$: Effect = (req$) =>
2
req$.pipe(
3
map(req => req.params.version), // (typeof req.params) = any
4
map(version => `Version: ${version}`), // (typeof version) = any
5
map(message => ({ body: message })),
6
);
Copied!
✅ New:
1
const example$: HttpEffect = (req$) =>
2
req$.pipe(
3
map(req => req.params.version), // (typeof req.params) = unknown
4
map(version => `Version: ${version}`), // ❌ type error!
5
map(message => ({ body: message })),
6
);
7
8
👇
9
10
const example$: HttpEffect = (req$) =>
11
req$.pipe(
12
map(req => req.params.version), // (typeof req.params) = unknown
13
map(version => version as string), // or use validation middleware
14
map(version => `Version: ${version}`), // ✅ looks fine!
15
map(message => ({ body: message })),
16
);
Copied!

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:
1
const error$: ErrorEffect = (req$, _, error) =>
2
req$.pipe(
3
mapTo(error),
4
// ...
5
);
Copied!
✅ New:
1
const error$: HttpErrorEffect = (req$, _, meta) =>
2
req$.pipe(
3
mapTo(meta.error),
4
// ...
5
);
Copied!

httpListener error effect

HTTP error effect is registered inside httpListener configuration object via error$ instead of errorEffect.
❌ Old:
1
export default httpListener({
2
// ...
3
errorEffect: // ...
4
});
Copied!
✅ New:
1
export default httpListener({
2
// ...
3
error$: // ...
4
});
Copied!

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:
1
import { createContext } from '@marblejs/core';
2
import * as http from 'http';
3
import httpListener from './http.listener';
4
5
const server = http
6
.createServer(httpListener)
7
.listen(1337);
Copied!
✅ New:
    direct usage
1
import { createContext } from '@marblejs/core';
2
import * as http from 'http';
3
import httpListener from './http.listener';
4
5
const httpListenerWithContext = httpListener
6
.run(createContext());
7
8
const server = http
9
.createServer(httpListenerWithContext)
10
.listen(1337);
Copied!
    using createServer
1
import { createContext, createServer } from '@marblejs/core';
2
import httpListener from './http.listener';
3
4
const server = createServer({
5
port: 1337,
6
httpListener,
7
});
8
9
server.run();
Copied!

@marblejs/middleware-body

Every body parsing middleware should be registered via function invocation. It allows you to pass an optional middleware configuration object.
❌ Old:
1
import { bodyParser$ } '@marblejs/middleware-body';
2
3
httpListener({
4
middlewares: [bodyParser$],
5
// ...
6
});
Copied!
✅ New:
1
import { bodyParser$ } '@marblejs/middleware-body';
2
3
httpListener({
4
middlewares: [bodyParser$()],
5
// ...
6
});
Copied!

@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:
1
import { logger$, loggerWithOpts$ } '@marblejs/middleware-logger';
2
3
httpListener({
4
middlewares: [
5
logger$,
6
// or
7
loggerWithOpts$(),
8
],
9
// ...
10
});
Copied!
✅ New:
1
import { bodyParser$ } '@marblejs/middleware-body';
2
3
httpListener({
4
middlewares: [logger$()],
5
// ...
6
});
Copied!
Last modified 1yr ago