|
| 1 | +# @polka/url [](https://npmjs.org/package/@polka/url) |
| 2 | + |
| 3 | +> Super fast, memoized `req.url` parser; _not_ limited to [Polka][polka]! |
| 4 | +
|
| 5 | +Parses the `url` from a [`IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage) request. |
| 6 | + |
| 7 | +If the `req.url` contains a hash (`#`) then the native [`url.parse`](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost) will be used, returning a complete [`Url`](https://nodejs.org/api/url.html#url_class_url).<br>Otherwise, the return object will only contain the following keys: `search`, `query`, `pathname`, `path`, `href`, and `_raw`. |
| 8 | + |
| 9 | +> **Note:** This library does not process `protocol`, `hostname`, `port`, etc.<br>This is because the incoming `req.url` value only begins with the path information. |
| 10 | +
|
| 11 | +Parsed requests will be mutated with a `_parsedUrl` key, containing the returned output. This is used for future memoization, so as to avoid parsing the same `url` value multiple times. |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +``` |
| 16 | +$ npm install --save @polka/url |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```js |
| 22 | +const parse = require('@polka/url'); |
| 23 | + |
| 24 | +let req = { url: '/foo/bar?fizz=buzz' }; |
| 25 | +let foo = parse(req); |
| 26 | +//=> { search: '?fizz=buzz', |
| 27 | +//=> query: 'fizz=buzz', |
| 28 | +//=> pathname: '/foo/bar', |
| 29 | +//=> path: '/foo/bar?fizz=buzz', |
| 30 | +//=> href: '/foo/bar?fizz=buzz', |
| 31 | +//=> _raw: '/foo/bar?fizz=buzz' } |
| 32 | + |
| 33 | +// Attaches result for future memoization |
| 34 | +assert.deepEqual(foo, req._parsedUrl); //=> true |
| 35 | + |
| 36 | +// Process a request w/ a hashtag |
| 37 | +req = { url: '/foo/bar?fizz=buzz#hello' }; |
| 38 | +let bar = parse(req); |
| 39 | +//=> Url { |
| 40 | +//=> protocol: null, |
| 41 | +//=> slashes: null, |
| 42 | +//=> auth: null, |
| 43 | +//=> host: null, |
| 44 | +//=> port: null, |
| 45 | +//=> hostname: null, |
| 46 | +//=> hash: '#hello', |
| 47 | +//=> search: '?fizz=buzz', |
| 48 | +//=> query: 'fizz=buzz', |
| 49 | +//=> pathname: '/foo/bar', |
| 50 | +//=> path: '/foo/bar?fizz=buzz', |
| 51 | +//=> href: '/foo/bar?fizz=buzz#hello', |
| 52 | +//=> _raw: '/foo/bar?fizz=buzz#hello' } |
| 53 | + |
| 54 | +// Also attaches result for future memoization |
| 55 | +assert.deepEqual(bar, req._parsedUrl); //=> true |
| 56 | +``` |
| 57 | + |
| 58 | +## API |
| 59 | + |
| 60 | +### url(req) |
| 61 | +Returns: `Object` or `undefined` |
| 62 | + |
| 63 | +> **Important:** The `req` must have a `url` key, otherwise `undefined` will be returned.<br>If no input is provided at all, a `TypeError` will be thrown. |
| 64 | +
|
| 65 | +#### req |
| 66 | +Type: `IncomingMessage` |
| 67 | + |
| 68 | +The incoming HTTP request (`req`). |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## Support |
| 73 | + |
| 74 | +Any issues or questions can be sent to the [Polka][polka] repo, but please specify that your inquiry is about `@polka/url` specifically. |
| 75 | + |
| 76 | + |
| 77 | +## License |
| 78 | + |
| 79 | +MIT © [Luke Edwards](https://lukeed.com) |
| 80 | + |
| 81 | +[polka]: https://github.com/lukeed/polka |
0 commit comments