add base variable product support to api package

This commit is contained in:
Ron Rennick 2021-02-26 17:07:40 -04:00
parent 533feb7713
commit e677380094
7 changed files with 311 additions and 3 deletions

View File

@ -2,3 +2,4 @@ export * from './abstract';
export * from './shared';
export * from './simple-product';
export * from './variation';
export * from './variable-product';

View File

@ -70,9 +70,9 @@ export class ProductDownload {
}
/**
* A product's attributes.
* Attribute base class.
*/
export class ProductAttribute {
export abstract class AbstractAttribute {
/**
* The ID of the attribute.
*
@ -86,7 +86,12 @@ export class ProductAttribute {
* @type {string}
*/
public readonly name: string = '';
}
/**
* A product's attributes.
*/
export class ProductAttribute extends AbstractAttribute {
/**
* The sort order of the attribute.
*
@ -121,6 +126,29 @@ export class ProductAttribute {
* @param {Partial.<ProductAttribute>} properties The properties to set.
*/
public constructor( properties?: Partial< ProductAttribute > ) {
super();
Object.assign( this, properties );
}
}
/**
* Default attributes for variable products.
*/
export class ProductDefaultAttribute extends AbstractAttribute {
/**
* The option selected for the attribute.
*
* @type {string}
*/
public readonly option: string = '';
/**
* Creates a new product default attribute.
*
* @param {Partial.<ProductDefaultAttribute>} properties The properties to set.
*/
public constructor( properties?: Partial< ProductDefaultAttribute > ) {
super();
Object.assign( this, properties );
}
}

View File

@ -69,4 +69,4 @@ export type ProductDeliveryUpdateParams = 'daysToDownload' | 'downloadLimit' | '
/**
* Properties exclusive to the Variable product type.
*/
export type ProductVariableTypeUpdateParams = 'defaultAttributes' | 'variations';
export type ProductVariableUpdateParams = 'defaultAttributes' | 'variations';

View File

@ -0,0 +1,173 @@
import {
AbstractProduct,
IProductCommon,
IProductCrossSells,
IProductInventory,
IProductSalesTax,
IProductShipping,
IProductUpSells,
ProductSearchParams,
} from './abstract';
import {
ProductInventoryUpdateParams,
ProductCommonUpdateParams,
ProductSalesTaxUpdateParams,
ProductCrossUpdateParams,
ProductShippingUpdateParams,
ProductUpSellUpdateParams,
ProductVariableUpdateParams,
StockStatus,
BackorderStatus,
Taxability, ProductDefaultAttribute,
} from './shared';
import { ProductVariation } from './variation';
import { HTTPClient } from '../../http';
import { variableProductRESTRepository } from '../../repositories';
import {
CreatesModels,
DeletesModels,
ListsModels,
ModelRepositoryParams,
ReadsModels,
UpdatesModels,
} from '../../framework';
/**
* The parameters that variable products can update.
*/
type VariableProductUpdateParams = ProductVariableUpdateParams
& ProductCommonUpdateParams
& ProductCrossUpdateParams
& ProductInventoryUpdateParams
& ProductSalesTaxUpdateParams
& ProductShippingUpdateParams
& ProductUpSellUpdateParams;
/**
* The parameters embedded in this generic can be used in the ModelRepository in order to give
* type-safety in an incredibly granular way.
*/
export type VariableProductRepositoryParams =
ModelRepositoryParams< VariableProduct, never, ProductSearchParams, VariableProductUpdateParams >;
/**
* An interface for listing variable products using the repository.
*
* @typedef ListsVariableProducts
* @alias ListsModels.<VariableProduct>
*/
export type ListsVariableProducts = ListsModels< VariableProductRepositoryParams >;
/**
* An interface for creating variable products using the repository.
*
* @typedef CreatesVariableProducts
* @alias CreatesModels.<VariableProduct>
*/
export type CreatesVariableProducts = CreatesModels< VariableProductRepositoryParams >;
/**
* An interface for reading variable products using the repository.
*
* @typedef ReadsVariableProducts
* @alias ReadsModels.<VariableProduct>
*/
export type ReadsVariableProducts = ReadsModels< VariableProductRepositoryParams >;
/**
* An interface for updating variable products using the repository.
*
* @typedef UpdatesVariableProducts
* @alias UpdatesModels.<VariableProduct>
*/
export type UpdatesVariableProducts = UpdatesModels< VariableProductRepositoryParams >;
/**
* An interface for deleting variable products using the repository.
*
* @typedef DeletesVariableProducts
* @alias DeletesModels.<VariableProduct>
*/
export type DeletesVariableProducts = DeletesModels< VariableProductRepositoryParams >;
/**
* The base for the Variable product object.
*/
export class VariableProduct extends AbstractProduct implements
IProductCommon,
IProductCrossSells,
IProductInventory,
IProductSalesTax,
IProductShipping,
IProductUpSells {
/**
* @see ./abstracts/cross-sells.ts
*/
public readonly crossSellIds: Array<number> = [];
/**
* @see ./abstracts/upsell.ts
*/
public readonly upSellIds: Array<number> = [];
/**
* @see ./abstracts/inventory.ts
*/
public readonly onePerOrder: boolean = false;
public readonly trackInventory: boolean = false;
public readonly remainingStock: number = -1;
public readonly stockStatus: StockStatus = ''
public readonly backorderStatus: BackorderStatus = BackorderStatus.Allowed;
public readonly canBackorder: boolean = false;
public readonly isOnBackorder: boolean = false;
/**
* @see ./abstracts/sales-tax.ts
*/
public readonly taxStatus: Taxability = Taxability.ProductAndShipping;
public readonly taxClass: string = '';
/**
* @see ./abstracts/shipping.ts
*/
public readonly weight: string = '';
public readonly length: string = '';
public readonly width: string = '';
public readonly height: string = '';
public readonly requiresShipping: boolean = false;
public readonly isShippingTaxable: boolean = false;
public readonly shippingClass: string = '';
public readonly shippingClassId: number = 0;
/**
* Default product attributes.
*
* @type {ReadonlyArray.<ProductDefaultAttribute>}
*/
public readonly defaultAttributes: readonly ProductDefaultAttribute[] = [];
/**
* Product variations.
*
* @type {ReadonlyArray.<ProductVariation>}
*/
public readonly variations: readonly ProductVariation[] = [];
/**
* Creates a new Variable product instance with the given properties
*
* @param {Object} properties The properties to set in the object.
*/
public constructor( properties?: Partial< VariableProduct > ) {
super();
Object.assign( this, properties );
}
/**
* Creates a model repository configured for communicating via the REST API.
*
* @param {HTTPClient} httpClient The client for communicating via HTTP.
*/
public static restRepository( httpClient: HTTPClient ): ReturnType< typeof variableProductRESTRepository > {
return variableProductRESTRepository( httpClient );
}
}

View File

@ -1,7 +1,9 @@
import { createProductTransformer } from './shared';
import { simpleProductRESTRepository } from './simple-product';
import { variableProductRESTRepository } from './variable-product';
export {
createProductTransformer,
simpleProductRESTRepository,
variableProductRESTRepository,
};

View File

@ -24,6 +24,7 @@ import {
ProductDownload,
ProductImage,
ProductTerm,
VariableProduct,
} from '../../../models';
import { createMetaDataTransformer } from '../shared';
@ -378,3 +379,26 @@ export function createProductShippingTransformation(): ModelTransformation[] {
return transformations;
}
/**
* Variable product specific properties transformations
*/
export function createProductVariableTransformation(): ModelTransformation[] {
const transformations = [
new PropertyTypeTransformation(
{
id: PropertyType.Integer,
name: PropertyType.String,
option: PropertyType.String,
variations: PropertyType.Integer,
},
),
new KeyChangeTransformation< VariableProduct >(
{
defaultAttributes: 'default_attributes',
},
),
];
return transformations;
}

View File

@ -0,0 +1,80 @@
import { HTTPClient } from '../../../http';
import { ModelRepository } from '../../../framework';
import {
VariableProduct,
ModelID,
CreatesVariableProducts,
DeletesVariableProducts,
ListsVariableProducts,
ReadsVariableProducts,
VariableProductRepositoryParams,
UpdatesVariableProducts,
} from '../../../models';
import {
createProductTransformer,
createProductCrossSellsTransformation,
createProductInventoryTransformation,
createProductSalesTaxTransformation,
createProductShippingTransformation,
createProductUpSellsTransformation,
createProductVariableTransformation,
} from './shared';
// @todo add child rest methods
import {
restCreate,
restDelete,
// restDeleteChild,
restList,
// restListChild,
restRead,
// restReadChild,
restUpdate,
// restUpdateChild,
} from '../shared';
/**
* Creates a new ModelRepository instance for interacting with models via the REST API.
*
* @param {HTTPClient} httpClient The HTTP client for the REST requests to be made using.
* @return {
* ListsVariableProducts|
* CreatesVariableProducts|
* ReadsVariableProducts|
* UpdatesVariableProducts|
* DeletesVariableProducts
* } The created repository.
*/
export function variableProductRESTRepository( httpClient: HTTPClient ): ListsVariableProducts
& CreatesVariableProducts
& ReadsVariableProducts
& UpdatesVariableProducts
& DeletesVariableProducts {
// @todo child url function
const buildURL = ( id: ModelID ) => '/wc/v3/products/' + id;
const crossSells = createProductCrossSellsTransformation();
const inventory = createProductInventoryTransformation();
const salesTax = createProductSalesTaxTransformation();
const shipping = createProductShippingTransformation();
const upsells = createProductUpSellsTransformation();
const variable = createProductVariableTransformation();
const transformations = [
...crossSells,
...inventory,
...salesTax,
...shipping,
...upsells,
...variable,
];
const transformer = createProductTransformer<VariableProduct>( 'variable', transformations );
// @todo create and add variation repository params
return new ModelRepository(
restList< VariableProductRepositoryParams >( () => '/wc/v3/products', VariableProduct, httpClient, transformer ),
restCreate< VariableProductRepositoryParams >( () => '/wc/v3/products', VariableProduct, httpClient, transformer ),
restRead< VariableProductRepositoryParams >( buildURL, VariableProduct, httpClient, transformer ),
restUpdate< VariableProductRepositoryParams >( buildURL, VariableProduct, httpClient, transformer ),
restDelete< VariableProductRepositoryParams >( buildURL, httpClient ),
);
}