static-api icon indicating copy to clipboard operation
static-api copied to clipboard

Generate the number of items in an array

Open noraj opened this issue 6 years ago • 2 comments

In your example you have only a hash structure like that

{
  "items": {
    "id789789": {
      "key": "value"
    },
    "id1238965": {
      "key": "value"
    }
  }
}

But I'm using some arrays in my JSON files like that:

{
  "items": [
    {
      "key": "value"
    },
    {
      "key": "value"
    }
  ]
}

So you have a generated tree where you know keys and so you can call them: items/id789789/id789789.json and items/id1238965/id1238965.json.

But because I have an array, I have incremental ids: items/0/0.json and items/1/1.json.

When you have no array you assume that you know the structure or/and the unique ids. But when you have an array the API need to tell you there is two items in this array so you know you can call for id 0 and 1. So in my example items/items.json should add an additional items: 2 in the generated JSON structure.

So the generated output of items/items.json should be something like that

{
  "items_n": 2,
  "items": [
    {
      "key": "value"
    },
    {
      "key": "value"
    }
  ]
}

Where _n must be configurable.

So then I know I can call items/0/0.json:

{
  "key": "value"
}

and items/1/1.json:

{
  "key": "value"
}

Else the client need to do an infinite loop and stop when he get an http error 404.

noraj avatar May 11 '18 20:05 noraj

By the way I used:

var srcData = require('./temp/data.json'), //load your json file
    path = require('path'),
    staticApi = require('static-api'); //load the module

var destFolder = path.join(__dirname, 'temp/api/');

new staticApi({
    outputFolder: destFolder, //where the data will be stored
    object: srcData //the object to create the file structure from
});

noraj avatar May 11 '18 21:05 noraj

I think this issue goes with pagination from the todo.

noraj avatar May 28 '18 22:05 noraj