Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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('/'),
r.matchType('POST'),
matchEvent,
ServerEvent,
httpListener,
HttpServerEffect,
useContext,
} from '@marblejs/core';
import { mapToServer, MarbleWebSocketServer } from '@marblejs/websockets';
import { logger$ } from '@marblejs/middleware-logger';
import { merge } from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { websocketsServer } from './websockets.server';
export const WsServerToken = createContextToken('MarbleWebSocketServer');
const root$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect((req$, { ask }) => {
const wsServer = useContext(WsServerToken)(ask);
return req$.pipe(
tap(() => wsServer.sendBroadcastResponse({ type: 'ROOT', payload: 'Hello' })),
map(body => ({ body })),
);
}));
const upgrade$: HttpServerEffect = (event$, { ask }) =>
event$.pipe(
matchEvent(ServerEvent.upgrade),
mapToServer({
path: '/api/:version/ws',
server: ask(WsServerToken),
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('/'),
r.matchType('POST'),
r.useEffect(req$ => req$.pipe(
use(postUserValidator$),
map(req => req.body),
mergeMap(Dao.postUser),
map(body => ({ body })),
)));
export const user$ = combineRoutes('/user', {
middlewares: [authorize$],
effects: [getUserList$, getUser$, postUser$],
});
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(
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',
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(
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('*'),
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,
maxFieldCount: 2,
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(
r.matchPath('/stream/:dir*'),
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(
mergeMap(() => throwError(
import { HttpEffectResponse, httpListener, r } from '@marblejs/core';
import { map, mapTo } from 'rxjs/operators';
import { createLambda, ProxyType } from '../../';
import { awsApiGatewayMiddleware$ } from '../awsLambda.middleware';
import { bodyParser$ } from '@marblejs/middleware-body';
import { Readable } from 'stream';
const effect$ = r.pipe(
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'),
}))
)));
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),
mergeMap(readFile(STATIC_PATH)),
map(body => ({ body }))
)));
export const static$ = combineRoutes(
'/static',
[ postFile$, getFileStream$, getFile$ ],
);