Skip to content

Commit

Permalink
fix: prune empty query parameters (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Zaostrovskii Dmitrii <dmitrii.zaostrovskii@dxc.com>
  • Loading branch information
zaostrovskii and Zaostrovskii Dmitrii authored Feb 20, 2024
1 parent 3461043 commit 8973f56
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
44 changes: 26 additions & 18 deletions libs/base-http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,30 @@ import type { HttpClient, HttpContext, HttpHeaders, HttpParams } from '@angular/
import type { Observable } from 'rxjs';

export interface IAngularHttpRequestOptions {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
headers?: HttpHeaders | { [header: string]: string | string[] };
observe?: 'body';
context?: HttpContext;
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean> };
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}

export abstract class BaseHttpService {
constructor(private relativePath: string, protected http: HttpClient) { }
constructor(
private relativePath: string,
protected http: HttpClient
) {}

protected get<TResult>(url: string, options?: IAngularHttpRequestOptions): Observable<TResult> {
return this.http.get<TResult>(this.getPath(url), options);
}

protected post<TResult, TData = {}>(
url: string,
data?: TData,
options?: IAngularHttpRequestOptions
): Observable<TResult> {
protected post<TResult, TData = {}>(url: string, data?: TData, options?: IAngularHttpRequestOptions): Observable<TResult> {
return this.http.post<TResult>(this.getPath(url), data, options);
}

protected put<TResult, TData = {}>(
url: string,
data?: TData,
options?: IAngularHttpRequestOptions
): Observable<TResult> {
protected put<TResult, TData = {}>(url: string, data?: TData, options?: IAngularHttpRequestOptions): Observable<TResult> {
return this.http.put<TResult>(this.getPath(url), data, options);
}

Expand All @@ -46,7 +37,24 @@ export abstract class BaseHttpService {
return this.relativePath;
}

protected prune(url: string): string {
const uri = new URL(url, window.location.origin);
if (!uri.search) {
return url;
}

const toDelete: string[] = [];
uri.searchParams.forEach((value, key) => {
if (!value) {
toDelete.push(key);
}
});

toDelete.forEach((key) => uri.searchParams.delete(key));
return `${uri.pathname}${uri.search}`;
}

private getPath(url: string): string {
return `${this.relativePath}${url ? `/${url}` : ''}`;
return this.prune(`${this.relativePath}${url ? `/${url}` : ''}`);
}
}
18 changes: 6 additions & 12 deletions libs/download.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ export interface IDownloadResult {
}

interface IAngularHttpRequestOptionsBlob {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
headers?: HttpHeaders | { [header: string]: string | string[] };
observe: 'response';
context?: HttpContext;
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean> };
reportProgress?: boolean;
responseType: 'blob';
withCredentials?: boolean;
}



export class DownloadFileService extends BaseHttpService {
private readonly fileNameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
private readonly defaultFileName = 'unknown filename';
Expand All @@ -37,18 +31,18 @@ export class DownloadFileService extends BaseHttpService {
method: 'post' | 'get',
data?: {},
saveAs?: string,
options?: IAngularHttpRequestOptions,
options?: IAngularHttpRequestOptions
): Promise<IDownloadResult> {
const downloadOptions: IAngularHttpRequestOptionsBlob = {
...options,
observe: 'response',
responseType: 'blob'
}
};

const request =
method === 'get'
? this.http.get(`${this.path}/${url}`, downloadOptions)
: this.http.post(`${this.path}/${url}`, data, downloadOptions);
? this.http.get(this.prune(`${this.path}/${url}`), downloadOptions)
: this.http.post(this.prune(`${this.path}/${url}`), data, downloadOptions);

const response = (await request.toPromise())!;
const filename = this.getFileName(response, saveAs);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luxbss/gengen",
"version": "1.2.5",
"version": "1.2.6",
"description": "Tool for generating models and Angular services based on OpenAPIs and Swagger's JSON",
"bin": {
"gengen": "./bin/index.js"
Expand Down

0 comments on commit 8973f56

Please sign in to comment.