Coder Social home page Coder Social logo

ns-restlet-framework's Introduction

Overview

NetSuite RESTlet framework. User defines the models and methods, the framework handles the REST.

Installation

Place the out javascript files in the SuiteScripts folder of your NetSuite account.

Deploy the restlet.js file as a SS2.0 RESTlet.

It is recommended that modifications are made using TypeScript.

Usage

The framework responds to two parameters, which can be passed either on the query URL or post body:

  • model: Which record we want to interact with.
  • method: What we want to do with this record.

Usually we would refine a record type for each model, but abstract types can be built. For example a "store purchase" abstract could upsert a customer, and create a sales order for it.

Calling the API

Calling and endpoint:

Example:
REQUEST: /app/site/hosting/restlet.nl?script=888&deploy=1&model=customer&method=view
RESPONSE: [{"response":true}]

No model set:

Example:
REQUEST: /app/site/hosting/restlet.nl?script=888&deploy=1
RESPONSE: [{"models":["customer"]}]

No method set:

Example:
REQUEST: /app/site/hosting/restlet.nl?script=888&deploy=1&model=customer
RESPONSE: [{"methods":["list","view"]}]

Configuration

In order to work with the framework, we'll need to build a model and controller entry for each record type (or abstract) we want to interact with. This is an example for the customer record.

Models (models.ts)

/**
 * Customer Model
 */
export class Customer extends ModelClass {
 
    public list(): object[] {
        log.debug("list", this.params);
        return [{response: true}];
    }
 
    public view(): object[] {
        log.debug("view", this.params);
        return [{response: true}];
    }
 
    public update(): object[] {
        log.debug("update", this.params);
        return [{response: true}];
    }
 
    public create(): object[] {
        log.debug("create", this.params);
        return [{response: true}];
    }
    
    public remove(): object[] {
        log.debug("remove", this.params);
        return [{response: true}];
    }
}

In the models file we build a model Class for the Customer, and define it's methods. Each method read/writes to the record, and returns an object array with the desired response data.

Methods (controller.ts)

/**
 * Customer Controller
 */
export class Customer extends ControllerClass implements ControllerInterface {
 
    GET() {
 
        //Get models
        const model = new Model.Customer(this.params);
 
        //Bind to methods by name
        const query = new QueryLoader({
            "list": model.list,
            "view": model.view
        });
 
        //Run query
        return query.run(this.method);
    }
 
    PUT() {
 
        //Get models
        const model = new Model.Customer(this.params);
 
        //Bind to methods by name
        const query = new QueryLoader({
            "update": model.update
        });
 
        //Run query
        return query.run(this.method);
    }
 
    POST() {
 
        //Get models
        const model = new Model.Customer(this.params);
 
        //Bind to methods by name
        const query = new QueryLoader({
            "create": model.create
        });
 
        //Run query
        return query.run(this.method);
    }
 
    DELETE() {
 
        //Get models
        const model = new Model.Customer(this.params);
 
        //Bind to methods by name
        const query = new QueryLoader({
            "remove": model.remove
        });
 
        //Run query
        return query.run(this.method);
    }
}

In the controller file we set actions for each HTTP method, and declare all url/body name/methods under the QueryLoader constructor. Please note the extensions and implementations, these are required on Typescript.

License

GNU GPL see LICENSE.

Use at your own discretion. Test before using in production.

Author

Felipe Chang [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.