URI Parser for Catberry Framework
Installation
npm install catberry-uri
Description
This is well-tested URI parser implementation that has been developed strictly according to RFC 3986.
It supports percent-encoding for all URI components except scheme and port. Percent encoding/decoding happens automatically.
It validates scheme and port component values during URI recombination.
It implements relative reference resolution algorithm from RFC 3986 5.2.
Usage
Parse URI
const catberryURI = ;const URI = catberryURIURI;const uri = 'http://user:pass@example.org:3000/some/path?some=value&some2=value&some2=value2&some3#fragment';console;
And you will get such object
scheme: 'http' authority: userInfo: user: 'user' password: 'pass' port: '3000' host: 'example.org' path: '/some/path' query: values: some: 'value' some2: 'value' 'value2' some3: null fragment: 'fragment' }
This object is instance of URI
constructor, authority
field is instance
of Authority
, authority.userInfo
is instance of UserInfo
and query
is
instance of Query
.
Get URI string
You can safely edit every component of parsed URI and then get URI string back.
When you convert URI object to string scheme and port values are validated and
could throw exception if values are not satisfy /^[a-z]+[a-z\d\+\.-]*$/i
and
/^\d+$/
respectively.
To get string representation of all these URI components just use toString()
uriauthorityuserInfo; // user:passuriauthority; // user:pass@example.org:3000uriquery; // some=value&some2=valueconsole;// http://user:pass@example.org:3000/some/path?some=value&some2=value&some2=value2&some3#fragment
Building new URI
Also, every URI component including URI itself has clone
method to create
a cloned URI component for building another URI.
You can create new empty URI like this:
const uri = ;urischeme = 'http';// there is a static method in the URI classuriauthority = URI;// and also, an instance methoduriauthorityuserInfo = uri;uriauthorityuserInfouser = 'user';uriauthorityuserInfopassword = 'pass';uriauthorityhost = 'example.org';uriauthorityport = '3000';uripath = '/some/path';uriquery = uri;uriqueryvalues = some: 'value' some2: 'value' 'value2' some3: nullurifragment = 'fragment'; console;// http://user:pass@example.org:3000/some/path?some=value&some2=value&some2=value2&some3#fragment
All URI components are optional and null
by default.
null
and undefined
values of every component are ignored otherwise any value
will be converted to string.
Resolve relative URI
Also, you can resolve any relative URI using base URI.
const uri = '../../../../g';const baseUri = 'http://a/b/c/d;p?q'; const absoluteUri = uri;console; // http://a/g
Interface
/** * Creates a new URI authority component. * @param {string?} Existing string. * @return */ static {} /** * Creates a new URI authority component. * @param {string?} Existing string. * @return */ {} /** * Creates a new URI user info component. * @param {string?} Existing string. * @return */ static {} /** * Creates a new URI user info component. * @param {string?} Existing string. * @return */ {} /** * Creates a new URI query component. * @param {string?} Existing string. * @return */ static /** * Creates a new URI query component. * @param {string?} Existing string. * @return */ {} /** * Creates new instance of URI according to RFC 3986. * @param {string?} uriString URI string to parse components. */ {} /** * Converts a URI reference that might be relative to a given base URI * into the reference's target URI. * https://tools.ietf.org/html/rfc3986#section-5.2 * @param * @returns */ {} /** * Clones current URI to a new object. * @returns */ {} /** * Recomposes URI components to URI string, * https://tools.ietf.org/html/rfc3986#section-5.3 * @returns */ {}}
Contributing
There are a lot of ways to contribute:
- Give it a star
- Join the Gitter room and leave a feedback or help with answering users' questions
- Submit a bug or a feature request
- Submit a PR
Denis Rechkunov denis.rechkunov@gmail.com