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

remote-method-api

v5.0.0

Published

A simple mapping from a remote method name to execution of code

Downloads

4

Readme

Remote Method API

A simple mapping from a remote method name to execution of code.

The Remote Method API is an alternative to REST. Instead of limiting oneself to the built-in and inextensible HTTP methods POST, PUT, PATCH, DELETE and GET, with a Remote Method API you can use as many methods as you need and also name them as you like. The HTTP method names do not fit with the standard CRUD (create, read, update, delete) method names.

Related packages

This package uses the interface RemoteMethodCall of package remote-method-call.

To carry out a remote method call from inside the browser take a look at the package postonly-request. It uses the HTTP usage style POSTonly which minimizes the locations were parameters are put into. Basically does it use the POST HTTP method only and every parameter is put inside the body of the HTTP message. It is an alternative to the REST usage style of HTTTP.

There is also a branch which is adjusted for the use with the Coderitter API architecture.

Install

npm install remote-method-api

Overview

This package works in conjunction with remote-method-call which provides a simple interface to describe a remote method call.

interface RemoteMethodCall {
  method: string
  parameter?: any
}

Register remote methods

import { RemoteMethodApi } from 'remote-method-api'

let api = new RemoteMethodApi

// register remote methods
api.methods = {
  'User.create': async (parameter: any) => { return 'success' },
  'User.read': async (parameter: any) => { return [] },
  'User.update': async (parameter: any) => { return 'success' },
  'User.delete': async (parameter: any) => { return 'success' }
}

Call a remote method

let remoteMethodCall: RemoteMethodCall = {
  method: 'User.create',
  // the parameter can be of any type
  parameter: { name: 'Ruben' }
}

let result = await api.callMethod(remoteMethodCall)

result == 'success'

React to thrown exceptions inside the remotely called method

If the remotely called method throws an exception then a simple object containing an error property with an error message is returned by method callMethod.

{ error: `There was an error while executing remote method '${method}': ${e.message}` }

You can customize this behaviour by subclassing RemoteMethodApi.

class YourApi extends RemoteMethodApi {
  onMethodError(error: any, method: string, parameter: any): any {
    return new YourError(error, method, parameter)
  }
}

React to not supported remote method calls

If the remotely called method is not supported then a simple object containing an error property with an error message is return by method callMethod.

{ error: `Remote method '${method}' not supported.` }

You can customize this behaviour by subclassing RemoteMethodApi.

class YourApi extends RemoteMethodApi {
  onRemoteMethodNotSupported(method: string, parameter: any): any {
    return new YourError(method, parameter)
  }
}

Create a sophisticated remote method call handler with MethodCall interface

Instead of assigning a remote method call directly as a function you can also assign any object satisfying the interface MethodCall.

import { MethodCall } from 'remote-method-call'

export default interface MethodCall {
  callMethod(parameter: any): Promise<any>
}