Skip to content

Commit

Permalink
fix: prune empty query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaostrovskii Dmitrii committed Feb 20, 2024
1 parent 3461043 commit 70bf041
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
39 changes: 20 additions & 19 deletions libs/base-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import type { HttpClient, HttpContext, HttpHeaders, HttpParams } from '@angular/common/http';
import type { Observable } from 'rxjs';
import { pruneEmptyQueryParams } from './utils';

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 +46,8 @@ export abstract class BaseHttpService {
return this.relativePath;
}

private getPath(url: string): string {
return `${this.relativePath}${url ? `/${url}` : ''}`;
protected getPath(url: string): string {
const prunedUrl = pruneEmptyQueryParams(url);
return `${this.relativePath}${prunedUrl ? `/${prunedUrl}` : ''}`;
}
}
26 changes: 13 additions & 13 deletions libs/download.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ 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 +39,16 @@ 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);
method === 'get' ? this.http.get(this.getPath(url), downloadOptions) : this.http.post(this.getPath(url), data, downloadOptions);

const response = (await request.toPromise())!;
const filename = this.getFileName(response, saveAs);
Expand Down
22 changes: 22 additions & 0 deletions libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ export function getBasePath(alias: string, relativePath: string): string {

return `${basePath}${relativePath}`;
}

export function pruneEmptyQueryParams(url: string): string {
const querySeparator = '?';
const queryParametersSeparator = '&';

if (!url || !url.includes(querySeparator)) {
return url;
}

const splitedUrl = url.split(querySeparator);

const path = splitedUrl[0];
const query = splitedUrl[1];

const prunedQuery = query
.split(queryParametersSeparator)
.filter((x) => !x.trimEnd().endsWith('='))
.join(queryParametersSeparator)
.slice(0);

return prunedQuery.length ? `${path}${querySeparator}${prunedQuery}` : path;
}
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 70bf041

Please sign in to comment.