Description

A sample script which covers how to use apiwriter (http://apiwriter.codeplex.com) to generate content for your REST based web api.

Details

There are two folders
- /docs: contains a sample script to generate resources. This is the interesting purpose of this sample.
- /svc: a sample node web api for the docs script to hit. Simply run node app.js to start up the sample svc. This is not interesting - it's just meant to be a sample service to play with the doc generation api.

/docs/genapi.js

This simple sets some request options, set some doc options and then tells it to generate docs
by looking in the resources folder and running each script. The recommendation is one script per
resource.

/docs/resources/discussions.js

A sample script to generate output for the "discussions" resources. This is the most interesting use of the apiwriter.

It creates a context object for state between the calls. This must be exported as getContext.
You then have to export a method called submitRequests.

In the callback, the result is passed back to you so you can update the context and in subsequent
requests, you can reference data from the context like {createdId}.

var apiwriter = require('apiwriter');

exports.getContext = function() {
    return {
        createdId: null
    };
};

exports.submitRequests = function()
{
    apiwriter.postJson('/discussions', {"title":"A discussion", "author":"me"},
        function(context, result) {
            context.createdId = result.responseBody.id;
        });

    apiwriter.postJson('/discussions', {"title":"Another discussion", "author":"me"});
    apiwriter.getJson('/discussions',
        function(context, result) {
            context.discussionId = result.responseBody[0].id;
        });

    apiwriter.getJson('/discussions/{createdId}');
    apiwriter.deleteJson('/discussions/{createdId}');
}

Last edited May 20, 2013 at 6:55 PM by bryanmacfarlane, version 2