@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
108 lines (107 loc) • 4.35 kB
TypeScript
import { AdminSession } from '../../public/node/session.js';
/**
* A scope supported by the Shopify Admin API.
*/
export type AdminAPIScope = 'graphql' | 'themes' | 'collaborator';
/**
* It represents the options to authenticate against the Shopify Admin API.
*/
interface AdminAPIOAuthOptions {
/** Store to request permissions for. */
storeFqdn: string;
/** List of scopes to request permissions for. */
scopes: AdminAPIScope[];
}
/**
* A scope supported by the Partners API.
*/
export type PartnersAPIScope = 'cli';
interface PartnersAPIOAuthOptions {
/** List of scopes to request permissions for. */
scopes: PartnersAPIScope[];
}
/**
* A scope supported by the Developer Platform API.
*/
export type AppManagementAPIScope = 'https://api.shopify.com/auth/organization.apps.manage';
interface AppManagementAPIOauthOptions {
/** List of scopes to request permissions for. */
scopes: AppManagementAPIScope[];
}
/**
* A scope supported by the Storefront Renderer API.
*/
export type StorefrontRendererScope = 'devtools';
interface StorefrontRendererAPIOAuthOptions {
/** List of scopes to request permissions for. */
scopes: StorefrontRendererScope[];
}
export type BusinessPlatformScope = 'destinations';
interface BusinessPlatformAPIOAuthOptions {
/** List of scopes to request permissions for. */
scopes: BusinessPlatformScope[];
}
/**
* It represents the authentication requirements and
* is the input necessary to trigger the authentication
* flow.
*/
export interface OAuthApplications {
adminApi?: AdminAPIOAuthOptions;
storefrontRendererApi?: StorefrontRendererAPIOAuthOptions;
partnersApi?: PartnersAPIOAuthOptions;
businessPlatformApi?: BusinessPlatformAPIOAuthOptions;
appManagementApi?: AppManagementAPIOauthOptions;
}
export interface OAuthSession {
admin?: AdminSession;
partners?: string;
storefront?: string;
businessPlatform?: string;
appManagement?: string;
userId: string;
}
type AuthMethod = 'partners_token' | 'device_auth' | 'theme_access_token' | 'custom_app_token' | 'none';
/**
* Retrieves the user ID from the current session or returns 'unknown' if not found.
*
* This function performs the following steps:
* 1. Checks for a cached user ID in memory (obtained in the current run).
* 2. Attempts to fetch it from the local storage (from a previous auth session).
* 3. Checks if a custom token was used (either as a theme password or partners token).
* 4. If a custom token is present in the environment, generates a UUID and uses it as userId.
* 5. If after all this we don't have a userId, then reports as 'unknown'.
*
* @returns A Promise that resolves to the user ID as a string.
*/
export declare function getLastSeenUserIdAfterAuth(): Promise<string>;
export declare function setLastSeenUserIdAfterAuth(id: string): void;
/**
* Retrieves the last seen authentication method used in the current session.
*
* This function checks for the authentication method in the following order:
* 1. Returns the cached auth method if it's not 'none'.
* 2. Checks for a cached session, which implies 'device_auth' was used.
* 3. Checks for a partners token in the environment.
* 4. Checks for a theme password in the environment.
* 5. If none of the above are true, returns 'none'.
*
* @returns A Promise that resolves to the last seen authentication method as an AuthMethod type.
*/
export declare function getLastSeenAuthMethod(): Promise<AuthMethod>;
export declare function setLastSeenAuthMethod(method: AuthMethod): void;
export interface EnsureAuthenticatedAdditionalOptions {
noPrompt?: boolean;
forceRefresh?: boolean;
forceNewSession?: boolean;
}
/**
* This method ensures that we have a valid session to authenticate against the given applications using the provided scopes.
*
* @param applications - An object containing the applications we need to be authenticated with.
* @param _env - Optional environment variables to use.
* @param options - Optional extra options to use.
* @returns An instance with the access tokens organized by application.
*/
export declare function ensureAuthenticated(applications: OAuthApplications, _env?: NodeJS.ProcessEnv, { forceRefresh, noPrompt, forceNewSession }?: EnsureAuthenticatedAdditionalOptions): Promise<OAuthSession>;
export {};