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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ordercloud/oc-codegen

v0.0.1-26

Published

OpenAPI codegen tool built for the OrderCloud API

Readme

Open API Specification

The OpenAPI Specification formerly known as the Swagger Specification is a standard for describing, consuming and visualizing RESTful APIs. The OrderCloud API publishes a new definition every time a new version is released. This enables OrderCloud developers to generate SDKs, API documentation, and even the Devcenter.

This tool takes in an OrderCloud OpenAPI spec along with a set of templates and generates an output. In our case, that output is a set of javascript SDKs but it really could be anything that needs data about the OrderCloud API. Our hope is that outside developers might find some use for it as well, perhaps for an SDK in another language not familiar to us.

⚙️ Installation

npm install @ordercloud/oc-codegen

or

yarn add @ordercloud/oc-codegen

Usage

With the command-line interface

oc-codegen --help

Usage: oc-codegen [options]

A codegen tool for the OrderCloud API

Options:
  -v, --version                           output the version number
  -t, --templates <folder>                (required) where to locate handlebars templates
  -i, --input-spec <path>                 path to valid openapi spec v3.0.0+ (defaults to https://api.ordercloud.io/v1/openapi/v3)
  -o, --output <folder>                   where to write the generated files (defaults to current directory)
  -k, --hooks <filepath>                  path to your hooks file
  -b, --handlebars-extensions <filepath>  path to your handlebars extensions file
  -d, --debug                             prints the template data that is passed to handlebars
  -c, --clean                             cleans output directory before writing files to it (default: false)
  -h, --help                              output usage information

Examples

The shortest possible syntax

oc-codegen -t './path/to/templates-folder';

This will feed the formatted swagger spec to your handlebars templates and output the content to the current directory

As a module in your project

Using imports

import path from 'path';
import codegen from '@ordercloud/oc-codegen';

codegen
  .generate({
    templates: 'path/to/templates-folder',
    inputSpec: null, // default: https://api.ordercloud.io/v1/openapi/v3
    output: null, // default: current directory
    hooks: null,
    handlebarsExtensions: null,
    clean: null, // default: false
    debug: null, // default: false
  })
  .then(function() {
    console.log('Done!');
  })
  .catch(function(err) {
    console.error('Something went wrong: ' + err.message);
  });

Using requires

const codegen = require('@ordercloud/oc-codegen');
const path = require('path');

codegen.default
  .generate({
    inputSpec: '/path/to/oc-spec.json',
    templates: '/path/to/templates-folder',
    output: null, // default: current directory
    hooks: null,
    handlebarsExtensions: null,
    clean: false,
    debug: false,
  })
  .then(function() {
    console.log('Done!');
  })
  .catch(function(err) {
    console.error('Something went wrong: ' + err.message);
  });

🔧 Creating your own templates

Templates define the skeleton for how your code will be generated. We use handlebars for the templating engine. There are three different types of files that can exist in your templates directory:

  1. Static - copied over as-is with no dynamic content
  2. Static Template - copied over once and processed by handlebars
  3. Contextual Template - generates multiple files with one handlebars template where each file is a different context (resource, model, or operation)

Template Data

Each template has access to the formatted ordercloud spec. Additionally, contextual templates get injected with data for each context (operation, resource, or model).

The debug option will print the templatedata to stdout which you can then pipe into a file. For example:

oc-codegen -d > templateData.json

Example Templates Directory

Consider the following directory

templates
│   README.md.hbs
│
└───models
    │   _MODEL_.js.hbs
    │   ExtraModel.js

README.md.hbs is a static template and as such will generate one README.md file but will have context from the api spec to add dynamic data. For example we might want to set API version in the readme.

_MODEL_.js.hbs is a contextual template. The _MODEL_ piece will be replaced by the current model being generated and have context injected for that model. To generate a contextual resource template include _RESOURCE_ in the file name and similarly for contextual operation templates include _OPERATION_ in the file name.

ExtraModel.js is a static file that will simply get copied over as-is during code generation

Hooks 🎣

Hook into oc-codegen's processing pipeline with hooks!

Implement hooks by exporting them from a javascript file. If your hook performs async work you'll need to make sure it returns a promise, otherwise you can just return directly.

There are three types of hooks for each data type (operation, model, resource)

  • filter{dataType} - filter the result set
  • format{dataType} - replace oc-codegens formatting with your own
  • postFormat{dataType} - run custom formatting after oc-codegen has done initial formatting

There is also one hook at the end of all data type hooks called postFormatTemplateData

module.exports.postProcess = function(templateData, rawSpec) {
  // return your modified template data
  return templateData;
};

💁 Custom Handlebars Helpers

In addition to the standard handlebars helpers you can define your own custom helpers.

First create a javascript file in your project

function handlebarsExt(Handlebars) {
  /**
   * Function to append 'bar' to the end of a word
   */
  Handlebars.registerHelper('appendBar', word => {
    return word + 'bar';
  });
}
module.exports = handlebarsExt;

Now simply use the helper in your handlebars template and then when you call the cli pass it the path to the extensions file so that the cli can register the helpers prior to compilation.

oc-codegen -t './path/to/templates-folder' -b './path/to/handlebars-extensions';

📄 License

OrderCloud's oc-codegen is an open-sourced software licensed under the MIT license.

🤝 Contributing

Check out our Contributing guide.