feature/wrike (#17)

* Fix code styles

* Add support for WRIKE

* Add tests
This commit is contained in:
Manuel Bouza
2019-04-26 13:05:14 +02:00
committed by Tobias Miesel
parent 25773cc661
commit a9d1726707
9 changed files with 118 additions and 83 deletions

View File

@@ -1,17 +1,28 @@
import UrlPattern from "url-pattern"
import {
isFunction,
isUndefined,
compose,
toPairs,
map,
pipe
} from "lodash/fp"
import { isFunction, isUndefined, compose, toPairs, map, pipe, isNil } from "lodash/fp"
import { asArray } from "./index"
import queryString from "query-string"
const extractQueryParams = (queryParams, query) => {
return toPairs(queryParams).reduce((acc, [key, param]) => {
acc[key] = query[param]
function parseUrl(url) {
const urlObject = new URL(url)
const { origin, pathname, search } = urlObject
let { hash } = urlObject
const query = {
...queryString.parse(search),
...queryString.parse(hash),
}
if (hash) {
hash = hash.match(/#[^&]+/)[0]
}
return { origin, pathname, hash, query }
}
function extractQueryParams(queryParams, query) {
return toPairs(queryParams).reduce((acc, [key, params]) => {
const param = asArray(params).find(param => !isNil(query[param]))
if (param) {
acc[key] = query[param]
}
return acc
}, {})
}
@@ -37,9 +48,9 @@ const parseServices = compose(
return new UrlPattern(...pattern)
}
return new UrlPattern(pattern)
})
}),
})),
toPairs
toPairs,
)
export const createEnhancer = document => service => {
@@ -57,19 +68,16 @@ export const createEnhancer = document => service => {
description: evaluate(service.description),
projectId: evaluate(service.projectId),
taskId: evaluate(service.taskId),
position: service.position || { right: "calc(2rem + 5px)" }
position: service.position || { right: "calc(2rem + 5px)" },
}
}
export const createMatcher = remoteServices => {
const services = parseServices(remoteServices)
return tabUrl => {
const { origin, pathname, hash, search } = new URL(tabUrl)
const { origin, pathname, hash, query } = parseUrl(tabUrl)
const url = `${origin}${pathname}${hash}`
const query = queryString.parse(search)
const service = services.find(service =>
service.patterns.some(pattern => pattern.match(url))
)
const service = services.find(service => service.patterns.some(pattern => pattern.match(url)))
if (!service) {
return
@@ -78,10 +86,7 @@ export const createMatcher = remoteServices => {
const pattern = service.patterns.find(pattern => pattern.match(url))
let match = pattern.match(url)
if (service.queryParams) {
const extractedQueryParams = extractQueryParams(
service.queryParams,
query
)
const extractedQueryParams = extractQueryParams(service.queryParams, query)
match = { ...extractedQueryParams, ...match }
}
@@ -89,7 +94,7 @@ export const createMatcher = remoteServices => {
...match,
...service,
url: tabUrl,
match
match,
}
}
}
@@ -99,6 +104,6 @@ export const createServiceFinder = remoteServices => document => {
const enhancer = createEnhancer(document)
return pipe(
matcher,
enhancer
enhancer,
)
}