Skip to content

Commit

Permalink
feat: add fallback to flp-pipe (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pismenskiy authored Jul 20, 2020
1 parent ef827b1 commit 1bf8cb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/main/ts/pipes/flp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import StackTrace from 'stacktrace-js'
import { IClientEventDto, LogLevel } from '@qiwi/substrate'
import { IPipe, ITransmittable, TPipeline } from '../interfaces'
import { panMaskerPipe } from './masker'
import { createHttpPipe, IHttpPipeOpts } from './http'
import { IHttpPipeOpts } from './http'
import { createHttpPipeFallback } from './httpFallback'
import { identity } from '../utils'

const DEFAULT_LEVEL = LogLevel.INFO
Expand Down Expand Up @@ -65,9 +66,11 @@ export const eventifyPipe: IPipe = {
},
}

export const createFlpPipeline = (opts: IHttpPipeOpts, batchUrl?: string): TPipeline => {
const httpPipe = createHttpPipe(opts)
const httpPipeBatch = batchUrl ? createHttpPipe({ ...opts, url: batchUrl }) : httpPipe
export const createFlpPipeline = (opts: IHttpPipeOpts[] | IHttpPipeOpts, batchUrl?: IHttpPipeOpts[] | IHttpPipeOpts): TPipeline => {
const httpPipe = createHttpPipeFallback(([] as IHttpPipeOpts[]).concat(opts))
const httpPipeBatch = batchUrl
? createHttpPipeFallback(([] as IHttpPipeOpts[]).concat(batchUrl))
: httpPipe

const httpPipeResolver: IPipe = ({
type: httpPipe.type,
Expand Down
23 changes: 12 additions & 11 deletions src/test/ts/pipes/flp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { eventifyPipe, createFlpPipeline } from '../../../main/ts/pipes/flp'
import { createTransmittable, createTransmitter } from '../../../main/ts'
import { eventifyPipe, createFlpPipeline, createTransmittable, createTransmitter } from '../../../main/ts'

import { HttpMethod } from '@qiwi/substrate'
import StackTrace from 'stacktrace-js'

Expand Down Expand Up @@ -50,15 +50,17 @@ describe('flpPipeline', () => {
const batchUrl = 'https://reqres.in/api/unknown'

it('createFlpPipeline factory returns a pipeline', () => {
expect(createFlpPipeline({ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET }, batchUrl)).toEqual(expect.any(Array))
expect(createFlpPipeline(
{ url: 'https://reqres.in/api/users/2', method: HttpMethod.GET },
{ url: batchUrl, method: HttpMethod.GET })).toEqual(expect.any(Array))
})

it('executes eventify, masker and http pipes consequentially with batch', async () => {
const spy = jest.spyOn(window, 'fetch')
const flpPipeline = createFlpPipeline({
url,
method: HttpMethod.POST,
}, batchUrl)
const flpPipeline = createFlpPipeline(
{ url, method: HttpMethod.POST },
{ url: batchUrl, method: HttpMethod.POST }
)
const transmitter = createTransmitter({
pipeline: flpPipeline,
})
Expand Down Expand Up @@ -88,10 +90,9 @@ describe('flpPipeline', () => {

it('executes eventify, masker and http pipes consequentially', async () => {
const spy = jest.spyOn(window, 'fetch')
const flpPipeline = createFlpPipeline({
url,
method: HttpMethod.POST,
}, batchUrl)
const flpPipeline = createFlpPipeline(
{ url, method: HttpMethod.POST },
{ url: batchUrl, method: HttpMethod.GET })
const transmitter = createTransmitter({
pipeline: flpPipeline,
})
Expand Down

0 comments on commit 1bf8cb9

Please sign in to comment.