1.1.1 • Published 3 years ago

@elebase/sdk v1.1.1

Weekly downloads
78
License
MIT
Repository
github
Last release
3 years ago

Elebase JavaScript Development Kit

An isomorphic API client and development kit for the Elebase platform.

Installation

NPM

npm install @elebase/sdk --save

Yarn

yarn add @elebase/sdk

Usage

This package can be used in web browsers and Node.js environments.

N.B. A module bundler such as Webpack is required for use in a web browser.

Importing the library

const ele = require('@elebase/sdk');

Creating API Client Instances

Create an instance of the Project API client:

const projectAPI = ele.api({
  key: { public: 'PUBLIC API KEY', private: 'PRIVATE API KEY' },
  locales: ['en', 'es', 'fr'],
  phases: [4],
  project: 'PROJECT ID',
  user: 'USER ID'
});

Create an instance of the Geo Tools API client:

const geoAPI = ele.geo({ token: 'API TOKEN' });

Both the ele.api() and ele.geo() functions accept an API client configuration object with the properties described in the table below.

PropertyTypeDescriptionRequired
httpobjectAn HTTP client configuration object with the following optional properties: headers: an object with HTTP headers to include with each API requesttimeout: the number of milliseconds to wait before aborting an API request (default: 20000)logging: a boolean indicating whether or not verbose HTTP request/response logging should be enabled (default: false)
keyobjectAn object with public and private API keys. These keys can be generated and retrieved from the API Keys screen of the web app. Note: API keys with POST, PUT, and/or DELETE privileges should never be exposed in client-side code. It's also good practice to never commit them to a remote code repository (even if the repository is private). Instead, the keys can be defined as environment variables and made available in server-side code via Node's process.env object using a package like dotenv. This setting is required if token is not defined.
localesarrayAn array of locale codes to include in the Accept-Language header of each API request. See the Localization section of the API documentation for more details.
phasesarrayAn array of integers representing the phases to filter Entries by. For example, if this configuration option is set to [4], each GET /entries request will automatically include a phase parameter in the URL with a value of 4. This can be overridden on a per-request basis. For more details on the phase parameter, see the description in this table.
projectstringThe ID of an Elebase Project.
tokenstringAn API token. An API token can be generated for any existing API key via the API Keys screen of the web app. Note: API tokens with POST, PUT, and/or DELETE privileges should never be exposed in client-side code. It's also good practice to never commit them to a remote code repository (even if the repository is private). Instead, tokens can be defined as environment variables and made available in server-side code via Node's process.env object using a package like dotenv. This setting is required if key is not defined and ignored if key is defined.
userstringThe ID or authentication token of a User to include in the Authorization header of each API request. This can be overridden on a per-request basis. Note: If a request requires a User ID or token, it will be indicated in the API documentation.
versionstringThe API version to target (default: v1).

Sending API Requests

Send a request to the Project API for the first 10 published Entries when ordering by title:

(async () => {
  try {
    const options = { params: { limit: 10, order: 'title', page: '1', phase: '4' } };
    const res = await api.get('/entries', options);
    console.info(res.data.index);
  } catch (err) {
    console.error(err);
  }
})();

Send a request to the Geo Tools API for the elevation at given location:

(async () => {
  try {
    const options = { data: { geometry: { type: 'Point', coordinates: [-109.80856, 45.16372] } } };
    const res = await geoAPI.post('/elevations', options);
    console.info(`Elevation: ${Math.round(res.data.coordinates[2])} meters`);
  } catch (err) {
    console.error(err);
  }
})();

API Request Methods

VerbMethod
GETapi.get()
POSTapi.create() or api.post()
PUTapi.update() or api.put() (Project API only)
DELETEapi.delete() (Project API only)

All methods support two arguments: an API endpoint path (e.g., /entries or /measurements) and an optional Request Configuration object, which is described in the table below.

Request Configuration Object

PropertyTypeDescription
dataobjectAn object to encode as a JSON string and send in the body of the request. This setting is for POST and PUT requests only.
firstbooleanIf true and this is a GET request, the first object in the data.index array of the response body will be returned instead of an object with index and total properties. If the data.index array is empty, a null value will be returned. Setting this option to true is useful when only one result is expected or desired—when filtering by ID, for example. This setting is for Project API requests only.
headersobjectCustom HTTP headers to include with the request.
localesarrayAn array of locale codes to include in the Accept-Language header of the request. See the Localization section of the API documentation for more details. This setting is for Project API requests only.
paramsobjectA plain or URLSearchParams object containing query parameters to include in the URL of the request.
userstringThe ID or authentication token of a User to include in the Authorization header of the request. Note: If a request requires a User ID or token, that requirement will be specified in the API documentation. This setting is for Project API requests only.

Handling API Responses

All API request methods return Promises that either resolve with a Response object or reject with a RequestError object. Details on both of these objects are provided below.

Response Object
PropertyTypeDescription
dataobject/nullThe JSON-decoded response body (or null if the body was empty).
headersobjectThe HTTP response headers.
statusintegerThe HTTP response status code.
Project API RequestError Object
PropertyTypeDescription
datamixedThe data property from the Error object returned by the API endpoint.
headersobjectResponse headers relevant to the error (e.g., the X-Usage-Limit-Info and X-Usage-Limit-Time headers from a 429 response).
idstringThe id property from the Error object returned by the API endpoint.
messagestringA basic error message that includes the full JSON Error object returned by the API endpoint.
statusintegerThe status code of the response.
Geo API RequestError Object
PropertyTypeDescription
codestringThe code property from the Error object returned by the API endpoint.
headersobjectResponse headers relevant to the error (e.g., the X-Usage-Limit-Info and X-Usage-Limit-Time headers from a 429 response).
infostringThe info property from the Error object returned by the API endpoint.
statusintegerThe status code of the response.
typestringThe type property from the Error object returned by the API endpoint.

API Documentation

Full API references can be found in the Learn section of our website.

1.1.1

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago