TypeScript icon, indicating that this package has built-in type declarations

    2.0.2 • Public • Published



    npm npm downloads

    HTTP library that powers our Node.js SDK.

    💾 Installation

    You can use your package manager (npm or yarn) to install the HTTP utilities:

    npm install --save @journyio/http


    yarn add @journyio/http

    🔌 Getting started


    Example of GET request:

    import { HttpClientNode, HttpRequest, HttpHeaders } from "@journyio/http";
    import { URL } from "url";
    const client = new HttpClientNode(/* timeoutInMillis = */ 5000);
    const response = await client.send(
      new HttpRequest(
        new URL(`https://api.domain/users?email=${encodeURIComponent(email)}`),
        new HttpHeaders({ "x-api-key": "my-api-key" })

    Example of POST request:

    const response = await client.send(
      new HttpRequest(
        new URL("https://api.domain/users"),
        new HttpHeaders({
          "x-api-key": "my-api-key",
          "content-type": "application/json",
        JSON.stringify({ email: "user@acme.com" })

    HttpClientNode doesn't support redirects yet.

    🤔️ Why another HTTP client?

    While fetch is great... It's not yet natively available in Node.js. It's also missing an important feature: an interface.

    interface HttpClient {
      send(request: HttpRequest): Promise<HttpResponse>;

    Let's say you have an API client:

    class API {
      async getUser(id: string): Promise<User> {
        const response = await fetch(/* ... */);
        return await response.json();

    If we want to test this API client, we need to mock HTTP requests. It's really hard to do and often requires magic.

    By depending on an interface for an HTTP client we can make this a lot easier:

    class API {
      constructor(private readonly httpClient: HttpClient) {}
      async getUser(id: string): Promise<User> {
        const response = await this.httpClient.send(
          new HttpRequest(/* ... */)
        return JSON.parse(response.getBody());

    In our test we can use HttpClientFixed:

    class HttpClientFixed implements HttpClient {
      private lastRequest: HttpRequest | undefined;
      constructor(private readonly response: HttpResponse) {}
      async send(request: HttpRequest): Promise<HttpResponse> {
        this.lastRequest = request;
        return this.response;
      getLastRequest() {
        return this.lastRequest;

    Our test will look something like this:

    test("our API client works", async () => {
      const http = new HttpClientFixed(
        new HttpResponse(
          new HttpHeaders({ "x-ratelimit-remaining": "200" }),
      const api = new API(http);
      expect(await api.getUser("id")).toEqual({ id: "id", name: "Hans" });
      expect(http.getLastRequest()).toEqual(new HttpRequest(/* ... */));

    In case your client should be able to return different responses depending on the path of the request, you can take a look at the HttpClientCallback class. Here you can give a custom callback to the client, which allows for a lot of flexibility.

    In PHP world this concept is known as PSR-18: HTTP Client.

    Of course, this makes only sense in TypeScript world. Interfaces are not available in JavaScript.

    Apart from testing, there are more benefits...

    We can log requests for debugging purposes:

    class HttpClientConsoleLogging implements HttpClient {
      constructor(private readonly client: HttpClient) {}
      async send(request: HttpRequest) {
        console.log("Request...", request.getMethod(), request.getURL().toString());
        const response = await this.client.send(request);
        console.log("Response...", response.getStatusCode(), response.getBody());
        return response;
    const http = new HttpClientConsoleLogging(
      new HttpClientNode()
    const api = new API(http);
    // Request and response will be logged to the console...
    await api.getUser("id");

    More ideas (not included in this package):

    • Store requests (and responses) of calls that failed
    • Keep track of rate limits
    • Add credentials to requests
    • ...

    💯 Tests

    To run the tests:

    npm run test


    We welcome your feedback, ideas and suggestions. We really want to make your life easier, so if we’re falling short or should be doing something different, we want to hear about it.

    Please create an issue or contact us via the chat on our website.

    🔒 Security

    If you discover any security related issues, please email hans at journy io instead of using the issue tracker.


    npm i @journyio/http

    DownloadsWeekly Downloads






    Unpacked Size

    18.1 kB

    Total Files


    Last publish


    • hansjourny
    • manujourny