UNPKG

video-ad-sdk

Version:

VAST/VPAID SDK that allows video ads to be played on top of any player

36 lines (35 loc) 1.48 kB
import { getVASTAdTagURI, isWrapper } from '../vastSelectors'; import { requestAd } from './requestAd'; import { getNextAd } from './helpers/getNextAd'; import { markAdAsRequested } from './helpers/adUtils'; const validateChain = (vastChain) => { if (!Array.isArray(vastChain)) { throw new TypeError('Invalid VAST chain'); } if (vastChain.length === 0) { throw new Error('No next ad to request'); } }; /** * Requests the next ad in the VAST Chain. * * @param vastChain Array of {@link VastResponse}. * @param options Options Map. The allowed properties are: * @returns Returns a Promise that will resolve with a VastChain with the newest VAST response at the beginning of the array. * If the {@link VastChain} had an error. The first VAST response of the array will contain an error and an errorCode entry. */ export const requestNextAd = (vastChain, options) => { validateChain(vastChain); const [vastResponse] = vastChain; const nextAd = getNextAd(vastResponse, options); if (nextAd) { const newVastResponse = Object.assign(Object.assign({}, vastResponse), { ad: nextAd }); const newVastChain = [newVastResponse, ...vastChain.slice(1)]; markAdAsRequested(nextAd); if (isWrapper(nextAd)) { return requestAd(getVASTAdTagURI(nextAd), options, newVastChain); } return Promise.resolve(newVastChain); } return requestNextAd(vastChain.slice(1), options); };