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

raml-typescript-generator

v1.1.1

Published

Generate a TypeScript API client from RAML

Downloads

36

Readme

RAML TypeScript Generator

NPM version NPM downloads

Generate a TypeScript (Angular2, Ionic2 or similar) JavaScript API client from RAML. Expose defined "schemas" as Typescript interfaces resp. types.

  • Allows developers to take advantage of clean API specification already at compile time.
  • Reduces chance of misinterpretation of REST actions and exchanged payloads
  • Eases API version updates as breaking changes should surface immediately and not only at runtime.

Based on provided RAML file, a self-contained Node module is produced, including following assets:

  • Chained DSL with support for call signature and output document parsing
  • README.md output
  • index.ts output
  • All required scaffolding to allow easy use of generated module in own projects or publish to npmjs
  • Legacy-style plain old Javascript legacy.js output (refer to mulesoft/raml-javascript-generator)

Generator based on raml-generator provided by MuleSoft.

Installation

npm install raml-typescript-generator -g

Usage

Sets up node API module based on RAML file.

Example use:

  1. Create new module (Hint:Create directory outside your GIT-managed project file structure if you want to check the module in to separate repository.):
 mkdir myApiModule
 cd myApiModule
 
 cp ../wherever/from/myApi.raml .
 raml-typescript-generator myApi.raml -o .
 npm i
 npm run build
  1. Update package.json to, for example, reflect author, URL locations, versions etc.
  2. Use module in own application w/out distributing to npmjs yet.
cd ../myProject
npm i ../myApiModule

At this point, you should be able to use your custom API library as standard Node module within your application - refer to README.MD in produced module for usage instructions.

  1. Check in to version control (git)
cd ../myApiModule
git add .
git commit -a

# Create repo on github or similar, do not initialize with readme or similar...
# -> https://github.com/new
git remote add origin https://github.com/yourName/myApiModule.git
git push --set-upstream origin master
  1. Publish to npmjs

Attention! This cannot be undone! Ensure you do not push private code or data or any credentials! Ensure to update package version number using SemVer

npm publish

Hint: After updates in RAML file, API code can be re-generated using npm run regen. Note that only essential attributes of package.json are updated! Warning! If you perform a breaking change on the API, it is recommended to bump the version number and produce a new API module instead!

Contributing

npm run build

#node dist\bin.js <path-to-yrml-file> -o <path-to-desired-output-directory>
#Example:
node dist\bin.js ..\..\src\api\yaas\document.raml -o ..\..\src\api\yaas\document
  • Package up and install as global library.
npm install -g

Now you should be able to run raml-typescript-generator as described below in any custom module location.

API Usage

Please refer to README.MD generated in your new API module for details!

Setup

Include in TypeScript project like:

import {SchemaApi} from 'schema-api';

...
export class Foo {
  schemaApi: SchemaApi;

  constructor() {
    this.schemaApi = new SchemaApi({
      getHeaders: () => {
        return (authService.getAuthHeaders())
      }
    });
  }
...
  • Assuming API is encapsulated in single client class, no handling of setting up service as singleton (yet).
  • Option parameter getHeaders can be used to specify callback function to add custom headers to every request, for example to handle authentication.
  • Use option baseUri to switch endpoints.

API Call

Call typescript functions like:

        this.schemaApi.tenant("foo").GET("bar", 1, 100) 
          .subscribe(s => {
            if (s._200) {
              console.log(s._200.myAttr)
            }
            else {
              console.log("Error obtaining schemas", s);
            }
          });
  • All parameters are strongly typed based on the RAML specification.
  • All mandatory parameters are moved to the front, optional ones at the end. Uses Typescript param?:type syntax.
  • Resulting document is wrapped into typed "response" document based on the response code. The resulting object structure therefore is fully accessible to TypeScript compiler: Example:
GET(  totalCount:boolean, 
      pageNumber?:number, 
      pageSize?:number)
    :Observable<{headers:any,
                responseCode:number, 
                bodyRaw:any,
                "_200"?:SchemasData.SchemasData,
                "_400"?:ErrorMessage.ErrorMessage,
                "_401"?:ErrorMessage.ErrorMessage,
                "_403"?:ErrorMessage.ErrorMessage}> 
  • Full header, response code and raw body exposed directly.
  • TODO: Currently resulting document is NOT verified against Schema specification!

Limitations

  • Used schema generator does not (yet) support $ref external references
  • Only explicitly named schemas are exposed via type or interface
  • Result document not validated against schema.

License

Apache License 2.0