@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
70 lines • 2.16 kB
JavaScript
import { zod } from '../../../public/node/schema.js';
const DateSchema = zod.preprocess((arg) => {
if (typeof arg === 'string' || arg instanceof Date)
return new Date(arg);
return null;
}, zod.date());
/**
* The schema represents an Identity token.
*/
const IdentityTokenSchema = zod.object({
accessToken: zod.string(),
refreshToken: zod.string(),
expiresAt: DateSchema,
scopes: zod.array(zod.string()),
userId: zod.string(),
});
/**
* The schema represents an application token.
*/
const ApplicationTokenSchema = zod.object({
accessToken: zod.string(),
expiresAt: DateSchema,
scopes: zod.array(zod.string()),
});
/**
* This schema represents the format of the session
* that we cache in the system to avoid unnecessary
* token exchanges.
*
* @example
* ```
* {
* "accounts.shopify.com": {
* "identity": {...} // IdentityTokenSchema
* "applications": {
* "${domain}-application-id": { // Admin APIs includes domain in the key
* "accessToken": "...",
* },
* "$application-id": { // ApplicationTokenSchema
* "accessToken": "...",
* },
* }
* },
* "identity.spin.com": {...}
* }
* ```
*/
export const SessionSchema = zod.object({}).catchall(zod.object({
/**
* It contains the identity token. Before usint it, we exchange it
* to get a token that we can use with different applications. The exchanged
* tokens for the applications are stored under applications.
*/
identity: IdentityTokenSchema,
/**
* It contains exchanged tokens for the applications the CLI
* authenticates with. Tokens are scoped under the fqdn of the applications.
*/
applications: zod.object({}).catchall(ApplicationTokenSchema),
}));
/**
* Confirms that a given identity token structure matches what the schema currently defines.
*
* A full re-auth is the expectation if this validation fails.
*/
export function validateCachedIdentityTokenStructure(identityToken) {
const parsed = IdentityTokenSchema.safeParse(identityToken);
return parsed.success;
}
//# sourceMappingURL=schema.js.map