# @wearelucid/api-fetcher

> Generates multiple JSON files from fetching API endpoints (i18n supported) 🚀

Latest version **3.1.1** (published 2021-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @wearelucid/api-fetcher
pnpm add @wearelucid/api-fetcher
yarn add @wearelucid/api-fetcher
bun add @wearelucid/api-fetcher
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2021-11-05 |
| First published | 2018-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 53.2 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Lucid |
| Maintainers | d-simon, fabianellenberger, marcoeh, sebastianbayer, jones_s |

## Links

- npm: https://www.npmjs.com/package/@wearelucid/api-fetcher
- Repository: https://github.com/wearelucid/api-fetcher
- Homepage: https://github.com/wearelucid/api-fetcher#readme
- Issues: https://github.com/wearelucid/api-fetcher/issues
- npm.io page: https://npm.io/package/@wearelucid/api-fetcher

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^0.21.1
- [figlet](https://npm.io/package/figlet.md) ^1.2.0
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.1.0

## Recent versions

- 3.1.1 (latest) — 2021-11-05
- 1.2.4 (v1-hotfix) — 2022-04-22
- 2.3.1 (no-sem-vers-tag2.3.1) — 2021-11-17
- 2.3.0 — 2021-11-05
- 3.1.0 — 2020-02-26
- 3.0.0 — 2020-02-26
- 2.2.0 — 2019-11-08
- 2.1.0 — 2019-11-07
- 2.0.0 — 2019-07-25
- 1.2.3 — 2019-02-14
- 1.2.2 — 2019-01-22
- 1.2.1 — 2018-10-14
- 1.2.0 — 2018-09-30

## README

# Api fetcher

[![npm version](https://img.shields.io/npm/v/@wearelucid/api-fetcher.svg?style=flat-square)](https://www.npmjs.com/package/@wearelucid/api-fetcher)

Generates multiple JSON files from fetching (WordPress) API endpoints (i18n supported) 🚀

## Installation
```bash
yarn add @wearelucid/api-fetcher
```

## Usage
Be aware that this package uses ES6 syntax!

Add a script to your package.json
```JSON
"scripts": {
  "fetch": "node fetchData.js"
}
```
Since node currently does not support es2015 module syntax, we need to install [`babel-cli`](https://yarnpkg.com/en/package/babel-cli) and add the script like so:
```JSON
"scripts": {
  "fetch": "babel-node --presets env -- fetchData.js"
}
```

```bash
yarn fetch
```

## Example

### Full Example (Wordpress)

```javascript
import fetcher from 'api-fetcher'

const config = {
  savePath: './static/data',
  compressJSON: true, // setting this to false may help debugging :-)
  perPage: 5000, // arbitrary
  itemsPerPage: 10, // set for pagination (default will automatically be 10)
  languages: [
    { lang: 'de', locale: 'de_CH' },
    { lang: 'en', locale: 'en_US' }
  ],
  apiUrl: 'https://your-backend/api'
}

fetcher.log.printText('Lucid')
fetcher.log.printConfig(config)

// fetch paginated posts
fetcher.paginate('posts', { posts: { method: fetcher.getWPPostType, postType: 'posts', transforms: [removeFieldsFromPost] } },

// fetch bundled data
fetcher.bundle('basic', {
  pages: { method: fetcher.getWPPostType, postType: 'pages', transforms: [removeFieldsFromPost, decodeTitle], filters: [showOnlyPublished] },
  menus: { method: fetcher.getWPMenus },
  options: { method: fetcher.getWPOptionsPage, slug: 'options' },

  // If you need to get categories and custom taxonomies
  categories: { method: fetcher.getWPCategories },
  formats: { method: fetcher.getWPCustomTaxonomy, taxonomy: 'formats' }
}, config)


/**
* Filter (Note: This filter is an example. It is not needed. Wordpress by default only delivers published posts and pages)
*/
function showOnlyPublished (data) {
  return (data && data.length) ? data.filter(p => p.status === 'publish') : data
}

/**
* Delete fields we don't need (anymore)
*/
function removeFieldsFromPost (data) {
  return fetcher.applyToOneOrMany(_removeFieldsFromPost, data)
}

function _removeFieldsFromPost (data) {
  delete data._links
  return data
}
```

### Generating Multiple Bundles
If you want to generate multiple json bundles you can invoke `fetcher.bundle()` with different a name like so:
```javascript
fetcher.bundle('basic', {
  pages: { method: fetcher.getWPPostType, postType: 'pages', transforms: [removeFieldsFromPost, decodeTitle] },
  posts: { method: fetcher.getWPPostType, postType: 'posts', transforms: [removeFieldsFromPost, decodeTitle] }
}, config)
```

### Generating Paginated Collections
You can also generated paginated collections like so:
```javascript
fetcher.paginate('posts', { posts: { method: fetcher.getWPPostType, postType: 'posts', transforms: [removeFieldsFromPost, decodeTitle] } }, config)
fetcher.paginate('pages', { pages: { method: fetcher.getWPPostType, postType: 'pages', transforms: [removeFieldsFromPost, decodeTitle] } }, config)
```

In some cases you might also want to load all items of the once you loaded paginated (for having all the data):
```javascript
fetcher.bundle('fileName', { posts: { method: fetcher.getWPPostType, postType: 'posts', transforms: [removeFieldsFromPost, decodeTitle] } }, config)
```

Default items per page will be set to `10`.
You can provide the variable `itemsPerPage` inside your config.
```
itemsPerPage: 10
```

This will generate a collection of json files (with your specified naming), in this case:
```bash
posts.de.1.json
posts.de.2.json
posts.de.3.json
```

---
_Source: https://npm.io/package/@wearelucid/api-fetcher · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
