Skip to content

Commit

Permalink
refactor(query.builder.ts): use AllFields type alias in select and so…
Browse files Browse the repository at this point in the history
…rt methods

feat(client-request.ts): add queryParams method to handle query parameters in GET requests
  • Loading branch information
mdwitr0 committed Jun 23, 2023
1 parent 7e91c25 commit d43b8e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
32 changes: 10 additions & 22 deletions src/builder/query.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,26 @@ import { IQueryFields } from '../interfaces/query-fields.interface';
import { MovieFields } from '../types/movie-fields.type';
import { PersonFields } from '../types/person-fields.type';
import { Pagination } from '../classes/pagination';
import {
AllFields,
IQueryBuilder,
} from '../interfaces/query-builder.interface';

export abstract class QueryBuilder<T extends IQueryFields> {
export abstract class QueryBuilder<T extends IQueryFields>
implements IQueryBuilder<T>
{
protected query: any;

constructor() {
this.query = {};
}

select(
fields: (
| T['BooleanFields']
| T['NumberFields']
| T['DateFields']
| T['StringFields']
)[],
): this {
select(fields: AllFields<T>[]): this {
this.query.selectFields = fields;
return this;
}

sort(
field:
| T['BooleanFields']
| T['NumberFields']
| T['DateFields']
| T['StringFields'],
sortType: SORT_TYPE | '1' | '-1',
): this {
sort(field: AllFields<T>, sortType: SORT_TYPE | '1' | '-1'): this {
if (!this.query.sortField) {
this.query.sortField = [];
this.query.sortType = [];
Expand All @@ -42,11 +34,7 @@ export abstract class QueryBuilder<T extends IQueryFields> {
}

filterExact(
field:
| T['BooleanFields']
| T['NumberFields']
| T['DateFields']
| T['StringFields'],
field: AllFields<T>,
value: string | number | boolean | SPECIAL_VALUE,
): this {
if (!this.query[field]) this.query[field] = [];
Expand Down
23 changes: 19 additions & 4 deletions src/client-request.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import { VERSIONS } from './enums/version.enum';
import { IResponseError } from './interfaces/error.interface';
import { VERSIONS } from './enums/version.enum';
import { IResponse } from './interfaces/response.interface';
import { queryParams } from './helper/query-params';

export class ClientRequest {
constructor(
private readonly API_KEY: string,
private readonly API_URL: string,
) {}

private queryParams(params: { [key: string]: any }): string {
let urlSearchParams = new URLSearchParams();

for (let key in params) {
if (Array.isArray(params[key])) {
for (let value of params[key]) {
urlSearchParams.append(key, value);
}
} else {
urlSearchParams.append(key, params[key]);
}
}

return urlSearchParams.toString();
}

async get<T, P extends Record<string, unknown>>(
version: VERSIONS,
path: string,
params?: P,
): Promise<IResponse<T>> {
try {
const response = await fetch(
`${this.API_URL}/${version}${path}?${queryParams(params as any)}`,
`${this.API_URL}/${version}${path}?${this.queryParams(params as any)}`,
{ headers: { 'X-API-KEY': this.API_KEY } },
);

// If the HTTP response status is not 200, throw an error
// Если статус HTTP-ответа не 200, выбрасывается ошибка
if (!response.ok) {
const error: IResponseError = await response.json();
throw new Error(
Expand Down

0 comments on commit d43b8e2

Please sign in to comment.