From 5016dc78bb372bd8bda9656cc9c86990b0d2014a Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Fri, 29 Aug 2025 00:35:13 +0530 Subject: [PATCH 1/2] feat: implement v2 order management functions and refactor WizCommerce class for versioning support --- src/index.ts | 195 +++++++----------------------------- src/order/v2/create.ts | 9 ++ src/order/v2/delete.ts | 8 ++ src/order/v2/get.ts | 9 ++ src/order/v2/index.ts | 7 ++ src/order/v2/list.ts | 9 ++ src/order/v2/patchStatus.ts | 10 ++ src/order/v2/types.ts | 172 +++++++++++++++++++++++++++++++ src/order/v2/update.ts | 10 ++ src/url.ts | 14 +++ src/wizcommerce-v1.ts | 156 +++++++++++++++++++++++++++++ src/wizcommerce-v2.ts | 22 ++++ 12 files changed, 464 insertions(+), 157 deletions(-) create mode 100644 src/order/v2/create.ts create mode 100644 src/order/v2/delete.ts create mode 100644 src/order/v2/get.ts create mode 100644 src/order/v2/index.ts create mode 100644 src/order/v2/list.ts create mode 100644 src/order/v2/patchStatus.ts create mode 100644 src/order/v2/types.ts create mode 100644 src/order/v2/update.ts create mode 100644 src/url.ts create mode 100644 src/wizcommerce-v1.ts create mode 100644 src/wizcommerce-v2.ts diff --git a/src/index.ts b/src/index.ts index 574a959..fb585fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,161 +1,42 @@ -import { HttpClient } from "./core/httpClient.js"; -import * as Customer from "./customer/index.js"; -import * as Product from "./product/index.js"; -import * as Order from "./order/index.js"; -import * as Invoice from "./invoice/index.js"; -import * as Attribute from "./attribute/index.js"; -import * as Inventory from "./inventory/index.js"; -import * as PaymentMethod from "./paymentmethod/index.js"; -import * as PriceList from "./pricelist/index.js"; -import * as ProductPricing from "./productpricing/index.js"; -import * as SalesRep from "./user/index.js"; -import * as Shipment from "./shipment/index.js"; +import { WizCommerceV1 } from "./wizcommerce-v1.js"; +import { WizCommerceV2 } from "./wizcommerce-v2.js"; +import { URL } from "./url.js"; export class WizCommerce { - static LOCAL = "http://localhost:8080/v1"; - static STAGING = "https://api-staging.sourcerer.tech/v1"; - static PRODUCTION = "https://api.yourdomain.com/v1"; - - private client: HttpClient; - - constructor(env: string, apiKey: string) { - this.client = new HttpClient(apiKey, env); + static LOCAL = URL.local; + static STAGING = URL.staging; + static API = URL.production; + + customer: any; + product: any; + order: any; + invoice: any; + attribute: any; + inventory: any; + paymentmethod: any; + pricelist: any; + productpricing: any; + salesrep: any; + shipment: any; + + constructor(baseUrl: string, apiKey: string) { + if (/\/v2\b/.test(baseUrl)) { + const instance = new WizCommerceV2(baseUrl, apiKey); + this.order = instance.order; + // All other properties remain undefined for v2 + } else { + const instance = new WizCommerceV1(baseUrl, apiKey); + this.customer = instance.customer; + this.product = instance.product; + this.order = instance.order; + this.invoice = instance.invoice; + this.attribute = instance.attribute; + this.inventory = instance.inventory; + this.paymentmethod = instance.paymentmethod; + this.pricelist = instance.pricelist; + this.productpricing = instance.productpricing; + this.salesrep = instance.salesrep; + this.shipment = instance.shipment; + } } - - customer = { - list: (params?: Customer.CustomerListRequest) => - Customer.list(this.client, params), - get: (id: string) => Customer.get(this.client, id), - create: (data: Customer.CustomerCreateRequest) => - Customer.create(this.client, data), - update: (id: string, data: Partial) => - Customer.update(this.client, id, data), - delete: (id: string) => Customer.delete(this.client, id), - address: { - list: ( - customerId: string, - params?: Customer.address.AddressListRequest - ) => Customer.address.list(this.client, customerId, params), - get: (customerId: string, addressId: string) => - Customer.address.get(this.client, customerId, addressId), - create: ( - customerId: string, - data: Customer.address.AddressCreateRequest - ) => Customer.address.create(this.client, customerId, data), - update: ( - customerId: string, - addressId: string, - data: Customer.address.AddressUpdateRequest - ) => Customer.address.update(this.client, customerId, addressId, data), - delete: (customerId: string, addressId: string) => - Customer.address.delete(this.client, customerId, addressId), - }, - contact: { - list: ( - customerId: string, - params?: Customer.contact.ContactListRequest - ) => Customer.contact.list(this.client, customerId, params), - get: (customerId: string, contactId: string) => - Customer.contact.get(this.client, customerId, contactId), - create: ( - customerId: string, - data: Customer.contact.ContactCreateRequest - ) => Customer.contact.create(this.client, customerId, data), - update: ( - customerId: string, - contactId: string, - data: Customer.contact.ContactUpdateRequest - ) => Customer.contact.update(this.client, customerId, contactId, data), - delete: (customerId: string, contactId: string) => - Customer.contact.delete(this.client, customerId, contactId), - }, - }; - - product = { - list: (params?: Product.ProductListRequest) => - Product.list(this.client, params), - get: (id: string) => Product.get(this.client, id), - create: (data: Product.ProductCreateRequest) => - Product.create(this.client, data), - update: (id: string, data: Product.ProductUpdateRequest) => - Product.update(this.client, id, data), - delete: (id: string) => Product.delete(this.client, id), - }; - - order = { - list: (params?: Order.OrderListRequest) => Order.list(this.client, params), - get: (id: string) => Order.get(this.client, id), - create: (data: Order.OrderCreateRequest) => Order.create(this.client, data), - update: (id: string, data: Order.OrderUpdateRequest) => - Order.update(this.client, id, data), - delete: (id: string) => Order.delete(this.client, id), - patchStatus: (id: string, data: Order.OrderPatchStatusRequest) => - Order.patchStatus(this.client, id, data), - }; - - invoice = { - list: (params?: Invoice.InvoiceListParams) => - Invoice.list(this.client, params), - get: (id: string) => Invoice.get(this.client, id), - create: (data: Invoice.InvoiceCreateRequest) => - Invoice.create(this.client, data), - update: (id: string, data: Invoice.InvoiceUpdateRequest) => - Invoice.update(this.client, id, data), - delete: (id: string) => Invoice.delete(this.client, id), - }; - - attribute = { - list: (params?: Attribute.AttributeRequest) => - Attribute.list(this.client, params), - }; - - inventory = { - list: (params?: Inventory.InventoryRequest) => - Inventory.list(this.client, params), - update: (data: Inventory.InventoryCreateRequest) => - Inventory.update(this.client, data), - }; - - paymentmethod = { - list: (params?: PaymentMethod.PaymentMethodRequest) => - PaymentMethod.list(this.client, params), - create: (data: PaymentMethod.PaymentMethodCreateRequest) => - PaymentMethod.create(this.client, data), - delete: (id: string) => PaymentMethod.delete(this.client, id), - }; - - pricelist = { - list: (params?: PriceList.PriceListRequest) => - PriceList.list(this.client, params), - get: (id: string) => PriceList.get(this.client, id), - create: (data: PriceList.PriceListCreateRequest) => - PriceList.create(this.client, data), - update: (id: string, data: PriceList.PriceListUpdateRequest) => - PriceList.update(this.client, id, data), - delete: (id: string) => PriceList.delete(this.client, id), - }; - - productpricing = { - list: (id: string, params?: ProductPricing.ProductPriceRequest) => - ProductPricing.list(this.client, id, params), - update: (id: string, data: ProductPricing.ProductPriceRequest) => - ProductPricing.update(this.client, id, data), - }; - - salesrep = { - list: (params?: SalesRep.SalesRepListRequest) => - SalesRep.list(this.client, params), - }; - - shipment = { - list: (params?: Shipment.ShipmentRequest) => - Shipment.list(this.client, params), - get: (params: any) => Shipment.get(this.client, params), - create: (data: Shipment.ShipmentCreateRequest) => - Shipment.create(this.client, data), - update: (id: string, data: Shipment.ShipmentUpdateRequest) => - Shipment.update(this.client, id, data), - delete: (params: Shipment.ShipmentPathParams) => - Shipment.delete(this.client, params), - }; } diff --git a/src/order/v2/create.ts b/src/order/v2/create.ts new file mode 100644 index 0000000..c84c3da --- /dev/null +++ b/src/order/v2/create.ts @@ -0,0 +1,9 @@ +import { HttpClient } from "../../core/httpClient.js"; +import { OrderCreateRequest, Order } from "./types.js"; + +export async function create( + client: HttpClient, + data: OrderCreateRequest +): Promise { + return client.post("/orders", data); +} diff --git a/src/order/v2/delete.ts b/src/order/v2/delete.ts new file mode 100644 index 0000000..d0297fe --- /dev/null +++ b/src/order/v2/delete.ts @@ -0,0 +1,8 @@ +import { HttpClient } from "../../core/httpClient.js"; + +export async function deleteOrder( + client: HttpClient, + orderId: string +): Promise { + return client.delete(`/orders/${orderId}`); +} diff --git a/src/order/v2/get.ts b/src/order/v2/get.ts new file mode 100644 index 0000000..b33e657 --- /dev/null +++ b/src/order/v2/get.ts @@ -0,0 +1,9 @@ +import { HttpClient } from "../../core/httpClient.js"; +import { Order } from "./types"; + +export async function get( + client: HttpClient, + orderId: string +): Promise { + return client.get(`/orders/${orderId}`); +} \ No newline at end of file diff --git a/src/order/v2/index.ts b/src/order/v2/index.ts new file mode 100644 index 0000000..a463e57 --- /dev/null +++ b/src/order/v2/index.ts @@ -0,0 +1,7 @@ +export { list } from "./list.js"; +export { get } from "./get.js"; +export { create } from "./create.js"; +export { update } from "./update.js"; +export { deleteOrder as delete } from "./delete.js"; +export { patchStatus } from "./patchStatus.js"; +export * from "./types.js"; diff --git a/src/order/v2/list.ts b/src/order/v2/list.ts new file mode 100644 index 0000000..4fcc34e --- /dev/null +++ b/src/order/v2/list.ts @@ -0,0 +1,9 @@ +import { HttpClient } from "../../core/httpClient.js"; +import { OrderListRequest, Order } from "./types.js"; + +export async function list( + client: HttpClient, + params?: OrderListRequest +): Promise { + return client.get("/orders", params); +} \ No newline at end of file diff --git a/src/order/v2/patchStatus.ts b/src/order/v2/patchStatus.ts new file mode 100644 index 0000000..4d60cc0 --- /dev/null +++ b/src/order/v2/patchStatus.ts @@ -0,0 +1,10 @@ +import { HttpClient } from "../../core/httpClient.js"; +import { OrderPatchStatusRequest, Order } from "./types.js"; + +export async function patchStatus( + client: HttpClient, + orderId: string, + data: OrderPatchStatusRequest +): Promise { + return client.patch(`/orders/${orderId}/status`, data); +} \ No newline at end of file diff --git a/src/order/v2/types.ts b/src/order/v2/types.ts new file mode 100644 index 0000000..44ac322 --- /dev/null +++ b/src/order/v2/types.ts @@ -0,0 +1,172 @@ +import { Pagination } from "../../core/types.js"; +export interface Order { + id: string; + referenceId?: string; + displayId?: string; + customerId: string; + status?: "draft" | "confirmed" | "cancelled"; + amount?: number; + amountPaid?: number; + paymentStatus?: string; + fulfillmentStatus?: string; + shipmentDate?: string; + dueDate?: string; + createdAt?: string; + updatedAt?: string; + paymentTerm?: string; + shippingMethod?: string; + freightTerm?: string; + discountValue?: number; + taxValue?: number; + shippingCharge?: number; + customerNote?: string; + internalNote?: string; + purchaseOrderNumber?: string; + source?: string; + priceListId?: string; + paymentMethodId?: string; + billingAddress?: OrderAddress; + shippingAddress?: OrderAddress; + primaryContact?: OrderContact; + containerInfo?: ContainerInfo; + customerConsent?: CustomerConsent; + attributes?: Attribute[]; + additionalCharges?: OrderCharge[]; + charges?: OrderCharge[]; + lineItems: OrderLineItem[]; + customerReferenceId?: string; + customerName?: string; + card?: PaymentCard; +} + +export interface OrderLineItem { + /** + * Product ID (JSON: "product_id") + */ + productId?: string; + sku: string; + quantity: number; + unitPrice: number; + shippedQuantity?: number; + invoicedQuantity?: number; + cancelledQuantity?: number; + pickedQuantity?: number; + discount?: number; + discountType?: "percentage" | "value"; + isAdhoc?: boolean; + note?: string; +} + +export interface OrderCharge { + name: string; + type: string; + valueType?: "percentage" | "value"; + amount: number; +} + +export interface CustomerConsent { + hasConsent: boolean; + consentFileId: string; +} + +export interface ContainerInfo { + name?: string; + key: string; + unit: string; + volume: number; +} + +export interface OrderAddress { + id?: string; + referenceId?: string; + firstName?: string; + lastName?: string; + phone?: string; + email?: string; + addressLine1?: string; + addressLine2?: string; + city?: string; + state?: string; + country?: string; + zipCode?: string; + attributes?: Attribute[]; +} + +export interface OrderContact { + id?: string; + firstName?: string; + lastName?: string; + email?: string; + phone?: string; + designation?: string; +} + +export interface Attribute { + name: string; + value: string; +} + +export interface PaymentCard { + id: string; + token: string; + paymentMethodType: string; + brand: string; + cardType: string; + lastFourDigits: string; + cardExp: string; + externalCustomerId: string; +} + +export interface OrderListRequest extends Pagination { + sortBy?: "created_at" | "updated_at"; + sort?: "asc" | "desc"; + referenceIds?: string[]; + orderStatus?: "draft" | "confirmed" | "cancelled"; + source?: string; + createdAfter?: string; + createdBefore?: string; + updatedAfter?: string; + updatedBefore?: string; +} + +export interface OrderCreateRequest { + referenceId: string; + displayId?: string; + customerId: string; + paymentTerm?: string; + shippingMethod?: string; + freightTerm?: string; + amount?: number; + amountPaid?: number; + discountValue?: number; + taxValue?: number; + shippingCharge?: number; + shipmentDate?: string; + dueDate?: string; + paymentStatus?: string; + paymentMethodId?: string; + fulfillmentStatus?: string; + createdAt?: string; + updatedAt?: string; + customerNote?: string; + internalNote?: string; + status?: "draft" | "confirmed"; + source?: string; + purchaseOrderNumber?: string; + customerConsent?: CustomerConsent; + containerInfo?: ContainerInfo; + billingAddress?: OrderAddress; + shippingAddress?: OrderAddress; + primaryContact?: OrderContact; + lineItems: OrderLineItem[]; + attributes?: Attribute[]; + additionalCharges?: OrderCharge[]; +} + +export interface OrderUpdateRequest extends Partial> { + lineItems?: OrderLineItem[]; +} + +export interface OrderPatchStatusRequest { + status: "draft" | "confirmed" | "cancelled"; +} diff --git a/src/order/v2/update.ts b/src/order/v2/update.ts new file mode 100644 index 0000000..0d2bdfa --- /dev/null +++ b/src/order/v2/update.ts @@ -0,0 +1,10 @@ +import { HttpClient } from "../../core/httpClient.js"; +import { OrderUpdateRequest, Order } from "./types.js"; + +export async function update( + client: HttpClient, + orderId: string, + data: OrderUpdateRequest +): Promise { + return client.put(`/orders/${orderId}`, data); +} \ No newline at end of file diff --git a/src/url.ts b/src/url.ts new file mode 100644 index 0000000..22b4b28 --- /dev/null +++ b/src/url.ts @@ -0,0 +1,14 @@ +export const URL = { + local: { + V1: "http://localhost:8080/v1", + V2: "http://localhost:8080/v2", + }, + staging: { + V1: "https://api-staging.sourcerer.tech/v1", + V2: "https://api-staging.sourcerer.tech/v2", + }, + production: { + V1: "https://api.wizcommerce.com/v1", + V2: "https://api.wizcommerce.com/v2", + }, +} as const; \ No newline at end of file diff --git a/src/wizcommerce-v1.ts b/src/wizcommerce-v1.ts new file mode 100644 index 0000000..79af472 --- /dev/null +++ b/src/wizcommerce-v1.ts @@ -0,0 +1,156 @@ +import { HttpClient } from "./core/httpClient.js"; +import * as Customer from "./customer/index.js"; +import * as Product from "./product/index.js"; +import * as Order from "./order/index.js"; +import * as Invoice from "./invoice/index.js"; +import * as Attribute from "./attribute/index.js"; +import * as Inventory from "./inventory/index.js"; +import * as PaymentMethod from "./paymentmethod/index.js"; +import * as PriceList from "./pricelist/index.js"; +import * as ProductPricing from "./productpricing/index.js"; +import * as SalesRep from "./user/index.js"; +import * as Shipment from "./shipment/index.js"; + +export class WizCommerceV1 { + public client: HttpClient; + constructor(baseUrl: string, apiKey: string) { + this.client = new HttpClient(apiKey, baseUrl); + } + + customer = { + list: (params?: Customer.CustomerListRequest) => + Customer.list(this.client, params), + get: (id: string) => Customer.get(this.client, id), + create: (data: Customer.CustomerCreateRequest) => + Customer.create(this.client, data), + update: (id: string, data: Partial) => + Customer.update(this.client, id, data), + delete: (id: string) => Customer.delete(this.client, id), + address: { + list: ( + customerId: string, + params?: Customer.address.AddressListRequest + ) => Customer.address.list(this.client, customerId, params), + get: (customerId: string, addressId: string) => + Customer.address.get(this.client, customerId, addressId), + create: ( + customerId: string, + data: Customer.address.AddressCreateRequest + ) => Customer.address.create(this.client, customerId, data), + update: ( + customerId: string, + addressId: string, + data: Customer.address.AddressUpdateRequest + ) => Customer.address.update(this.client, customerId, addressId, data), + delete: (customerId: string, addressId: string) => + Customer.address.delete(this.client, customerId, addressId), + }, + contact: { + list: ( + customerId: string, + params?: Customer.contact.ContactListRequest + ) => Customer.contact.list(this.client, customerId, params), + get: (customerId: string, contactId: string) => + Customer.contact.get(this.client, customerId, contactId), + create: ( + customerId: string, + data: Customer.contact.ContactCreateRequest + ) => Customer.contact.create(this.client, customerId, data), + update: ( + customerId: string, + contactId: string, + data: Customer.contact.ContactUpdateRequest + ) => Customer.contact.update(this.client, customerId, contactId, data), + delete: (customerId: string, contactId: string) => + Customer.contact.delete(this.client, customerId, contactId), + }, + }; + + product = { + list: (params?: Product.ProductListRequest) => + Product.list(this.client, params), + get: (id: string) => Product.get(this.client, id), + create: (data: Product.ProductCreateRequest) => + Product.create(this.client, data), + update: (id: string, data: Product.ProductUpdateRequest) => + Product.update(this.client, id, data), + delete: (id: string) => Product.delete(this.client, id), + }; + + order = { + list: (params?: Order.OrderListRequest) => Order.list(this.client, params), + get: (id: string) => Order.get(this.client, id), + create: (data: Order.OrderCreateRequest) => Order.create(this.client, data), + update: (id: string, data: Order.OrderUpdateRequest) => + Order.update(this.client, id, data), + delete: (id: string) => Order.delete(this.client, id), + patchStatus: (id: string, data: Order.OrderPatchStatusRequest) => + Order.patchStatus(this.client, id, data), + }; + + invoice = { + list: (params?: Invoice.InvoiceListParams) => + Invoice.list(this.client, params), + get: (id: string) => Invoice.get(this.client, id), + create: (data: Invoice.InvoiceCreateRequest) => + Invoice.create(this.client, data), + update: (id: string, data: Invoice.InvoiceUpdateRequest) => + Invoice.update(this.client, id, data), + delete: (id: string) => Invoice.delete(this.client, id), + }; + + attribute = { + list: (params?: Attribute.AttributeRequest) => + Attribute.list(this.client, params), + }; + + inventory = { + list: (params?: Inventory.InventoryRequest) => + Inventory.list(this.client, params), + update: (data: Inventory.InventoryCreateRequest) => + Inventory.update(this.client, data), + }; + + paymentmethod = { + list: (params?: PaymentMethod.PaymentMethodRequest) => + PaymentMethod.list(this.client, params), + create: (data: PaymentMethod.PaymentMethodCreateRequest) => + PaymentMethod.create(this.client, data), + delete: (id: string) => PaymentMethod.delete(this.client, id), + }; + + pricelist = { + list: (params?: PriceList.PriceListRequest) => + PriceList.list(this.client, params), + get: (id: string) => PriceList.get(this.client, id), + create: (data: PriceList.PriceListCreateRequest) => + PriceList.create(this.client, data), + update: (id: string, data: PriceList.PriceListUpdateRequest) => + PriceList.update(this.client, id, data), + delete: (id: string) => PriceList.delete(this.client, id), + }; + + productpricing = { + list: (id: string, params?: ProductPricing.ProductPriceRequest) => + ProductPricing.list(this.client, id, params), + update: (id: string, data: ProductPricing.ProductPriceRequest) => + ProductPricing.update(this.client, id, data), + }; + + salesrep = { + list: (params?: SalesRep.SalesRepListRequest) => + SalesRep.list(this.client, params), + }; + + shipment = { + list: (params?: Shipment.ShipmentRequest) => + Shipment.list(this.client, params), + get: (params: any) => Shipment.get(this.client, params), + create: (data: Shipment.ShipmentCreateRequest) => + Shipment.create(this.client, data), + update: (id: string, data: Shipment.ShipmentUpdateRequest) => + Shipment.update(this.client, id, data), + delete: (params: Shipment.ShipmentPathParams) => + Shipment.delete(this.client, params), + }; +} diff --git a/src/wizcommerce-v2.ts b/src/wizcommerce-v2.ts new file mode 100644 index 0000000..b46dbbf --- /dev/null +++ b/src/wizcommerce-v2.ts @@ -0,0 +1,22 @@ +import { HttpClient } from "./core/httpClient.js"; +import * as OrderV2 from "./order/v2/index.js"; + +export class WizCommerceV2 { + public client: HttpClient; + constructor(baseUrl: string, apiKey: string) { + this.client = new HttpClient(apiKey, baseUrl); + } + + order = { + list: (params?: OrderV2.OrderListRequest) => + OrderV2.list(this.client, params), + get: (id: string) => OrderV2.get(this.client, id), + create: (data: OrderV2.OrderCreateRequest) => + OrderV2.create(this.client, data), + update: (id: string, data: OrderV2.OrderUpdateRequest) => + OrderV2.update(this.client, id, data), + delete: (id: string) => OrderV2.delete(this.client, id), + patchStatus: (id: string, data: OrderV2.OrderPatchStatusRequest) => + OrderV2.patchStatus(this.client, id, data), + }; +} From e31ad0d313031cc386a97c7e8becd8ebe6427fc5 Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Fri, 29 Aug 2025 01:23:18 +0530 Subject: [PATCH 2/2] feat: add v2 order management support and refactor WizCommerce class for versioning --- README.md | 34 +--- docs/v2/Order.md | 421 +++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 49 +++++- src/url.ts | 2 +- 4 files changed, 473 insertions(+), 33 deletions(-) create mode 100644 docs/v2/Order.md diff --git a/README.md b/README.md index 78662ed..b1055ec 100644 --- a/README.md +++ b/README.md @@ -35,32 +35,16 @@ npm install wizcommerce ```ts import { WizCommerce } from "wizcommerce"; -const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); - -// List customers with pagination -wiz.customer.list({ page: 1, page_size: 20 }).then(console.log); - -// Create a customer -wiz.customer - .create({ - companyName: "Acme Inc.", - displayName: "Acme", - referenceId: "ACME-001", - email: "info@acme.com", - addresses: [ - /* ... */ - ], - contacts: [ - /* ... */ - ], - attributes: [ - /* ... */ - ], - }) - .then(console.log); +// For v1 endpoints (all resources) +const wizV1 = new WizCommerce(WizCommerce.API.V1, ""); +wizV1.customer.list({ page: 1, page_size: 20 }).then(console.log); + +// For v2 endpoints (only order resource) +const wizV2 = new WizCommerce(WizCommerce.API.V2, ""); +wizV2.order.list({ page: 1, page_size: 20 }).then(console.log); // Error handling example -wiz.customer.get("bad-id").catch((err) => { +wizV1.customer.get("bad-id").catch((err) => { console.error("API error:", err); }); ``` @@ -136,7 +120,7 @@ For detailed documentation, see [Product API Documentation](docs/Product.md). ### Order -For detailed documentation, see [Order API Documentation](docs/Order.md). +For detailed documentation, see [Order API Documentation](docs/Order.md) (v1) or [Order API Documentation (v2)](docs/v2/Order.md). - `wiz.order.list(params?)` - `wiz.order.get(id)` diff --git a/docs/v2/Order.md b/docs/v2/Order.md new file mode 100644 index 0000000..14d07a4 --- /dev/null +++ b/docs/v2/Order.md @@ -0,0 +1,421 @@ +# Order + +This module provides methods to work with order resources in the WizCommerce API. + +| Method | Description | +| ------------------------------- | -------------------- | +| [**list**](#list) | List all orders | +| [**get**](#get) | Get a specific order | +| [**create**](#create) | Create an order | +| [**update**](#update) | Update an order | +| [**delete**](#delete) | Delete an order | +| [**patchStatus**](#patchStatus) | Update order status | + +## list + +> list(params?: OrderListRequest): Promise + +List all orders + +### Example + +```typescript +import { WizCommerce } from "wizcommerce"; + +const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); + +// List orders with pagination +wiz.order + .list({ + page: 1, + page_size: 20, + sort_by: "created_at", + sort: "desc", + status: "new", + }) + .then((orders) => { + console.log(`Found ${orders.length} orders`); + console.log(orders); + }) + .catch((error) => { + console.error("Error listing orders:", error); + }); +``` + +### Parameters + +| Name | Type | Description | Required | +| ---------------- | ---------- | -------------------------- | -------- | +| **page** | **number** | Page number for pagination | No | +| **page_size** | **number** | Number of items per page | No | +| **sort_by** | **string** | Field to sort by | No | +| **sort** | **string** | Sort direction (asc/desc) | No | +| **status** | **string** | Filter by order status | No | +| **customer_id** | **string** | Filter by customer ID | No | +| **reference_id** | **string** | Filter by reference ID | No | + +### Response + +```json +[ + { + "id": "order_123456", + "customer_id": "cust_123456", + "reference_id": "ORD-001", + "order_number": "10001", + "status": "new", + "total_amount": 150.0, + "currency": "USD", + "payment_status": "pending", + "shipping_address": { + "id": "addr_789012", + "name": "John Doe", + "address_line1": "123 Main St" + // ... other address fields + }, + "billing_address": { + "id": "addr_345678", + "name": "John Doe", + "address_line1": "123 Main St" + // ... other address fields + }, + "items": [ + { + "id": "item_123456", + "product_id": "prod_123456", + "quantity": 2, + "price": 75.0 + // ... other item fields + } + ], + "created_at": "2023-01-15T10:30:00Z", + "updated_at": "2023-01-15T10:30:00Z" + } + // ... more orders +] +``` + +## get + +> get(id: string): Promise + +Get a specific order + +### Example + +```typescript +import { WizCommerce } from "wizcommerce"; + +const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); + +// Get a specific order +wiz.order + .get("order_123456") + .then((order) => { + console.log("Order details:", order); + }) + .catch((error) => { + console.error("Error fetching order:", error); + }); +``` + +### Parameters + +| Name | Type | Description | Required | +| ------ | ---------- | ----------- | -------- | +| **id** | **string** | Order ID | Yes | + +### Response + +```json +{ + "id": "order_123456", + "customer_id": "cust_123456", + "customer": { + "id": "cust_123456", + "name": "Acme Inc.", + "email": "info@acme.com", + "phone": "+1-415-555-1234" + }, + "reference_id": "ORD-001", + "order_number": "10001", + "status": "new", + "total_amount": 150.0, + "subtotal": 140.0, + "tax_amount": 10.0, + "shipping_amount": 0.0, + "discount_amount": 0.0, + "currency": "USD", + "payment_status": "pending", + "shipping_address": { + "id": "addr_789012", + "name": "John Doe", + "address_line1": "123 Main St", + "address_line2": "Suite 200", + "city": "San Francisco", + "state": "CA", + "country": "USA", + "zip_code": "94105", + "phone": "+1-415-555-5678" + }, + "billing_address": { + "id": "addr_345678", + "name": "John Doe", + "address_line1": "123 Main St", + "address_line2": "Suite 200", + "city": "San Francisco", + "state": "CA", + "country": "USA", + "zip_code": "94105", + "phone": "+1-415-555-5678" + }, + "items": [ + { + "id": "item_123456", + "product_id": "prod_123456", + "product_name": "Widget Pro", + "sku": "WID-PRO-001", + "quantity": 2, + "unit_price": 70.0, + "price": 140.0, + "tax_amount": 10.0, + "discount_amount": 0.0, + "attributes": [ + { + "name": "Color", + "value": "Red" + } + ] + } + ], + "notes": "Please deliver during business hours", + "attributes": [ + { + "name": "priority", + "value": "high" + } + ], + "created_at": "2023-01-15T10:30:00Z", + "updated_at": "2023-01-15T10:30:00Z" +} +``` + +## create + +> create(data: OrderCreateRequest): Promise + +Create a new order + +### Example + +```json +{ + "customer_id": "cust_123456", + "reference_id": "ORD-002", + "shipping_address_id": "addr_789012", + "billing_address_id": "addr_345678", + "currency": "USD", + "notes": "Please deliver during business hours", + "items": [ + { + "product_id": "prod_123456", + "quantity": 2, + "unit_price": 75.0, + "discount_amount": 0, + "notes": "Urgent delivery" + }, + { + "product_id": "prod_234567", + "quantity": 1, + "unit_price": 50.0, + "discount_amount": 5.0, + "notes": "" + } + ], + "attributes": [ + { + "name": "priority", + "value": "high" + } + ] +} +``` + +### Parameters + +| Name | Type | Description | Required | +| -------- | ---------------------- | ----------- | -------- | +| **data** | **OrderCreateRequest** | Order data | Yes | + +#### OrderCreateRequest + +| Name | Type | Description | Required | +| ----------------------- | ---------------------- | ------------------- | -------- | +| **customer_id** | **string** | Customer ID | Yes | +| **reference_id** | **string** | Unique reference ID | Yes | +| **shipping_address_id** | **string** | Shipping address ID | Yes | +| **billing_address_id** | **string** | Billing address ID | No | +| **currency** | **string** | Currency code | Yes | +| **notes** | **string** | Order notes | No | +| **items** | **OrderItemRequest[]** | Order items | Yes | +| **attributes** | **any[]** | Order attributes | No | + +#### OrderItemRequest + +| Name | Type | Description | Required | +| ------------------- | ---------- | --------------- | -------- | +| **product_id** | **string** | Product ID | Yes | +| **quantity** | **number** | Quantity | Yes | +| **unit_price** | **number** | Unit price | Yes | +| **discount_amount** | **number** | Discount amount | No | +| **notes** | **string** | Item notes | No | + +### Response + +Returns an [OrderDetailsResponse](#get) object on success. + +## update + +> update(id: string, data: OrderUpdateRequest): Promise + +Update an existing order + +### Example + +```typescript +import { WizCommerce } from "wizcommerce"; + +const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); + +// Update an existing order +const orderUpdates = { + notes: "Please deliver on Monday", + items: [ + { + id: "item_123456", + quantity: 3, + }, + ], +}; + +wiz.order + .update("order_123456", orderUpdates) + .then((updatedOrder) => { + console.log("Order updated:", updatedOrder); + }) + .catch((error) => { + console.error("Error updating order:", error); + }); +``` + +### Parameters + +| Name | Type | Description | Required | +| -------- | ---------------------- | ----------------- | -------- | +| **id** | **string** | Order ID | Yes | +| **data** | **OrderUpdateRequest** | Order update data | Yes | + +#### OrderUpdateRequest + +| Name | Type | Description | Required | +| ----------------------- | ---------------------------- | --------------------- | -------- | +| **reference_id** | **string** | Unique reference ID | No | +| **shipping_address_id** | **string** | Shipping address ID | No | +| **billing_address_id** | **string** | Billing address ID | No | +| **notes** | **string** | Order notes | No | +| **items** | **OrderItemUpdateRequest[]** | Order items to update | No | +| **attributes** | **any[]** | Order attributes | No | + +#### OrderItemUpdateRequest + +| Name | Type | Description | Required | +| ------------------- | ---------- | ---------------------------- | -------- | +| **id** | **string** | Item ID (for existing items) | No | +| **product_id** | **string** | Product ID (for new items) | No | +| **quantity** | **number** | Quantity | No | +| **unit_price** | **number** | Unit price | No | +| **discount_amount** | **number** | Discount amount | No | +| **notes** | **string** | Item notes | No | + +### Response + +Returns an [OrderDetailsResponse](#get) object on success. + +## delete + +> delete(id: string): Promise + +Delete an order + +### Example + +```typescript +import { WizCommerce } from "wizcommerce"; + +const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); + +// Delete an order +wiz.order + .delete("order_123456") + .then(() => { + console.log("Order deleted successfully"); + }) + .catch((error) => { + console.error("Error deleting order:", error); + }); +``` + +### Parameters + +| Name | Type | Description | Required | +| ------ | ---------- | ----------- | -------- | +| **id** | **string** | Order ID | Yes | + +### Response + +No content returned on success. + +## patchStatus + +> patchStatus(id: string, data: OrderStatusRequest): Promise + +Update an order's status + +### Example + +```typescript +import { WizCommerce } from "wizcommerce"; + +const wiz = new WizCommerce(WizCommerce.PRODUCTION, ""); + +// Update an order's status +const statusUpdate = { + status: "processing", +}; + +wiz.order + .patchStatus("order_123456", statusUpdate) + .then((updatedOrder) => { + console.log("Order status updated:", updatedOrder); + }) + .catch((error) => { + console.error("Error updating order status:", error); + }); +``` + +### Parameters + +| Name | Type | Description | Required | +| -------- | ---------------------- | ------------------ | -------- | +| **id** | **string** | Order ID | Yes | +| **data** | **OrderStatusRequest** | Status update data | Yes | + +#### OrderStatusRequest + +| Name | Type | Description | Required | +| ---------- | ---------- | ---------------- | -------- | +| **status** | **string** | New order status | Yes | + +### Response + +Returns an [OrderDetailsResponse](#get) object on success. diff --git a/src/index.ts b/src/index.ts index fb585fb..4156c5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,10 +2,15 @@ import { WizCommerceV1 } from "./wizcommerce-v1.js"; import { WizCommerceV2 } from "./wizcommerce-v2.js"; import { URL } from "./url.js"; +const VERSION_MAP = { + v1: WizCommerceV1, + v2: WizCommerceV2, +}; + export class WizCommerce { static LOCAL = URL.local; static STAGING = URL.staging; - static API = URL.production; + static API = URL.api; customer: any; product: any; @@ -20,12 +25,30 @@ export class WizCommerce { shipment: any; constructor(baseUrl: string, apiKey: string) { - if (/\/v2\b/.test(baseUrl)) { - const instance = new WizCommerceV2(baseUrl, apiKey); - this.order = instance.order; - // All other properties remain undefined for v2 - } else { - const instance = new WizCommerceV1(baseUrl, apiKey); + if (!apiKey || typeof apiKey !== "string" || apiKey.trim() === "") { + throw new Error( + "WizCommerce: API key is required and must be a non-empty string." + ); + } + if (!baseUrl || typeof baseUrl !== "string" || baseUrl.trim() === "") { + throw new Error( + "WizCommerce: baseUrl is required and must be a non-empty string." + ); + } + const versionMatch = baseUrl.match(/\/v(\d+)\b/); + if (!versionMatch) { + throw new Error( + "WizCommerce: baseUrl must end with /v1 or /v2 (e.g., https://api.wizcommerce.com/v1)" + ); + } + const versionKey = `v${versionMatch[1]}`; + const SDKClass = VERSION_MAP[versionKey as keyof typeof VERSION_MAP]; + if (!SDKClass) { + throw new Error(`WizCommerce: Unsupported API version '${versionKey}'.`); + } + const instance = new SDKClass(baseUrl, apiKey); + + if (instance instanceof WizCommerceV1) { this.customer = instance.customer; this.product = instance.product; this.order = instance.order; @@ -37,6 +60,18 @@ export class WizCommerce { this.productpricing = instance.productpricing; this.salesrep = instance.salesrep; this.shipment = instance.shipment; + } else if (instance instanceof WizCommerceV2) { + this.customer = undefined; + this.product = undefined; + this.order = instance.order; + this.invoice = undefined; + this.attribute = undefined; + this.inventory = undefined; + this.paymentmethod = undefined; + this.pricelist = undefined; + this.productpricing = undefined; + this.salesrep = undefined; + this.shipment = undefined; } } } diff --git a/src/url.ts b/src/url.ts index 22b4b28..f93edaf 100644 --- a/src/url.ts +++ b/src/url.ts @@ -7,7 +7,7 @@ export const URL = { V1: "https://api-staging.sourcerer.tech/v1", V2: "https://api-staging.sourcerer.tech/v2", }, - production: { + api: { V1: "https://api.wizcommerce.com/v1", V2: "https://api.wizcommerce.com/v2", },