GithubHelp home page GithubHelp logo

misantronic / pretend Goto Github PK

View Code? Open in Web Editor NEW

This project forked from knisterpeter/pretend

0.0 0.0 0.0 177 KB

A decorator based http webservice client written in typescript

License: MIT License

TypeScript 100.00%

pretend's Introduction

pretend

GitHub license Travis Coveralls branch npm Commitizen friendly Standard Version Greenkeeper badge

A decorator based http webservice client build with typescript (inspired bei feign).

Features

  • Handle REST based webservices
  • Configure a decoder (defaults to JSON)
  • Generic request/response interceptor chain
  • Basic authentication
  • Request parameters (currently on GET requests)
  • Custom headers per method

Usage

Installation

Install as npm package:

npm install pretend --save

Note: To work on node.js (server-side) the fetch must be polyfilled. This could easy be done importing isomorphic-fetch.

API

class Test {

  @Headers('Accept: application/json')
  @Get('/path/{id}', true)
  public async get(id: string, parameters: any) {}

  @Post('/path')
  public async post(body: any) {}

  @Put('/path')
  public async put() {}

  @Delete('/path/:id')
  public async delete(id: string) {}

}

async function call() {
  const client = Pretend
                  .builder()
                  .target(Test, 'http://host:port/');
  const result = await client.get('some-id', {'name': 'value'});
}

// Executes a GET request to 'http://host:port/path/some-id?name=value'
call();

Decoders, basicAuthentication and requestInterceptors are all special forms of the more generic interceptors which could be chained per request/response.

  // Configure a text based decoder
  const client = Pretend
                  .builder()
                  .decoder(Pretend.TextDecoder)
                  .target(Test, 'http://host:port/');
  // Configure basic authentication
  const client = Pretend
                  .builder()
                  .basicAuthentication('user', 'pass')
                  .target(Test, 'http://host:port/');
  // Configure a request interceptor
  const client = Pretend
                  .builder()
                  .requestInterceptor((request) => {
                    request.options.headers['X-Custom-Header'] = 'value';
                    return request;
                  })
                  .target(Test, 'http://host:port/');

Interceptors

Multiple interceptors could be added to each builder. The order of interceptor calls will result in a chain of calls like illistrated below:

  // Configure a request interceptor
  const client = Pretend
                  .builder()
                  .interceptor(async (chain, request) => {
                    console.log('interceptor 1: request');
                    const response = await chain(request);
                    console.log('interceptor 1: response');
                    return response;
                  })
                  .interceptor(async (chain, request) => {
                    console.log('interceptor 2: request');
                    const response = await chain(request);
                    console.log('interceptor 2: response');
                    return response;
                  })
                  .target(Test, 'http://host:port/');
             +---------------+    +---------------+
Request ---> |               | -> |               |
             | Interceptor 1 |    | Interceptor 2 | -> HTTP REST call
Response <-- |               | <- |               |
             +---------------+    +---------------+

This leads to the following console output:

interceptor 1: request
interceptor 2: request
interceptor 2: response
interceptor 1: response

Future ideas / Roadmap

  • Named parameters

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.