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

service-model

v0.4.0

Published

An object oriented web service framework inspired by Windows Communication Foundation.

Downloads

43

Readme

Build Status

service-model

An object oriented web service framework inspired by Windows Communication Foundation

The service-model module provides an extensible web service framework for Node. The API is inspired by the Windows Communication Foundation (WCF). This module focuses on the dispatching of service requests and does not handle the underlying transport or encoding. Therefore, service-model must be paired with a server component such as Express or Restify. Also note that you'll need the appropriate middleware for the type of transport you are using, such as the JSON body-parser. API documentation is available here.

Installation

service-model can be installed using npm:

$ npm install service-model --save

Getting Started

For brevity, the example here is only given in TypeScript. JavaScript examples coming soon.

Defining a Service Contract

We define a service contract, Calculator, that has two operations, add and subtract. The contract is specified using decorators on the service implementation, CalculatorService.

import { Operation, Contract } from "service-model";

@Contract("Calculator")
export class CalculatorService {

    @Operation()
    add(x: number, y: number, callback: ResultCallback<number>): void {
    
        callback(null, x + y);
    }

    @Operation()
    subtract(x: number, y: number, callback: ResultCallback<number>): void {
    
        callback(null, x - y);
    }
}

Configuring the DispatcherFactory

Once our service is defined, we add it to a DispatcherFactory. We then add an endpoint to the service, providing the name of the contract, the base address for the endpoint, and a list of endpoint behaviors.

import { DispatcherFactory, RpcBehavior } from "service-model";

var factory = new DispatcherFactory();

factory.addService(CalculatorService)
           .addEndpoint("Calculator", "/api/rpc/calculator", [new RpcBehavior()]);

In this case, we add the RpcBehavior which indicates that operations on this endpoint will be available through RPC. Operations can also be made available through REST by adding the RestBehavior which will be described later.

Dispatching Requests

The previously configured factory is then used to create a RequestDispatcher which is responsible for handling service requests. In this example we configure an Express web server to delegate all requests with a base path of "/api" to the RequestDispatcher. The ExpressRequestContext comes packaged in the library but a RequestContext can easily be created for other server platforms.

import * as express from "express";
import { ExpressRequestContext } from "service-model";

var app = express();
var dispatcher = factory.createDispatcher();

app.use("/api*", (req, res) => {
    dispatcher.dispatch(new ExpressRequestContext(req, res));
});

app.listen(3000);

REST Services

Service operations can be made available through a REST api by adding decorators to the service implementation methods. Revisiting the previously defined CalculatorService we add the WebGet decorator to the add and subtract methods.

import { Operation, Contract, WebGet } from "service-model";

@Contract("Calculator")
export class CalculatorService {

    @Operation()
    @WebGet("/add/{x}/{y}")
    add(x: number, y: number, callback: ResultCallback<number>): void {
    
        callback(null, x + y);
    }

    @Operation()
    @WebGet("/subtract/{x}/{y}")
    subtract(x: number, y: number, callback: ResultCallback<number>): void {
    
        callback(null, x - y);
    }
}

The WebGet decorator defines a UrlTemplate for each method which is used to choose the operation and decode method parameters.

We then create an endpoint with the RestBehavior. The CalculatorService now has two endpoints, supporting both REST and RPC.

import { RestBehavior } from "service-model";

factory.addService(CalculatorService)
           .addEndpoint("Calculator", "/api/rest/calculator", [new RestBehavior()]);

The service operations are now available using HTTP GET requests:

$ curl http://localhost:3000/api/rest/calculator/add/1/2

3

License

Licensed under the Apache License 2.0. Note that Microsoft released an open source implementation of their .NET Framework as the Mono Project. The components of the Mono Project that relate to this module are licensed under the MIT X11 terms.