Giter Club home page Giter Club logo

wildcard-intercept's Introduction

Wildcard Intercept

Library for Wildcard API to intercept calls.

// ./example/endpoints.js

const {endpoints} = require('@wildcard-api/server');
const intercept = require('@brillout/wildcard-intercept'); // npm install @brillout/wildcard-intercept

endpoints.hello = intercept(function(name) {
  return 'Hi '+name+'!';
}, myListener);

endpoints.mirror = intercept(function(msg) {
  return msg.split('').reverse().join('');
}, myListener);

function myListener({endpointName, endpointArgs, endpointResult, endpointError, context}) {
  console.log({endpointName, endpointArgs, endpointResult, endpointError, context});
}
// ./example/client.js

const {endpoints} = require('@wildcard-api/client');
const wildcard = require('@wildcard-api/client');

wildcard.serverUrl = 'http://localhost:3000';

(async function() {
  let greeting = await endpoints.hello('Johnson');
  console.log(greeting);
  greeting = await endpoints.mirror(greeting);
  console.log(greeting);
})();

Client console.log result:

Hi Johnson!
!nosnhoJ iH

Server console.log result:

{
  endpointName: 'hello',
  endpointArgs: [ 'Johnson' ],
  endpointResult: 'Hi Johnson!',
  endpointError: undefined,
  context: {
    headers: {
      'content-type': 'application/json',
      'accept-encoding': 'gzip,deflate',
      'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
      connection: 'close',
      accept: '*/*',
      'content-length': '0',
      host: 'localhost:3000'
    }
  }
}
{
  endpointName: 'mirror',
  endpointArgs: [ 'Hi Johnson!' ],
  endpointResult: '!nosnhoJ iH',
  endpointError: undefined,
  context: {
    headers: {
      'content-type': 'application/json',
      'accept-encoding': 'gzip,deflate',
      'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
      connection: 'close',
      accept: '*/*',
      'content-length': '0',
      host: 'localhost:3000'
    }
  }
}

The source code of @brillout/wildcard-intercept is only 42 LOC; you may want to implement your own interceptor!

wildcard-intercept's People

Contributors

brillout avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.