wc-store-api-types
v0.1.0
Published
TypeScript types and Zod schemas for WooCommerce Store API
Downloads
116
Maintainers
Readme
wc-store-api-types
TypeScript types and Zod schemas for the WooCommerce Store API.
Installation
npm install wc-store-api-typesUsage
TypeScript Types
import type {
ProductResponse,
CartResponse,
CheckoutRequest,
CheckoutResponse,
} from 'wc-store-api-types'
// Use types for API responses
async function fetchProducts(): Promise<ProductResponse[]> {
const response = await fetch('/wp-json/wc/store/v1/products')
return response.json()
}Zod Schema Validation
import {
ProductResponseSchema,
CartResponseSchema,
CheckoutRequestSchema,
} from 'wc-store-api-types'
// Validate API responses at runtime
const response = await fetch('/wp-json/wc/store/v1/products/123')
const data = await response.json()
const product = ProductResponseSchema.parse(data) // Throws if invalid
// Safe parse (doesn't throw)
const result = ProductResponseSchema.safeParse(data)
if (result.success) {
console.log(result.data.name)
} else {
console.error(result.error.issues)
}Validate Request Bodies
import { CheckoutRequestSchema } from 'wc-store-api-types'
const checkoutData = {
billing_address: {
first_name: 'John',
last_name: 'Doe',
email: '[email protected]',
address_1: '123 Main St',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
},
payment_method: 'cod',
}
// Validate before sending
const validatedData = CheckoutRequestSchema.parse(checkoutData)Available Types
Product Types
ProductResponse- Full product objectProductsResponse- Array of productsProductsRequest- Query parameters for product listingsProductCategoryResponse- Product categoryProductBrandResponse- Product brandProductAttributeResponse- Product attributeAttributeTermResponse- Attribute termProductReviewResponse- Product reviewCollectionDataResponse- Aggregated collection data
Cart Types
CartResponse- Full cart objectCartItemResponse- Cart line itemAddItemRequest- Add item to cart requestUpdateItemRequest- Update cart item requestRemoveItemRequest- Remove cart item requestApplyCouponRequest- Apply coupon requestRemoveCouponRequest- Remove coupon requestSelectShippingRateRequest- Select shipping rate requestUpdateCustomerRequest- Update customer info request
Checkout Types
CheckoutRequest- Place order requestCheckoutResponse- Checkout/order responseCheckoutUpdateRequest- Update draft order requestPaymentResult- Payment result object
Address Types
BillingAddress- Billing addressShippingAddress- Shipping addressBaseAddress- Base address fields
Shipping Types
ShippingPackage- Shipping package with ratesShippingRate- Individual shipping rate
Common Types
CurrencyResponse- Currency informationImageResponse- Image attachmentMonetaryValue- String representing monetary valueStockStatus- Stock status enum
Auto-generating Types from API
This package includes scripts to generate types from live WooCommerce instances.
Fetch JSON Schemas
# Fetch from default sites (sites.json)
npm run generate:schemas
# Fetch from specific site
npm run generate:schemas -- --site https://example.comGenerate TypeScript from Schemas
npm run generate:typesCompare Generated vs Manual Types
npm run compareTesting
Tests validate schemas against real WooCommerce API responses.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watchConfigure test sites in sites.json:
{
"sites": [
"https://woocommerce.com",
"https://your-wc-store.com"
]
}Development
# Install dependencies
npm install
# Type check
npm run typecheck
# Build
npm run build
# Run tests
npm testLicense
MIT
