0.5.0 • Published 4 years ago

@projectunic0rn/pub-api-client v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

pub-api-client Publish

Client for the Pub API

NOTE: Pre-major versions (i.e. <v1.0.0) do not follow semantic versioning scheme and BREAKING CHANGES bumps MINOR values instead.

Todos

  • Implement core functionality
  • Consider moving peer dependencies to be solid dependencies
  • Add tests
  • Release v1.0.0

Installation

Install the package using npm by entering this command:

npm install @projectunic0rn/pub-api-client

Or, if you are using Yarn:

yarn add @projectunic0rn/pub-api-client

Usage

ApiClient

The following snippet shows how to use the ApiClient class to make a request to the Pub API for all projects:

const { ApiClient } = require('@projectunic0rn/pub-api-client');

const client = new ApiClient();

client.project
  .getAll()
  .then((response) => {
    if (response.error) {
      return Promise.reject(response.error.message);
    }

    return response.data;
  })
  .then(console.log)
  .catch(console.error);

Normally, an app will have several requests of different types. To avoid re-creating a client for every request, you can choose to initialize it first to a variable and export that variable so other modules can import it.

The constructor also accepts an object to configure the API Client instance:

type ClientOptions {
  /**
   * If set to `production`, the request will be sent to the Production API
   * server. Otherwise, the request is sent to the test API server. Default is
   * `production`.
   */
  readonly mode?: 'production' | 'development';
};

MockClient

In case that you are working with an unstable internet connection, you might want to use the Mock API client.

Update your code to use the mock API client:

const { MockClient } = require('@projectunic0rn/pub-api-client');

const client = MockClient();

client.project.getAll().then((response) => {
  // ... do something with response
});

TypeScript

The library includes type definitions for you to use on your TypeScript projects:

import { ApiClient, MockClient } from '@projectunic0rn/pub-api-client';
import type { Client, Project } from '@projectunic0rn/pub-api-client':

const client: Client = process.env.NODE_ENV === 'production' ? new ApiClient() : new MockClient();

async function fetchProjects(): Promise<Project[]> {
  const response = await client.project.getAll();

  if (response.error) {
    const { message } = response.error;

    if (message == 'client/response-valid-but-ok-field-is-falsey') {
      console.log("api server doesn't work for some reason");
    }

    return [];
  }

  return response.data;
}

License

Copyright 2020 Project Unicorn

This project is licensed under the terms of the MIT License.

0.5.0

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago