Skip to content

Commit

Permalink
migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamero committed Jan 3, 2023
1 parent 3dd313d commit 18fc72f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
{{#node}}
// TODO: evaluate if we can easily get rid of this library
import {{^supportsES6}}* as{{/supportsES6}} FormData from "form-data";
import { URLSearchParams } from 'url';
import { URL, URLSearchParams } from 'url';
import * as http from 'http';
import * as https from 'https';
{{/node}}
{{/platforms}}
{{#platforms}}
{{^deno}}
import {{^supportsES6}}* as{{/supportsES6}} URLParse from "url-parse";
{{/deno}}
{{/platforms}}

import { Observable, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};

{{#platforms}}
Expand Down Expand Up @@ -57,44 +53,6 @@ export type HttpFile = {{{fileContentDataType}}} & { readonly name: string };
{{/node}}
{{/platforms}}

{{#platforms}}
{{#deno}}
/**
* URLParse Wrapper for Deno
*/
class URLParse {
private url: URL;
constructor(address: string, _parser: boolean) {
this.url = new URL(address);
}

public set(_part: 'query', obj: {[key: string]: string | undefined}) {
for (const key in obj) {
const value = obj[key];
if (value) {
this.url.searchParams.set(key, value);
} else {
this.url.searchParams.set(key, "");
}
}
}

public get query() {
const obj: {[key: string]: string} = {};
for (const [key, value] of this.url.searchParams.entries()) {
obj[key] = value;
}
return obj;
}

public toString() {
return this.url.toString();
}
}
{{/deno}}
{{/platforms}}

export class HttpException extends Error {
public constructor(msg: string) {
super(msg);
Expand All @@ -112,7 +70,7 @@ export type RequestBody = undefined | string | FormData | URLSearchParams;
export class RequestContext {
private headers: { [key: string]: string } = {};
private body: RequestBody = undefined;
private url: URLParse;
private url: URL;
{{#platforms}}
{{#node}}
private agent: http.Agent | https.Agent | undefined = undefined;
Expand All @@ -126,23 +84,25 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/*
* Returns the url set in the constructor including the query string
*
*/
public getUrl(): string {
return this.url.toString();
return this.url.toString().endsWith("/") ?
this.url.toString().slice(0, -1)
: this.url.toString();
}

/**
* Replaces the url set in the constructor with this url.
*
*/
public setUrl(url: string) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/**
Expand Down Expand Up @@ -171,9 +131,7 @@ export class RequestContext {
}

public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
this.url.searchParams.set(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as URLParse from "url-parse";

import { Observable, from } from '../rxjsStub';

export * from './isomorphic-fetch';
Expand All @@ -23,7 +23,6 @@ export enum HttpMethod {
*/
export type HttpFile = Blob & { readonly name: string };


export class HttpException extends Error {
public constructor(msg: string) {
super(msg);
Expand All @@ -41,7 +40,7 @@ export type RequestBody = undefined | string | FormData | URLSearchParams;
export class RequestContext {
private headers: { [key: string]: string } = {};
private body: RequestBody = undefined;
private url: URLParse;
private url: URL;

/**
* Creates the request context using a http method and request resource url
Expand All @@ -50,23 +49,25 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/*
* Returns the url set in the constructor including the query string
*
*/
public getUrl(): string {
return this.url.toString();
return this.url.toString().endsWith("/") ?
this.url.toString().slice(0, -1)
: this.url.toString();
}

/**
* Replaces the url set in the constructor with this url.
*
*/
public setUrl(url: string) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/**
Expand Down Expand Up @@ -95,9 +96,7 @@ export class RequestContext {
}

public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
this.url.searchParams.set(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import URLParse from "url-parse";

import { Observable, from } from '../rxjsStub';

export * from './isomorphic-fetch';
Expand All @@ -23,7 +23,6 @@ export enum HttpMethod {
*/
export type HttpFile = Blob & { readonly name: string };


export class HttpException extends Error {
public constructor(msg: string) {
super(msg);
Expand All @@ -41,7 +40,7 @@ export type RequestBody = undefined | string | FormData | URLSearchParams;
export class RequestContext {
private headers: { [key: string]: string } = {};
private body: RequestBody = undefined;
private url: URLParse;
private url: URL;

/**
* Creates the request context using a http method and request resource url
Expand All @@ -50,23 +49,25 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/*
* Returns the url set in the constructor including the query string
*
*/
public getUrl(): string {
return this.url.toString();
return this.url.toString().endsWith("/") ?
this.url.toString().slice(0, -1)
: this.url.toString();
}

/**
* Replaces the url set in the constructor with this url.
*
*/
public setUrl(url: string) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/**
Expand Down Expand Up @@ -95,9 +96,7 @@ export class RequestContext {
}

public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
this.url.searchParams.set(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as URLParse from "url-parse";

import { Observable, from } from '../rxjsStub';

export * from './isomorphic-fetch';
Expand All @@ -23,7 +23,6 @@ export enum HttpMethod {
*/
export type HttpFile = Blob & { readonly name: string };


export class HttpException extends Error {
public constructor(msg: string) {
super(msg);
Expand All @@ -41,7 +40,7 @@ export type RequestBody = undefined | string | FormData | URLSearchParams;
export class RequestContext {
private headers: { [key: string]: string } = {};
private body: RequestBody = undefined;
private url: URLParse;
private url: URL;

/**
* Creates the request context using a http method and request resource url
Expand All @@ -50,23 +49,25 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/*
* Returns the url set in the constructor including the query string
*
*/
public getUrl(): string {
return this.url.toString();
return this.url.toString().endsWith("/") ?
this.url.toString().slice(0, -1)
: this.url.toString();
}

/**
* Replaces the url set in the constructor with this url.
*
*/
public setUrl(url: string) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/**
Expand Down Expand Up @@ -95,9 +96,7 @@ export class RequestContext {
}

public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
this.url.searchParams.set(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO: evaluate if we can easily get rid of this library
import * as FormData from "form-data";
import { URLSearchParams } from 'url';
import { URL, URLSearchParams } from 'url';
import * as http from 'http';
import * as https from 'https';
import * as URLParse from "url-parse";

import { Observable, from } from '../rxjsStub';

export * from './isomorphic-fetch';
Expand Down Expand Up @@ -31,7 +31,6 @@ export type HttpFile = {
name: string
};


export class HttpException extends Error {
public constructor(msg: string) {
super(msg);
Expand All @@ -49,7 +48,7 @@ export type RequestBody = undefined | string | FormData | URLSearchParams;
export class RequestContext {
private headers: { [key: string]: string } = {};
private body: RequestBody = undefined;
private url: URLParse;
private url: URL;
private agent: http.Agent | https.Agent | undefined = undefined;

/**
Expand All @@ -59,23 +58,25 @@ export class RequestContext {
* @param httpMethod http method
*/
public constructor(url: string, private httpMethod: HttpMethod) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/*
* Returns the url set in the constructor including the query string
*
*/
public getUrl(): string {
return this.url.toString();
return this.url.toString().endsWith("/") ?
this.url.toString().slice(0, -1)
: this.url.toString();
}

/**
* Replaces the url set in the constructor with this url.
*
*/
public setUrl(url: string) {
this.url = new URLParse(url, true);
this.url = new URL(url);
}

/**
Expand Down Expand Up @@ -104,9 +105,7 @@ export class RequestContext {
}

public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
this.url.searchParams.set(name, value);
}

/**
Expand Down
Loading

0 comments on commit 18fc72f

Please sign in to comment.