woocommerce/tests/e2e/api
Christopher Allford 8e7ef263a2 Adjusted the package exports to be easier to consume 2020-09-21 11:21:35 -07:00
..
src Adjusted the package exports to be easier to consume 2020-09-21 11:21:35 -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 Renamed the `@woocommerce/factories` package to `@woocommerce/api` 2020-09-03 12:28:17 -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 Adjusted the package exports to be easier to consume 2020-09-21 11:21:35 -07:00
package.json Revised the base tsconfig to match Gutenberg more closely 2020-09-16 15:14:53 -07:00
tsconfig.json Adjusted the package exports to be easier to consume 2020-09-21 11:21:35 -07:00

README.md

API

A simple interface for interacting with a WooCommerce installation.

Installation

bash npm install @woocommerce/api --save-dev

Usage

Consumers of this package should rely on an instance of ModelRegistry to access the factories. Here is an example of how to initialize and use the package to generate a simple product:

import { 
    AdapterTypes,
    initializeUsingBasicAuth,
    ModelRegistry,
    registerSimpleProduct,
    SimpleProduct
} from 'e2e/api';

// The ModelRegistry instance is where all of the factories and adapters are stored in an easy-to-access way.
const modelRegistry = new ModelRegistry()

// Call the register functions to add a kind of factory to the model registry.
// This will also add any adapters we've created for the factory, allowing it
// to be created on the server.
registerSimpleProduct( modelRegistry );

// Before you can use the included API adapter you need to initialize it using one of the utility methods.
// If you do not initialize the API adapters they will not be able to make requests to the API.
// Note that these utility functions only set up adapters that have been registered already
// and so further calls to `registeryXXX` functions will have adapters that aren't ready.
initializeUsingBasicAuth( modelRegistry, 'https://test.test/wp-json', 'admin', 'password' );
initializeUsingOAuth( modelRegistry, 'https://test.test/wp-json', 'consumer_key', 'consumer_secret' );

// In order to actually create the models on the server, each registered factory must have an adapter set.
// You can do this on a per-factory basis using
modelRegistry.changeFactoryAdapter( SimpleProduct, AdapterTypes.API );
// You can do this to all factories registered using
modelRegistry.changeAllFactoryAdapters( AdapterTypes.API );

// Once all of the initialization has been taken care of you can create models!
// Any fields that are not defined will be filled out by random data.
const product = await modelRegistry.getFactory( SimpleProduct ).create( { name: 'Test Product' } );
// You can now access the ID of the created model using `product.id`!

// You can also create models in bulk!
const poducts = await modelRegistry.getFactory( SimpleProduct ).createList( 5 );
// You now have an array of products to work with!

Custom Models

Custom Adapters