This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user