Skip to content

Instantly share code, notes, and snippets.

View JozefFlakus's full-sized avatar
💭
λ

Józef Flakus JozefFlakus

💭
λ
View GitHub Profile
@JozefFlakus
JozefFlakus / user.controller.ts
Created August 28, 2018 20:38
Marble.js - sample controller example
const getUsers$ = EffectFactory
.matchPath('/')
.matchType('GET')
.use(req$ => req$.pipe(
switchMap(Dao.getUsers),
map(users => ({ body: users })),
));
const postUser$ = EffectFactory
.matchPath('/')
const effect$: Effect = (req$) =>
req$.pipe(
// ...
);
const middleware$: Middleware = (req$, res) =>
req$.pipe(
// ...
);
@JozefFlakus
JozefFlakus / auth.middleware.ts
Last active August 28, 2018 20:50
Marble.js - Example middleware
export const authorize$: Middleware = (req$) =>
req$.pipe(
switchMap(req => iif(
() => !isAuthorized(req),
throwError(new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED)),
of(req),
)),
);
import { use, matchEvent } from '@marblejs/core';
import { WsEffect } from '@marblejs/websockets';
import { t, eventValidator$ } from '@marblejs/middleware-io';
export const sum$: WsEffect = event$ =>
event$.pipe(
matchEvent('SUM')
);
export const add$: WsEffect = (event$, ...args) =>
import { use, r } from '@marblejs/core';
const postUser$ = r.pipe(
r.matchPath('/'),
r.matchType('POST'),
r.use(authorize$),
r.useEffect(req$ => req$.pipe(
use(postUserValidator$),
map(req => req.body),
mergeMap(createUser),
export const server = createServer({
port: 1337,
httpListener,
dependencies: [
bindTo(WsServerToken)(webSocketListener().run),
],
});
// ...
const userSchema: t.type({
id: t.string,
name: t.string,
age: t.number,
roles: t.array(t.union([
t.literal('ADMIN'),
t.literal('GUEST'),
])),
});
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 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}/`)),
@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'),