Giter Site home page Giter Site logo

mswjs / node-match-path Goto Github PK

View Code? Open in Web Editor NEW
30.0 3.0 6.0 373 KB

Matches a URL against a path. Parameters, wildcards, RegExp.

Home Page: https://npmjs.com/package/node-match-path

JavaScript 6.71% TypeScript 93.29%
path route url match routing request wildcard martcher

node-match-path's People

Contributors

andrewswerlick avatar ariperkkio avatar dependabot[bot] avatar kettanaito avatar marcosvega91 avatar timdeschryver avatar v-gjy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-match-path's Issues

Does not work with urls that have a request content type extension and parameters

I haven't tested this extensively, but I'm using this library via MSW and I think I've found a fairly common use case that is not supported. Specifically, this library will not handle a url like
/api/books/:bookId.json.

In manually inspecting this file https://github.com/mswjs/node-match-path/blob/master/src/pathToRegExp.ts, I believe the problem is that line 19 does not include a "." in the list of characters to terminate the parameter match.

I'll try to submit a pull request and appropriate tests in the next day or so, but figured I'd go ahead and report the issue.

Escape question marks ("?")

Current behavior

Question marks in the string path are returned as-is, creating a logical RegExp operator.

Expected behavior

Question marks in the string path are escaped: \?.

Trailing path parameter matches query parameters

Steps to reproduce

match('/user/:userId', '/user/abc-123?hello=true')

Current behavior

{ params: { userId: 'abc-123?hello=true' } }

Expected behavior

{ params: { userId: 'abc-123' } }

Query parameter must be disregarded during path parameter match.

Underscore in parameter name is forbidden

Currently the RegExp for path parameter names does not account for the underscore character "_". This produces falsely negative path matches for paths that include parameters that contain underscores.

Current behavior

match('/user/:user_id', '/user/1')
// no match

Expected behavior

match('/user/:user_id', 'user/1')
// { matches: true, params: { user_id: 1 } }

Patch matching is case-sensitive

Current behavior

Expected: /foo
Actual: /Foo

Result: no match.

Expected behavior

Should behave as ExpressJS path matching in terms of case-sensitivity.

Expected: /foo
Actual: /Foo
Actual: /foo

Result: matches.

Support optional parameter notation

Expected result

const path = '/image/:transforms?/:version?/:id'

match(path, '/image/a,b,c/1.2.3/id') // { params: { transforms: 'a,b,c', version: '1.2.3', id: 'id' } }
match(path, '/image/a,b,c/id') // { params: { transforms: 'a,b,c', version: null, id: 'id' } }
match(path, '/image/id') // { params: { transforms: null, version: null, id: 'id' } }

Reproduce this behavior in the Express Route Tester.

Wildcard expects at least one character

Current behavior

/user/details*

Matches: /user/details0
Doesn't match: /user/details

Expected behavior

Matches: /user/details and /user/details0

This is the behavior exhibited by ExpressJS, we should respect that.

Path matching is sensitive to trailing slash

Consider this usage example:

import { match } from 'node-match-path'

match('https://test.msw.io', 'https://test.msw.io/')

Current behavior

The given path won't match the expected path.

Expected behavior

Trailing slash is ignored when matching paths.

Motivation

Trailing slash is often appended by the client automatically. For example, executing fetch('https://test.msw.io') would perform an actual request to https://test.msw.io/.

Support RegExp as "path" argument

I think the library should support RegExp supplied as the path argument in order to be compatible with path-to-regexp. Since the given string path is converted to a RegExp, handling an explicit RegExp won't be a big problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.