2.0.12 • Published 6 years ago

static-data v2.0.12

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

Build Status

static-data

A library for saving data to json, and then fetching that data in your application

installation

npm i --save static-data

simple usage set

const sd = require('static-data');
const fetch = require('node-fetch');

const filesToSet = [
  {
    path: './public/yipee.json',
    getJson: async () => {
      return {
        food: 'yummy'
      }
    }
  }, {
    path: './public/gitHubUsers.json',
    getJson: async () => {
      const res = await fetch('https://api.github.com/users')
      const json = await res.json();
      return json;
    }
  }
]

sd.set(filesToSet);

after running the above code, the two following files will be created: ./public/yipee.json ./public/gitHubUsers.json

less simple usage set

const sd = require('static-data');
const fetch = require('node-fetch');

const getFiles = async () => {
  const res = await fetch('https://api.github.com/users')
  const gitHubUsersJson = await res.json();

  const individualUsersJson = await Promise.all(
    gitHubUsersJson.map(user => ({
      path: `/users/${user.id}`,
      getJson: async() => {
        const request = await        fetch(`https://api.github.com/users/${user.id}`);
        return await request.json();
      }
    }))
  )

  return [
    {
      path: '/users',
      getJson: gitHubUsersJson
    },
    ...individualUsersJson
  ]
}

sd.set(getFiles, {
	pathPrefix: './public',
	pathSuffix: '.json'
});

After running the above code the following files will be created: ./public/users.json, ./public/users/1.json, ./public/users/2.json, ...etc

Later on in your application you can request your data fetch

const res = await fetch('http://localhost:3000/public/users.json')
const users = await res.json();

const user1Res = await fetch('http://localhost:3000/public/users/1.json')
const user1 = await user1Res.json();
2.0.12

6 years ago

2.0.11

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago