import ApiClient from './ApiClient'; import { mapConnectionResponse } from './ConnectionClient'; import * as C from './types/Catalog'; import { Paginated } from './types/Paginated'; import * as P from './types/Product'; import urls from './urls'; export const mapCatalogLocationResponse = ( c: C.CatalogLocationResponse, ): C.CatalogLocation => ({ id: c.id, name: c.name, }); export const mapCatalogResponse = (c: C.CatalogResponse): C.Catalog => ({ id: c.id, name: c.name, connection: c.connection && mapConnectionResponse(c.connection), createdAt: c.created_at, updatedAt: c.updated_at, profile: c.profile && { id: c.profile.id, name: c.profile.name, email: c.profile.email, }, settings: c.settings, }); export const mapProductResponse = (p: P.Product): P.Product => ({ id: p.id, name: p.name, }); export default class CatalogClient { async getCatalogs( this: ApiClient, params: C.GetCatalogsQuery = {}, ): Promise> { const sortByMap = { createdAt: 'created_at', updatedAt: 'updated_at', } as const; const data = await this.requestProtected< void, Paginated >({ method: 'GET', url: urls.catalogs({ page: params.page, page_size: params.pageSize, sort_by: params.sortBy && sortByMap[params.sortBy], sort_direction: params.sortDirection, }), }); return { total: data.total, data: data.data.map(mapCatalogResponse), }; } async getProducts( this: ApiClient, catalogId: string, params: P.GetProductsQuery = {}, ): Promise> { const sortByMap = { createdAt: 'created_at', updatedAt: 'updated_at', } as const; const data = await this.requestProtected< void, Paginated >({ method: 'GET', url: urls.products(catalogId, { page: params.page, page_size: params.pageSize, sort_by: params.sortBy && sortByMap[params.sortBy], sort_direction: params.sortDirection, visibility: params.visibility, location_id: params.locationId, }), }); return { total: data.total, data: data.data.map(mapProductResponse), }; } async createCatalog( this: ApiClient, params: C.CreateCatalog, ): Promise { const data = await this.requestProtected< C.CreateCatalogRequest, C.CreateCatalogResponse >({ method: 'POST', url: urls.catalogs(), body: { name: params.name, connection_id: params.connectionId, settings: params.settings, }, }); return mapCatalogResponse(data); } async updateCatalog( this: ApiClient, id: string, params: C.UpdateCatalog, ): Promise { await this.requestProtected< C.UpdateCatalogRequest, C.UpdateCatalogResponse >({ method: 'PATCH', url: urls.catalog(id), body: { name: params.name, }, }); } async deleteCatalog(this: ApiClient, id: string): Promise { await this.requestProtected< C.UpdateCatalogRequest, C.CreateCatalogResponse >({ method: 'DELETE', url: urls.catalog(id), }); } async getCatalogLocations( this: ApiClient, catalogId: string, params: C.GetCatalogsQuery = {}, ): Promise> { const sortByMap = { createdAt: 'created_at', updatedAt: 'updated_at', } as const; const data = await this.requestProtected< void, Paginated >({ method: 'GET', url: urls.catalogLocations(catalogId, { page: params.page, page_size: params.pageSize, sort_by: params.sortBy && sortByMap[params.sortBy], sort_direction: params.sortDirection, }), }); return { total: data.total, data: data.data.map(mapCatalogLocationResponse), }; } }