Skip to content
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
44 changes: 33 additions & 11 deletions src/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ const appendQueryParam = (
return params
}

const selectStrictQueryParams = (tokens: Token[]) =>
tokens
.filter(t => /^query-parameter/.test(t.type))
.reduce<Record<string, string>>((acc, token) => {
if (token.otherVal[0]) {
acc[token.val[0] as string] = token.otherVal[0]
}

return acc
}, {})

export interface PathOptions {
/**
* Query parameters buiding and matching options, see
Expand Down Expand Up @@ -185,21 +196,27 @@ export class Path<T extends Record<string, any> = Record<string, any>> {
}
// Extract query params
const queryParams = parseQueryParams(path, options.queryParams)
const unexpectedQueryParams = Object.keys(queryParams).filter(
const hasUnexpectedQueryParams = Object.keys(queryParams).some(
p => !this.isQueryParam(p)
)

if (unexpectedQueryParams.length === 0) {
// Extend url match
Object.keys(queryParams).forEach(
// @ts-ignore
p => (match[p] = (queryParams as any)[p])
)
const strictSearchParams = selectStrictQueryParams(this.tokens)

return match
const hasUnmatchedStrictQueryParam = Object.keys(queryParams).some(p =>
strictSearchParams[p] ? strictSearchParams[p] !== queryParams[p] : false
)

if (hasUnexpectedQueryParams || hasUnmatchedStrictQueryParam) {
return null
}

return null
// Extend url match
Object.keys(queryParams).forEach(
// @ts-ignore
p => (match[p] = (queryParams as any)[p])
)

return match
}

public partialTest(
Expand Down Expand Up @@ -316,12 +333,17 @@ export class Path<T extends Record<string, any> = Record<string, any>> {
return base
}

const defaultSearchParams = selectStrictQueryParams(this.tokens)

const searchParams = this.queryParams
.filter(p => Object.keys(params).indexOf(p) !== -1)
.filter(
p => Object.keys(params).indexOf(p) !== -1 || defaultSearchParams[p]
)
.reduce<Record<string, any>>((sparams, paramName) => {
sparams[paramName] = params[paramName]
sparams[paramName] = params[paramName] || defaultSearchParams[paramName]
return sparams
}, {})

const searchPart = buildQueryParams(searchParams, options.queryParams)

return searchPart ? base + '?' + searchPart : base
Expand Down
2 changes: 1 addition & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const rules: IRule[] = [
},
{
name: 'query-parameter',
pattern: /^(?:\?|&)(?::)?([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})/
pattern: /^(?:\?|&)(?::)?([a-zA-Z0-9-_]*[a-zA-Z0-9]{1})(?:=([a-zA-Z0-9-_]+))?/
},
{
name: 'delimiter',
Expand Down
19 changes: 19 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ describe('Path', () => {
})
})

it('should match and build paths of query parameters with strict value', () => {
const path = new Path('/pages?id&page=test&search')

expect(path.build()).toBe('/pages?page=test')
expect(path.build({ page: 'foo' })).toBe('/pages?page=foo')

const otherTestParams = {
id: 'test',
search: 'query'
}

expect(path.test('/pages?id=test&page=test&search=query')).toEqual({
...otherTestParams,
page: 'test'
})

expect(path.test('/pages?id=test&page=foo&search=query')).toBeNull()
})

it('should match and build paths with url and query parameters', () => {
const path = new Path('/users/profile/:id-:id2?:id3')
expect(path.hasQueryParams).toBe(true)
Expand Down