npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dadi/apidoc

v1.5.2

Published

API documentation middleware for DADI API

Downloads

2

Readme

DADI API Documentation

npm (scoped) JavaScript Style Guide

API documentation middleware for DADI API

Introduction

This package provides a set of auto-generated documentation for your API installation, reading information from the collection schemas and custom endpoints to describe the available HTTP methods and parameters required to interact with the API.

A guide to acquiring an access token

Authentication

Describing a GET request for the User collection

GET Request

Describing a POST request for the User collection

POST Request

Installation

$ npm install @dadi/apidoc --save

Add an apidoc section to the API's configuration file

"apidoc": {
  "title": "<Project Name> Content API",
  "description": "This is the _Content API_ for [Example](http://www.example.com).",
  "markdown": false,
  "path": "docs",
  "generateCodeSnippets": false,
  "themeVariables": "default",
  "themeTemplate": "triple",
  "themeStyle": "default",
  "themeCondenseNav":	true,
  "themeFullWidth": false
}

Initialise the middleware

This example shows API Doc being initialised in the installed API's entry point, the main.js file, after the server has started.

var server = require('@dadi/api')
var config = require('@dadi/api').Config
var log = require('@dadi/api').Log

server.start(function() {
  log.get().info('API Started')
})

// add documentation route
require('@dadi/apidoc').init(server, config)

Browse the documentation

By default the documentation can be browsed using the route /api/1.0/docs.

For example of your API is running at http://api.example.com then your documentation is available at http://api.example.com/api/1.0/docs.

Generating Code Snippets

If you want to generate code snippets (made possible by the configuration option generateCodeSnippets) you'll need to ensure sure your system has the following:

  1. Ruby, and the Ruby gem awesome_print:
$ gem install awesome_print
  1. The httpsnippet package:
$ npm install httpsnippet --global

Documenting custom endpoints

API collections are automatically documented using values from with the collection schema files. To have your documentation include useful information about custom endpoints, add JSDoc comments to the endpoint files:

/**
 * Adds two numbers together.
 *
 * ```js
 * var result = add(1, 2);
 * ```
 *
 * @param {int} `num1` The first number.
 * @param {int} `num2` The second number.
 * @returns {int} The sum of the two numbers.
 * @api public
 */

Showing useful example values

To show example data in the documentation that isn't simply the default of "Hello World!", you can add properties to fields in the API collection schema.

example

The example property is a static value that will be the same every time you view the documentation.

"platform": {
  "type": "String",
  "required": true,
  "example": "twitter",
  "validation": {
    "regex": {
      "pattern": "twitter|facebook|instagram"
    }
  }
}

testDataFormat

The testDataFormat property allows you to specify any type from the faker package, which will insert a random value of the selected type each time the documentation is viewed:

"email": {
  "type": "String",
  "required": true,
  "validation": {
    "regex": {
      "pattern": ".+@.+"
    }
  },
  "testDataFormat": "{{internet.email}}"
}

See a list of available options here.

Excluding Collections, Endpoints and Fields

Often an API build contains collections and collection fields that are meant for internal use and including them in the API documentation is undersirable.

To exclude collections and fields from your generated documentation, see the following sections.

Excluding Collections

Add a private property to the collection specification's settings section:

{
  "fields": {
    "title": {
      "type": "String",
      "required": true
    },
    "author": {
      "type": "Reference",
      "settings": {
        "collection": "people"
      }
    }
  },
  "settings": {
    "cache": true,
    "count": 40,
    "sort": "title",
    "sortOrder": 1,
    "private": true
  }
}

Excluding Endpoints

Add a private property to the endpoint file's model.settings section:

module.exports.get = function (req, res, next) {
  res.setHeader('content-type', 'application/json')
  res.statusCode = 200
  res.end(JSON.stringify({message: 'Hello World'}))
}

module.exports.model = {
  "settings": {
    "cache": true,
    "authenticate": false,
    "private": true
  }
}

Excluding Fields

Add a private property to the field specification:

{
  "fields": {
    "title": {
      "type": "String",
      "required": true
    },
    "internalId": {
      "type": "Number",
      "required": true,
      "private": true
    }
  },
  "settings": {
    "cache": true,
    "count": 40,
    "sort": "title",
    "sortOrder": 1
  }
}

Roadmap

  • Template customisation

Licence

DADI is a data centric development and delivery stack, built specifically in support of the principles of API first and COPE.

Copyright notice (C) 2016 DADI+ Limited [email protected] All rights reserved

This product is part of DADI. DADI is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version ("the GPL"). If you wish to use DADI outside the scope of the GPL, please contact us at [email protected] for details of alternative licence arrangements.

This product may be distributed alongside other components available under different licences (which may not be GPL). See those components themselves, or the documentation accompanying them, to determine what licences are applicable.

DADI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

The GNU General Public License (GPL) is available at http://www.gnu.org/copyleft/gpl.html. A copy can be found in the file GPL distributed with these files.

This copyright notice MUST APPEAR in all copies of the product!