@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
103 lines (102 loc) • 3.96 kB
TypeScript
import type { Optional } from 'utility-types';
import type { GuestRequest, LoginRequest, Oauth, ParamRpcHandler, RegisterRequest, RpcContextWithSession, RpcHandler, UpdatePasswordByHashRequest } from '../../types';
import { ErrorResponse } from '../../errors';
/**
* Ensure error is a fetch error with one of the status codes
*
* @param error Any error object.
* @param httpStatuses Array of HTTP status codes to check against.
*
* @returns A Response object if the error matches the criteria, undefined otherwise.
* It will return an `ErrorResponse` if the provided Error is a `FetchError`
* or an `OAuthRequestError` with a `FetchError` cause, and its status is included in httpStatuses.
* Otherwise, it returns undefined.
*/
export declare const convertErrorForRpcCall: (error: any, httpStatuses: Array<number>) => Response | undefined;
/**
* Handles the post-login logic, including fetching user data, updating tokens,
* merging baskets and wishlists, and creating a user-bound session.
*
* @param context The RPC context with session information.
* @param tokens The OAuth tokens.
*
* @returns The result of the post login operation. It will return
* an `ErrorResponse` if fetching the user or merging baskets/wishlists fails.
*/
export declare function postLogin(context: RpcContextWithSession, tokens: Oauth): Promise<ErrorResponse | undefined>;
/**
* Handles OAuth login requests.
*
* @param login The login request data. Includes `email` and `password` keys.
* @param context The RPC context.
*
* @returns A Response object indicating the login status. It will return
* an `ErrorResponse` if login fails.
*/
export declare const oauthLogin: ParamRpcHandler<Optional<LoginRequest, 'shop_id'>, undefined>;
/**
* Handles OAuth user registration requests.
*
* @param register The registration request data.
* @param context The RPC context.
*
* @returns A Response object indicating the registration status. It will return
* an `ErrorResponse` if registration fails.
*/
export declare const oauthRegister: ParamRpcHandler<Optional<RegisterRequest, 'shop_id'>, undefined>;
/**
* Handles OAuth guest login requests.
*
* @param guest The guest login request data.
* @param context The RPC context.
*
* @returns A Response object indicating the login status. It will return
* an `ErrorResponse` if guest login fails.
*/
export declare const oauthGuestLogin: ParamRpcHandler<Optional<GuestRequest, 'shop_id'>, undefined>;
/**
* Refreshes the access token using the refresh token.
*
* @param context The RPC context.
*
* @returns An object indicating the success status of the refresh operation.
* It will return an `ErrorResponse` if refreshing the token fails.
*/
export declare const refreshAccessToken: RpcHandler<{
success: boolean;
}>;
/**
* Revokes the current access token and destroys the session.
*
* @param context The RPC context.
*
* @returns An object indicating the result of the revocation.
* It will return an `ErrorResponse` if revoking the token fails.
*/
export declare const oauthRevokeToken: RpcHandler<{
result: boolean;
}>;
/**
* Sends a password reset email to the specified email address.
*
* @param email The email address for password reset. Includes email key.
* @param context The RPC context.
*
* @returns An object indicating the success status of the operation.
* It will return an `ErrorResponse` if sending the password reset email fails.
*/
export declare const oauthForgetPassword: RpcHandler<{
email: string;
}, {
success: boolean;
}>;
/**
* Updates the password using a password hash.
*
* @param passwordHash The password hash data.
* @param context The RPC context.
*
* @returns A Response object indicating the status of the password update.
* It will return an `ErrorResponse` if updating the password fails.
*/
export declare const updatePasswordByHash: ParamRpcHandler<Optional<UpdatePasswordByHashRequest, 'shop_id'>, undefined>;