Implementation of the WHATWG URL Standard.
- Source:
- See:
Example
const urlEncoder = require('postman-url-encoder')
// Encoding URL string to Node.js compatible Url object
urlEncoder.toNodeUrl('郵便屋さん.com/foo&bar/{baz}?q=("foo")#`hash`')
// Encoding URI component
urlEncoder.encode('qüêry štrìng')
// Encoding query string object
urlEncoder.encodeQueryString({ q1: 'foo', q2: ['bãr', 'baž'] })
Methods
(inner) encode(value) → {String}
Percent-encode the given string using QUERY_ENCODE_SET.
Parameters:
Name | Type | Description |
---|---|---|
value |
String | String to percent-encode |
Returns:
Percent-encoded string
- Type
- String
Example
// returns 'foo%20%22%23%26%27%3C%3D%3E%20bar'
encode('foo "#&\'<=> bar')
// returns ''
encode(['foobar'])
(inner) encodeQueryString(query) → {String}
Percent-encode the URL query string or x-www-form-urlencoded body object according to RFC3986.
Parameters:
Name | Type | Description |
---|---|---|
query |
Object | Object representing query or urlencoded body |
Returns:
Percent-encoded string
- Type
- String
Example
// returns 'q1=foo&q2=bar&q2=baz'
encodeQueryString({ q1: 'foo', q2: ['bar', 'baz'] })
(inner) resolveNodeUrl(base, relative) → {String}
Resolves a relative URL with respect to given base URL. This is a replacement method for Node's url.resolve() which is compatible with URL object generated by toNodeUrl().
Parameters:
Name | Type | Description |
---|---|---|
base |
Object | String | URL string or toNodeUrl() object |
relative |
String | Relative URL to resolve |
Returns:
Resolved URL
- Type
- String
Example
// returns 'http://postman.com/baz'
resolveNodeUrl('http://postman.com/foo/bar', '/baz')
(inner) toLegacyNodeUrl(url) → {Url}
Converts URL string into Node.js compatible Url object using the v1 encoder.
Parameters:
Name | Type | Description |
---|---|---|
url |
String | URL string |
Returns:
Node.js compatible Url object
- Type
- Url
(inner) toNodeUrl(url, disableEncoding) → {Url}
Converts PostmanUrl / URL string into Node.js compatible Url object.
Parameters:
Name | Type | Description |
---|---|---|
url |
PostmanUrl | String | URL string or PostmanUrl object |
disableEncoding |
Boolean | Turn encoding off |
Returns:
Node.js like parsed and encoded object
- Type
- Url
Examples
Using URL string
toNodeUrl('郵便屋さん.com/foo&bar/{baz}?q=("foo")#`hash`')
// returns
// {
// protocol: 'http:',
// slashes: true,
// auth: null,
// host: 'xn--48jwgn17gdel797d.com',
// port: null,
// hostname: 'xn--48jwgn17gdel797d.com',
// hash: '#%60hash%60',
// search: '?q=(%22foo%22)',
// query: 'q=(%22foo%22)',
// pathname: '/foo&bar/%7Bbaz%7D',
// path: '/foo&bar/%7Bbaz%7D?q=(%22foo%22)',
// href: 'http://xn--48jwgn17gdel797d.com/foo&bar/%7Bbaz%7D?q=(%22foo%22)#%60hash%60'
// }
Using PostmanUrl instance
toNodeUrl(new sdk.Url({
host: 'example.com',
query: [{ key: 'foo', value: 'bar & baz' }]
}))