UNPKG

@levo-so/client

Version:

<img alt="Levo" src="https://static.levocdn.com/png/Levo-Logo.png" width="50" height="50">

281 lines (280 loc) 12.3 kB
import type { Client } from './client'; import type { LevoCollectionCreateInput, LevoCollectionRoot, LevoCollectionUpdateInput } from './generated'; import type { BaseOptions, HeaderType, MembershipType } from './types'; export declare class Membership { private client; constructor(client: Client); /** * Get the account details of the currently signed in user using the access token. * * @example * * const me = await levo.membership.getMe('<token>'); * * console.log(me.first_name); // Outputs the first name of the user * * @example * * const me = await levo.membership.getMe(request.cookies); * * console.log(me.first_name); // Outputs the first name of the user * * @param {string|Record<string,string>} token - The access token or the cookie object to get the access token. * @returns {Promise<LevoCollectionRoot['so_levo_membership_account']>} The account object. */ getMe(token: string | Record<string, string>, options?: BaseOptions): Promise<LevoCollectionRoot['so_levo_membership_account']>; /** * Update the account details of the currently signed in user using the access token. * * @example * * const modified = await levo.membership.updateMe('<token>', { * first_name: 'John', * last_name: 'Doe', * }); * * console.log(modified.first_name); // Outputs the first name of the user * * @example * * const modified = await levo.membership.updateMe(request.cookies, { * first_name: 'John', * last_name: 'Doe', * }); * * console.log(modified.first_name); // Outputs the first name of the user * * @param {string|Record<string,string>} token - The access token or the cookie object to get the access token. * @param {LevoCollectionUpdateInput} data - The data to update. * @returns {Promise<LevoCollectionRoot['so_levo_membership_account']>} The updated account object. * @throws {LevoError.Platform} If the user is not signed in. * @throws {LevoError.Platform} If the user's account is not found. * @throws {LevoError.Platform} If the input data is invalid. * @throws {LevoError.Platform} If the workspace is not found. */ updateMe(token: string | Record<string, string>, data: Partial<LevoCollectionUpdateInput['so_levo_membership_account']>, options?: BaseOptions): Promise<LevoCollectionRoot['so_levo_membership_account']>; /** * Update the account details of the currently signed in user using the access token. * * @example * * const me = await levo.membership.changePassword('<token>', { * password: 'new-password', * }); * * console.log(me._id); // Outputs the ID of the user, since the request was successful * * @example * * const me = await levo.membership.changePassword(request.cookies, { * password: 'new-password', * }); * * console.log(me._id); // Outputs the ID of the user, since the request was successful * * @param {string|Record<string,string>} token - The access token or the cookie object to get the access token. * @param {LevoCollectionUpdateInput} data - The data to update. * @returns {Promise<LevoCollectionRoot['so_levo_membership_account']>} The updated account object. * @throws {LevoError.Platform} If the user is not signed in. * @throws {LevoError.Platform} If the workspace is not found. */ changePassword(token: string | Record<string, string>, data: Pick<LevoCollectionCreateInput['so_levo_membership_account'], 'password'>, options?: BaseOptions): Promise<LevoCollectionRoot['so_levo_membership_account']>; /** * * Sign in with email/username and password. * * This method will sign in a user with their email and password and return the user's account details and a cookie for the domain set in Levo. * * @example * * const { data: me, cookies } = await levo.membership.signInWithPassword({ * email: 'example@example.com', * password: 'password', * }); * * for (const cookie of cookies){ * response.append('Set-Cookie', cookie); * } * console.log(me.first_name); // Outputs the first name of the user * * @param {MembershipType.SignInWithPassword} data - The data to sign in with. * @param {MembershipType.MembershipOptions} options - The options for the request. * @returns {Promise<{ * data: LevoCollectionRoot['so_levo_membership_account']; * cookies: string[]; * headers: HeaderType; * token: MembershipType.TokenResponse; * }>} The signed in user's account details, cookies, headers, and token. * @throws {LevoError.Platform} If the email does not exist. * @throws {LevoError.Platform} If the workspace is not found. */ signInWithPassword(data: MembershipType.SignInWithPassword, options?: MembershipType.MembershipOptions): Promise<{ data: LevoCollectionRoot['so_levo_membership_account']; cookies: string[]; headers: HeaderType; token: MembershipType.TokenResponse; }>; /** * * Sign up with password and other details. * * This method will sign up a user with their email/username, password, and other details and return the user's account details and a cookie for the domain set in Levo. * * @example * * const { data: me, cookies } = await levo.membership.signupWithPassword({ * email: 'example@example.com', * password: 'password', * }); * * for (const cookie of cookies){ * response.append('Set-Cookie', cookie); * } * console.log(me.first_name); // Outputs the first name of the user * * @param {LevoCollectionCreateInput['so_levo_membership_account']} data - The data to sign up with.0 * @param {MembershipType.MembershipOptions} options - The options for the request. * @returns {Promise<{ * data: LevoCollectionRoot['so_levo_membership_account']; * cookies: string[]; * headers: HeaderType; * token: MembershipType.TokenResponse; * }>} The signed in user's account details, cookies, headers, and token. * @throws {LevoError.Platform} If the email already exists. * @throws {LevoError.Platform} If the workspace is not found. */ signupWithPassword(data: LevoCollectionCreateInput['so_levo_membership_account'], options?: MembershipType.MembershipOptions): Promise<{ data: LevoCollectionRoot['so_levo_membership_account']; cookies: string[]; headers: HeaderType; token: MembershipType.TokenResponse; }>; /** * * Sign out the currently signed in user. * * This method will sign out the currently signed in user and return a boolean indicating whether the operation was successful. * * @example * * const { data: isSuccess } = await levo.membership.signOut('<token>'); * * console.log(isSuccess); // Outputs true if the user was signed out successfully * * @example * * const { data: isSuccess, cookies } = await levo.membership.signOut(request.cookies); * * console.log(isSuccess); // Outputs true if the user was signed out successfully * * @param {string} token - The access token of the user to sign out. * @returns {Promise<{ * data: boolean; * headers: HeaderType; * cookies: string[]; * }>} The `data` if the user was signed out successfully, the `headers` for the response, and the `cookie` for the response. * @throws {LevoError.Platform} If the domain is not found. * @throws {LevoError.Platform} If the workspace is not found. */ signOut(token: string | Record<string, string>, options?: MembershipType.MembershipOptions): Promise<{ data: boolean; headers: HeaderType; cookies: string[]; }>; /** * * Get the OAuth URLs for the specified redirect URI. * * This method will return the OAuth URLs for the specified redirect URI. The redirect URI is used to redirect the user to the OAuth provider's login page after they have successfully signed in. * * @example * * const urls = await levo.membership.getOAuthURLs('https://example.com/callback'); * * console.log(urls.google); // Outputs the Google OAuth URL * * @param {string} redirect_uri - The redirect URI to use for the OAuth URLs. * @returns {Promise<Record<string, string>>} An object containing the OAuth URLs for each provider. */ getOAuthURLs(redirect_uri: string, options?: MembershipType.MembershipOptions): Promise<Record<string, string>>; /** * * Request one-time password (OTP) for a user. * * This method will request a one-time password (OTP) for a user and return a reference to the OTP. * * @example * * const data = await levo.membership.requestOtp({ * content: 'john.doe@example.com', * medium: 'email', * }); * * console.log(data._id); // Outputs the ID of the OTP which meaning the request was successful * * @param {MembershipType.RequestOtpInput} data - The data to request the OTP. * @returns {Promise<MembershipType.RequestOtpResponse>} The ID and content of the OTP. * @throws {LevoError.Platform} If the email is invalid. * @throws {LevoError.Platform} If the workspace is not found. */ requestOtp(data: MembershipType.RequestOtpInput): Promise<MembershipType.RequestOtpResponse>; /** * * Sign in with one-time password (OTP). * * This method will sign in a user with their one-time password (OTP) and return the user's account details and a cookie for the domain set in Levo. * * @example * * const { data: me, cookies } = await levo.membership.signInWithOtp({ * content: 'john.doe@example.com', * code: '1234', * }); * * for (const cookie of cookies){ * response.append('Set-Cookie', cookie); * } * console.log(me.first_name); // Outputs the first name of the user who signed in meaning the request was successful * * @param {MembershipType.SignInWithOtpInput} data - The data to sign in with. * @param {MembershipType.MembershipOptions} options - The options for the request. * @returns {Promise<{ * data: LevoCollectionRoot['so_levo_membership_account']; * cookies: string[]; * headers: HeaderType; * token: MembershipType.TokenResponse; * }>} The signed in user's account details, cookies, headers, and token. * @throws {LevoError.Platform} If the email is invalid. * @throws {LevoError.Platform} If the OTP is invalid. * @throws {LevoError.Platform} If the workspace is not found. */ signInWithOtp(data: MembershipType.SignInWithOtpInput, options?: MembershipType.MembershipOptions): Promise<{ data: LevoCollectionRoot['so_levo_membership_account']; cookies: string[]; headers: HeaderType; token: MembershipType.TokenResponse; }>; /** * * Request a magic link to sign in user directly from email. * * This method will send a magic link to the user's email and return a reference to the magic link. * * @example * * const data = await levo.membership.requestMagicLink({ * content: 'john.doe@example.com', * medium: 'email', * redirect_uri: 'https://example.com/callback', * }); * * console.log(data.content); // john.doe@example.com * * @param {MembershipType.RequestMagicLinkInput} data - The data to request the magic link. * @returns {Promise<MembershipType.RequestMagicLinkResponse>} The reference to the magic link. * @throws {LevoError.Platform} If the email is invalid. * @throws {LevoError.Platform} If the redirect uri is invalid. * @throws {LevoError.Platform} If the workspace is not found. */ requestMagicLink(data: MembershipType.RequestMagicLinkInput, options?: MembershipType.MembershipOptions): Promise<MembershipType.RequestOtpResponse>; }