Restify Swagger 1.2 Integrator

0.1.8 · abandoned · verified Sun Apr 19

node-restify-swagger is a utility that generates API documentation in the Swagger 1.2 format for web services built with the Restify framework. Published with its latest version 0.1.8 in 2015, the package is largely unmaintained and relies on the deprecated Swagger 1.2 specification, which has since been superseded by OpenAPI Specification (OAS) 2.0 and 3.x. It integrates with `node-restify-validation` to derive documentation from validation schemas, offering basic endpoint documentation and model definitions. Due to its age, it is not recommended for new projects and is unlikely to be compatible with modern Restify versions (which are currently at 11.x and support Node.js v14.x and v16.x) or contemporary API documentation tools.

Common errors

Warnings

Install

Imports

Quickstart

Sets up a basic Restify server with Swagger 1.2 documentation, including a POST route with input validation and model definitions, demonstrating how to configure and generate API specs.

const restify = require('restify');
const restifySwagger = require('node-restify-swagger');
const restifyValidation = require('node-restify-validation');

const server = restify.createServer({ name: 'MyRestifyAPI' });
server.use(restify.queryParser());
server.use(restify.bodyParser({ mapParams: true })); // Ensure bodyParser is enabled for POST/PUT requests
server.use(restifyValidation.validationPlugin({
    errorsAsArray: false,
}));

restifySwagger.configure(server, {
    description: 'Description of my API',
    title: 'Title of my API',
    allowMethodInModelNames: true
});

server.post({
    url: '/animals',
    swagger: {
        summary: 'Add animal',
        docPath: 'zoo'
    },
    validation: {
        name: { isRequired: true, isAlpha:true, scope: 'body' },
        locations: { isRequired: true, type:'array', swaggerType: 'Location', scope: 'body' }
    },
    models: {
        Location: {
            id: 'Location',
            properties: {
                name: { type: 'string' },
                continent: { type: 'string' }
            }
        }
    }
}, function (req, res, next) {
    // Example: process the validated body
    console.log('Received animal:', req.body);
    res.send(201, req.body);
    return next();
});

restifySwagger.loadRestifyRoutes();

server.listen(8001, function () {
    console.log('%s listening at %s', server.name, server.url);
    console.log('Swagger 1.2 spec available at: http://localhost:8001/swagger/resources.json');
    console.log('Zoo endpoint documentation at: http://localhost:8001/swagger/zoo');
});

view raw JSON →