Skip to content

Commit

Permalink
feat: introduce device info pipe
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
oljekechoro authored Jul 6, 2020
1 parent 43b97bc commit 06e868b
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 5 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"@types/lodash.once": "^4.1.6",
"fast-luhn": "^1.1.0",
"lodash.once": "^4.1.1",
"lodash.set": "^4.3.2",
"platform": "^1.3.5",
"safe-json-stringify": "^1.2.0",
"tslib": "^1.11.1"
},
Expand All @@ -67,6 +69,8 @@
"@semantic-release/npm": "^7.0.3",
"@types/jest": "^25.1.3",
"@types/jest-json-schema": "^2.1.1",
"@types/lodash.set": "^4.3.6",
"@types/platform": "^1.3.2",
"@types/safe-json-stringify": "^1.1.0",
"@typescript-eslint/eslint-plugin": "^2.22.0",
"@typescript-eslint/parser": "^2.22.0",
Expand Down
1 change: 1 addition & 0 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { createHttpPipe } from './pipes/http'
export { createMaskerPipe, panMaskerPipe } from './pipes/masker'
export { createFlpPipeline, eventifyPipe } from './pipes/flp'
export { createDeviceInfoPipe, getDeviceInfo } from './pipes/deviceInfo'
export { createTransmitter, createTransmittable } from './transmitter'

export * from './interfaces'
Expand Down
22 changes: 21 additions & 1 deletion src/main/ts/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { IPromise } from '@qiwi/substrate'

export interface IBrowserInfo {
name?: string
version?: string | null
layout?: string | null
}

export interface IOperationalSystemInfo {
architecture?: number
family?: string | null
version?: string | null
}

export interface IDeviceInfo {
browser: IBrowserInfo
model: {
product?: string | null,
manufacturer?: string | null
},
os?: IOperationalSystemInfo
}

export interface ITransmittable {
data: any
err?: any
meta: {
history: Array<IHistoryEntry>

}
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/ts/pipes/deviceInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IDeviceInfo, IPipe, IPipeOutput, ITransmittable } from '../interfaces'
import { IPromise } from '@qiwi/substrate'
import platform from 'platform'
import { set, clone } from '../utils'

export const type = 'device-info'

export const getDeviceInfo = (userAgent?: string): IDeviceInfo => {
const parsedData = userAgent ? platform.parse(userAgent) : platform
const { name, version, layout, product, manufacturer, os } = parsedData
return {
browser: {
name,
version,
layout
},
model: {
product,
manufacturer
},
os
}
}

export const createDeviceInfoPipe = (): IPipe => ({
type,
execute ({ data }: ITransmittable): IPromise<IPipeOutput> {
const output = set(clone(data), 'meta.deviceInfo', getDeviceInfo())

return Promise.resolve([null, output])
}
})
10 changes: 8 additions & 2 deletions src/main/ts/pipes/flp.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { IClientEventDto, LogLevel } from '@qiwi/substrate'
import { IPipe, ITransmittable, TPipeline } from '../interfaces'
import { IClientEventDto as ISubstrateClientEventDto, IClientEventMeta, LogLevel } from '@qiwi/substrate'
import { IDeviceInfo, IPipe, ITransmittable, TPipeline } from '../interfaces'
import { panMaskerPipe } from './masker'
import { createHttpPipe, IHttpPipeOpts } from './http'

const DEFAULT_LEVEL = LogLevel.INFO

export const type = 'flp-eventify'

export interface IClientEventDto extends ISubstrateClientEventDto {
meta?: IClientEventMeta & {
deviceInfo?: IDeviceInfo & Record<string, any>
}
}

export const eventifyPipe: IPipe = {
type,
async execute ({ data }: ITransmittable) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/ts/utils/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { deepMap } from './deepmap'

export const clone = (src: any): any => deepMap(src, (v) => v)
3 changes: 3 additions & 0 deletions src/main/ts/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import set from 'lodash.set'
export { deepMap } from './deepmap'
export { clone } from './clone'
export { set }
33 changes: 33 additions & 0 deletions src/test/ts/pipes/deviceInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createDeviceInfoPipe, getDeviceInfo, IDeviceInfo } from '../../../main/ts'

describe('deviceInfoPipe', () => {
it('is returned by factory', () => {
const maskerPipe = createDeviceInfoPipe()

expect(maskerPipe.type).toBe('device-info')
expect(maskerPipe.execute).toEqual(expect.any(Function))
})
})

describe('getDeviceInfo', () => {
it('returns device info', () => {
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
const expected: IDeviceInfo = {
browser: {
name: 'Chrome',
version: '83.0.4103.116',
layout: 'Blink'
},
model: {
product: null,
manufacturer: null
},
os: {
architecture: 64,
family: 'OS X',
version: '10.14.6'
}
}
expect<IDeviceInfo>(getDeviceInfo(userAgent)).toMatchObject(expected)
})
})
2 changes: 1 addition & 1 deletion src/test/ts/pipes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('httpPipe', () => {
const transmittable: ITransmittable = { data: null, meta: { history: [] } }

return expect(httpPipe.execute(transmittable, noop))
.resolves.toEqual([null, {
.resolves.toMatchObject([null, {
data: {
id: 2,
email: '[email protected]',
Expand Down
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,13 @@
dependencies:
"@types/lodash" "*"

"@types/lodash.set@^4.3.6":
version "4.3.6"
resolved "https://registry.yarnpkg.com/@types/lodash.set/-/lodash.set-4.3.6.tgz#33e635c2323f855359225df6a5c8c6f1f1908264"
integrity sha512-ZeGDDlnRYTvS31Laij0RsSaguIUSBTYIlJFKL3vm3T2OAZAQj2YpSvVWJc0WiG4jqg9fGX6PAPGvDqBcHfSgFg==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.149"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
Expand Down Expand Up @@ -764,6 +771,11 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/platform@^1.3.2":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/platform/-/platform-1.3.2.tgz#c0404c4e2a08e04295ab3d1241a856c2a3cab06e"
integrity sha512-Tn6OuJDAG7bJbyi4R7HqcxXp1w2lmIxVXqyNhPt1Bm0FO2EWIi3CI87JVzF7ncqK0ZMPuUycS3wTMIk85EeF1Q==

"@types/qs@^6.2.31":
version "6.9.1"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.1.tgz#937fab3194766256ee09fcd40b781740758617e7"
Expand Down Expand Up @@ -4820,6 +4832,11 @@ lodash.once@^4.1.1:
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=

lodash.snakecase@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz#39d714a35357147837aefd64b5dcbb16becd8f8d"
Expand Down Expand Up @@ -6155,6 +6172,11 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

platform@^1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
integrity sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==

pn@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"
Expand Down Expand Up @@ -7888,11 +7910,16 @@ [email protected]:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==

typescript@^3.4, typescript@^3.8.3:
typescript@^3.4:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==

typescript@^3.8.3:
version "3.9.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.6.tgz#8f3e0198a34c3ae17091b35571d3afd31999365a"
integrity sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==

uglify-js@^3.1.4:
version "3.8.0"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.8.0.tgz#f3541ae97b2f048d7e7e3aa4f39fd8a1f5d7a805"
Expand Down

0 comments on commit 06e868b

Please sign in to comment.