1.2.0 • Published 7 months ago

@palmetto/product-catalog-sdk v1.2.0

Weekly downloads
-
License
-
Repository
github
Last release
7 months ago

Palmetto Products API SDK

An isomorphic client for integrating with the Palmetto Product Catalog API

Installation

yarn add @palmetto/product-catalog-sdk

Getting started

  1. Create a new client

    import { ProductsApiClient } from "@palmetto/product-catalog-sdk";
    
    const client = new ProductsApiClient({
      // Does not include /api
      apiUrl: "https://next.palmetto.energy",
      // Use an OAuth access token or PAT from users.palmetto.com
      authToken: "xxxxx",
    });

Usage

Make a request

const result = await client.findClassifications({
  path: "hvac",
});

if (result.ok) {
  console.log(result.data);
}

Handle request errors

const result = await client.createClassification({
  slug: "hvac",
  name: "HVAC",
  recommendationSettings: { enabled: false },
});

if (result.ok) {
  // ...
} else {
  if (result.error.code === "ERROR_RESPONSE") {
    switch (result.error.response.error) {
      case "BadRequest":
        console.log("validation errors:", result.error.response.validations);
        break;
      case "Conflict":
        console.log("Classification path is already in use");
        break;
      // ...
    }
  } else if (result.error.code === "SERVICE_UNAVAILABLE") {
    console.error("Unable to connect!");
  }
}

Use middleware

client.use({
  onResponse(request: Request, response: Response) {
    const payload = {
      message: `${request.method} ${request.url} ${response.status} ${response.statusText}`,
      http: {
        method: request.method,
        status_code: response.status,
        url: request.url,
      },
    };

    if (response.status <= 299) {
      logger.log(payload);
    } else if (response.status <= 499) {
      logger.warn(payload);
    } else {
      logger.error({
        ...payload,
        error: { message: response.statusText },
      });
    }
  },
});

Add a header to a request

const result = await client.findClassifications(
  {},
  {
    headers: { "X-Custom": "myvalue" },
  },
);

Add a header to all requests

client.setHeader("X-Custom", "myvalue");

Change auth token for all requests

client.setAuthToken("xxxxx");

Use different auth token for a single request

const result = await client.findClassifications(
  { path: "hvac" },
  {
    authToken: "xxxxx",
  },
);
1.2.0

7 months ago

1.1.0

7 months ago

1.0.0

8 months ago

0.0.10

1 year ago

0.0.11

11 months ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago