-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwaitrose.ts
More file actions
1109 lines (950 loc) · 47.4 KB
/
waitrose.ts
File metadata and controls
1109 lines (950 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Waitrose API Client
*
* A dependency-free TypeScript client for the Waitrose grocery API.
* Reverse-engineered from the Waitrose Android app v3.9.1.
*
* Usage:
* const client = new WaitroseClient();
* await client.login(username, password);
* const trolley = await client.getTrolley();
*/
const GRAPHQL_URL = "https://www.waitrose.com/api/graphql-prod/graph/live";
const SEARCH_API_URL = "https://www.waitrose.com/api/content-prod/v2/cms/publish/productcontent";
const PRODUCTS_API_URL = "https://www.waitrose.com/api/products-prod/v1/products";
const CLIENT_ID = "ANDROID_APP";
// ============================================================================
// GraphQL Operations
// ============================================================================
const QUERIES = {
// Session
NewSession: `mutation NewSession($input: SessionInput) { generateSession(session: $input) { __typename ...SessionPayload failures { type message } } } fragment SessionPayload on SetSessionPayload { accessToken refreshToken customerId customerOrderId customerOrderState defaultBranchId expiresIn }`,
RefreshSession: `mutation RefreshSession($input: SessionInput) { generateSession(session: $input) { __typename ...SessionPayload failures { type message } } } fragment SessionPayload on SetSessionPayload { accessToken refreshToken customerId customerOrderId customerOrderState defaultBranchId expiresIn }`,
DeleteSession: `mutation DeleteSession { deleteSession }`,
// Shopping Context
GetShoppingContext: `query GetShoppingContext { shoppingContext { customerId customerOrderId customerOrderState defaultBranchId } }`,
// Account
GetAccountInfoAndMembership: `query GetAccountInfoAndMembership { getAccountProfile { id email contactAddress { __typename ...ContactAddress } } getMemberships { memberships { number type } } } fragment Addressee on Addressee { title firstName lastName contactNumber } fragment ContactAddress on Address { id line1 line2 line3 town region country postalCode addressee { __typename ...Addressee } }`,
// Trolley
GetTrolley: `query GetTrolley($orderId: ID!) { getTrolley(orderId: $orderId) { checkoutReadiness { __typename ...CheckoutReadiness } products { __typename ...TrolleyProduct } slotChangeable trolley { __typename ...TrolleyResponse } instantCheckout failures { __typename ...TrolleyFailure } } } fragment CheckoutReadiness on CheckoutReadiness { slotTypeValid } fragment TrolleyProductCategory on TrolleyProductCategory { id name } fragment TrolleyPrice on Price { amount currencyCode } fragment Quantity on Quantity { amount uom } fragment QuantityPrice on QuantityPrice { price { __typename ...TrolleyPrice } quantity { __typename ...Quantity } } fragment Hfss on Hfss { status } fragment ProductImage on ProductImage { extraLarge large medium small } fragment Group on Group { name } fragment TrolleyProductPromotion on TrolleyProductPromotion { groups { __typename ...Group } myWaitrosePromotion promotionDescription promotionExpiryDate promotionId promotionTypeCode promotionUnitPrice { __typename ...TrolleyPrice } promotionalPricePerUnit discount { type } hidden } fragment AvailableDate on AvailableDate { startDate endDate } fragment Restriction on Restriction { availableDates { __typename ...AvailableDate } } fragment ProductReview on ProductReview { averageRating reviewCount } fragment ProductServings on ProductServings { max min } fragment ProductWeight on ProductWeight { uoms } fragment TrolleyProduct on TrolleyProduct { categories { __typename ...TrolleyProductCategory } currentSaleUnitPrice { __typename ...QuantityPrice } defaultQuantity { __typename ...Quantity } displayPrice displayPriceEstimated displayPriceQualifier formattedPriceRange formattedWeightRange hfss { __typename ...Hfss } id leadTime lineNumber maxPersonalisedMessageLength name brandName productImageUrls { __typename ...ProductImage } productType promotions { __typename ...TrolleyProductPromotion } restriction { __typename ...Restriction } reviews { __typename ...ProductReview } servings { __typename ...ProductServings } substitutionsProhibited size thumbnail weights { __typename ...ProductWeight } depositCharge { __typename ...TrolleyPrice } } fragment SlotOptionDatesType on SlotOptionDatesType { date type } fragment Conflict on Conflict { productId lineNumber messages priority outOfStock resolutionActions prohibitedActions itemId type slotOptionDates { __typename ...SlotOptionDatesType } } fragment TrolleyItem on TrolleyItem { canSubstitute lineNumber noteToShopper personalisedMessage quantity { __typename ...Quantity } reservedQuantity totalPrice { __typename ...TrolleyPrice } triggeredPromotions trolleyItemId untriggeredPromotions } fragment TrolleyItemCounts on TrolleyItemCounts { hardConflicts noConflicts softConflicts } fragment TrolleyTotals on TrolleyTotals { collectionMinimumOrderValue { __typename ...TrolleyPrice } deliveryCharge { __typename ...TrolleyPrice } deliveryMinimumOrderValue { __typename ...TrolleyPrice } itemTotalEstimatedCost { __typename ...TrolleyPrice } minimumSpendThresholdMet savingsFromOffers { __typename ...TrolleyPrice } savingsFromMyWaitrose { __typename ...TrolleyPrice } totalDepositCharge { __typename ...TrolleyPrice } totalEstimatedCost { __typename ...TrolleyPrice } trolleyItemCounts { __typename ...TrolleyItemCounts } } fragment TrolleyResponse on TrolleyResponse { amendingOrder conflicts { __typename ...Conflict } orderId trolleyItems { __typename ...TrolleyItem } trolleyTotals { __typename ...TrolleyTotals } } fragment TrolleyFailure on TrolleyFailure { message type }`,
UpdateTrolleyItems: `mutation UpdateTrolleyItems($trolleyItemsInput: [TrolleyItemInput!], $orderId: ID!) { updateTrolleyItems(trolleyItems: $trolleyItemsInput, orderId: $orderId) { products { __typename ...TrolleyProduct } trolley { __typename ...TrolleyResponse } instantCheckout failures { __typename ...TrolleyFailure } } } fragment TrolleyProductCategory on TrolleyProductCategory { id name } fragment TrolleyPrice on Price { amount currencyCode } fragment Quantity on Quantity { amount uom } fragment QuantityPrice on QuantityPrice { price { __typename ...TrolleyPrice } quantity { __typename ...Quantity } } fragment Hfss on Hfss { status } fragment ProductImage on ProductImage { extraLarge large medium small } fragment Group on Group { name } fragment TrolleyProductPromotion on TrolleyProductPromotion { groups { __typename ...Group } myWaitrosePromotion promotionDescription promotionExpiryDate promotionId promotionTypeCode promotionUnitPrice { __typename ...TrolleyPrice } promotionalPricePerUnit discount { type } hidden } fragment AvailableDate on AvailableDate { startDate endDate } fragment Restriction on Restriction { availableDates { __typename ...AvailableDate } } fragment ProductReview on ProductReview { averageRating reviewCount } fragment ProductServings on ProductServings { max min } fragment ProductWeight on ProductWeight { uoms } fragment TrolleyProduct on TrolleyProduct { categories { __typename ...TrolleyProductCategory } currentSaleUnitPrice { __typename ...QuantityPrice } defaultQuantity { __typename ...Quantity } displayPrice displayPriceEstimated displayPriceQualifier formattedPriceRange formattedWeightRange hfss { __typename ...Hfss } id leadTime lineNumber maxPersonalisedMessageLength name brandName productImageUrls { __typename ...ProductImage } productType promotions { __typename ...TrolleyProductPromotion } restriction { __typename ...Restriction } reviews { __typename ...ProductReview } servings { __typename ...ProductServings } substitutionsProhibited size thumbnail weights { __typename ...ProductWeight } depositCharge { __typename ...TrolleyPrice } } fragment SlotOptionDatesType on SlotOptionDatesType { date type } fragment Conflict on Conflict { productId lineNumber messages priority outOfStock resolutionActions prohibitedActions itemId type slotOptionDates { __typename ...SlotOptionDatesType } } fragment TrolleyItem on TrolleyItem { canSubstitute lineNumber noteToShopper personalisedMessage quantity { __typename ...Quantity } reservedQuantity totalPrice { __typename ...TrolleyPrice } triggeredPromotions trolleyItemId untriggeredPromotions } fragment TrolleyItemCounts on TrolleyItemCounts { hardConflicts noConflicts softConflicts } fragment TrolleyTotals on TrolleyTotals { collectionMinimumOrderValue { __typename ...TrolleyPrice } deliveryCharge { __typename ...TrolleyPrice } deliveryMinimumOrderValue { __typename ...TrolleyPrice } itemTotalEstimatedCost { __typename ...TrolleyPrice } minimumSpendThresholdMet savingsFromOffers { __typename ...TrolleyPrice } savingsFromMyWaitrose { __typename ...TrolleyPrice } totalDepositCharge { __typename ...TrolleyPrice } totalEstimatedCost { __typename ...TrolleyPrice } trolleyItemCounts { __typename ...TrolleyItemCounts } } fragment TrolleyResponse on TrolleyResponse { amendingOrder conflicts { __typename ...Conflict } orderId trolleyItems { __typename ...TrolleyItem } trolleyTotals { __typename ...TrolleyTotals } } fragment TrolleyFailure on TrolleyFailure { message type }`,
EmptyTrolley: `mutation EmptyTrolley($orderId: ID!) { emptyTrolley(orderId: $orderId) { products { __typename ...TrolleyProduct } trolley { __typename ...TrolleyResponse } instantCheckout failures { __typename ...TrolleyFailure } } } fragment TrolleyProductCategory on TrolleyProductCategory { id name } fragment TrolleyPrice on Price { amount currencyCode } fragment Quantity on Quantity { amount uom } fragment QuantityPrice on QuantityPrice { price { __typename ...TrolleyPrice } quantity { __typename ...Quantity } } fragment Hfss on Hfss { status } fragment ProductImage on ProductImage { extraLarge large medium small } fragment Group on Group { name } fragment TrolleyProductPromotion on TrolleyProductPromotion { groups { __typename ...Group } myWaitrosePromotion promotionDescription promotionExpiryDate promotionId promotionTypeCode promotionUnitPrice { __typename ...TrolleyPrice } promotionalPricePerUnit discount { type } hidden } fragment AvailableDate on AvailableDate { startDate endDate } fragment Restriction on Restriction { availableDates { __typename ...AvailableDate } } fragment ProductReview on ProductReview { averageRating reviewCount } fragment ProductServings on ProductServings { max min } fragment ProductWeight on ProductWeight { uoms } fragment TrolleyProduct on TrolleyProduct { categories { __typename ...TrolleyProductCategory } currentSaleUnitPrice { __typename ...QuantityPrice } defaultQuantity { __typename ...Quantity } displayPrice displayPriceEstimated displayPriceQualifier formattedPriceRange formattedWeightRange hfss { __typename ...Hfss } id leadTime lineNumber maxPersonalisedMessageLength name brandName productImageUrls { __typename ...ProductImage } productType promotions { __typename ...TrolleyProductPromotion } restriction { __typename ...Restriction } reviews { __typename ...ProductReview } servings { __typename ...ProductServings } substitutionsProhibited size thumbnail weights { __typename ...ProductWeight } depositCharge { __typename ...TrolleyPrice } } fragment SlotOptionDatesType on SlotOptionDatesType { date type } fragment Conflict on Conflict { productId lineNumber messages priority outOfStock resolutionActions prohibitedActions itemId type slotOptionDates { __typename ...SlotOptionDatesType } } fragment TrolleyItem on TrolleyItem { canSubstitute lineNumber noteToShopper personalisedMessage quantity { __typename ...Quantity } reservedQuantity totalPrice { __typename ...TrolleyPrice } triggeredPromotions trolleyItemId untriggeredPromotions } fragment TrolleyItemCounts on TrolleyItemCounts { hardConflicts noConflicts softConflicts } fragment TrolleyTotals on TrolleyTotals { collectionMinimumOrderValue { __typename ...TrolleyPrice } deliveryCharge { __typename ...TrolleyPrice } deliveryMinimumOrderValue { __typename ...TrolleyPrice } itemTotalEstimatedCost { __typename ...TrolleyPrice } minimumSpendThresholdMet savingsFromOffers { __typename ...TrolleyPrice } savingsFromMyWaitrose { __typename ...TrolleyPrice } totalDepositCharge { __typename ...TrolleyPrice } totalEstimatedCost { __typename ...TrolleyPrice } trolleyItemCounts { __typename ...TrolleyItemCounts } } fragment TrolleyResponse on TrolleyResponse { amendingOrder conflicts { __typename ...Conflict } orderId trolleyItems { __typename ...TrolleyItem } trolleyTotals { __typename ...TrolleyTotals } } fragment TrolleyFailure on TrolleyFailure { message type }`,
// Orders
GetOrders: `query GetOrders($getPendingOrdersInput: GetOrdersInput, $getPreviousOrdersInput: GetOrdersInput, $getAmendingOrderInput: GetOrdersInput) { pendingOrders: getOrders(getOrdersInput: $getPendingOrdersInput) { content { __typename ...Order } links { rel title href } } previousOrders: getOrders(getOrdersInput: $getPreviousOrdersInput) { content { __typename ...Order } links { rel title href } } amendingOrder: getOrders(getOrdersInput: $getAmendingOrderInput) { content { __typename ...Order } } } fragment Price on OrderPrice { amount currencyCode } fragment OrderAddress on OrderAddress { id line1 line2 line3 postalCode town region country } fragment OrderSlot on OrderSlot { branchId branchName branchAddress { __typename ...OrderAddress } type startDateTime endDateTime amendOrderCutoffDateTime deliveryAddress { __typename ...OrderAddress } status } fragment Order on OrderContent { customerOrderId status created lastUpdated links { rel title href } totals { estimated { totalPrice { __typename ...Price } toPay { __typename ...Price } } actual { paid { __typename ...Price } } } slots { __typename ...OrderSlot } containsEntertainingLines orderLines { lineNumber } }`,
GetPendingOrders: `query GetPendingOrders($getPendingOrdersInput: GetOrdersInput) { pendingOrders: getOrders(getOrdersInput: $getPendingOrdersInput) { content { __typename ...Order } links { rel title href } } } fragment Price on OrderPrice { amount currencyCode } fragment OrderAddress on OrderAddress { id line1 line2 line3 postalCode town region country } fragment OrderSlot on OrderSlot { branchId branchName branchAddress { __typename ...OrderAddress } type startDateTime endDateTime amendOrderCutoffDateTime deliveryAddress { __typename ...OrderAddress } status } fragment Order on OrderContent { customerOrderId status created lastUpdated links { rel title href } totals { estimated { totalPrice { __typename ...Price } toPay { __typename ...Price } } actual { paid { __typename ...Price } } } slots { __typename ...OrderSlot } containsEntertainingLines orderLines { lineNumber } }`,
GetPreviousOrders: `query GetPreviousOrders($getPreviousOrdersInput: GetOrdersInput) { previousOrders: getOrders(getOrdersInput: $getPreviousOrdersInput) { content { __typename ...Order } links { rel title href } } } fragment Price on OrderPrice { amount currencyCode } fragment OrderAddress on OrderAddress { id line1 line2 line3 postalCode town region country } fragment OrderSlot on OrderSlot { branchId branchName branchAddress { __typename ...OrderAddress } type startDateTime endDateTime amendOrderCutoffDateTime deliveryAddress { __typename ...OrderAddress } status } fragment Order on OrderContent { customerOrderId status created lastUpdated links { rel title href } totals { estimated { totalPrice { __typename ...Price } toPay { __typename ...Price } } actual { paid { __typename ...Price } } } slots { __typename ...OrderSlot } containsEntertainingLines orderLines { lineNumber } }`,
GetOrder: `query GetOrder($customerOrderId: String) { getOrder(customerOrderId: $customerOrderId) { customerOrderId status created lastUpdated orderLines { __typename ...OrderLine } slots { __typename ...OrderSlot } containsEntertainingLines substitutionsAllowed bagless paperStatement links { rel title href } totals { actual { paid { __typename ...Price } savings { __typename ...Price } carrierBagCharge { __typename ...Price } deliveryCharge { __typename ...Price } depositCharge { __typename ...Price } offerSavings { __typename ...Price } partnerDiscountSavings { __typename ...Price } membershipSavings { __typename ...Price } pickedPrice { __typename ...Price } } estimated { giftCards { __typename ...Price } giftVouchers { __typename ...Price } paymentCard { __typename ...Price } carrierBagCharge { __typename ...Price } deliveryCharge { __typename ...Price } depositCharge { __typename ...Price } orderLines { __typename ...Price } offerSavings { __typename ...Price } membershipSavings { __typename ...Price } incentiveSavings { __typename ...Price } totalSavings { __typename ...Price } totalPrice { __typename ...Price } toPay { __typename ...Price } } } paymentInfo { giftCards { __typename ...OrderGiftCard } giftVouchers { __typename ...OrderGiftVoucher } cardPayment { __typename ...CardPayment } } } } fragment Quantity on Quantity { amount uom } fragment Price on OrderPrice { amount currencyCode } fragment PersonalisedMessage on PersonalisedInfo { message } fragment OrderLine on OrderLine { lineNumber orderLineStatus estimatedQuantity { __typename ...Quantity } quantity { __typename ...Quantity } estimatedUnitPrice { __typename ...Price } estimatedTotalPrice { __typename ...Price } estimatedDepositCharge { __typename ...Price } estimatedPrice { __typename ...Price } price { __typename ...Price } unitPrice { __typename ...Price } depositCharge { __typename ...Price } totalPrice { __typename ...Price } substitutionAllowed noteToShopper personalisedInfos { __typename ...PersonalisedMessage } } fragment OrderAddress on OrderAddress { id line1 line2 line3 postalCode town region country } fragment OrderSlot on OrderSlot { branchId branchName branchAddress { __typename ...OrderAddress } type startDateTime endDateTime amendOrderCutoffDateTime deliveryAddress { __typename ...OrderAddress } status } fragment OrderGiftCard on OrderGiftCard { serialNumber remainingBalance { __typename ...Price } amountToDeduct { __typename ...Price } } fragment OrderGiftVoucher on OrderGiftVoucher { serialNumber status value { __typename ...Price } } fragment CardPayment on CardPayment { cardType cardholderName maskedCardNumber startDate expiryDate businessAccount billingAddress { __typename ...OrderAddress } }`,
CancelOrder: `mutation CancelOrder($input: ID!) { cancelOrder(customerOrderId: $input) { failures { __typename ...OrderFailure } } } fragment OrderFailure on OrderFailure { type message }`,
InitiateAmendOrder: `mutation InitiateAmendOrder($input: ID!) { amendOrder(customerOrderId: $input) { failures { __typename ...OrderFailure } } } fragment OrderFailure on OrderFailure { type message }`,
CancelAmendOrder: `mutation CancelAmendOrder($input: ID!) { cancelAmendOrder(customerOrderId: $input) { failures { __typename ...OrderFailure } } } fragment OrderFailure on OrderFailure { type message }`,
// Slots
CurrentSlot: `query CurrentSlot($input: CurrentSlotInput) { currentSlot(currentSlotInput: $input) { slotType branchId addressId postcode startDateTime endDateTime expiryDateTime orderCutoffDateTime amendOrderCutoffDateTime shopByDateTime deliveryCharge { amount currencyCode } slotGridType } }`,
SlotDates: `query SlotDates($slotDatesInput: SlotDatesInput) { slotDates(slotDatesInput: $slotDatesInput) { content { id dayOfWeek } failures { message type } } }`,
SlotDays: `query SlotDays($slotDaysInput: SlotDaysInput) { slotDays(slotDaysInput: $slotDaysInput) { content { id branchId slotType date slots { id startDateTime endDateTime shopByDateTime status slotGridType charge { currencyCode amount } greenSlot deliveryPassSlot } } failures { message type } variant } }`,
BookSlot: `mutation BookSlot($input: BookSlotInput) { bookSlot(bookSlotInput: $input) { slotExpiryDateTime orderCutoffDateTime amendOrderCutoffDateTime shopByDateTime failures { type message } variant } }`,
// Campaigns
GetCampaigns: `query GetCampaigns { campaigns { id name marketingStartDate marketingEndDate startDate endDate } }`,
};
// ============================================================================
// Types
// ============================================================================
/** GraphQL error structure */
interface GraphQLError {
message: string;
locations?: Array<{ line: number; column: number }>;
path?: string[];
}
/** GraphQL response with errors */
type GraphQLResponse<T> = T & { errors?: GraphQLError[] };
/** Standard API failure type */
export interface ApiFailure {
type: string;
message: string;
}
/** Slot type options */
export type SlotType = "DELIVERY" | "COLLECTION";
/** Standard unit of measure (C62 = "each") */
export type UnitOfMeasure = "C62" | "KGM" | "GRM";
/** Slot date with day of week */
export interface SlotDate {
id: string;
dayOfWeek: string;
}
/** Book slot result */
export interface BookSlotResult {
slotExpiryDateTime: string;
orderCutoffDateTime: string;
amendOrderCutoffDateTime?: string;
shopByDateTime?: string;
}
export interface Price {
amount: number;
currencyCode: string;
}
export interface Quantity {
amount: number;
uom: string;
}
export interface Session {
accessToken: string;
refreshToken: string;
customerId: string;
customerOrderId: string;
customerOrderState: string;
defaultBranchId: string;
expiresIn: number;
}
export interface ShoppingContext {
customerId: string;
customerOrderId: string;
customerOrderState: string;
defaultBranchId: string;
}
export interface TrolleyProduct {
id: string;
lineNumber: string;
name: string;
brandName: string;
displayPrice: string;
size: string;
thumbnail: string;
productType: string;
}
export interface TrolleyItem {
lineNumber: string;
trolleyItemId: number;
quantity: Quantity;
totalPrice: Price;
canSubstitute: boolean;
noteToShopper: string | null;
}
export interface TrolleyTotals {
totalEstimatedCost: Price;
itemTotalEstimatedCost: Price;
deliveryCharge: Price | null;
savingsFromOffers: Price | null;
savingsFromMyWaitrose: Price | null;
}
export interface Trolley {
orderId: string;
trolleyItems: TrolleyItem[];
trolleyTotals: TrolleyTotals;
conflicts: unknown[];
}
export interface TrolleyResponse {
products: TrolleyProduct[];
trolley: Trolley;
failures: ApiFailure[] | null;
}
export interface OrderSlot {
branchId: string;
branchName: string;
type: string;
startDateTime: string;
endDateTime: string;
status: string;
}
export interface Order {
customerOrderId: string;
status: string;
created: string;
lastUpdated: string;
slots: OrderSlot[];
totals: {
estimated: { totalPrice: Price; toPay: Price };
actual: { paid: Price | null };
};
}
export interface Slot {
id: string;
startDateTime: string;
endDateTime: string;
shopByDateTime: string;
status: string;
charge: Price;
greenSlot: boolean;
deliveryPassSlot: boolean;
}
export interface SlotDay {
id: string;
branchId: string;
slotType: string;
date: string;
slots: Slot[];
}
export interface AccountProfile {
id: string;
email: string;
contactAddress: {
id: string;
line1: string;
line2: string;
line3: string;
town: string;
postalCode: string;
};
}
export interface Membership {
number: string;
type: string;
}
export interface TrolleyItemInput {
lineNumber: string;
quantity: { amount: number; uom: UnitOfMeasure };
noteToShopper?: string;
canSubstitute?: boolean;
}
export interface CurrentSlot {
slotType: string | null;
branchId: string | null;
addressId: string | null;
postcode: string | null;
startDateTime: string | null;
endDateTime: string | null;
expiryDateTime: string | null;
orderCutoffDateTime: string | null;
amendOrderCutoffDateTime: string | null;
shopByDateTime: string | null;
deliveryCharge: Price | null;
slotGridType: string | null;
}
export interface OrderLine {
lineNumber: string;
orderLineStatus: string;
estimatedQuantity: Quantity | null;
quantity: Quantity | null;
estimatedUnitPrice: Price | null;
estimatedTotalPrice: Price | null;
estimatedPrice: Price | null;
price: Price | null;
unitPrice: Price | null;
totalPrice: Price | null;
substitutionAllowed: boolean;
noteToShopper: string | null;
}
export interface OrderTotals {
estimated: {
totalPrice: Price | null;
toPay: Price | null;
deliveryCharge: Price | null;
offerSavings: Price | null;
membershipSavings: Price | null;
};
actual: {
paid: Price | null;
savings: Price | null;
deliveryCharge: Price | null;
};
}
export interface OrderDetails {
customerOrderId: string;
status: string;
created: string;
lastUpdated: string;
orderLines: OrderLine[];
slots: OrderSlot[];
containsEntertainingLines: boolean;
substitutionsAllowed: boolean;
bagless: boolean;
totals: OrderTotals;
}
export interface Campaign {
id: string;
name: string;
marketingStartDate: string;
marketingEndDate: string;
startDate: string;
endDate: string;
}
// ============================================================================
// Product Search Types (REST API)
// ============================================================================
/** Sort options for product search */
export type SearchSortBy =
| "RELEVANCE"
| "PRICE_LOW_2_HIGH"
| "PRICE_HIGH_2_LOW"
| "A_2_Z"
| "Z_2_A"
| "TOP_RATED"
| "MOST_POPULAR"
| "CATEGORY_RANKING";
/** Search tag for filtering */
export interface SearchTag {
group: string;
value: string;
}
/** Filter tag for filtering results */
export interface FilterTag {
group: string;
value: string;
}
/** Search query parameters */
export interface SearchQueryParams {
/** Search term (for text search) */
searchTerm?: string;
/** Category path (for browsing) */
category?: string;
/** Sort order */
sortBy?: SearchSortBy;
/** Pagination offset (0-based) */
start?: number;
/** Page size (max 128 for search, 15 for orders) */
size?: number;
/** Search tags for filtering */
searchTags?: SearchTag[];
/** Filter tags for filtering */
filterTags?: FilterTag[];
/** Branch ID for availability */
branchId?: string;
/** Promotion ID to filter by promotion */
promotionId?: string;
/** Category level depth */
categoryLevel?: number;
}
/** Product promotion information */
export interface ProductPromotion {
promotionId: string;
promotionDescription: string;
promotionTypeCode: string;
promotionExpiryDate?: string;
promotionUnitPrice?: Price;
myWaitrosePromotion: boolean;
}
/** Product review information */
export interface ProductReview {
averageRating: number;
reviewCount: number;
}
/** Product image URLs */
export interface ProductImageUrls {
small?: string;
medium?: string;
large?: string;
extraLarge?: string;
}
/** Product details from search results */
export interface SearchProduct {
id: string;
lineNumber: string;
name: string;
brandName?: string;
displayPrice: string;
displayPriceEstimated?: boolean;
displayPriceQualifier?: string;
formattedPriceRange?: string;
formattedWeightRange?: string;
size?: string;
thumbnail?: string;
productImageUrls?: ProductImageUrls;
productType?: string;
promotions?: ProductPromotion[];
reviews?: ProductReview;
currentSaleUnitPrice?: {
price: Price;
quantity: Quantity;
};
defaultQuantity?: Quantity;
leadTime?: number;
categories?: Array<{ id: string; name: string }>;
depositCharge?: Price;
hasDepositCharge?: boolean;
servings?: { min?: number; max?: number };
isHfss?: boolean;
marketingBadges?: string[];
}
/** Favourite category in search results */
export interface FavouriteCategory {
id: string;
name: string;
productCount: number;
}
/** Search results response */
export interface SearchResponse {
/** Products matching the search */
products: SearchProduct[];
/** Total number of matching products */
totalMatches: number;
/** Favourite categories (for logged-in users) */
favouriteCategories?: FavouriteCategory[];
/** Personalisation information */
personalisation?: {
experimentId?: string;
variant?: string;
};
}
/** Product details from batch lookup by line numbers */
export interface ProductDetail {
lineNumber: string;
name: string;
brandName?: string;
displayPrice?: string;
size?: string;
thumbnail?: string;
productImageUrls?: ProductImageUrls;
currentSaleUnitPrice?: {
price: Price;
quantity: Quantity;
};
}
/** Category information for browsing */
export interface CategoryInfo {
id: string;
name: string;
parentCategoryId?: string;
isRootCategory?: boolean;
childCategories?: CategoryInfo[];
productCount?: number;
}
// ============================================================================
// API Client
// ============================================================================
export class WaitroseClient {
private accessToken: string | null = null;
private refreshToken: string | null = null;
private customerId: string | null = null;
private customerOrderId: string | null = null;
private defaultBranchId: string | null = null;
/** Execute a GraphQL query/mutation */
private async graphql<T>(query: string, variables: Record<string, unknown> = {}): Promise<T> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": "Waitrose/3.9.1 (Android)",
};
if (this.accessToken) {
headers["Authorization"] = `Bearer ${this.accessToken}`;
}
const response = await fetch(GRAPHQL_URL, {
method: "POST",
headers,
body: JSON.stringify({ query, variables }),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`HTTP ${response.status}: ${text}`);
}
const json = await response.json() as GraphQLResponse<T>;
if (json.errors?.length) {
throw new Error(`GraphQL Error: ${json.errors.map(e => e.message).join(", ")}`);
}
return json as T;
}
/** Execute a REST API call to the content/search API */
private async restApi(
endpoint: "search" | "browse",
body: Record<string, unknown>
): Promise<SearchResponse> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": "Waitrose/3.9.1 (Android)",
};
if (this.accessToken) {
headers["Authorization"] = `Bearer ${this.accessToken}`;
}
// Use -1 for anonymous users, customerId for logged-in users
const customerId = this.customerId || "-1";
const url = `${SEARCH_API_URL}/${endpoint}/${customerId}?clientType=WEB_APP`;
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`HTTP ${response.status}: ${text}`);
}
// The API returns products inside componentsAndProducts[].searchProduct
const raw = await response.json() as {
totalMatches: number;
productsInResultset?: number;
componentsAndProducts?: Array<{ searchProduct?: SearchProduct }>;
};
// Map the raw response to our cleaner SearchResponse type
const products: SearchProduct[] = [];
if (raw.componentsAndProducts) {
for (const item of raw.componentsAndProducts) {
if (item.searchProduct) {
products.push(item.searchProduct);
}
}
}
return {
products,
totalMatches: raw.totalMatches,
};
}
// ==========================================================================
// Session Management
// ==========================================================================
/** Log in with username and password */
async login(username: string, password: string): Promise<Session> {
const result = await this.graphql<{ data: { generateSession: Session & { failures: ApiFailure[] | null } } }>(
QUERIES.NewSession,
{ input: { username, password, clientId: CLIENT_ID } }
);
const session = result.data.generateSession;
if (session.failures?.length) {
throw new Error(`Login failed: ${session.failures.map(f => f.message).join(", ")}`);
}
this.accessToken = session.accessToken;
this.refreshToken = session.refreshToken;
this.customerId = session.customerId;
this.customerOrderId = session.customerOrderId;
this.defaultBranchId = session.defaultBranchId;
return session;
}
/**
* Re-authenticate using stored credentials.
* Note: The Waitrose API doesn't support token refresh via GraphQL -
* re-login is required when the token expires.
*/
async reAuthenticate(username: string, password: string): Promise<Session> {
return this.login(username, password);
}
/** Check if the client is authenticated */
isAuthenticated(): boolean {
return !!this.accessToken;
}
/** Log out and delete the session */
async logout(): Promise<void> {
await this.graphql(QUERIES.DeleteSession);
this.accessToken = null;
this.refreshToken = null;
this.customerId = null;
this.customerOrderId = null;
}
/** Get the current order ID */
getOrderId(): string | null {
return this.customerOrderId;
}
/** Get the current customer ID */
getCustomerId(): string | null {
return this.customerId;
}
// ==========================================================================
// Shopping Context
// ==========================================================================
/** Get the current shopping context */
async getShoppingContext(): Promise<ShoppingContext> {
const result = await this.graphql<{ data: { shoppingContext: ShoppingContext } }>(QUERIES.GetShoppingContext);
return result.data.shoppingContext;
}
// ==========================================================================
// Account
// ==========================================================================
/** Get account profile and membership info */
async getAccountInfo(): Promise<{ profile: AccountProfile; memberships: Membership[] | null }> {
const result = await this.graphql<{
data: {
getAccountProfile: AccountProfile;
getMemberships: { memberships: Membership[] } | null
}
}>(QUERIES.GetAccountInfoAndMembership);
return {
profile: result.data.getAccountProfile,
memberships: result.data.getMemberships?.memberships || null,
};
}
// ==========================================================================
// Trolley
// ==========================================================================
/** Get the current trolley contents */
async getTrolley(orderId?: string): Promise<TrolleyResponse> {
const id = orderId || this.customerOrderId;
if (!id) throw new Error("No order ID available");
const result = await this.graphql<{ data: { getTrolley: TrolleyResponse } }>(
QUERIES.GetTrolley,
{ orderId: id }
);
return result.data.getTrolley;
}
/** Add or update items in the trolley */
async updateTrolleyItems(items: TrolleyItemInput[], orderId?: string): Promise<TrolleyResponse> {
const id = orderId || this.customerOrderId;
if (!id) throw new Error("No order ID available");
const result = await this.graphql<{ data: { updateTrolleyItems: TrolleyResponse } }>(
QUERIES.UpdateTrolleyItems,
{ trolleyItemsInput: items, orderId: id }
);
return result.data.updateTrolleyItems;
}
/** Add an item to the trolley by line number */
async addToTrolley(lineNumber: string, quantity: number = 1, uom: UnitOfMeasure = "C62"): Promise<TrolleyResponse> {
return this.updateTrolleyItems([{ lineNumber, quantity: { amount: quantity, uom } }]);
}
/** Remove an item from the trolley */
async removeFromTrolley(lineNumber: string): Promise<TrolleyResponse> {
return this.updateTrolleyItems([{ lineNumber, quantity: { amount: 0, uom: "C62" } }]);
}
/** Empty the entire trolley */
async emptyTrolley(orderId?: string): Promise<TrolleyResponse> {
const id = orderId || this.customerOrderId;
if (!id) throw new Error("No order ID available");
const result = await this.graphql<{ data: { emptyTrolley: TrolleyResponse } }>(
QUERIES.EmptyTrolley,
{ orderId: id }
);
return result.data.emptyTrolley;
}
// ==========================================================================
// Orders
// ==========================================================================
/**
* Get all orders (pending and previous)
* @param limit Max number of orders per category (API max is 15)
*/
async getOrders(limit: number = 10): Promise<{ pending: Order[]; previous: Order[] }> {
const [pending, previous] = await Promise.all([
this.getPendingOrders(limit),
this.getPreviousOrders(limit),
]);
return { pending, previous };
}
/**
* Get pending orders only
* @param limit Max number of orders to return (API max is 15)
*/
async getPendingOrders(limit: number = 10): Promise<Order[]> {
// API has a max page size of 15
const effectiveLimit = Math.min(limit, 15);
const result = await this.graphql<{ data: { pendingOrders: { content: Order[] } } }>(
QUERIES.GetPendingOrders,
{
getPendingOrdersInput: {
size: effectiveLimit,
sortBy: "+", // ASCENDING
statuses: ["PAYMENT_FAILED", "PLACED", "FULFIL", "PAID", "PICKED"]
}
}
);
return result.data.pendingOrders?.content || [];
}
/**
* Get previous/completed orders
* @param limit Max number of orders to return (API max is 15)
*/
async getPreviousOrders(limit: number = 10): Promise<Order[]> {
// API has a max page size of 15
const effectiveLimit = Math.min(limit, 15);
const result = await this.graphql<{ data: { previousOrders: { content: Order[] } } }>(
QUERIES.GetPreviousOrders,
{
getPreviousOrdersInput: {
size: effectiveLimit,
sortBy: "-", // DESCENDING
statuses: ["COMPLETED", "CANCELLED", "REFUND_PENDING"]
}
}
);
return result.data.previousOrders?.content || [];
}
/** Get details for a specific order */
async getOrder(customerOrderId: string): Promise<OrderDetails> {
const result = await this.graphql<{ data: { getOrder: OrderDetails } }>(
QUERIES.GetOrder,
{ customerOrderId }
);
return result.data.getOrder;
}
/** Cancel an order */
async cancelOrder(customerOrderId: string): Promise<void> {
const result = await this.graphql<{ data: { cancelOrder: { failures: ApiFailure[] | null } } }>(
QUERIES.CancelOrder,
{ input: customerOrderId }
);
if (result.data.cancelOrder.failures?.length) {
throw new Error(`Cancel failed: ${result.data.cancelOrder.failures.map(f => f.message).join(", ")}`);
}
}
/** Start amending an existing order */
async initiateAmendOrder(customerOrderId: string): Promise<void> {
const result = await this.graphql<{ data: { amendOrder: { failures: ApiFailure[] | null } } }>(
QUERIES.InitiateAmendOrder,
{ input: customerOrderId }
);
if (result.data.amendOrder.failures?.length) {
throw new Error(`Amend failed: ${result.data.amendOrder.failures.map(f => f.message).join(", ")}`);
}
}
/** Cancel amending an order */
async cancelAmendOrder(customerOrderId: string): Promise<void> {
const result = await this.graphql<{ data: { cancelAmendOrder: { failures: ApiFailure[] | null } } }>(
QUERIES.CancelAmendOrder,
{ input: customerOrderId }
);
if (result.data.cancelAmendOrder.failures?.length) {
throw new Error(`Cancel amend failed: ${result.data.cancelAmendOrder.failures.map(f => f.message).join(", ")}`);
}
}
// ==========================================================================
// Slots
// ==========================================================================
/** Get the currently booked slot */
async getCurrentSlot(postcode?: string): Promise<CurrentSlot | null> {
const result = await this.graphql<{ data: { currentSlot: CurrentSlot | null } }>(
QUERIES.CurrentSlot,
{ input: { postcode, customerOrderId: this.customerOrderId } }
);
return result.data.currentSlot;
}
/** Get available slot dates */
async getSlotDates(slotType: SlotType, branchId?: string, addressId?: string): Promise<SlotDate[]> {
const result = await this.graphql<{
data: {
slotDates: {
content: SlotDate[];
failures: ApiFailure[] | null;
}
}
}>(QUERIES.SlotDates, {
slotDatesInput: {
slotType,
branchId: branchId || this.defaultBranchId,
customerOrderId: this.customerOrderId,
addressId,
},
});
if (result.data.slotDates.failures?.length) {
throw new Error(`Get slots failed: ${result.data.slotDates.failures.map(f => f.message).join(", ")}`);
}
return result.data.slotDates.content;
}
/** Get available slots for specific days */
async getSlotDays(slotType: SlotType, fromDate: string, branchId?: string, addressId?: string): Promise<SlotDay[]> {
const result = await this.graphql<{
data: {
slotDays: {
content: SlotDay[];
failures: ApiFailure[] | null;
}
}
}>(QUERIES.SlotDays, {
slotDaysInput: {
slotType,
branchId: branchId || this.defaultBranchId,
customerOrderId: this.customerOrderId,
addressId,
fromDate,
},
});
if (result.data.slotDays.failures?.length) {
throw new Error(`Get slot days failed: ${result.data.slotDays.failures.map(f => f.message).join(", ")}`);
}
return result.data.slotDays.content;
}
/** Book a delivery/collection slot */
async bookSlot(slotId: string, slotType: SlotType, addressId?: string): Promise<BookSlotResult> {
const result = await this.graphql<{
data: {
bookSlot: BookSlotResult & { failures: ApiFailure[] | null };
}
}>(QUERIES.BookSlot, {
input: {
slotId,
slotType,
addressId,
},
});
if (result.data.bookSlot.failures?.length) {
throw new Error(`Book slot failed: ${result.data.bookSlot.failures.map(f => f.message).join(", ")}`);
}
return result.data.bookSlot;
}
// ==========================================================================
// Campaigns
// ==========================================================================
/** Get active campaigns */
async getCampaigns(): Promise<Campaign[]> {
const result = await this.graphql<{ data: { campaigns: Campaign[] } }>(
QUERIES.GetCampaigns
);
return result.data.campaigns;
}
// ==========================================================================
// Product Search (REST API)
// ==========================================================================
/**
* Search for products by text query
*
* @example
* ```ts
* // Simple search
* const results = await client.searchProducts("organic milk");
*
* // Search with options (size defaults to API default, max ~128)
* const results = await client.searchProducts("milk", {
* sortBy: "PRICE_LOW_2_HIGH",
* size: 24
* });
* ```
*/
async searchProducts(
searchTerm: string,
options: Omit<SearchQueryParams, "searchTerm" | "category"> = {}
): Promise<SearchResponse> {
const queryParams: SearchQueryParams = {
searchTerm,
start: options.start ?? 0,
sortBy: options.sortBy ?? "RELEVANCE",
...options,
};
// Add branch ID if we have one
if (this.defaultBranchId && !queryParams.branchId) {
queryParams.branchId = this.defaultBranchId;
}
return this.restApi("search", {
customerSearchRequest: { queryParams },
});
}
/**
* Browse products by category
*
* @example
* ```ts
* // Browse a category
* const results = await client.browseProducts("groceries/bakery/bread");
*
* // Browse with sorting
* const results = await client.browseProducts("groceries/dairy", {
* sortBy: "MOST_POPULAR"
* });
* ```
*/
async browseProducts(
category: string,
options: Omit<SearchQueryParams, "searchTerm" | "category"> = {}
): Promise<SearchResponse> {
const queryParams: SearchQueryParams = {
category,
start: options.start ?? 0,
sortBy: options.sortBy ?? "RELEVANCE",
...options,
};
// Add branch ID if we have one
if (this.defaultBranchId && !queryParams.branchId) {
queryParams.branchId = this.defaultBranchId;
}
return this.restApi("browse", {
customerSearchRequest: { queryParams },
});
}
/**
* Get product details by line numbers
*
* @example
* ```ts
* const products = await client.getProductsByLineNumbers(["123456", "789012"]);
* console.log(products[0].name); // "Waitrose Organic Milk 2 Pints"
* ```
*/
async getProductsByLineNumbers(lineNumbers: string[]): Promise<ProductDetail[]> {
if (lineNumbers.length === 0) {
return [];
}
// Join line numbers with + as per the API format
const lineNumbersParam = lineNumbers.join("+");
const url = `${PRODUCTS_API_URL}/${lineNumbersParam}`;
const params: Record<string, string> = {
view: "EXTENDED",
excludeLinesWithConflicts: "false",
filterByCustomerSlot: "false",
};
if (this.defaultBranchId) {