Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const rootValiadtor$ = requestValidator$({
params: t.type({
number: t.string,
}),
});
const test$ = r.pipe(
r.matchPath('/test'),
r.matchType('GET'),
r.useEffect((req$, { ask }) => req$.pipe(
mergeMap(() => useContext(ClientToken)(ask).emit({ type: 'TEST' })),
mapTo(({ body: 'OK' })),
)),
);
const fib$ = r.pipe(
r.matchPath('/fib/:number'),
r.matchType('GET'),
r.useEffect((req$, ctx) => {
const client = useContext(ClientToken)(ctx.ask);
return req$.pipe(
use(rootValiadtor$),
map(req => Number(req.params.number)),
mergeMap(number => forkJoin(
client.send({ type: 'FIB', payload: number + 0 }),
client.send({ type: 'FIB', payload: number + 1 }),
client.send({ type: 'FIB', payload: number + 2 }),
client.send({ type: 'FIB', payload: number + 3 }),
client.send({ type: 'FIB', payload: number + 4 }),
)),
map(body => ({ body })),
import { throwError, of } from 'rxjs';
import { map, switchMap, catchError, mergeMap } from 'rxjs/operators';
import { Dao } from '../fakes/dao.fake';
import { authorize$ } from '../middlewares/auth.middleware';
const getUserValidator$ = requestValidator$({
params: t.type({ id: t.string }),
});
const postUserValidator$ = requestValidator$({
body: t.type({
user: t.type({ id: t.string })
}),
});
const getUserList$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mergeMap(Dao.getUsers),
map(users => ({ body: users })),
)));
const getUser$ = r.pipe(
r.matchPath('/:id'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
use(getUserValidator$),
mergeMap(req => of(req).pipe(
map(req => req.params.id),
switchMap(Dao.getUserById),
map(user => ({ body: user })),
r.matchPath('/error'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mergeMap(() => throwError(
new HttpError('Route not implemented', HttpStatus.NOT_IMPLEMENTED, { reason: 'Not implemented' })
)),
)));
const webSockets$ = r.pipe(
r.matchPath('/ws'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
switchToProtocol('websocket')
)));
const notFound$ = r.pipe(
r.matchPath('*'),
r.matchType('*'),
r.useEffect(req$ => req$.pipe(
mergeMap(() => throwError(
new HttpError('Route not found', HttpStatus.NOT_FOUND)
)),
)));
export const api$ = combineRoutes(
'/api/:version',
[ root$, user$, static$, notImplemented$, webSockets$, notFound$ ],
);
r.matchPath('/'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
map((req): HttpEffectResponse => ({
status: 200,
body: {
path: req.path,
body: req.body,
event: req.apiGatewayEvent,
context: req.apiGatewayContext,
},
}))
)),
);
const chunkedTransferEncodingEffect$ = r.pipe(
r.matchPath('/chunked-transfer'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mapTo(({
status: 200,
headers: {
'transfer-encoding': 'chunked',
},
body: new Readable({
read(): void {
this.push(JSON.stringify({ key: 'value' }));
this.push(null);
}
}),
}))
))
import { streamFileTo } from '../multipart.util';
const TMP_PATH = path.resolve(__dirname, '../../../../tmp');
const memoryMultipart$ = r.pipe(
r.matchPath('/memory'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$()),
map(req => ({ body: {
files: req.files,
fields: req.body,
}}))
)));
const memoryWithOptionsMultipart$ = r.pipe(
r.matchPath('/memory-with-options'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$({
maxFileCount: 1,
maxFieldCount: 2,
maxFileSize: 20,
})),
map(req => ({ body: {
files: req.files,
fields: req.body,
}}))
)));
const filesystemMultipart$ = r.pipe(
r.matchPath('/filesystem'),
import * as path from 'path';
import { r, combineRoutes, use } from '@marblejs/core';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { multipart$ } from '@marblejs/middleware-multipart';
import { streamFileTo } from '@marblejs/middleware-multipart/dist/multipart.util';
import { readFile } from '@marblejs/core/dist/+internal';
import { map, mergeMap } from 'rxjs/operators';
const STATIC_PATH = path.resolve(__dirname, '../../../../assets');
const TMP_PATH = path.resolve(__dirname, '../../../../tmp');
const getFileValidator$ = requestValidator$({
params: t.type({ dir: t.string })
});
const postFile$ = r.pipe(
r.matchPath('/upload'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$({
files: ['image_1'],
stream: streamFileTo(TMP_PATH),
})),
map(req => ({
body: {
file: req.files,
body: req.body,
},
}))
)));
const getFileStream$ = r.pipe(
const postUserValidator$ = requestValidator$({
body: t.type({
user: t.type({ id: t.string })
}),
});
const getUserList$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mergeMap(Dao.getUsers),
map(users => ({ body: users })),
)));
const getUser$ = r.pipe(
r.matchPath('/:id'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
use(getUserValidator$),
mergeMap(req => of(req).pipe(
map(req => req.params.id),
switchMap(Dao.getUserById),
map(user => ({ body: user })),
catchError(() => throwError(
new HttpError('User does not exist', HttpStatus.NOT_FOUND)
))
)),
)));
const postUser$ = r.pipe(
r.matchPath('/'),
import { throwError } from 'rxjs';
import { map, mergeMap, tap } from 'rxjs/operators';
import { user$ } from './user.effects';
import { static$ } from './static.effects';
import { WsServerToken } from '../tokens';
const rootValiadtor$ = requestValidator$({
params: t.type({
version: t.union([
t.literal('v1'),
t.literal('v2'),
]),
}),
});
const root$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect((req$, _, { ask }) => req$.pipe(
use(rootValiadtor$),
map(req => req.params.version),
map(version => `API version: ${version}`),
tap(message => ask(WsServerToken).map(server =>
server.sendBroadcastResponse({ type: 'ROOT', payload: message })),
),
map(message => ({ body: message })),
)));
const notImplemented$ = r.pipe(
r.matchPath('/error'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
import * as fs from 'fs';
import * as path from 'path';
import * as request from 'supertest';
import { map } from 'rxjs/operators';
import { r, use, httpListener, createContext } from '@marblejs/core';
import { multipart$ } from '../multipart.middleware';
import { streamFileTo } from '../multipart.util';
const TMP_PATH = path.resolve(__dirname, '../../../../tmp');
const memoryMultipart$ = r.pipe(
r.matchPath('/memory'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$()),
map(req => ({ body: {
files: req.files,
fields: req.body,
}}))
)));
const memoryWithOptionsMultipart$ = r.pipe(
r.matchPath('/memory-with-options'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$({
maxFileCount: 1,
r.matchPath('/upload'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(multipart$({
files: ['image_1'],
stream: streamFileTo(TMP_PATH),
})),
map(req => ({
body: {
file: req.files,
body: req.body,
},
}))
)));
const getFileStream$ = r.pipe(
r.matchPath('/stream/:dir*'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
use(getFileValidator$),
map(req => req.params.dir),
map(dir => fs.createReadStream(path.resolve(STATIC_PATH, dir))),
map(body => ({ body })),
)),
);
const getFile$ = r.pipe(
r.matchPath('/:dir*'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
use(getFileValidator$),
map(req => req.params.dir),