cycle-express-driver
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -27,3 +27,3 @@ "use strict"; | ||
const request = Object.assign({ | ||
id: cuid(), | ||
id: cuid() | ||
}, req); | ||
@@ -40,3 +40,3 @@ request.locals = request.locals || {}; | ||
driverRouter.route = (path) => { | ||
let nestedRouter = express.Router(); | ||
const nestedRouter = express.Router(); | ||
router.use(path, nestedRouter); | ||
@@ -55,7 +55,8 @@ return createRouterStream(nestedRouter, streamAdapter); | ||
if (!res) { | ||
throw new Error(`request with id ${response.id} not found`); | ||
console.warn(`request with id ${response.id} not found`); | ||
return; | ||
} | ||
let terminateRequestWith; | ||
const methods = []; | ||
for (let key in response) { | ||
for (const key in response) { | ||
if (typeof res[key] === 'function') { | ||
@@ -62,0 +63,0 @@ if (terminateRequestWithMethodsMap[key]) { |
{ | ||
"name": "cycle-express-driver", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Cycle.js driver for Express framework", | ||
@@ -13,3 +13,4 @@ "main": "built/index.js", | ||
"clean": "rm -rf built || :", | ||
"build": "yarn run clean && tsc -p ." | ||
"build": "yarn run clean && tsc -p .", | ||
"watch": "tsc -p . --watch" | ||
}, | ||
@@ -16,0 +17,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# cycle-express-driver | ||
# cycle-express-driver [![npm version](https://badge.fury.io/js/cycle-express-driver.svg)](https://badge.fury.io/js/cycle-express-driver) | ||
[Express.js](http://expressjs.com/) driver for [cycle.js](http://cycle.js.org/) forked from [here](https://github.com/whitecolor/cycle-express) | ||
@@ -3,0 +3,0 @@ |
177
src/index.ts
@@ -1,124 +0,125 @@ | ||
import { DriverFunction } from '@cycle/base' | ||
import * as cuid from 'cuid' | ||
import * as express from 'express' | ||
import * as methods from 'methods' | ||
import { DriverFunction } from '@cycle/base'; | ||
import * as cuid from 'cuid'; | ||
import * as express from 'express'; | ||
import * as methods from 'methods'; | ||
export type RoutePath = string | RegExp | ||
export type RoutePath = string | RegExp; | ||
export interface RouterSourceTemplate<T> { | ||
route: (path: RoutePath) => RouterSourceTemplate<T> | ||
get: (path: RoutePath) => T | ||
post: (path: RoutePath) => T | ||
put: (path: RoutePath) => T | ||
delete: (path: RoutePath) => T | ||
route: (path: RoutePath) => RouterSourceTemplate<T>; | ||
get: (path: RoutePath) => T; | ||
post: (path: RoutePath) => T; | ||
put: (path: RoutePath) => T; | ||
delete: (path: RoutePath) => T; | ||
} | ||
export type RouterSource = RouterSourceTemplate<any> | ||
export type RouterSource = RouterSourceTemplate<any>; | ||
export interface Request extends express.Request { | ||
id: string | ||
locals: any | ||
id: string; | ||
locals: any; | ||
} | ||
export interface Response { | ||
id: string | ||
status?: number | ||
send: any | ||
id: string; | ||
status?: number; | ||
send: any; | ||
} | ||
const noop = () => undefined | ||
const noop = () => undefined; | ||
const terminateRequestWithMethodsMap = [ | ||
'download', | ||
'end', | ||
'json', | ||
'jsonp', | ||
'redirect', | ||
'render', | ||
'send', | ||
'sendFile', | ||
'sendStatus' | ||
'download', | ||
'end', | ||
'json', | ||
'jsonp', | ||
'redirect', | ||
'render', | ||
'send', | ||
'sendFile', | ||
'sendStatus' | ||
].reduce((obj, method) => { | ||
obj[method] = true | ||
return obj | ||
}, {}) | ||
obj[method] = true; | ||
return obj; | ||
}, {}); | ||
const requestsStore: { | ||
[prop: string]: { | ||
req: Request, | ||
res: express.Response | ||
} | ||
} = {} | ||
[prop: string]: { | ||
req: Request, | ||
res: express.Response | ||
} | ||
} = {}; | ||
const createRouterStream = (router, streamAdapter) => { | ||
const driverRouter: any = {} | ||
const createRouteStream = (method, path) => { | ||
const { stream, observer } = streamAdapter.makeSubject() | ||
const driverRouter: any = {}; | ||
const createRouteStream = (method, path) => { | ||
const { stream, observer } = streamAdapter.makeSubject(); | ||
router[method](path, (req: express.Request, res: express.Response) => { | ||
const request = Object.assign({ | ||
id: cuid() | ||
}, req) as Request | ||
router[method](path, (req: express.Request, res: express.Response) => { | ||
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) | ||
}) | ||
observer.next(request); | ||
}); | ||
return stream | ||
} | ||
return stream; | ||
}; | ||
methods.concat('all').forEach((method: string) => { | ||
driverRouter[method] = (path: RoutePath) => createRouteStream(method, path) | ||
}) | ||
methods.concat('all').forEach((method: string) => { | ||
driverRouter[method] = (path: RoutePath) => createRouteStream(method, path); | ||
}); | ||
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 createRouterStream(nestedRouter, streamAdapter); | ||
}; | ||
return driverRouter as RouterSource | ||
} | ||
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) => { | ||
const { res } = requestsStore[response.id] | ||
const driverFunction: DriverFunction = (outgoing$, streamAdapter) => { | ||
streamAdapter.streamSubscribe<Response>(outgoing$, { | ||
complete: noop, | ||
error: noop, | ||
next: (response) => { | ||
const { res } = requestsStore[response.id]; | ||
if (!res) { | ||
throw new Error(`request with id ${response.id} not found`) | ||
} | ||
if (!res) { | ||
console.warn(`request with id ${response.id} not found`); | ||
return; | ||
} | ||
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) | ||
} | ||
} | ||
} | ||
for (const key in response) { | ||
if (typeof res[key] === 'function') { | ||
if (terminateRequestWithMethodsMap[key]) { | ||
terminateRequestWith = key; | ||
} else { | ||
methods.push(key); | ||
} | ||
} | ||
} | ||
if (terminateRequestWith) { methods.push(terminateRequestWith) } | ||
if (terminateRequestWith) { methods.push(terminateRequestWith); } | ||
methods.forEach((method) => res[method](response[method])) | ||
methods.forEach((method) => res[method](response[method])); | ||
if (terminateRequestWith) { | ||
delete requestsStore[response.id] | ||
} | ||
} | ||
}) | ||
if (terminateRequestWith) { | ||
delete requestsStore[response.id]; | ||
} | ||
} | ||
}); | ||
return createRouterStream(router, streamAdapter) | ||
} | ||
return createRouterStream(router, streamAdapter); | ||
}; | ||
return driverFunction | ||
} | ||
return driverFunction; | ||
}; |
{ | ||
"extends": "tslint:latest", | ||
"rules": { | ||
"semicolon": [true, "never"], | ||
"semicolon": [true, "always"], | ||
"quotemark": [true, "single"], | ||
@@ -6,0 +6,0 @@ "interface-name": [true, "never-prefix"], |
@@ -5,6 +5,6 @@ declare module 'cuid' { | ||
*/ | ||
type Generate = () => string | ||
type Generate = () => string; | ||
const a: Generate | ||
export = a | ||
const a: Generate; | ||
export = a; | ||
} |
declare module 'methods' { | ||
const a: any | ||
export = a | ||
const a: any; | ||
export = a; | ||
} |
39323
250