All Projects โ†’ eduardoboucas โ†’ Static Api Generator

eduardoboucas / Static Api Generator

๐Ÿ”ง Generate a static JSON API from a tree of directories and files

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Static Api Generator

Json Api Php
JSON-API (http://jsonapi.org) responses in PHP.
Stars: โœญ 426 (+321.78%)
Mutual labels:  api, json-api
Forbes400
JSON Api Of The Forbes 400 Richest People List
Stars: โœญ 7 (-93.07%)
Mutual labels:  api, json-api
Element Api
Create a JSON API/Feed for your elements in Craft.
Stars: โœญ 493 (+388.12%)
Mutual labels:  api, json-api
Laravel5 Jsonapi
Laravel 5 JSON API Transformer Package
Stars: โœญ 313 (+209.9%)
Mutual labels:  api, json-api
Json Api Dart
JSON:API client for Dart/Flutter
Stars: โœญ 53 (-47.52%)
Mutual labels:  api, json-api
Polr
๐Ÿšก A modern, powerful, and robust URL shortener
Stars: โœญ 4,147 (+4005.94%)
Mutual labels:  api, json-api
Graphiti
Stylish Graph APIs
Stars: โœญ 783 (+675.25%)
Mutual labels:  api, json-api
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: โœญ 219 (+116.83%)
Mutual labels:  api, json-api
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: โœญ 50 (-50.5%)
Mutual labels:  api, json-api
Nodemcu Espress
Ultra-Lightweight and modular Node.js express like http server for NodeMCU. web - ESP8266
Stars: โœญ 39 (-61.39%)
Mutual labels:  api, static
Api Generator
PHP-code generator for Laravel framework, with complete support of JSON-API data format
Stars: โœญ 244 (+141.58%)
Mutual labels:  api, json-api
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: โœญ 67 (-33.66%)
Mutual labels:  api, json-api
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: โœญ 242 (+139.6%)
Mutual labels:  api, json-api
Para
Open source back-end server for web, mobile and IoT. The backend for busy developers. (self-hosted or hosted)
Stars: โœญ 389 (+285.15%)
Mutual labels:  api, json-api
Datoji
A tiny JSON storage service. Create, Read, Update, Delete and Search JSON data.
Stars: โœญ 222 (+119.8%)
Mutual labels:  api, json-api
Api
ๅงฌ้•ฟไฟกAPI For Docker ไธ€ไธชๅŸบไบŽๅคš็ง็ผ–็จ‹่ฏญ่จ€ๅผ€ๆบๅ…่ดนไธ้™ๅˆถๆไพ›็”Ÿๆดปๅธธ็”จ,ๅ‡บ่กŒๆœๅŠก,ๅผ€ๅ‘ๅทฅๅ…ท,้‡‘่žๆœๅŠก,้€š่ฎฏๆœๅŠกๅ’Œๅ…ฌ็›Šๅคงๆ•ฐๆฎ็š„ๅนณๅฐ.
Stars: โœญ 743 (+635.64%)
Mutual labels:  api, json-api
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: โœญ 191 (+89.11%)
Mutual labels:  api, json-api
Rpc
Simple RPC style APIs with generated clients & servers.
Stars: โœญ 192 (+90.1%)
Mutual labels:  api, json-api
Server
Serve your Rubix ML models in production with scalable stand-alone model inference servers.
Stars: โœญ 30 (-70.3%)
Mutual labels:  api, json-api
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: โœญ 8,439 (+8255.45%)
Mutual labels:  api, static
Pluma logo

npm (scoped) JavaScript Style Guide Build Status

Static API generator is a Node.js application that creates a basic JSON API from a tree of directories and files. Think of a static site generator, like Jekyll or Hugo, but for APIs.

It takes your existing data files, which you may already be using to feed a static site generator or similar, and creates an API layer with whatever structure you want, leaving the original files untouched. Static API generator helps you deliver your data to client-side applications or third-party syndication services.

Couple it with services like GitHub Pages or Netlify and you can serve your API right from the repository too. ๐Ÿฆ„



1. Installation

  • Install via npm

    npm install static-api-generator --save
    
  • Require the module and create an API

    const API = require('static-api-generator')
    const api = new API(constructorOptions)
    

2. Usage

Imagine the following repository holding data about movies, organised by language, genre and year. Information about each movie will be in its own YAML file named after the movie.

input/
โ”œโ”€โ”€ english
โ”‚   โ”œโ”€โ”€ action
โ”‚   โ”‚   โ”œโ”€โ”€ 2016
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ deadpool.yaml
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ the-great-wall.yaml
โ”‚   โ”‚   โ””โ”€โ”€ 2017
โ”‚   โ”‚       โ”œโ”€โ”€ logan.yaml
โ”‚   โ”‚       โ””โ”€โ”€ the-fate-of-the-furious.yaml
โ”‚   โ””โ”€โ”€ horror
โ”‚       โ””โ”€โ”€ 2017
โ”‚           โ”œโ”€โ”€ alien-covenant.yaml
โ”‚           โ””โ”€โ”€ get-out.yaml
โ””โ”€โ”€ portuguese
    โ””โ”€โ”€ action
        โ””โ”€โ”€ 2016
            โ””โ”€โ”€ tropa-de-elite.yaml

2.1. Initialisation

Create an API by specifying its blueprint, so that the static API generator can understand how the data is structured, and the directory where the generated files should be saved.

const moviesApi = new API({
  blueprint: 'source/:language/:genre/:year/:movie',
  outputPath: 'output'
})

2.2 Generating endpoints

The following will generate and endpoint for each movie (e.g. /english/action/2016/deadpool.json).

moviesApi.generate({
  endpoints: ['movie']
})

Endpoints can be created for any data level. The following creates additional endpoints at the genre level (e.g. /english/action.json).

moviesApi.generate({
  endpoints: ['genre', 'movie']
})

It's also possible to manipulate the hierarchy of the data by choosing a different root level. For example, one could generate endpoints for each genre without a separation enforced by language, its original parent level. This means being able to create endpoints like /action.json (as opposed to /english/action.json), where all action movies are listed regardless of their language.

moviesApi.generate({
  endpoints: ['genre', 'movie'],
  root: 'genre'
})

3. API

3.1. Constructor

const API = require('static-api-generator')
const api = new API({
  addIdToFiles: Boolean,
  blueprint: String,
  pluralise: Boolean,
  outputPath: String
})

The constructor method takes an object with the following properties.


  • addIdToFiles

    Whether to add an id field to uniquely identify each data file. IDs are generated by computing an MD5 hash of the full path of the file.

    Default:

    false

    Example result:

    "review_id": "96a9b996439528ecb9050774c3e79ff2"


  • blueprint

    Required. A path describing the hierarchy and nomenclature of the data. It should start with a static path to the directory where all the files are located, followed by the name of each data level (starting with a colon).

    For the pluralise option to work well, the names of the data levels should be singular (e.g. languages vs. language)

    Example:

    'input/:language/:genre/:year/:movie'


  • outputPath

    Required. The path to the directory where endpoint files should be created.

    Example:

    'output'


  • pluralise

    The name of each data level (e.g. "genre") is used in the singular form when identifying a single entity (e.g. {"genre_id": "12345"}) and in the plural form when identifying a list of entities (e.g. {"genres": [...]}). That behaviour can be disabled by setting this property to false.

    Default:

    true


3.2. Method: generate

api.generate({
  endpoints: Array,
  entriesPerPage: Number,
  index: String,
  levels: Array,
  root: String,
  sort: Object
})

The generate method creates one or more endpoints. It takes an object with the following properties.


  • endpoints

    The names of the data levels to create individual endpoints for, which means generating a JSON file for each file or directory that corresponds to a given level.

    Default:

    []

    Example:

    ['genre', 'movie']


  • entriesPerPage

    The maximum number of entries (data files) to include in an endpoint file. If the number of entries exceeds this number, additional endpoints are created (e.g. /action.json, /action-2.json, etc.).

    Default:

    10

    Example:

    3


  • index

    The name of the main/index endpoint file.

    Default:

    The pluralised name of the root level (e.g. languages).

    Example:

    languages (generates languages.json)


  • levels

    An array containing the names of the levels to include in the endpoints. By default, an endpoint for a level will include all its child levels (e.g. an endpoint for a genre will have a list of years, which will have a list of movies). If a level is omitted (e.g. year), then all its entries are grouped together (i.e. an endpoint for a genre will have a list of all its movies, without any separation by year).

    Default:

    All levels

    Example:

    ['language', 'genre', 'movie']


  • root

    The name of the root level. When this doesn't correspond to the first level in the blueprint, the data tree is manipulated so that all entries become grouped by the new root level.

    Default:

    The name of the first level of the blueprint (e.g. language)

    Example:

    genre


  • sort

    An object that defines how the various data levels should be sorted. For levels corresponding to directories, an object with a single property (order) is expected, determining whether directory names are sorted from A-Z (ascending) or Z-A (descending).

    When levels contain entries, an additional property (field) defines what field of the data files should be used to sort the entries.

    Default:

    {} (directories will be sorted alphabetically from A-Z, entries will be sorted alphabetically from A-Z based on their filename)

    Example:

    {
      genre: {
        order: 'descending'
      },
      movie: {
        field: 'budget',
        order: 'descending'
      }
    }
    

4. Q&A

  • Why did you build this?

    GitHub has been the centrepiece of my daily workflow as a developer for many years. I love the idea of using a repository not only for version control, but also as the single source of truth for a data set. As a result, I created several projects that explore the usage of GitHub repositories as data stores, and I've used that approach in several professional and personal projects, including my own site/blog.

  • Couldn't Jekyll, Hugo or XYZ do the same thing?

    Possibly. Most static site generators are capable of generating JSON, but usually using awkward/brittle methods. Most of those applications were built to generate HTML pages and that's where they excel on. I tried to create a minimalistic and easy-as-it-gets way of generating something very specific: a bunch of JSON files that, when bundled together, form a very basic API layer.

  • Where can I host this?

    GitHub Pages is a very appealing option, since you could serve the API right from the repository. It has CORS enabled, so you could easily consume it from a client-side application using React, Angular, Vue or whatever you prefer. You could even use a CI service like Travis to listen for commits on a given branch (say master) and automatically run Static API Generator and push the generated output to a gh-pages branch, making the process of generating the API when data changes fully automated.

    Netlify is also very cool and definitely worth trying.

  • Would it be possible to add feature X, Y or Z?

    Probably. File an issue or, even better, a pull request and I'm happy to help. Bare in mind that this is a side project (one of too many) which I'm able to dedicate a very limited amount of time to, so please be patient and try to understand if I tell you I don't have the capacity to build what you're looking for.

  • Who designed the logo?

    The logo was created by Arthur Shlain from The Noun Project and it's licensed under a Creative Commons Attribution license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].