hyperpop.js/publications.ts

55 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-11-15 21:57:36 +00:00
import { Paginacion, paginacionToSearchParams, Paging } from "./common.js";
2021-11-01 20:48:42 +00:00
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(
2021-11-15 21:57:36 +00:00
host: string,
2021-11-01 20:48:42 +00:00
token: string,
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(
2021-11-15 21:57:36 +00:00
`${host}/api/Aperture/Publications?${searchParams.toString()}`,
2021-11-01 20:48:42 +00:00
{
headers: {
accesstoken: token,
},
}
);
const json = await res.json();
console.debug(json);
if (json.Message) {
throw new Error(`Tango: ${json.Message}`);
}
return json;
}