@putiikkipalvelu/storefront-sdk 0.2.1 → 0.2.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/index.cjs +136 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +260 -188
- package/dist/index.d.ts +260 -188
- package/dist/index.js +125 -30
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -611,13 +611,19 @@ function createCartResource(fetcher) {
|
|
|
611
611
|
}
|
|
612
612
|
|
|
613
613
|
// src/resources/shipping.ts
|
|
614
|
+
function calculateCartWeight(items) {
|
|
615
|
+
return items.reduce((total, item) => {
|
|
616
|
+
const itemWeight = item.variation?.weight ?? item.product.weight ?? 0.5;
|
|
617
|
+
return total + itemWeight * item.cartQuantity;
|
|
618
|
+
}, 0);
|
|
619
|
+
}
|
|
614
620
|
function createShippingResource(fetcher) {
|
|
615
621
|
return {
|
|
616
622
|
/**
|
|
617
623
|
* Get all available shipment methods for the store.
|
|
618
624
|
* Returns methods without pickup locations - use `getWithLocations` for postal code specific data.
|
|
619
625
|
*
|
|
620
|
-
* @param options - Fetch options
|
|
626
|
+
* @param options - Fetch options including optional cartItems for weight-based filtering
|
|
621
627
|
* @returns Available shipment methods
|
|
622
628
|
*
|
|
623
629
|
* @example
|
|
@@ -628,22 +634,34 @@ function createShippingResource(fetcher) {
|
|
|
628
634
|
* console.log(`${method.name}: ${method.price / 100}€`);
|
|
629
635
|
* });
|
|
630
636
|
* ```
|
|
637
|
+
*
|
|
638
|
+
* @example Weight-based filtering
|
|
639
|
+
* ```typescript
|
|
640
|
+
* // Pass cart items - SDK calculates weight automatically
|
|
641
|
+
* const { shipmentMethods } = await client.shipping.getMethods({
|
|
642
|
+
* cartItems: cartItems
|
|
643
|
+
* });
|
|
644
|
+
* ```
|
|
631
645
|
*/
|
|
632
646
|
async getMethods(options) {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
647
|
+
const params = new URLSearchParams();
|
|
648
|
+
if (options?.cartItems?.length) {
|
|
649
|
+
const cartWeight = calculateCartWeight(options.cartItems);
|
|
650
|
+
params.set("cartWeight", cartWeight.toString());
|
|
651
|
+
}
|
|
652
|
+
const queryString = params.toString();
|
|
653
|
+
const url = `/api/storefront/v1/shipment-methods${queryString ? `?${queryString}` : ""}`;
|
|
654
|
+
return fetcher.request(url, {
|
|
655
|
+
method: "GET",
|
|
656
|
+
...options
|
|
657
|
+
});
|
|
640
658
|
},
|
|
641
659
|
/**
|
|
642
660
|
* Get shipment methods with pickup locations for a specific postal code.
|
|
643
661
|
* Calls the Shipit API to fetch nearby pickup points (parcel lockers, etc.)
|
|
644
662
|
*
|
|
645
663
|
* @param postalCode - Customer's postal code (e.g., "00100")
|
|
646
|
-
* @param options - Fetch options
|
|
664
|
+
* @param options - Fetch options including optional cartItems for weight-based filtering
|
|
647
665
|
* @returns Shipment methods and nearby pickup locations with pricing
|
|
648
666
|
*
|
|
649
667
|
* @example
|
|
@@ -659,23 +677,28 @@ function createShippingResource(fetcher) {
|
|
|
659
677
|
* });
|
|
660
678
|
* ```
|
|
661
679
|
*
|
|
662
|
-
* @example
|
|
680
|
+
* @example Weight-based filtering with postal code
|
|
663
681
|
* ```typescript
|
|
664
|
-
* const { pricedLocations } = await client.shipping.getWithLocations(
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
* loc => loc.carrier === "Posti"
|
|
682
|
+
* const { shipmentMethods, pricedLocations } = await client.shipping.getWithLocations(
|
|
683
|
+
* "00100",
|
|
684
|
+
* { cartItems: cartItems }
|
|
668
685
|
* );
|
|
686
|
+
*
|
|
687
|
+
* // Only shows methods that support the cart's total weight
|
|
669
688
|
* ```
|
|
670
689
|
*/
|
|
671
690
|
async getWithLocations(postalCode, options) {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
)
|
|
691
|
+
const params = new URLSearchParams();
|
|
692
|
+
if (options?.cartItems?.length) {
|
|
693
|
+
const cartWeight = calculateCartWeight(options.cartItems);
|
|
694
|
+
params.set("cartWeight", cartWeight.toString());
|
|
695
|
+
}
|
|
696
|
+
const queryString = params.toString();
|
|
697
|
+
const url = `/api/storefront/v1/shipment-methods/${encodeURIComponent(postalCode)}${queryString ? `?${queryString}` : ""}`;
|
|
698
|
+
return fetcher.request(url, {
|
|
699
|
+
method: "GET",
|
|
700
|
+
...options
|
|
701
|
+
});
|
|
679
702
|
}
|
|
680
703
|
};
|
|
681
704
|
}
|
|
@@ -688,11 +711,12 @@ function createCustomerResource(fetcher) {
|
|
|
688
711
|
return {
|
|
689
712
|
/**
|
|
690
713
|
* Register a new customer account.
|
|
691
|
-
*
|
|
714
|
+
* A verification email is sent automatically by the server.
|
|
715
|
+
* The customer must verify their email before logging in.
|
|
692
716
|
*
|
|
693
717
|
* @param data - Registration data (firstName, lastName, email, password)
|
|
694
718
|
* @param fetchOptions - Fetch options
|
|
695
|
-
* @returns Created customer
|
|
719
|
+
* @returns Created customer data and success message
|
|
696
720
|
*
|
|
697
721
|
* @example
|
|
698
722
|
* ```typescript
|
|
@@ -703,7 +727,7 @@ function createCustomerResource(fetcher) {
|
|
|
703
727
|
* password: 'securePassword123'
|
|
704
728
|
* });
|
|
705
729
|
*
|
|
706
|
-
* //
|
|
730
|
+
* // Verification email is sent automatically by the server
|
|
707
731
|
* console.log('Account created:', message);
|
|
708
732
|
* ```
|
|
709
733
|
*/
|
|
@@ -851,21 +875,21 @@ function createCustomerResource(fetcher) {
|
|
|
851
875
|
);
|
|
852
876
|
},
|
|
853
877
|
/**
|
|
854
|
-
* Resend the
|
|
855
|
-
*
|
|
878
|
+
* Resend the verification email for an unverified customer.
|
|
879
|
+
* A new verification email is sent automatically by the server.
|
|
856
880
|
*
|
|
857
881
|
* @param customerId - The customer's ID (from failed login response)
|
|
858
882
|
* @param fetchOptions - Fetch options
|
|
859
|
-
* @returns
|
|
883
|
+
* @returns Success message
|
|
860
884
|
* @throws ValidationError if customer is already verified or not found
|
|
861
885
|
*
|
|
862
886
|
* @example
|
|
863
887
|
* ```typescript
|
|
864
888
|
* // After login fails with requiresVerification
|
|
865
|
-
* const {
|
|
889
|
+
* const { message } = await client.customer.resendVerification(customerId);
|
|
866
890
|
*
|
|
867
|
-
* //
|
|
868
|
-
*
|
|
891
|
+
* // Verification email is sent automatically by the server
|
|
892
|
+
* console.log(message); // "Verification email sent."
|
|
869
893
|
* ```
|
|
870
894
|
*/
|
|
871
895
|
async resendVerification(customerId, fetchOptions) {
|
|
@@ -878,6 +902,77 @@ function createCustomerResource(fetcher) {
|
|
|
878
902
|
}
|
|
879
903
|
);
|
|
880
904
|
},
|
|
905
|
+
/**
|
|
906
|
+
* Request a password reset for a customer account.
|
|
907
|
+
* The server sends a password reset email directly to the customer.
|
|
908
|
+
* Returns success even if email doesn't exist (to prevent email enumeration).
|
|
909
|
+
*
|
|
910
|
+
* Note: The reset token is never exposed to the client for security.
|
|
911
|
+
* The email is sent server-side with the reset link.
|
|
912
|
+
*
|
|
913
|
+
* @param email - Customer's email address
|
|
914
|
+
* @param fetchOptions - Fetch options
|
|
915
|
+
* @returns Generic success message (same whether email exists or not)
|
|
916
|
+
*
|
|
917
|
+
* @example
|
|
918
|
+
* ```typescript
|
|
919
|
+
* const response = await client.customer.forgotPassword('john@example.com');
|
|
920
|
+
*
|
|
921
|
+
* // Always show same message to user (email sent server-side)
|
|
922
|
+
* console.log(response.message);
|
|
923
|
+
* // "If an account exists with that email, password reset instructions have been sent."
|
|
924
|
+
* ```
|
|
925
|
+
*/
|
|
926
|
+
async forgotPassword(email, fetchOptions) {
|
|
927
|
+
return fetcher.request(
|
|
928
|
+
"/api/storefront/v1/customer/(auth)/forgot-password",
|
|
929
|
+
{
|
|
930
|
+
method: "POST",
|
|
931
|
+
body: { email },
|
|
932
|
+
...fetchOptions
|
|
933
|
+
}
|
|
934
|
+
);
|
|
935
|
+
},
|
|
936
|
+
/**
|
|
937
|
+
* Reset a customer's password using a valid reset token.
|
|
938
|
+
* The token is sent via email by the forgotPassword endpoint.
|
|
939
|
+
* After successful reset, all existing sessions are invalidated.
|
|
940
|
+
*
|
|
941
|
+
* @param token - Password reset token (from email link)
|
|
942
|
+
* @param password - New password (minimum 8 characters)
|
|
943
|
+
* @param fetchOptions - Fetch options
|
|
944
|
+
* @returns Success confirmation
|
|
945
|
+
* @throws ValidationError if token is invalid or expired
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```typescript
|
|
949
|
+
* // Token comes from the reset email link
|
|
950
|
+
* const token = searchParams.get('token');
|
|
951
|
+
*
|
|
952
|
+
* try {
|
|
953
|
+
* const { message } = await client.customer.resetPassword(token, newPassword);
|
|
954
|
+
* console.log(message); // "Password reset successful..."
|
|
955
|
+
*
|
|
956
|
+
* // Redirect to login page
|
|
957
|
+
* redirect('/login?reset=success');
|
|
958
|
+
* } catch (error) {
|
|
959
|
+
* if (error instanceof ValidationError) {
|
|
960
|
+
* // Token invalid or expired
|
|
961
|
+
* console.error('Please request a new password reset');
|
|
962
|
+
* }
|
|
963
|
+
* }
|
|
964
|
+
* ```
|
|
965
|
+
*/
|
|
966
|
+
async resetPassword(token, password, fetchOptions) {
|
|
967
|
+
return fetcher.request(
|
|
968
|
+
"/api/storefront/v1/customer/(auth)/reset-password",
|
|
969
|
+
{
|
|
970
|
+
method: "POST",
|
|
971
|
+
body: { token, password },
|
|
972
|
+
...fetchOptions
|
|
973
|
+
}
|
|
974
|
+
);
|
|
975
|
+
},
|
|
881
976
|
// =========================================================================
|
|
882
977
|
// Profile Management Methods
|
|
883
978
|
// =========================================================================
|