cycle-express-driver
Advanced tools
Comparing version 3.1.1 to 4.0.0
"use strict"; | ||
const xstream_1 = require("xstream"); | ||
const adapt_1 = require("@cycle/run/lib/adapt"); | ||
const cuid = require("cuid"); | ||
const express = require("express"); | ||
const methods = require("methods"); | ||
const noop = () => undefined; | ||
const terminateRequestWithMethodsMap = [ | ||
@@ -21,15 +22,19 @@ 'download', | ||
const requestsStore = {}; | ||
const createRouterStream = (router, streamAdapter) => { | ||
const createRouterSource = (router) => { | ||
const driverRouter = {}; | ||
const createRouteStream = (method, path) => { | ||
const { stream, observer } = streamAdapter.makeSubject(); | ||
router[method](path, (req, res) => { | ||
const request = Object.assign({ | ||
id: cuid() | ||
}, req); | ||
request.locals = request.locals || {}; | ||
requestsStore[request.id] = { req: request, res }; | ||
observer.next(request); | ||
const incoming$ = xstream_1.default.create({ | ||
start: (listener) => { | ||
router[method](path, (req, res) => { | ||
const request = Object.assign({ | ||
id: cuid() | ||
}, req); | ||
request.locals = request.locals || {}; | ||
requestsStore[request.id] = { req: request, res }; | ||
listener.next(request); | ||
}); | ||
}, | ||
stop: () => { } | ||
}); | ||
return stream; | ||
return adapt_1.adapt(incoming$); | ||
}; | ||
@@ -42,3 +47,3 @@ methods.concat('all').forEach((method) => { | ||
router.use(path, nestedRouter); | ||
return createRouterStream(nestedRouter, streamAdapter); | ||
return createRouterSource(nestedRouter); | ||
}; | ||
@@ -48,6 +53,4 @@ return driverRouter; | ||
exports.makeRouterDriver = (router) => { | ||
const driverFunction = (outgoing$, streamAdapter) => { | ||
streamAdapter.streamSubscribe(outgoing$, { | ||
complete: noop, | ||
error: noop, | ||
const driverFunction = (outgoing$) => { | ||
outgoing$.addListener({ | ||
next: (response) => { | ||
@@ -80,5 +83,5 @@ if (!requestsStore[response.id]) { | ||
}); | ||
return createRouterStream(router, streamAdapter); | ||
return createRouterSource(router); | ||
}; | ||
return driverFunction; | ||
}; |
/// <reference types="express" /> | ||
import { DriverFunction } from '@cycle/base'; | ||
import { DriverFunction } from '@cycle/run'; | ||
import * as express from 'express'; | ||
@@ -4,0 +4,0 @@ export declare type RoutePath = string | RegExp; |
{ | ||
"name": "cycle-express-driver", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"description": "Cycle.js driver for Express framework", | ||
@@ -17,6 +17,7 @@ "main": "built/index.js", | ||
"dependencies": { | ||
"@cycle/base": "^4.3.0", | ||
"@cycle/run": "^1.0.0", | ||
"@types/express": "^4.0.35", | ||
"cuid": "^1.3.8", | ||
"methods": "^1.1.2" | ||
"methods": "^1.1.2", | ||
"xstream": "^10.2.0" | ||
}, | ||
@@ -23,0 +24,0 @@ "devDependencies": { |
104
src/index.ts
@@ -1,5 +0,8 @@ | ||
import { DriverFunction } from '@cycle/base'; | ||
import * as cuid from 'cuid'; | ||
import * as express from 'express'; | ||
import * as methods from 'methods'; | ||
import xs, { Stream } from 'xstream' | ||
import { DriverFunction } from '@cycle/run' | ||
import { adapt } from '@cycle/run/lib/adapt' | ||
import * as cuid from 'cuid' | ||
import * as express from 'express' | ||
import { Router } from 'express' | ||
import * as methods from 'methods' | ||
@@ -29,4 +32,2 @@ export type RoutePath = string | RegExp; | ||
const noop = () => undefined; | ||
const terminateRequestWithMethodsMap = [ | ||
@@ -54,20 +55,24 @@ 'download', | ||
const createRouterStream = (router, streamAdapter) => { | ||
const driverRouter: any = {}; | ||
const createRouteStream = (method, path) => { | ||
const { stream, observer } = streamAdapter.makeSubject(); | ||
const createRouterSource = (router: Router) => { | ||
const driverRouter: any = {} | ||
const createRouteStream = (method, path) => { | ||
const incoming$ = xs.create<Request>({ | ||
start: (listener) => { | ||
router[method](path, (req: express.Request, res: express.Response) => { | ||
const request = Object.assign({ | ||
id: cuid() | ||
}, req) as Request; | ||
const request = Object.assign({ | ||
id: cuid() | ||
}, req) as Request | ||
request.locals = request.locals || {}; | ||
requestsStore[request.id] = { req: request, res }; | ||
request.locals = request.locals || {} | ||
requestsStore[request.id] = { req: request, res } | ||
observer.next(request); | ||
}); | ||
listener.next(request) | ||
}) | ||
}, | ||
stop: () => { } | ||
}) | ||
return stream; | ||
}; | ||
return adapt(incoming$) | ||
} | ||
@@ -78,7 +83,7 @@ methods.concat('all').forEach((method: string) => { | ||
driverRouter.route = (path: RoutePath) => { | ||
const nestedRouter = express.Router(); | ||
router.use(path, nestedRouter); | ||
return createRouterStream(nestedRouter, streamAdapter); | ||
}; | ||
driverRouter.route = (path: RoutePath) => { | ||
const nestedRouter = express.Router() | ||
router.use(path, nestedRouter) | ||
return createRouterSource(nestedRouter) | ||
} | ||
@@ -89,34 +94,20 @@ return driverRouter as RouterSource; | ||
export const makeRouterDriver = (router: express.Router) => { | ||
const driverFunction: DriverFunction = (outgoing$, streamAdapter) => { | ||
streamAdapter.streamSubscribe<Response>(outgoing$, { | ||
complete: noop, | ||
error: noop, | ||
next: (response) => { | ||
if (!requestsStore[response.id]) { | ||
console.warn(`request with id ${response.id} not found`); | ||
return; | ||
} | ||
const driverFunction: DriverFunction = (outgoing$: Stream<Response>) => { | ||
outgoing$.addListener({ | ||
next: (response) => { | ||
const { res } = requestsStore[response.id] | ||
const { res } = requestsStore[response.id]; | ||
if (!res) { | ||
throw new Error(`request with id ${response.id} not found`) | ||
} | ||
let terminateRequestWith: string | undefined; | ||
const methods: string[] = []; | ||
let terminateRequestWith: string | undefined | ||
const methods: string[] = [] | ||
for (const key in response) { | ||
if (typeof res[key] === 'function') { | ||
if (terminateRequestWithMethodsMap[key]) { | ||
terminateRequestWith = key; | ||
} else { | ||
methods.push(key); | ||
} | ||
} | ||
} | ||
if (terminateRequestWith) { methods.push(terminateRequestWith); } | ||
methods.forEach((method) => res[method](response[method])); | ||
if (terminateRequestWith) { | ||
delete requestsStore[response.id]; | ||
} | ||
for (const key in response) { | ||
if (typeof res[key] === 'function') { | ||
if (terminateRequestWithMethodsMap[key]) { | ||
terminateRequestWith = key | ||
} else { | ||
methods.push(key) | ||
} | ||
@@ -128,3 +119,6 @@ }); | ||
return driverFunction; | ||
}; | ||
return createRouterSource(router) | ||
} | ||
return driverFunction | ||
} |
Sorry, the diff of this file is not supported yet
39234
5
+ Added@cycle/run@^1.0.0
+ Addedxstream@^10.2.0
+ Added@cycle/run@1.0.0(transitive)
+ Addedsymbol-observable@1.2.0(transitive)
+ Addedxstream@10.9.0(transitive)
- Removed@cycle/base@^4.3.0
- Removed@cycle/base@4.3.0(transitive)