@porulle/adapter-woocommerce 0.74.1 → 0.74.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/catalog.js +53 -10
- package/dist/index.js +1 -1
- package/dist/orders.js +4 -3
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/catalog.ts +55 -10
- package/src/index.ts +1 -1
- package/src/orders.ts +4 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@porulle/adapter-woocommerce",
|
|
3
|
-
"version": "0.74.
|
|
3
|
+
"version": "0.74.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"zod": "^4.1.11",
|
|
19
|
-
"@porulle/core": "0.74.
|
|
19
|
+
"@porulle/core": "0.74.3"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^24.5.2",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"vitest": "^3.2.4",
|
|
26
26
|
"esbuild": "0.25.12",
|
|
27
27
|
"miniflare": "4.20260722.1",
|
|
28
|
-
"@porulle/
|
|
29
|
-
"@porulle/
|
|
28
|
+
"@porulle/eslint-config": "0.1.0",
|
|
29
|
+
"@porulle/typescript-config": "0.1.0"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
package/src/catalog.ts
CHANGED
|
@@ -66,8 +66,51 @@ function status(value: string | null | undefined): ChannelCatalogItem["status"]
|
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
/** "pa_color", "Color" and "color" are one attribute; "Blue" and the term slug "blue" one value. */
|
|
70
|
+
function attributeKey(value: string): string {
|
|
71
|
+
return value.trim().toLowerCase().replace(/^pa_/, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface OptionAxis {
|
|
75
|
+
name: string;
|
|
76
|
+
values: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The option axes a variable product sells along. WooCommerce lets a variation use a global
|
|
81
|
+
* attribute (`pa_color`) by its term SLUG ("blue") while the product lists the term NAME ("Blue") —
|
|
82
|
+
* or does not list that attribute at all (measured on WooCommerce's own sample catalogue). The axes
|
|
83
|
+
* are the product's declared variation attributes plus any its variations use, and each value is
|
|
84
|
+
* spelled as the product spells it where it declares one, so every variant's values are among them.
|
|
85
|
+
*/
|
|
86
|
+
function optionAxes(product: WooProduct, variations: WooVariation[]): { axes: OptionAxis[]; resolve: (name: string, option: string) => { name: string; value: string } } {
|
|
87
|
+
const axes = new Map<string, OptionAxis>();
|
|
88
|
+
for (const attribute of product.attributes.filter((candidate) => candidate.variation === true)) {
|
|
89
|
+
axes.set(attributeKey(attribute.name), { name: attribute.name, values: [...attribute.options] });
|
|
90
|
+
}
|
|
91
|
+
const resolve = (name: string, option: string): { name: string; value: string } => {
|
|
92
|
+
const key = attributeKey(name);
|
|
93
|
+
let axis = axes.get(key);
|
|
94
|
+
if (!axis) {
|
|
95
|
+
axis = { name: name.replace(/^pa_/, "").replace(/^./, (first) => first.toUpperCase()), values: [] };
|
|
96
|
+
axes.set(key, axis);
|
|
97
|
+
}
|
|
98
|
+
const declared = axis.values.find((value) => attributeKey(value) === attributeKey(option));
|
|
99
|
+
if (declared) return { name: axis.name, value: declared };
|
|
100
|
+
const shown = option.replace(/^./, (first) => first.toUpperCase());
|
|
101
|
+
axis.values.push(shown);
|
|
102
|
+
return { name: axis.name, value: shown };
|
|
103
|
+
};
|
|
104
|
+
for (const row of variations) for (const attribute of row.attributes) if (attribute.option) resolve(attribute.name, attribute.option);
|
|
105
|
+
return { axes: [...axes.values()], resolve };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function variant(row: WooVariation, currency: string | undefined, resolve: (name: string, option: string) => { name: string; value: string }): ChannelCatalogVariant {
|
|
109
|
+
const optionValues = Object.fromEntries(row.attributes.flatMap((attribute) => {
|
|
110
|
+
if (!attribute.option) return [];
|
|
111
|
+
const resolved = resolve(attribute.name, attribute.option);
|
|
112
|
+
return [[resolved.name, resolved.value] as const];
|
|
113
|
+
}));
|
|
71
114
|
const rowPrices = prices(row, currency);
|
|
72
115
|
return {
|
|
73
116
|
externalId: row.id,
|
|
@@ -100,15 +143,17 @@ export function catalogItem(product: WooProduct, variations: WooVariation[], cur
|
|
|
100
143
|
images.push({ externalId: row.image.id, url: row.image.src, ...(row.image.alt ? { alt: row.image.alt } : {}), role: "gallery", sortOrder: images.length, variantExternalIds: [row.id] });
|
|
101
144
|
}
|
|
102
145
|
}
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
displayName: attribute.name,
|
|
106
|
-
sortOrder: attribute.position ?? index,
|
|
107
|
-
values: attribute.options.map((value, valueIndex) => ({ value, displayValue: value, sortOrder: valueIndex })),
|
|
108
|
-
}));
|
|
146
|
+
const sellable = product.type === "simple" ? [] : variations.filter((row) => row.status !== "private");
|
|
147
|
+
const { axes, resolve } = optionAxes(product, sellable);
|
|
109
148
|
const variants = product.type === "simple"
|
|
110
|
-
? [variant({ id: product.id, sku: product.sku, price: product.price, regular_price: product.regular_price, sale_price: product.sale_price, attributes: [] }, currency)]
|
|
111
|
-
:
|
|
149
|
+
? [variant({ id: product.id, sku: product.sku, price: product.price, regular_price: product.regular_price, sale_price: product.sale_price, attributes: [] }, currency, resolve)]
|
|
150
|
+
: sellable.map((row) => variant(row, currency, resolve));
|
|
151
|
+
const options = product.type === "simple" ? [] : axes.map((axis, index) => ({
|
|
152
|
+
name: axis.name,
|
|
153
|
+
displayName: axis.name,
|
|
154
|
+
sortOrder: index,
|
|
155
|
+
values: axis.values.map((value, valueIndex) => ({ value, displayValue: value, sortOrder: valueIndex })),
|
|
156
|
+
}));
|
|
112
157
|
const itemStatus = status(product.status);
|
|
113
158
|
return {
|
|
114
159
|
externalId: product.id,
|
package/src/index.ts
CHANGED
|
@@ -60,7 +60,7 @@ export function wooConnector(options: WooConnectorOptions = {}): ChannelConnecto
|
|
|
60
60
|
async probeStore(input) {
|
|
61
61
|
const storeDomain = normalizeStoreDomain(input, { allowPrivateHosts: transport.allowPrivateHosts });
|
|
62
62
|
if (!storeDomain) {
|
|
63
|
-
const why =
|
|
63
|
+
const why = refusedStoreUrl(input);
|
|
64
64
|
return refused(why === "http" ? "WOO_STORE_NOT_HTTPS" : "WOO_INVALID_STORE_DOMAIN", why === "http"
|
|
65
65
|
? "Your store must be served over https (with a valid certificate) to connect."
|
|
66
66
|
: why === "private_host" ? "That address is not a public website." : `"${input}" is not a store address. Enter it like https://shop.example.com.`);
|
package/src/orders.ts
CHANGED
|
@@ -156,9 +156,10 @@ export async function pushOrder(client: WooClient, slice: ChannelOrderSlice, opt
|
|
|
156
156
|
const draftId = draft && typeof draft === "object" && "new_draft_order_id" in draft ? z.coerce.number().int().positive().safeParse(draft.new_draft_order_id) : undefined;
|
|
157
157
|
// A refusal can leave a checkout-draft behind; it is ours, so it goes.
|
|
158
158
|
if (draftId?.success) await client.send("DELETE", `/wc/v3/orders/${draftId.data}`, undefined, z.unknown(), { force: "true" });
|
|
159
|
-
// A 4xx is the store's answer. Anything else
|
|
160
|
-
// order: retriable, and the next
|
|
161
|
-
|
|
159
|
+
// A 4xx WooCommerce explained (its JSON error) is the store's answer. Anything else — a timeout,
|
|
160
|
+
// a 5xx, a firewall's page — may or may not have created the order: retriable, and the next
|
|
161
|
+
// attempt looks for it before creating anything.
|
|
162
|
+
const answered = error.status !== undefined && error.status >= 400 && error.status < 500 && error.body !== undefined;
|
|
162
163
|
return Err({ code: answered ? "WOO_ORDER_REJECTED" : error.code, message: answered ? `WooCommerce refused the order: ${error.message}` : `The store's answer to the order was lost (${error.message}); it is looked for before trying again.`, retriable: !answered });
|
|
163
164
|
}
|
|
164
165
|
const order = created.value;
|