Skip to content

Commit 93f804a

Browse files
dhairyadwivediPlopix
authored andcommitted
feat(js-api-client): PriceList now available in the hydrater
Update hydrate.ts feat(js-api-client): PriceList now available in the hydrater
1 parent f4b2777 commit 93f804a

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/js-api-client",
33
"license": "MIT",
4-
"version": "1.8.0",
4+
"version": "1.9.0",
55
"author": "Crystallize <[email protected]> (https://crystallize.com)",
66
"contributors": [
77
"Sébastien Morel <[email protected]>"

src/core/hydrate.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@ export type ProductHydrater = (
77
extraQuery?: any,
88
perProduct?: (item: string, index: number) => any,
99
perVariant?: (item: string, index: number) => any,
10-
usePIMApi?: boolean,
1110
) => Promise<any>;
1211

13-
function byPaths(client: ClientInterface): ProductHydrater {
12+
export type ProductHydraterOptions = {
13+
useSyncApiForSKUs?: boolean;
14+
marketIdentifiers?: string[];
15+
priceList?: string;
16+
priceForEveryone?: boolean;
17+
};
18+
19+
const priceListBlock = {
20+
startDate: true,
21+
endDate: true,
22+
price: true,
23+
identifier: true,
24+
modifier: true,
25+
modifierType: true,
26+
};
27+
28+
function byPaths(client: ClientInterface, options?: ProductHydraterOptions): ProductHydrater {
1429
return <T>(
1530
paths: string[],
1631
language: string,
@@ -42,6 +57,25 @@ function byPaths(client: ClientInterface): ProductHydrater {
4257
price: true,
4358
identifier: true,
4459
currency: true,
60+
...(options?.priceForEveryone === true
61+
? {
62+
priceForEveryone: priceListBlock,
63+
}
64+
: {}),
65+
...(options?.priceList
66+
? {
67+
priceList: {
68+
__args: { identifier: options.priceList },
69+
},
70+
}
71+
: {}),
72+
...(options?.marketIdentifiers
73+
? {
74+
priceFor: {
75+
__args: { marketIdentifiers: options.marketIdentifiers },
76+
},
77+
}
78+
: {}),
4579
},
4680
...(perVariant !== undefined ? perVariant(path, index) : {}),
4781
},
@@ -61,7 +95,7 @@ function byPaths(client: ClientInterface): ProductHydrater {
6195
};
6296
}
6397

64-
function bySkus(client: ClientInterface, options?: { useSyncApiForSKUs?: boolean }): ProductHydrater {
98+
function bySkus(client: ClientInterface, options?: ProductHydraterOptions): ProductHydrater {
6599
async function getPathForSkus(skus: string[], language: string): Promise<string[]> {
66100
const pathsSet = new Set<string>();
67101

@@ -159,18 +193,13 @@ function bySkus(client: ClientInterface, options?: { useSyncApiForSKUs?: boolean
159193

160194
return empty as any;
161195
}
162-
return byPaths(client)(paths, language, extraQuery, perProduct, perVariant);
196+
return byPaths(client, options)(paths, language, extraQuery, perProduct, perVariant);
163197
};
164198
}
165199

166-
export function createProductHydrater(
167-
client: ClientInterface,
168-
options?: {
169-
useSyncApiForSKUs?: boolean;
170-
},
171-
) {
200+
export function createProductHydrater(client: ClientInterface, options?: ProductHydraterOptions) {
172201
return {
173-
byPaths: byPaths(client),
202+
byPaths: byPaths(client, options),
174203
bySkus: bySkus(client, options),
175204
};
176205
}

src/types/product.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,23 @@ export interface ProductVariant {
4343
subscriptionPlans?: ProductVariantSubscriptionPlan[];
4444
}
4545

46+
export interface ProductVariantPriceList {
47+
startDate?: Date;
48+
endDate?: Date;
49+
price?: number;
50+
identifier?: string;
51+
modifier?: number;
52+
modifierType?: 'PERCENTAGE' | 'RELATIVE' | 'ABSOLUTE';
53+
}
54+
4655
export interface ProductPriceVariant {
4756
identifier: string;
4857
name?: string;
4958
price?: number;
5059
currency?: string;
60+
priceForEveryone?: ProductVariantPriceList;
61+
priceFor?: ProductVariantPriceList;
62+
priceList?: ProductVariantPriceList;
5163
}
5264

5365
export interface ProductStockLocation {

0 commit comments

Comments
 (0)