Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
feat(serve): allow serveEndpoints to allow wildcard query params
Browse files Browse the repository at this point in the history
You can allow any value for a query param by using a wildcard

fix #134
  • Loading branch information
tamj0rd2 authored and probot-auto-merge[bot] committed May 7, 2020
1 parent 526e9af commit d0d6f11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Here's a format that describes each config setting:
# Or...
endpoints:
- /my/endpoint1
- /my/endpoint2?hello=world # only served if request has query param "hello" with value "world"
# only served if request has query param "hello" with value "world"
- /my/endpoint2?hello=world
```

### request.serveEndpoint
Expand All @@ -94,13 +95,23 @@ Here's a format that describes each config setting:
([read more](https://expressjs.com/en/guide/routing.html#route-paths)).
Regex is not yet supported.<br>
This property is ignored in Test mode.
In Serve Mode, your config will be served on the `request.endpoints`
and also this endpoint.
In Serve Mode, your config will be served on each specified endpoint of
`request.endpoints` and also this endpoint.
- **Type**: string
- **Required?**: Required in Serve mode if `request.endpoints` is not provided
- **Example**:
```yaml
# will serve any endpoints starting with /api/books/
serveEndpoint: /api/books/*
# to require a query string with key 'hello' and value 'world'
serveEndpoint: /api/books/book1?hello=world
# to require a query string with key 'hello' set to any value (*)
serveEndpoint: /api/books/book1?hello=*
# to require multiple query params
serveEndpoint: /api/books/book1?hello=world&something=*&spongebob=squarepants
```

### request.method
Expand Down
17 changes: 17 additions & 0 deletions src/commands/serve/server/query-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import validateQuery from './query-validator'
import { randomString } from '~test-helpers'

jest.unmock('./query-validator')
jest.unmock('qs')
Expand Down Expand Up @@ -90,3 +91,19 @@ it('returns false if an object does not match deeply', () => {

expect(isValid).toBe(false)
})

describe('wildcard queries', () => {
it('returns true for any value', () => {
const endpoint = '/api/resource?hello=*'
const query = { hello: randomString() }

expect(validateQuery(endpoint, query)).toBe(true)
})

it('returns true for an array of any values', () => {
const endpoint = '/api/resource?hello=*'
const query = { hello: [randomString(), randomString()] }

expect(validateQuery(endpoint, query)).toBe(true)
})
})
1 change: 1 addition & 0 deletions src/commands/serve/server/query-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const isStringArray = (x: unknown): x is string[] => Array.isArray(x) && typeof

const compareQuery = (expected: Query[number], actual: Query[number]): boolean => {
if (typeof expected === 'string') {
if (expected === '*') return true
if (typeof actual === 'string') return expected === actual
if (isStringArray(actual)) return actual.includes(expected)
return false
Expand Down

0 comments on commit d0d6f11

Please sign in to comment.