Skip to content

Commit b00571d

Browse files
committed
Add ability to filter ?all requests
1 parent 127142a commit b00571d

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This is how you'd track pageviews for a website: (though note that this can be u
4444

4545
If you just want to get the views for an id and don't want to increment the views during a `GET` request, set `inc` to `false` in your query parameter. (`/x?inc=false`)
4646

47-
If you want to get all views for all ids, set the `all` query parameter to `true`. (`/?all=true`)
47+
If you want to get all views for all ids, set the `all` query parameter to `true`. (`/?all=true`) If you pass the `all` parameter to an id, all ids starting with that pathname will be included. (e.g. `/x?all=true` will match views for `/x`, `/xyz` but not `/y`)
4848

4949
## Built with
5050

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const { pushView } = require('./utils')
77
module.exports = async function (req, res) {
88
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true)
99
// Send all views down if "?all" is true
10-
if (query.all === 'true' || query.all === true) {
10+
if (String(query.all) === 'true') {
1111
const data = {
1212
data: {},
1313
time: Date.now()
1414
}
15-
for (let key of db.keys()) {
15+
for (let key of db.keys().filter(key => String(query.filter) === 'false' ? true : key.startsWith(pathname))) {
1616
data.data[key] = db.get(key)
1717
}
1818
send(res, 200, data)
@@ -25,7 +25,7 @@ module.exports = async function (req, res) {
2525
if (req.method !== 'GET' && req.method !== 'POST') {
2626
throw createError(400, 'Please make a GET or a POST request.')
2727
}
28-
const shouldIncrement = query.inc !== 'false' && query.inc !== false
28+
const shouldIncrement = String(query.inc) !== 'false'
2929
try {
3030
const currentViews = db.has(pathname) ? db.get(pathname).views.length : 0
3131
// Add a view and send the total views back to the client

tests/items.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,27 @@ describe('all', () => {
6868
expect(body.data['/route2'].views).toBeDefined()
6969
expect(body.data['/route2'].views.length).toBe(3)
7070
})
71+
72+
describe('filtering', () => {
73+
it('should filter based on pathname', async () => {
74+
await request(`${url}/rover`)
75+
await request(`${url}/route`)
76+
const body = JSON.parse(await request(`${url}/rover?all=true`))
77+
expect(Object.keys(body.data).length).toBe(1)
78+
expect(body.data['/rover'].views).toBeDefined()
79+
expect(body.data['/rover'].views.length).toBe(1)
80+
})
81+
82+
it('should filter based on starting with pathname', async () => {
83+
await request(`${url}/rover`)
84+
await request(`${url}/rover2`)
85+
await request(`${url}/route`)
86+
const body = JSON.parse(await request(`${url}/rover?all=true`))
87+
expect(Object.keys(body.data).length).toBe(2)
88+
expect(body.data['/rover'].views).toBeDefined()
89+
expect(body.data['/rover'].views.length).toBe(1)
90+
expect(body.data['/rover2'].views).toBeDefined()
91+
expect(body.data['/rover2'].views.length).toBe(1)
92+
})
93+
})
7194
})

0 commit comments

Comments
 (0)