Skip to content

Commit

Permalink
feat(woocomerce): adds productList loader
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriassuncx committed Nov 1, 2024
1 parent 2001618 commit 2bf920b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 7 deletions.
4 changes: 4 additions & 0 deletions woocommerce/loaders/product/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface Props {
slug: RequestURLParam;
}

/**
* @title WooCommerce Integration
* @description Product Details Page loader
*/
async function loader(
props: Props,
_req: Request,
Expand Down
88 changes: 88 additions & 0 deletions woocommerce/loaders/product/productList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Product } from "../../../commerce/types.ts";
import { AppContext } from "../../mod.ts";
import { toProduct } from "../../utils/transform.ts";
import { Order, OrderBy, Status, StockStatus } from "../../utils/types.ts";

export interface SearchProps {
search: string;
}

export interface ProductIDProps {
ids: string[];
}

export interface Props {
props: SearchProps | ProductIDProps;
/**
* @title Per Page
* @default 10
* @description Maximum number of items to be returned in result set. Default is 10.
*/
per_page?: number;
/**
* @description Order sort attribute ascending or descending. Default is desc.
*/
order?: Order;
/**
* @title Order By
* @description Sort collection by object attribute. Default is date.
*/
orderby?: OrderBy;
/**
* @title Status
* @description Limit result set to products assigned a specific status. Options: any, draft, pending, private and publish. Default is any.
*/
status?: Status;
/**
* @title Stock Status
* @description Limit result set to products with specified stock status. Default: instock.
*/
stock_status?: StockStatus;
/**
* @title Exclude IDs
* @description Ensure result set excludes specific IDs.
*/
exclude?: string[];
}

async function loader(
p: Props,
_req: Request,
ctx: AppContext,
): Promise<Product[] | null> {
const { api } = ctx;

const props = p.props ??
(p as unknown as Props["props"]);

let products;

const queryParams: Omit<Props, "props"> = {
order: p.order ?? "desc",
orderby: p.orderby ?? "date",
status: p.status ?? "any",
stock_status: p.stock_status ?? "instock",
per_page: p.per_page,
exclude: p.exclude,
};

if ("search" in props) {
products = await api["GET /wc/v3/products"]({
search: props.search,
...queryParams,
}).then((res) => res.json());
}

if ("ids" in props) {
products = await api["GET /wc/v3/products"]({
include: props.ids,
...queryParams,
}).then((res) => res.json());
}

if (!products) return null;

return products.map((product) => toProduct(product));
}

export default loader;
6 changes: 4 additions & 2 deletions woocommerce/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// This file is automatically updated during development when running `dev.ts`.

import * as $$$0 from "./loaders/product/productDetailsPage.ts";
import * as $$$1 from "./loaders/proxy.ts";
import * as $$$1 from "./loaders/product/productList.ts";
import * as $$$2 from "./loaders/proxy.ts";

const manifest = {
"loaders": {
"woocommerce/loaders/product/productDetailsPage.ts": $$$0,
"woocommerce/loaders/proxy.ts": $$$1,
"woocommerce/loaders/product/productList.ts": $$$1,
"woocommerce/loaders/proxy.ts": $$$2,
},
"name": "woocommerce",
"baseUrl": import.meta.url,
Expand Down
15 changes: 11 additions & 4 deletions woocommerce/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { BaseProduct, Category, OrderBy } from "./types.ts";
import {
BaseProduct,
Category,
Order,
OrderBy,
Status,
StockStatus,
} from "./types.ts";

export interface WooCommerceAPI {
"GET /wc/v3/products": {
Expand All @@ -12,8 +19,8 @@ export interface WooCommerceAPI {
slug?: string;
parent?: string;
parent_exclude?: string[];
status?: "any" | "draft" | "pending" | "private" | "publish";
stock_status?: "instock" | "outofstock" | "onbackorder";
status?: Status;
stock_status?: StockStatus;
type?: "simple" | "grouped" | "external" | "variable";
featured?: boolean;
tag?: string;
Expand All @@ -32,7 +39,7 @@ export interface WooCommerceAPI {
searchParams: {
page?: number;
per_page?: number;
order?: "asc" | "desc";
order?: Order;
orderby?: OrderBy;
hide_empty?: boolean;
parent?: number;
Expand Down
15 changes: 14 additions & 1 deletion woocommerce/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ export type OrderBy =
| "modified"
| "title"
| "slug"
| "date";
| "date"
| "id"
| "menu_order";

export type Status =
| "any"
| "draft"
| "pending"
| "private"
| "publish";

export type Order = "asc" | "desc";

export type StockStatus = "instock" | "outofstock" | "onbackorder";

export interface BaseProduct {
id: number;
Expand Down

0 comments on commit 2bf920b

Please sign in to comment.