woocommerce/tests/e2e/api
Christopher Allford c09d91f6ce Finished adding appropriate documentation to the API package 2020-09-25 11:11:17 -07:00
..
src Finished adding appropriate documentation to the API package 2020-09-25 11:11:17 -07:00
.eslintignore Renamed the `@woocommerce/factories` package to `@woocommerce/api` 2020-09-03 12:28:17 -07:00
.eslintrc.js Refactored the factory to use a repository instead of an adapter so that we can expose more API functionality 2020-09-07 16:00:27 -07:00
.gitignore Renamed the `@woocommerce/factories` package to `@woocommerce/api` 2020-09-03 12:28:17 -07:00
README.md Updated the readme for the API package 2020-09-21 12:32:44 -07:00
jest.config.js Refactored the HTTP layer to be more API-agnostic 2020-09-04 11:27:34 -07:00
package-lock.json Updated the JSDoc and type-safety of repositories to better document the implemented operations 2020-09-24 14:22:38 -07:00
package.json Fixed the package.json files array to exclude development content 2020-09-21 13:33:57 -07:00
tsconfig.json Updated the JSDoc and type-safety of repositories to better document the implemented operations 2020-09-24 14:22:38 -07:00

README.md

WooCommerce API Client

An isometric API client for interacting with WooCommerce installations. Here are the current and planned features:

  • TypeScript Definitions
  • Axios API Client with support for OAuth & basic auth
  • Repositories to simplify interaction with basic data types
  • Service classes for common activities such as changing settings

Usage

npm install @woocommerce/api --save-dev

Depending on what you're intending to get out of the API client there are a few different ways of using it.

REST API

The simplest way to use the client is directly:

import { HTTPClientFactory } from '@woocommerce/api';

// You can create an API client using the client factory with pre-configured middleware for convenience.
let httpClient = HTTPClientFactory.withBasicAuth(
    // The base URL of your REST API.
    'https://example.com/wp-json/',
    // The username for your WordPress user.
    'username',
    // The password for your WordPress user.
    'password',
);

// You can also create an API client configured for requests using OAuth.
httpClient = HTTPClientFactory.withOAuth(
    // The base URL of your REST API.
    'https://example.com/wp-json/',
    // The OAuth API Key's consumer secret.
    'consumer_secret',
    // The OAuth API Key's consumer password.
    'consumer_pasword',
);

// You can then use the client to make API requests.
httpClient.get( '/wc/v3/products' ).then( ( response ) => {
  // Access the status code from the response.
  response.statusCode;
  // Access the headers from the response.
  response.headers;
  // Access the data from the response, in this case, the products.
  response.data;
} );

Repositories

As a convenience utility we've created repositories for core data types that can simplify interacting with the API. These repositories provide CRUD methods for ease-of-use:

import { SimpleProduct } from '@woocommerce/api';

// Prepare the HTTP client that will be consumed by the repository.
// This is necessary so that it can make requests to the REST API.
const httpClient = HTTPClientFactory.withBasicAuth( 'https://example.com/wp-json/','username','password' );

const repository = SimpleProduct.restRepository( httpClient );

// The repository can now be used to create models.
const product = repository.create( { name: 'Simple Product', regularPrice: '9.99' } );

// The response will be one of the models with structured properties and TypeScript support.
product.id;