hyperpop.js/publications.ts

50 lines
1.3 KiB
TypeScript

import { Paginacion, paginacionToSearchParams, Paging } from "./common.js";
export interface PublicationsQuery {
paginacion?: Paginacion;
productCode?: string;
skuCode?: string;
variantCode?: string;
}
export interface Publication {
ProductCode: string;
Description: string;
VariantCode?: string;
VariantDescription?: string;
SkuCode: string;
}
export interface PublicationsResponse {
Paging: Paging;
Data: Publication[];
}
// https://github.com/TangoSoftware/ApiTiendas#art%C3%ADculos
export async function getPublications(
host: string,
headers: HeadersInit,
options: PublicationsQuery
): Promise<PublicationsResponse> {
let searchParams = new URLSearchParams();
paginacionToSearchParams(options.paginacion, searchParams);
if (options.skuCode) {
searchParams.set("skuCode", options.skuCode);
}
if (options.productCode) {
searchParams.set("productCode", options.productCode);
}
if (options.variantCode) {
searchParams.set("variantCode", options.variantCode);
}
const res = await fetch(
`${host}/api/Aperture/Publications?${searchParams.toString()}`,
{ headers }
);
const json = await res.json();
console.debug(json);
if (json.Message) {
throw new Error(`Tango: ${json.Message}`);
}
return json;
}