Files
pokemon-inventory/apps/api/src/modules/pricing/providers/http-pricing-provider.js
T
2026-09-09 21:58:32 -04:00

29 lines
869 B
JavaScript

import { createPricingProvider } from './pricing-provider.js';
export function createHttpPricingProvider({
id,
endpoint,
token,
fetchImplementation = fetch,
}) {
if (typeof endpoint !== 'string' || endpoint.trim().length === 0) {
throw new Error('Pricing provider endpoint is required.');
}
return createPricingProvider({
id,
async observe(input) {
const url = new URL(endpoint);
for (const [key, value] of Object.entries(input)) {
if (value) url.searchParams.set(key, value);
}
const response = await fetchImplementation(url, {
headers: token ? { authorization: `Bearer ${token}` } : {},
});
if (!response.ok) throw new Error(`Pricing provider returned HTTP ${response.status}.`);
const payload = await response.json();
return payload?.observations ?? payload;
},
});
}