Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Not an issue, question about request and querystrings #167

Open
sebilasse opened this issue May 27, 2016 · 2 comments
Open

Not an issue, question about request and querystrings #167

sebilasse opened this issue May 27, 2016 · 2 comments

Comments

@sebilasse
Copy link

sebilasse commented May 27, 2016

Hey,
currently writing an "auth" module using core/request and wonder if the parsing of querystrings could go to the request module as a responseType because I think it is common (?) :

import { UrlSearchParams } from '../../core/src/main';

/**
 * Add a filter that automatically parses incoming querystrings.
 */
filterRegistry.register(
    function (response: Response<any>, url: string, options: RequestOptions) {
        return typeof response.data && options && options.responseType === 'querystring';
    },
    function (response: Response<any>, url: string, options: RequestOptions): Object {
        return {
                data: (new Parameters(String(response.data))).get()
            };
    }
);
@sebilasse
Copy link
Author

sebilasse commented May 28, 2016

What I find confusing in general is the current inconsistency between ES and node.js' URL module:
E.g. currently the naming is
searchParams in ES 'URL'
and
query in node.js 'url' …

A feature request for UrlSearchParams:
dojo output { a: [ 'b' ], b: [ '1' ], doC: [ '' ] }
node output { a: 'b', b: '1', doC: '' }

I agree but it would be fine to have the node output interpretation as well ...

@kitsonk kitsonk added this to the 2016.06 milestone Jun 7, 2016
@kitsonk kitsonk modified the milestones: 2016.06, 2016.07 Jul 4, 2016
@sebilasse
Copy link
Author

@kitsonk fyi …
I am currently using this "polyfill" and I thought it would be fair to post it here :

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;

@kitsonk kitsonk modified the milestones: 2016.07, 2016.08 Aug 1, 2016
@kitsonk kitsonk removed this from the 2016.08 milestone Oct 4, 2016
@dylans dylans added this to the 2017.02 milestone Jan 12, 2017
@dylans dylans modified the milestones: 2017.03, 2017.02 Feb 19, 2017
@dylans dylans modified the milestones: 2017.03, 2017.04 Apr 2, 2017
@dylans dylans modified the milestones: 2017.04, 2017.05 Apr 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants