Skip to content

Instantly share code, notes, and snippets.

View JozefFlakus's full-sized avatar
💭
λ

Józef Flakus JozefFlakus

💭
λ
View GitHub Profile
@JozefFlakus
JozefFlakus / cqrs.ts
Last active February 11, 2021 18:59
Marble.js 3.0 - CQRS example
// 📄 postOfferFile.effect.ts
const postOffersFile$ = r.pipe(
r.matchPath('/offers/:id/file'),
r.matchType('POST'),
r.useEffect((req$, ctx) => {
const eventBusClient = useContext(EventBusClientToken)(ctx.ask);
return req$.pipe(
validateRequest,
@JozefFlakus
JozefFlakus / effect.ts
Last active February 6, 2020 08:06
Marble.js 3.0 - effect interface
interface Effect<I, O, Client> {
(input$: Observable<I>, ctx: EffectContext<Client>): Observable<O>;
}
interface HttpEffect<
I = HttpRequest,
O = HttpEffectResponse,
> extends Effect<I, O, HttpServer> {}
interface WsEffect<
@JozefFlakus
JozefFlakus / logging.ts
Last active January 27, 2020 22:05
Marble.js 3.0 - default logging
// HTTP server logger output
λ - 47700 - 2020-01-27 22:04:21 - http [Context] - Registered: "HttpRequestBusToken"
λ - 47700 - 2020-01-27 22:04:21 - http [Context] - Registered: "HttpServerClientToken"
λ - 47700 - 2020-01-27 22:04:21 - http [Context] - Registered: "HttpServerEventStreamToken"
λ - 47700 - 2020-01-27 22:04:21 - http [Context] - Registered: "LoggerToken"
λ - 47700 - 2020-01-27 22:04:21 - http [Router] - Effect mapped: /api/:version GET
λ - 47700 - 2020-01-27 22:04:21 - http [Router] - Effect mapped: /api/:version/user GET
λ - 47700 - 2020-01-27 22:04:21 - http [Router] - Effect mapped: /api/:version/user POST
λ - 47700 - 2020-01-27 22:04:21 - http [Router] - Effect mapped: /api/:version/user/:id GET
@JozefFlakus
JozefFlakus / async-readers.ts
Last active February 1, 2020 22:28
Marble.js 3.0 - async readers
bindEagerlyTo(Token)(async () => 'bar');
const foo = useContext(Token)(ask); // foo === 'bar'
// but...
bindTo(Token)(async () => 'bar');
const foo = useContext(Token)(ask); // foo === Promise<'bar'>
@JozefFlakus
JozefFlakus / continuous-http-streams.ts
Last active January 31, 2020 19:09
Marble.js 3.0 - continuous streams
const foo$ = r.pipe(
r.applyMeta({ continuous: true }),
r.matchPath('/'),
r.matchType('GET'),
r.useEffect((req$, ctx) => {
const reqBus$ = useContext(HttpRequestBusToken)(ctx.ask);
const terminate$ = reqBus$.pipe(filter(req => req.url === '/flush'));
return req$.pipe(
bufferWhen(() => terminate$),
@JozefFlakus
JozefFlakus / microservice.ts
Last active February 11, 2021 19:01
Marble.js 3.0 - microservice example
// 📄 publisher - fib.effect.ts
const fib$ = r.pipe(
r.matchPath('/fib/:number'),
r.matchType('GET'),
r.useEffect((req$, ctx) => {
const client = useContext(ClientToken)(ctx.ask);
return req$.pipe(
validateRequest,
@JozefFlakus
JozefFlakus / marblejs-example.ts
Created May 12, 2019 15:45
Marble.js - one file example
import { createServer, combineRoutes, httpListener, r } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';
import { bodyParser$ } from '@marblejs/middleware-body';
import { map, mapTo } from 'rxjs/operators';
// USERS API definition
const getUserList$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
const httpsOptions: https.ServerOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
};
const listening$: HttpServerEffect = event$ =>
event$.pipe(
matchEvent(ServerEvent.listening),
map(event => event.payload),
tap(({ port, host }) => console.log(`Running @ http://${host}:${port}/`)),
import { bodyParser$, jsonParser } from '@marblejs/middleware-body';
bodyParser$({
parser: jsonParser,
type: ['*/json', 'application/vnd.api+json'],
})
// or
import { bodyParser$, urlEncodedParser } from '@marblejs/middleware-body';
const userSchema: t.type({
id: t.string,
name: t.string,
age: t.number,
roles: t.array(t.union([
t.literal('ADMIN'),
t.literal('GUEST'),
])),
});