UNPKG

@playkit/playkit-api

Version:

43 lines (38 loc) 986 B
const client = ( path, { body, method, baseUrl, marketplaceId, apiKey, token, ...customConfig } = {} ) => { // Setup default headers const headers = { "Content-Type": "application/json", "x-api-key": apiKey, "X-Marketplace-Id": marketplaceId, }; // Add Auth header if token is present if (token) headers.Authorization = token; // Fetch config const defaultMethod = body ? "POST" : "GET"; const config = { method: method ? method : defaultMethod, ...customConfig, headers: { ...headers, ...customConfig.headers, }, }; // Stringify body and add to config if present if (body) config.body = JSON.stringify(body); // URL const url = `${baseUrl}/api/${path}`; // Fetch call return fetch(url, config).then(async (response) => { // Parse response const data = await response.json(); if (response.ok) { return data; } else { return Promise.reject(data); } }); }; export { client };