@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
75 lines • 2.33 kB
JavaScript
import { SessionsSchema } from './schema.js';
import { getSessions, removeCurrentSessionId, removeSessions, setSessions } from '../conf-store.js';
import { identityFqdn } from '../../../public/node/context/fqdn.js';
/**
* Serializes the session as a JSON and stores it in the system.
* @param session - the session to store.
*/
export async function store(sessions) {
const jsonSessions = JSON.stringify(sessions);
setSessions(jsonSessions);
}
/**
* Fetches the sessions from the local storage and returns it.
* If the format of the object is invalid, the method will discard it.
* @returns Returns a promise that resolves with the sessions object if it exists and is valid.
*/
export async function fetch() {
const content = getSessions();
if (!content) {
return undefined;
}
const contentJson = JSON.parse(content);
const parsedSessions = await SessionsSchema.safeParseAsync(contentJson);
if (parsedSessions.success) {
return parsedSessions.data;
}
else {
await remove();
return undefined;
}
}
/**
* Removes a session from the system.
*/
export async function remove() {
removeSessions();
removeCurrentSessionId();
}
/**
* Gets the session alias for a given user ID.
*
* @param userId - The user ID of the session to get the alias for.
* @returns The alias for the session if it exists, otherwise undefined.
*/
export async function getSessionAlias(userId) {
const sessions = await fetch();
if (!sessions)
return undefined;
const fqdn = await identityFqdn();
if (!sessions[fqdn] || !sessions[fqdn][userId])
return undefined;
return sessions[fqdn][userId].identity.alias;
}
/**
* Finds a session by its alias.
*
* @param alias - The alias to search for
* @returns The user ID if found, otherwise undefined
*/
export async function findSessionByAlias(alias) {
const sessions = await fetch();
if (!sessions)
return undefined;
const fqdn = await identityFqdn();
const fqdnSessions = sessions[fqdn];
if (!fqdnSessions)
return undefined;
for (const [userId, session] of Object.entries(fqdnSessions)) {
if (session.identity.alias === alias) {
return userId;
}
}
return undefined;
}
//# sourceMappingURL=store.js.map