Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat(utils): a general interceptor to functions returning Observables #519

Open
wants to merge 3 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"tslib": "^1.9.0",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.2.0",
"typescript": "^2.8.3",
"typescript": "^3.0.1",
"uuid": "^3.3.2"
}
}
10 changes: 10 additions & 0 deletions src/utils/interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Observable } from 'rxjs/Observable'

type Gate<T, U extends any[]> = (...args: U) => Observable<T>

export function wrap<T, U extends any[]>(
gate: Gate<T, U>,
interceptor: any = (_0: any, g: Gate<T, U>, args: U) => g(...args)
): (options?: {}) => (...args: U) => Observable<T> {
return (options: {} = {}) => (...args: U) => interceptor(options, gate, args)
}
1 change: 1 addition & 0 deletions test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import './mock'
import './apis'
import './sockets'
import './net'
import './utils/index'
1 change: 1 addition & 0 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './interceptor.spec'
111 changes: 111 additions & 0 deletions test/utils/interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { expect } from 'chai'
import { it, describe } from 'tman'
import { Observable } from 'rxjs'
import * as ix from '../../src/utils/interceptor'

describe.only('interceptor spec', () => {
it('wrap: empty condition for no-param gate function', function* () {
const result = 1
const gate = () => Observable.of(result)
const emptyIntercepted = ix.wrap(gate)
yield emptyIntercepted()().do((x) => {
expect(x).to.equal(result)
})
})

it('wrap: empty condition for has-param gate function', function* () {
const gate = (x: number, y: number) => Observable.of(x + y)
const emptyIntercepted = ix.wrap(gate)
yield emptyIntercepted()(1, 2).do((x) => {
expect(x).to.equal(3)
})
})

it('wrap: single interceptor for request', function* () {
const interceptor = (options: { checkDividedByZero: boolean }, gateFn: any, gateArgs: any[]) => {
if (options.checkDividedByZero) {
const divisor = gateArgs[1]
if (divisor === 0) {
return Observable.of(0)
}
}
return gateFn(...gateArgs)
}
const gate = (x: number, y: number) => Observable.of(x / y)
const wrapped = ix.wrap(gate, interceptor)
const intercepted = wrapped({ checkDividedByZero: true })
yield intercepted(1, 0)
.do((x) => {
expect(x).to.equal(0)
})
})

it('wrap: single interceptor for response', function* () {
const interceptor = (options: { catchError: boolean }, gateFn: any, gateArgs: any[]) => {
if (options.catchError) {
return gateFn(...gateArgs).catch((err: any) => Observable.of(err.message))
}
return gateFn(...gateArgs)
}
const gate = () => Observable.throw(new Error('hello'))
const wrapped = ix.wrap(gate, interceptor)
const intercepted = wrapped({ catchError: true })
yield intercepted()
.do((x) => {
expect(x).to.equal('hello')
})
})

it('wrap: single interceptor for both request and response', function* () {
const interceptor = (options: { checkDividedByZero: boolean, catchError: boolean }, gateFn: any, gateArgs: any[]) => {
let result: Observable<any>
if (options.checkDividedByZero) {
const divisor = gateArgs[1]
if (divisor === 0) {
result = Observable.throw(new Error('divided-by-zero'))
} else {
result = gateFn(...gateArgs)
}
} else {
result = gateFn(...gateArgs)
}
if (options.catchError) {
result = result.catch((err: any) => Observable.of(err.message))
}
return result
}
const gate = (x: number, y: number) => Observable.of(x / y)
const wrapped = ix.wrap(gate, interceptor)
const intercepted = wrapped({ checkDividedByZero: true, catchError: true })
yield intercepted(1, 0)
.do((x) => {
expect(x).to.equal('divided-by-zero')
})
})

it('wrap: one-value Observable to multi-value Observable conversion', function* () {
const cache = new Map([['leo', '你好,里奥']])
const interceptor = (options: { cacheThenRefresh: boolean }, gateFn: any, gateArgs: any[]) => {
const request$ = gateFn(...gateArgs)
if (options.cacheThenRefresh) {
const userName = gateArgs[0]
const requestAndCache = request$.do((hello: string) => cache.set(userName, hello))
return cache.has(userName)
? requestAndCache.startWith(cache.get(userName))
: requestAndCache
} else {
return request$
}
}
const gate = (userName: string) => {
return Observable.of(`hello ${userName}`)
}
const wrapped = ix.wrap(gate, interceptor)
const intercepted = wrapped({ cacheThenRefresh: true })
yield intercepted('leo').take(2).toArray()
.do((xs) => {
expect(xs).to.deep.equal(['你好,里奥', 'hello leo'])
expect(cache.get('leo')).to.equal('hello leo')
})
})
})
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2692,10 +2692,14 @@ typescript@^2.4.2:
version "2.6.2"
resolved "https://registry.npmjs.org/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"

typescript@^2.6.1, typescript@^2.8.3:
typescript@^2.6.1:
version "2.8.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"

typescript@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/typescript/-/typescript-3.0.1.tgz#43738f29585d3a87575520a4b93ab6026ef11fdb"

uglify-js@^2.6:
version "2.8.29"
resolved "https://registry.npmjs.org/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"
Expand Down