api-url-tree-generator

Generates an object with API urls as values

Usage no npm install needed!

<script type="module">
  import apiUrlTreeGenerator from 'https://cdn.skypack.dev/api-url-tree-generator';
</script>

README

Api generator for frontend

Generates an object with API urls as values

Installation

npm install --save-dev api-url-tree-generator

or

yarn add --dev api-url-tree-generator

Usage

const baseUrl = "https://something.com"
const apiRaw = {
  simpleRoute: 'simple-route',
  'other-route': 'other-route',
  'node': {
    nestedRoute: 'nested-route'
  }
}

const options = {
  // ...
}

const api = generateApi(baseUrl, apiRaw, options)
/*
  api => {
      simpleRoute: 'https://something.com/simple-route',
      'other-route': 'https://something.com/other-route',
      'node': {
        nestedRoute: 'https://something.com/node/nested-route'
      }
  }

  Also, api.node translates to https://something.com/node
*/

// request to 'https://something.com/simple-route'
const response = await fetch(api.simpleRoute);

// request to 'https://something.com/node'
const response = await fetch(api.node);

// request to 'https://something.com/node/nested-route'
const response = await fetch(api.node.nestedRoute);

Options

Option Type Description
withTailingSlash boolean Adds tailing slash to url

License

MIT