mars-rover-sdk

SDK for fetching mars perseverance rover images.

Usage no npm install needed!

<script type="module">
  import marsRoverSdk from 'https://cdn.skypack.dev/mars-rover-sdk';
</script>

README

@rover/sdk

SDK for fetching Mars Perseverance Rover images.

Installation

$ yarn add @rover/sdk

Or:

$ npm install @rover/sdk

Usage

const createSDK = require('@rover/sdk')
const sdk = createSDK()

const fetchImageByIndex = async () => {
  const res = await sdk.fetch(5)

  console.log(res.index) // 5
}

const fetchLatestImage = async () => {
  const res = await sdk.fetch('latest')

  console.log(res.index) // the latest image index
}

const fetchAllImages = async () => {
  const res = await sdk.fetch()

  t.is(typeof res[Symbol.asyncIterator], 'function')

  for await (let images of res) {
    images.forEach((image) => t.deepEqual(image, imgs[image.index]))
  }
}

/*
const info = await sdk.info()
const image = await sdk.fetch(1)
const image = await sdk.fetch('latest)
const iterator = await sdk.fetch()
const iterator = await sdk.fetch({ limit: 5, batchSize: 3 })
*/

API

RoverSDK([options])

Create a instance of the Rover SDK.

Supported options:

  • url: The API url. You can also set the ROVER_API_URL environment variable which have the same effect.

sdk.info()

Fetch metadata information (hypercore key and numImages).

async function fetchInfo() {
  const info = await sdk.info()
  console.log('Number of images', info.numImages)
}

sdk.fetch(index)

Fetch a single image by index. A promise is returned.

async function fetchImageByIndex() {
  const image = await sdk.fetch(1) // Fetch image with index 1
  console.log('Image 1', image)
}

sdk.fetch([options])

Fetch multiple images. An async iterator is returned.

async function fetchAllImages() {
  const iterator = await sdk.fetch() // No argument to fetch all

  for await (let images of res) {
    images.forEach((image) => {
      console.log('Image', image.index)
    })
  }
}

Optionally you can pass an object as options to change the fetching behavior:

The valid options are:

  • limit: Limit the amount of returned images. (defaults to undefined).

Example: the request bellow will only return 50 images.

    const iterator = await sdk.fetch({ limit: 50 })
    const images = []

    for await (let imgs of iterator) {
      images.push(...imgs)
    }

    console.log(images.length) // returns 50
}
  • batchSize Process images in batches. (defaults to 1).

Example: the request bellow will prefetch only 10 images on each iteration.

    const iterator = await sdk.fetch({ batchSize: 10 })

    for await (let images of iterator) {
      console.log(images.length) // returns 10
    }
}

Run tests

$ yarn install
$ yarn test

Or:

$ npm install
$ npm run test

License

MIT