@shopify/cli
Version:
A CLI tool to build for the Shopify platform
121 lines (118 loc) • 5.17 kB
JavaScript
import {
ensureAuthenticated,
exchangeCliTokenForAppManagementAccessToken,
exchangeCliTokenForBusinessPlatformAccessToken,
exchangeCustomPartnerToken,
isThemeAccessSession,
normalizeStoreFqdn,
remove,
setLastSeenAuthMethod,
setLastSeenUserIdAfterAuth
} from "./chunk-FRQN52H3.js";
import {
getPartnersToken,
nonRandomUUID
} from "./chunk-Q76H7OUL.js";
import {
BugError,
outputContent,
outputDebug,
outputToken
} from "./chunk-4NC2NVYY.js";
import {
init_cjs_shims
} from "./chunk-PKR7KJ6P.js";
// ../cli-kit/dist/public/node/session.js
init_cjs_shims();
async function ensureAuthenticatedPartners(scopes = [], env = process.env, options = {}) {
outputDebug(outputContent`Ensuring that the user is authenticated with the Partners API with the following scopes:
${outputToken.json(scopes)}
`);
let envToken = getPartnersToken();
if (envToken) {
let result = await exchangeCustomPartnerToken(envToken);
return { token: result.accessToken, userId: result.userId };
}
let tokens = await ensureAuthenticated({ partnersApi: { scopes } }, env, options);
if (!tokens.partners)
throw new BugError("No partners token found after ensuring authenticated");
return { token: tokens.partners, userId: tokens.userId };
}
async function ensureAuthenticatedAppManagementAndBusinessPlatform(options = {}, appManagementScopes = [], businessPlatformScopes = [], env = process.env) {
outputDebug(outputContent`Ensuring that the user is authenticated with the App Management API with the following scopes:
${outputToken.json(appManagementScopes)}
`);
let envToken = getPartnersToken();
if (envToken) {
let appManagmentToken = await exchangeCliTokenForAppManagementAccessToken(envToken), businessPlatformToken = await exchangeCliTokenForBusinessPlatformAccessToken(envToken);
return {
appManagementToken: appManagmentToken.accessToken,
userId: appManagmentToken.userId,
businessPlatformToken: businessPlatformToken.accessToken
};
}
let tokens = await ensureAuthenticated({ appManagementApi: { scopes: appManagementScopes }, businessPlatformApi: { scopes: businessPlatformScopes } }, env, options);
if (!tokens.appManagement || !tokens.businessPlatform)
throw new BugError("No App Management or Business Platform token found after ensuring authenticated");
return {
appManagementToken: tokens.appManagement,
userId: tokens.userId,
businessPlatformToken: tokens.businessPlatform
};
}
async function ensureAuthenticatedStorefront(scopes = [], password = void 0, forceRefresh = !1) {
if (password) {
let authMethod = isThemeAccessSession({ token: password, storeFqdn: "" }) ? "theme_access_token" : "custom_app_token";
return setLastSeenAuthMethod(authMethod), setLastSeenUserIdAfterAuth(nonRandomUUID(password)), password;
}
outputDebug(outputContent`Ensuring that the user is authenticated with the Storefront API with the following scopes:
${outputToken.json(scopes)}
`);
let tokens = await ensureAuthenticated({ storefrontRendererApi: { scopes } }, process.env, { forceRefresh });
if (!tokens.storefront)
throw new BugError("No storefront token found after ensuring authenticated");
return tokens.storefront;
}
async function ensureAuthenticatedAdmin(store, scopes = [], forceRefresh = !1, options = {}) {
outputDebug(outputContent`Ensuring that the user is authenticated with the Admin API with the following scopes for the store ${outputToken.raw(store)}:
${outputToken.json(scopes)}
`);
let tokens = await ensureAuthenticated({ adminApi: { scopes, storeFqdn: store } }, process.env, {
forceRefresh,
...options
});
if (!tokens.admin)
throw new BugError("No admin token found after ensuring authenticated");
return tokens.admin;
}
async function ensureAuthenticatedThemes(store, password, scopes = [], forceRefresh = !1) {
if (outputDebug(outputContent`Ensuring that the user is authenticated with the Theme API with the following scopes:
${outputToken.json(scopes)}
`), password) {
let session = { token: password, storeFqdn: await normalizeStoreFqdn(store) }, authMethod = isThemeAccessSession(session) ? "theme_access_token" : "custom_app_token";
return setLastSeenAuthMethod(authMethod), setLastSeenUserIdAfterAuth(nonRandomUUID(password)), session;
}
return ensureAuthenticatedAdmin(store, scopes, forceRefresh);
}
async function ensureAuthenticatedBusinessPlatform(scopes = []) {
outputDebug(outputContent`Ensuring that the user is authenticated with the Business Platform API with the following scopes:
${outputToken.json(scopes)}
`);
let tokens = await ensureAuthenticated({ businessPlatformApi: { scopes } }, process.env);
if (!tokens.businessPlatform)
throw new BugError("No business-platform token found after ensuring authenticated");
return tokens.businessPlatform;
}
function logout() {
return remove();
}
export {
ensureAuthenticatedPartners,
ensureAuthenticatedAppManagementAndBusinessPlatform,
ensureAuthenticatedStorefront,
ensureAuthenticatedAdmin,
ensureAuthenticatedThemes,
ensureAuthenticatedBusinessPlatform,
logout
};
//# sourceMappingURL=chunk-YF7CDH6L.js.map