feat(hiboutik): add Hiboutik API client, repositories and DTOs

HiboutikClient handles Basic auth and walks paginated endpoints. One read-only repository per entity (brands, categories, suppliers, products, stocks, customers).
This commit is contained in:
MastersGame
2026-11-05 23:00:47 +01:00
parent 8f377a1fe9
commit 2424a086e0
13 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { firstValueFrom } from 'rxjs';
/** Results per page of every paginated Hiboutik endpoint (see "Results per page" in the API docs). */
const HIBOUTIK_PAGE_SIZE = 250;
/** Low-level HTTP client for the Hiboutik REST API: base URL and Basic auth credentials come from the environment. */
@Injectable()
export class HiboutikClient {
private readonly baseUrl: string;
private readonly authHeader: string;
constructor(
private readonly httpService: HttpService,
private readonly configService: ConfigService,
) {
const username = this.configService.get<string>('HIBOUTIK_USERNAME') ?? '';
const password = this.configService.get<string>('HIBOUTIK_PASSWORD') ?? '';
this.authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
this.baseUrl = this.configService.get<string>('HIBOUTIK_BASE_URL') ?? '';
}
/** GET on a Hiboutik path (relative to `HIBOUTIK_BASE_URL`), returns the response body. */
async get<T>(path: string, params?: Record<string, unknown>): Promise<T> {
const response = await firstValueFrom(
this.httpService.get<T>(`${this.baseUrl}${path}`, {
headers: { Authorization: this.authHeader },
params,
}),
);
return response.data;
}
/**
* Fetches every page of a paginated endpoint (`p` param, starting at 1) and concatenates the results.
* Only for paginated endpoints: on a non-paginated one, a full first page would loop forever on the same results.
*/
async getAllPages<T>(path: string, baseParams: Record<string, unknown> = {}): Promise<T[]> {
const results: T[] = [];
let page = 1;
while (true) {
const pageData = await this.get<T[]>(path, { ...baseParams, p: page });
if (!pageData || pageData.length === 0) break;
results.push(...pageData);
// A page smaller than the page size is the last one: no need to request an empty page
if (pageData.length < HIBOUTIK_PAGE_SIZE) break;
page++;
}
return results;
}
}