@open-pioneer/http
TypeScript icon, indicating that this package has built-in type declarations

2.1.5 • Public • Published

@open-pioneer/http

This package provides the HttpService, which can be used to request resources over HTTP.

Quick start

Reference the interface http.HttpService from your service:

// build.config.mjs
import { defineBuildConfig } from "@open-pioneer/build-support";

export default defineBuildConfig({
    services: {
        MyService: {
            references: {
                httpService: "http.HttpService"
            }
        }
        // ...
    }
    // ...
});

The snippet above will inject the HttpService as httpService.

Then, from the implementation class of MyService, simply call fetch() on the service:

// MyService.js
export class MyService {
    constructor(serviceOptions) {
        this.httpService = serviceOptions.references.httpService;
    }

    async myMethod() {
        const response = await this.httpService.fetch(/* ... */);
    }
}

The signature of the fetch() method is compatible to the Browser's global fetch function. However, the HttpService's method should always be preferred to take advantage of future features (logging, proxy support, etc.).

Request interceptors

Note that the request interceptor API is experimental: it may change with a new minor release as a response to feedback.

The HttpService supports extension via request interceptors. Request interceptors can modify requests (query parameters, headers, etc.) before they are sent to the server.

To register a request interceptor, implement a service that provides "http.Interceptor":

// build.config.mjs
import { defineBuildConfig } from "@open-pioneer/build-support";

export default defineBuildConfig({
    services: {
        ExampleInterceptor: {
            provides: "http.Interceptor"
        }
        // ...
    }
    // ...
});
// ExampleInterceptor.ts
import { Interceptor, BeforeRequestParams } from "@open-pioneer/http";
export class ExampleInterceptor implements Interceptor {
    async beforeRequest?(params: BeforeRequestParams) {
        // Invoked for every request. See API documentation for more details.
    }
}

License

Apache-2.0 (see LICENSE file)

Readme

Keywords

Package Sidebar

Install

npm i @open-pioneer/http

Weekly Downloads

25

Version

2.1.5

License

Apache-2.0

Unpacked Size

30 kB

Total Files

14

Last publish

Collaborators

  • open_pioneer
  • antoniave_conterra
  • jessebluemr
  • mbeckem_conterra