This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Not an issue, question about request and querystrings #167
Labels
Milestone
Comments
What I find confusing in general is the current inconsistency between ES and node.js' URL module: A feature request for UrlSearchParams: I agree but it would be fine to have the node output interpretation as well ... |
@kitsonk fyi … import { has } from '../../core/src/main';
import { UrlSearchParams } from '../../core/src/main';
const url = (has('host-node')) ? require('url') : window.URL;
export class Parameters extends UrlSearchParams {
constructor(input?: any) {
super(input);
}
/**
* Returns a plain object with all first values OR
* the first value associated with a key here!
* @param key The key to return the first value for
* @return The first string value for the key
*/
get(key?: string): any {
if (!this.has(key)) {
return Object.keys(this._list).reduce((_o,key,i,arr): any => {
_o[key] = this._list[key][0];
return _o;
}, {});
}
return this._list[key][0];
}
}
class URL {
static protocolPattern = /^([a-z0-9.+-]+:)/i;
static hostlessProtocol = {
'javascript': true,
'javascript:': true
};
// protocols that always contain a // bit.
static slashedProtocol = {
'http': true,
'https': true,
'ftp': true,
'gopher': true,
'file': true,
'http:': true,
'https:': true,
'ftp:': true,
'gopher:': true,
'file:': true
}
static format(urlObject: any|string) {
if (has('host-node')) {
return url.format(urlObject);
};
return (new url(urlObject)).toString();
}
static parse(urlStr: string, parseQuery: boolean = false, slashesDenoteHost: boolean = false) {
if (has('host-node')) {
return url.parse(urlStr, parseQuery, slashesDenoteHost);
}
const U = new url(urlStr);
U.path = [U.pathname||'', U.search||''].join('');
// auth
const pw = (typeof U.password === 'string' && U.password.length) ?
[':',U.password].join('') : '';
U.auth = (typeof U.username === 'string' && U.username.length) ?
[U.username,U.password] : '';
// query
if (parseQuery) {
U.query = (typeof U.searchParams === 'object' ) ?
U.searchParams : new Parameters(U.search||'').get();
} else {
U.query = U.search;
}
// slashes
var rest = urlStr;
rest = rest.trim();
var proto: RegExpExecArray = URL.protocolPattern.exec(rest);
if (proto) {
var lowerProto = proto[0].toLowerCase();
U.protocol = lowerProto;
rest = rest.substr(proto[0].length);
}
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
var slashes = (rest.substr(0, 2) === '//');
var lowerProto = proto[0].toLowerCase();
if (slashes && !(lowerProto && URL.hostlessProtocol[lowerProto])) {
rest = rest.substr(2);
U.slashes = true;
}
}
return U;
}
static resolve(from: string, to: string) {
if (has('host-node')) {
return url.resolve(from, to);
};
/* TODO FIXME and implement browser */
}
}
export default URL; |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hey,
currently writing an "auth" module using core/request and wonder if the parsing of querystrings could go to the
request
module as aresponseType
because I think it is common (?) :The text was updated successfully, but these errors were encountered: