Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prune empty query parameters #136

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions libs/base-http.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}

@@ -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
@@ -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';
@@ -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);
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"