Skip to content

Commit

Permalink
feat: add matchUrl
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove captureUrl in favor of matchUrl.
  • Loading branch information
davidbonnet committed Aug 23, 2020
1 parent c87b0be commit d334d6b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 38 deletions.
17 changes: 9 additions & 8 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Server as WebSocketServer } from 'ws'

import {
cache,
captureUrl,
matchUrl,
combine,
exact,
exposeFolder,
Expand Down Expand Up @@ -121,6 +121,10 @@ const LINKS = [
href: '/test',
label: 'Test page',
},
{
href: '/project/123',
label: 'Match URL example',
},
{
href: '/sessions',
label: 'Current sessions',
Expand Down Expand Up @@ -255,13 +259,10 @@ const handle = combine(
),
),
}),
combine(
captureUrl(/\/([a-z_]+)\/([0-9]+)/, 'project', 'id'),
exact((request) =>
request.respond({
body: JSON.stringify(request.captures, null, 2),
}),
),
matchUrl(/\/([a-z_]+)\/([0-9]+)$/, ['project', 'id'], (request) =>
request.respond({
body: JSON.stringify(request.matches, null, 2),
}),
),
printHtmlMessage('Page not found', '', false),
)
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { ask } from './src/ask'
export { branch } from './src/branch'
export { cache } from './src/cache'
export { captureUrl } from './src/captureUrl'
export { clearSession } from './src/clearSession'
export { combine } from './src/combine'
export { decompress } from './src/decompress'
Expand All @@ -17,6 +16,7 @@ export { HTTPError } from './src/HTTPError'
export { isCompressible } from './src/isCompressible'
export { isStream } from './src/isStream'
export { log } from './src/log'
export { matchUrl } from './src/matchUrl'
export { MockRequest } from './src/MockRequest'
export { MockResponse } from './src/MockResponse'
export { MockSocket } from './src/MockSocket'
Expand Down
16 changes: 8 additions & 8 deletions src/captureUrl.js → src/matchUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL } from 'url'

export function captureUrl(pattern, ...names) {
export function matchUrl(pattern, groupNames, handler) {
return function (request, next) {
if (!request.href) {
const href = new URL(
Expand All @@ -14,16 +14,16 @@ export function captureUrl(pattern, ...names) {
if (match == null || match.index !== 0) {
return next(request)
}
if (request.captures == null) {
request.captures = {}
if (request.matches == null) {
request.matches = {}
}
const { captures } = request
const { length } = names
const { matches } = request
const { length } = groupNames
for (let i = 0; i < length; i++) {
const name = names[i]
captures[name] = match[i + 1]
const name = groupNames[i]
matches[name] = match[i + 1]
}
request.pathname = pathname.slice(match[0].length)
return next(request)
return handler(request, next)
}
}
17 changes: 10 additions & 7 deletions src/tests/captureUrl.js → src/tests/matchUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import { ask } from '../ask'
import { writeBody } from '../writeBody'
import { writeHeaders } from '../writeHeaders'
import { writeContentLength } from '../writeContentLength'
import { captureUrl } from '../captureUrl'
import { matchUrl } from '../matchUrl'
import { exact } from '../exact'
import { STATUS_CODES } from '../STATUS_CODES'

test('captures url patterns', async (assert) => {
test('matches url patterns', async (assert) => {
const handler = combine(
writeBody,
writeHeaders,
writeContentLength,
captureUrl(/\/([a-z_]+)\/([0-9]+)/, 'project', 'id'),
exact((request) =>
request.respond({
body: JSON.stringify(request.captures, null, 2),
}),
matchUrl(
/\/([a-z_]+)\/([0-9]+)/,
['project', 'id'],
exact((request) =>
request.respond({
body: JSON.stringify(request.matches, null, 2),
}),
),
),
(request) => request.respond({ statusCode: STATUS_CODES.NOT_FOUND }),
)
Expand Down
Binary file removed src/tests/snapshots/captureUrl.js.snap
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Snapshot report for `src/tests/captureUrl.js`
# Snapshot report for `src/tests/matchUrl.js`

The actual snapshot is saved in `captureUrl.js.snap`.
The actual snapshot is saved in `matchUrl.js.snap`.

Generated by [AVA](https://avajs.dev).

## captures url patterns
## matches url patterns

> matches the URL
Expand Down
Binary file added src/tests/snapshots/matchUrl.js.snap
Binary file not shown.
11 changes: 0 additions & 11 deletions src/tests/snapshots/writeCompressibleBody.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ Generated by [AVA](https://avajs.dev).
␍␊
eDQo=GwwA+CUAShBDioV6/gM=DQo=MA0KDQo=`

## writes body in gzip

> matches
`HTTP/1.1 200 OK␍␊
content-encoding: gzip␍␊
Connection: keep-alive␍␊
Transfer-Encoding: chunked␍␊
␍␊
aDQo=H4sIAAAAAAAAEw==DQo=MTc=DQo=C0otLsjPK05VSMpPqQQAwz31vw0AAAA=DQo=MA0KDQo=`

## writes uncompressed body if no compatible acceptable encoding found

> matches
Expand Down
Binary file modified src/tests/snapshots/writeCompressibleBody.js.snap
Binary file not shown.

0 comments on commit d334d6b

Please sign in to comment.