2020-07-01 19:08:08 +00:00
|
|
|
import { DeepPartial } from 'fishery';
|
|
|
|
import { Product } from './product';
|
|
|
|
import { AdapterTypes, ModelRegistry } from '../framework/model-registry';
|
|
|
|
import { ModelFactory } from '../framework/model-factory';
|
2020-07-02 05:07:27 +00:00
|
|
|
import { APIAdapter } from '../framework/api/api-adapter';
|
2020-07-02 20:33:30 +00:00
|
|
|
import faker from 'faker/locale/en';
|
2020-07-01 19:08:08 +00:00
|
|
|
|
|
|
|
export class SimpleProduct extends Product {
|
2020-09-04 18:27:34 +00:00
|
|
|
public constructor( partial: DeepPartial< SimpleProduct > = {} ) {
|
2020-07-01 19:08:08 +00:00
|
|
|
super( partial );
|
|
|
|
Object.assign( this, partial );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the simple product factory and adapters.
|
|
|
|
*
|
|
|
|
* @param {ModelRegistry} registry The registry to hold the model reference.
|
|
|
|
*/
|
2020-07-01 20:58:12 +00:00
|
|
|
export function registerSimpleProduct( registry: ModelRegistry ): void {
|
2020-07-01 19:08:08 +00:00
|
|
|
if ( null !== registry.getFactory( SimpleProduct ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:27:34 +00:00
|
|
|
const factory = ModelFactory.define< SimpleProduct, any, ModelFactory< SimpleProduct >>(
|
2020-07-01 19:08:08 +00:00
|
|
|
( { params } ) => {
|
2020-07-02 20:33:30 +00:00
|
|
|
return new SimpleProduct(
|
|
|
|
{
|
|
|
|
name: params.name ?? faker.commerce.productName(),
|
|
|
|
regularPrice: params.regularPrice ?? faker.commerce.price(),
|
|
|
|
},
|
|
|
|
);
|
2020-07-01 19:08:08 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
registry.registerFactory( SimpleProduct, factory );
|
|
|
|
|
2020-09-04 18:27:34 +00:00
|
|
|
const apiAdapter = new APIAdapter< SimpleProduct >(
|
2020-07-01 19:08:08 +00:00
|
|
|
'/wc/v3/products',
|
|
|
|
( model ) => {
|
|
|
|
return {
|
|
|
|
type: 'simple',
|
2020-07-02 17:56:21 +00:00
|
|
|
name: model.name,
|
|
|
|
regular_price: model.regularPrice,
|
2020-07-01 19:08:08 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
registry.registerAdapter( SimpleProduct, AdapterTypes.API, apiAdapter );
|
|
|
|
}
|