-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc9a6b3
commit 73473a6
Showing
7 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { IPipe, IPipeOutput, ITransmittable } from '../interfaces' | ||
import { IPromise } from '@qiwi/substrate' | ||
import { createHttpPipe, IHttpPipeOpts } from './http' | ||
import { executeFailproof } from '../utils' | ||
|
||
export const type = 'http-fallback' | ||
|
||
export const createHttpPipeFallback = (opts: IHttpPipeOpts[]): IPipe => { | ||
if (opts.length === 0) { | ||
throw new Error('createHttpPipeFallback opts must not be empty') | ||
} | ||
|
||
const httpPipes = opts.map(createHttpPipe) | ||
return { | ||
type, | ||
execute (transmittable : ITransmittable): IPromise<IPipeOutput> { | ||
return executeFailproof(transmittable, httpPipes) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { IPipe, IPipeOutput, ITransmittable } from '../interfaces' | ||
import { identity } from '.' | ||
|
||
export async function executeFailproof (transmittable: ITransmittable, pipeline: IPipe[]): Promise<IPipeOutput> { | ||
const pipe = pipeline.shift() as IPipe | ||
const [err, succ] = await pipe.execute(transmittable, identity) | ||
|
||
if (pipeline.length === 0) { | ||
return succ | ||
? [null, succ] | ||
: [err, null] | ||
} | ||
|
||
return executeFailproof(transmittable, pipeline) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { HttpMethod } from '@qiwi/substrate-types' | ||
import { createHttpPipeFallback, ITransmittable } from '../../../main/ts' | ||
|
||
import 'cross-fetch/polyfill' | ||
|
||
const noop = () => { /* noop */ } | ||
|
||
describe('httpPipe', () => { | ||
it('factory returns IPipe', () => { | ||
const httpPipeFallback = createHttpPipeFallback([{ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET }]) | ||
expect(httpPipeFallback.type).toBe('http-fallback') | ||
expect(httpPipeFallback.execute).toEqual(expect.any(Function)) | ||
}) | ||
|
||
it('returns remote data if succeeds', async () => { | ||
const httpPipe = createHttpPipeFallback([{ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET }]) | ||
const transmittable: ITransmittable = { data: null, meta: { history: [] } } | ||
|
||
return expect(httpPipe.execute(transmittable, noop)) | ||
.resolves.toMatchObject([null, { | ||
data: { | ||
id: 2, | ||
email: '[email protected]', | ||
first_name: 'Janet', | ||
last_name: 'Weaver', | ||
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg', | ||
}, | ||
}]) | ||
}) | ||
|
||
it('uses fallback urls', () => { | ||
const httpPipe = createHttpPipeFallback([ | ||
{ url: 'https://reqres.in/api/users/23', method: HttpMethod.GET }, | ||
{ url: 'https://reqres.in/api/unknown/23', method: HttpMethod.GET }, | ||
{ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET }, | ||
]) | ||
const transmittable: ITransmittable = { data: null, meta: { history: [] } } | ||
|
||
return expect(httpPipe.execute(transmittable, noop)) | ||
.resolves.toMatchObject([null, { | ||
data: { | ||
id: 2, | ||
email: '[email protected]', | ||
first_name: 'Janet', | ||
last_name: 'Weaver', | ||
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg', | ||
}, | ||
}]) | ||
}) | ||
|
||
it('handle errors', () => { | ||
const httpPipe = createHttpPipeFallback([ | ||
{ url: 'https://reqres.in/api/users/23', method: HttpMethod.GET }, | ||
{ url: 'https://reqres.in/api/unknown/23', method: HttpMethod.GET }, | ||
{ url: 'https://reqres.in/api/users/23', method: HttpMethod.GET }, | ||
]) | ||
const transmittable: ITransmittable = { data: null, meta: { history: [] } } | ||
|
||
return expect(httpPipe.execute(transmittable, noop)) | ||
.resolves.toEqual([new Error('Not Found'), null]) | ||
}) | ||
|
||
it('throw error when opts is empty', () => { | ||
return expect(() => createHttpPipeFallback([])) | ||
.toThrow(new Error('createHttpPipeFallback opts must not be empty')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters