@putiikkipalvelu/storefront-sdk 0.2.0 → 0.2.2
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 +98 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +237 -182
- package/dist/index.d.ts +237 -182
- package/dist/index.js +97 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -36,6 +36,14 @@ var ValidationError = class extends StorefrontError {
|
|
|
36
36
|
this.name = "ValidationError";
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
|
+
var VerificationRequiredError = class extends StorefrontError {
|
|
40
|
+
constructor(message, customerId) {
|
|
41
|
+
super(message, 403, "VERIFICATION_REQUIRED");
|
|
42
|
+
this.requiresVerification = true;
|
|
43
|
+
this.name = "VerificationRequiredError";
|
|
44
|
+
this.customerId = customerId;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
39
47
|
|
|
40
48
|
// src/utils/fetch.ts
|
|
41
49
|
var SDK_VERSION = "0.1.0";
|
|
@@ -96,15 +104,18 @@ function createFetcher(config) {
|
|
|
96
104
|
return { request };
|
|
97
105
|
}
|
|
98
106
|
async function handleErrorResponse(response) {
|
|
99
|
-
let
|
|
107
|
+
let errorData = {};
|
|
100
108
|
try {
|
|
101
109
|
const json = await response.json();
|
|
102
|
-
if (json && typeof json === "object"
|
|
103
|
-
|
|
110
|
+
if (json && typeof json === "object") {
|
|
111
|
+
errorData = json;
|
|
104
112
|
}
|
|
105
113
|
} catch {
|
|
106
114
|
}
|
|
107
|
-
const message =
|
|
115
|
+
const message = errorData.error || response.statusText || "Request failed";
|
|
116
|
+
if (errorData.requiresVerification && errorData.customerId) {
|
|
117
|
+
throw new VerificationRequiredError(message, errorData.customerId);
|
|
118
|
+
}
|
|
108
119
|
switch (response.status) {
|
|
109
120
|
case 401:
|
|
110
121
|
throw new AuthError(message);
|
|
@@ -677,11 +688,12 @@ function createCustomerResource(fetcher) {
|
|
|
677
688
|
return {
|
|
678
689
|
/**
|
|
679
690
|
* Register a new customer account.
|
|
680
|
-
*
|
|
691
|
+
* A verification email is sent automatically by the server.
|
|
692
|
+
* The customer must verify their email before logging in.
|
|
681
693
|
*
|
|
682
694
|
* @param data - Registration data (firstName, lastName, email, password)
|
|
683
695
|
* @param fetchOptions - Fetch options
|
|
684
|
-
* @returns Created customer
|
|
696
|
+
* @returns Created customer data and success message
|
|
685
697
|
*
|
|
686
698
|
* @example
|
|
687
699
|
* ```typescript
|
|
@@ -692,7 +704,7 @@ function createCustomerResource(fetcher) {
|
|
|
692
704
|
* password: 'securePassword123'
|
|
693
705
|
* });
|
|
694
706
|
*
|
|
695
|
-
* //
|
|
707
|
+
* // Verification email is sent automatically by the server
|
|
696
708
|
* console.log('Account created:', message);
|
|
697
709
|
* ```
|
|
698
710
|
*/
|
|
@@ -840,21 +852,21 @@ function createCustomerResource(fetcher) {
|
|
|
840
852
|
);
|
|
841
853
|
},
|
|
842
854
|
/**
|
|
843
|
-
* Resend the
|
|
844
|
-
*
|
|
855
|
+
* Resend the verification email for an unverified customer.
|
|
856
|
+
* A new verification email is sent automatically by the server.
|
|
845
857
|
*
|
|
846
858
|
* @param customerId - The customer's ID (from failed login response)
|
|
847
859
|
* @param fetchOptions - Fetch options
|
|
848
|
-
* @returns
|
|
860
|
+
* @returns Success message
|
|
849
861
|
* @throws ValidationError if customer is already verified or not found
|
|
850
862
|
*
|
|
851
863
|
* @example
|
|
852
864
|
* ```typescript
|
|
853
865
|
* // After login fails with requiresVerification
|
|
854
|
-
* const {
|
|
866
|
+
* const { message } = await client.customer.resendVerification(customerId);
|
|
855
867
|
*
|
|
856
|
-
* //
|
|
857
|
-
*
|
|
868
|
+
* // Verification email is sent automatically by the server
|
|
869
|
+
* console.log(message); // "Verification email sent."
|
|
858
870
|
* ```
|
|
859
871
|
*/
|
|
860
872
|
async resendVerification(customerId, fetchOptions) {
|
|
@@ -867,6 +879,77 @@ function createCustomerResource(fetcher) {
|
|
|
867
879
|
}
|
|
868
880
|
);
|
|
869
881
|
},
|
|
882
|
+
/**
|
|
883
|
+
* Request a password reset for a customer account.
|
|
884
|
+
* The server sends a password reset email directly to the customer.
|
|
885
|
+
* Returns success even if email doesn't exist (to prevent email enumeration).
|
|
886
|
+
*
|
|
887
|
+
* Note: The reset token is never exposed to the client for security.
|
|
888
|
+
* The email is sent server-side with the reset link.
|
|
889
|
+
*
|
|
890
|
+
* @param email - Customer's email address
|
|
891
|
+
* @param fetchOptions - Fetch options
|
|
892
|
+
* @returns Generic success message (same whether email exists or not)
|
|
893
|
+
*
|
|
894
|
+
* @example
|
|
895
|
+
* ```typescript
|
|
896
|
+
* const response = await client.customer.forgotPassword('john@example.com');
|
|
897
|
+
*
|
|
898
|
+
* // Always show same message to user (email sent server-side)
|
|
899
|
+
* console.log(response.message);
|
|
900
|
+
* // "If an account exists with that email, password reset instructions have been sent."
|
|
901
|
+
* ```
|
|
902
|
+
*/
|
|
903
|
+
async forgotPassword(email, fetchOptions) {
|
|
904
|
+
return fetcher.request(
|
|
905
|
+
"/api/storefront/v1/customer/(auth)/forgot-password",
|
|
906
|
+
{
|
|
907
|
+
method: "POST",
|
|
908
|
+
body: { email },
|
|
909
|
+
...fetchOptions
|
|
910
|
+
}
|
|
911
|
+
);
|
|
912
|
+
},
|
|
913
|
+
/**
|
|
914
|
+
* Reset a customer's password using a valid reset token.
|
|
915
|
+
* The token is sent via email by the forgotPassword endpoint.
|
|
916
|
+
* After successful reset, all existing sessions are invalidated.
|
|
917
|
+
*
|
|
918
|
+
* @param token - Password reset token (from email link)
|
|
919
|
+
* @param password - New password (minimum 8 characters)
|
|
920
|
+
* @param fetchOptions - Fetch options
|
|
921
|
+
* @returns Success confirmation
|
|
922
|
+
* @throws ValidationError if token is invalid or expired
|
|
923
|
+
*
|
|
924
|
+
* @example
|
|
925
|
+
* ```typescript
|
|
926
|
+
* // Token comes from the reset email link
|
|
927
|
+
* const token = searchParams.get('token');
|
|
928
|
+
*
|
|
929
|
+
* try {
|
|
930
|
+
* const { message } = await client.customer.resetPassword(token, newPassword);
|
|
931
|
+
* console.log(message); // "Password reset successful..."
|
|
932
|
+
*
|
|
933
|
+
* // Redirect to login page
|
|
934
|
+
* redirect('/login?reset=success');
|
|
935
|
+
* } catch (error) {
|
|
936
|
+
* if (error instanceof ValidationError) {
|
|
937
|
+
* // Token invalid or expired
|
|
938
|
+
* console.error('Please request a new password reset');
|
|
939
|
+
* }
|
|
940
|
+
* }
|
|
941
|
+
* ```
|
|
942
|
+
*/
|
|
943
|
+
async resetPassword(token, password, fetchOptions) {
|
|
944
|
+
return fetcher.request(
|
|
945
|
+
"/api/storefront/v1/customer/(auth)/reset-password",
|
|
946
|
+
{
|
|
947
|
+
method: "POST",
|
|
948
|
+
body: { token, password },
|
|
949
|
+
...fetchOptions
|
|
950
|
+
}
|
|
951
|
+
);
|
|
952
|
+
},
|
|
870
953
|
// =========================================================================
|
|
871
954
|
// Profile Management Methods
|
|
872
955
|
// =========================================================================
|
|
@@ -1303,14 +1386,12 @@ function createStorefrontClient(config) {
|
|
|
1303
1386
|
throw new Error("baseUrl is required");
|
|
1304
1387
|
}
|
|
1305
1388
|
const baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
1306
|
-
const maskedApiKey = config.apiKey.length > 8 ? `${config.apiKey.slice(0, 8)}...` : config.apiKey;
|
|
1307
1389
|
const fetcher = createFetcher({
|
|
1308
1390
|
apiKey: config.apiKey,
|
|
1309
1391
|
baseUrl,
|
|
1310
1392
|
timeout: config.timeout
|
|
1311
1393
|
});
|
|
1312
1394
|
return {
|
|
1313
|
-
apiKey: maskedApiKey,
|
|
1314
1395
|
baseUrl,
|
|
1315
1396
|
store: createStoreResource(fetcher),
|
|
1316
1397
|
products: createProductsResource(fetcher),
|
|
@@ -1497,6 +1578,7 @@ export {
|
|
|
1497
1578
|
RateLimitError,
|
|
1498
1579
|
StorefrontError,
|
|
1499
1580
|
ValidationError,
|
|
1581
|
+
VerificationRequiredError,
|
|
1500
1582
|
calculateCartWithCampaigns,
|
|
1501
1583
|
createStorefrontClient,
|
|
1502
1584
|
getPriceInfo,
|