Giter Club home page Giter Club logo

http-post-message's Introduction

htt-post-message

Travis branch npm Total Downloads Monthly Downloads GitHub tag

A generic messaging component to send HTTP style message payloads over the window.postMessage API. Requires an implementation of window postMessage proxy such as 'window-post-message-proxy'.

Documentation:

Installation

npm install --save http-post-message

Usage

The HttpPostMessage takes in an object that implements the IPostMessage interface which is just one method postMessage which returns a Promise.

In the case below we created a mock postMessage proxy, but in normal usage you would likely use the accompanying library window-post-message-proxy and configure it to handle http messages as shown in the section below

import * as hpm from 'http-post-message';

const stubWindowPostMessageProxy: hpm.IPostMessage = {
  postMessage(message: any) {
    console.log(message);
    return Promise.resolve(message):
  }
}

const httpPostMessage = new hpm.HttpPostMessage(stubWindowPostMessageProxy);

httpPostMessage.get('target/path')
  .then(response => {
    ...
  });

Methods

The object supports all the standard http methods, get, post, put, patch, and delete. You can also send an IRequest object directly to the lower-level send method.

Determining promise resolution state

The promise returned by the request will be fulfulled or rejected depending on how the instance of WindowPostMessageProxy provided to the constructor was configured.

Since we intend to use HTTP semantics, this library comes with static methods that can be provided to the WPMP class during construction as seen below:

import * as wpmp from 'window-post-message-proxy';
import * as hpm from 'http-post-message';

const windowPostMessageProxy = new wpmp.WindowPostMessageProxy({
  processTrackingProperties: {
      addTrackingProperties: hpm.HttpPostMessage.addTrackingProperties,
      getTrackingProperties: hpm.HttpPostMessage.getTrackingProperties,
  },
  isErrorMessage: hpm.HttpPostMessage.isErrorMessage
});

const httpPostMessage = new hpm.HttpPostMessage(windowPostMessageProxy);

Default Headers and Default Window

You may specify default headers and default target window to be sent with every request by supplying them during construction.

Providing the default target window in the constructor means you do not have to specify it in each individual request, but it can be over written to allow a single instance of HPM to communicate with multiple windows.

import * as hpm from 'http-post-message';

...

const httpPostMessage = new hpm.HttpPostMessage(wpmp, {
    'x-sdk-type': sdkType,
    'x-sdk-version': sdkVersion
  }, defaultTargetWindow);
});

http-post-message's People

Contributors

ali-hamud avatar anant-k-singh avatar mattmazzola avatar mayur007b avatar msftgits avatar pkwiecien avatar submarine-launched avatar wbreza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.