2.5.1 • Published 3 years ago

iab-vast-loader v2.5.1

Weekly downloads
64
License
MIT
Repository
github
Last release
3 years ago

iab-vast-loader

npm Dependencies Build Status Coverage Status JavaScript Standard Style

Loads and parses IAB VAST tags, resolving wrapped tags along the way.

Usage

import { VASTLoader } from 'iab-vast-loader'

const tagUrl = 'https://example.com/vast.xml'

// Create the loader
const loader = new VASTLoader(tagUrl)

// Load the tag chain and await the resulting Promise
loader.load()
  .then(chain => {
    console.info('Loaded VAST tags:', chain)
  })
  .catch(err => {
    console.error('Error loading tag:', err)
  })

This should work in both Node.js version 8 and above as well as in the browser. However, in the browser, you'll probably want to use a bundler first.

API

new VASTLoader(tagUrl[, options])

Creates a VAST loader.

loader.load()

Returns a Promise for an array of VAST instances. The VAST class is provided by iab-vast-model.

Error Handling

In addition to VASTLoader, the main module also exports the VASTLoaderError class, which maps errors to the VAST specification:

import { VASTLoader, VASTLoaderError } from 'iab-vast-loader'

const loader = new VASTLoader(tagUrl)

loader.load()
  .catch(err => {
    if (err instanceof VASTLoaderError) {
      console.error('VAST error: ' + err.code + ' ' + err.message)
    } else {
      console.error('Unknown error: ' + err)
    }
  })

As with iab-vast-model, if instanceof doesn't work for you, you may want to inspect error.$type instead. This issue can occur if you load multiple versions of iab-vast-loader, each with their own VASTLoaderError class.

Options

maxDepth

The maximum number of VAST documents to load within one chain. The default is 10.

timeout

The maximum number of milliseconds to spend per HTTP request. The default is 10,000.

credentials

Controls CORS behavior. You can pass a string, an array of strings, or a function producing either of those.

If you pass a string, it will be used as the value for the credentials option to every request. Valid values are 'omit' (the default), 'same-origin' and 'include'.

If you pass an array, each of the values in the array will be tried consecutively. For example, to first try each request with credentials and then without, you can pass ['include', 'omit'].

To control the behavior on a per-request basis, pass a function receiving the request URL and returning one of the accepted values. For example:

const loader = new VASTLoader(wrapperUrl, {
  credentials: uri => {
    if (uri.indexOf('.doubleclick.net/') >= 0) {
      return 'include'
    } else {
      return 'omit'
    }
  }
})

fetch

Sets the implementation of fetch, used to make HTTP requests. In Node.js, this defaults to node-fetch. In the browser, unfetch is used.

Events

A VASTLoader is an EventEmitter. To be notified about progress, you can subscribe to the events willFetch, didFetch, willParse, and didParse as follows:

loader
  .on('willFetch', ({ uri }) => {
    console.info('Fetching', uri)
  })
  .on('didFetch', ({ uri, body }) => {
    console.info('Fetched', body.length, 'bytes from', uri)
  })
  .on('willParse', ({ uri, body }) => {
    console.info('Parsing', uri)
  })
  .on('didParse', ({ uri, body, vast }) => {
    console.info('Parsed', uri)
  })
  .load()
  .then(chain => {
    console.info('Loaded VAST tags:', chain)
  })
  .catch(err => {
    console.error('Error loading tag:', err)
  })

Maintainer

Tim De Pauw

License

MIT

2.5.1

3 years ago

2.5.0

3 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

6 years ago

1.0.0

6 years ago

0.15.0

6 years ago

0.14.0

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.0

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago