@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
236 lines (235 loc) • 7.82 kB
JavaScript
import * as z from "zod/mini";
import { rpcMethods } from "@scayle/storefront-core";
import { builtinDrivers } from "unstorage";
export const RedirectsSchema = z.object({
enabled: z.boolean(),
queryParamWhitelist: z.optional(z.array(z.string())),
strategy: z.optional(z.enum(["before-request", "on-missing"]))
});
const SessionSchema = z.object({
/**
* The `sameSite` policy to use for the session cookie
*/
sameSite: z.optional(z.enum(["lax", "strict", "none"])),
/**
* The default `maxAge` (in seconds) set on the session cookie and default TTL for session store
*/
maxAge: z.optional(z.number()),
/**
* The name used for the session cookie
*/
cookieName: z.optional(z.string()),
/**
* The secret used for signing session cookies. If an array is passed, the last
* value is used for signing new cookies, but all values are used to verify cookies.
*/
secret: z.optional(z.union([z.string(), z.array(z.string())])),
/**
* Controls the domain option on the session cookie
*/
domain: z.optional(z.string())
});
const SapiSchema = z.object({
host: z.url(),
token: z.string().check(z.minLength(1))
});
const OAuthSchema = z.object({
apiHost: z.url(),
clientId: z.union([z.number(), z.string().check(z.minLength(1))]),
clientSecret: z.string().check(z.minLength(1))
});
const IdpSchema = z.object({
enabled: z.boolean(),
idpKeys: z.array(z.string()),
idpRedirectURL: z.string()
});
const LegacySchema = z.object({
enableSessionMigration: z.optional(z.boolean())
});
const builtinDriverKeys = Object.keys(builtinDrivers);
const compressionSchema = z.optional(
z.enum(["deflate", "gzip", "brotli", "none"])
);
const scayleKvConfigSchema = z.object({
driver: z.literal("scayleKv"),
compression: compressionSchema,
ttl: z.optional(z.number()),
disableClusterMode: z.optional(z.boolean())
});
const StorageSchema = z.discriminatedUnion("driver", [
z.object({
driver: z.enum(builtinDriverKeys),
compression: compressionSchema,
url: z.optional(z.url()),
token: z.optional(z.string()),
host: z.optional(z.string()),
port: z.optional(z.number()),
username: z.optional(z.string()),
password: z.optional(z.string()),
tls: z.optional(z.boolean()),
ttl: z.optional(z.number())
}),
scayleKvConfigSchema
]);
const AppKeysSchema = z.object({
wishlistKey: z.string(),
basketKey: z.string(),
hashAlgorithm: z.enum(["md5", "sha256", "none"])
});
const CheckoutShopConfigSchema = z.object({
token: z.string().check(z.minLength(1)),
secret: z.string().check(z.minLength(1)),
host: z.url(),
user: z.union([z.string(), z.number()]),
cbdExpiration: z.optional(z.number()),
shopId: z.optional(z.number())
});
const emptyString = z.string().check(
z.refine((val) => val === "", {
error: "Expected boolean, received string"
})
);
const ShopConfigSchema = z.object({
idp: z.optional(IdpSchema),
shopId: z.number(),
path: z.optional(z.union([z.string(), z.array(z.string())])),
domain: z.optional(z.string()),
locale: z.string(),
auth: z.object({
resetPasswordUrl: z.string()
}),
currency: z.string(),
checkout: CheckoutShopConfigSchema,
appKeys: z.optional(AppKeysSchema),
currencyFractionDigits: z.optional(z.number()),
isEnabled: z.optional(z.boolean()),
sessionConfig: z.optional(SessionSchema),
sapi: z.optional(SapiSchema),
/**
* @deprecated The `storage` option within `shopConfig` is deprecated. Consider using nuxt storage configuration instead.
*/
storage: z.optional(
z.object({
session: z.optional(StorageSchema),
cache: z.optional(StorageSchema)
})
),
isDefault: z.optional(z.union([z.boolean(), emptyString]))
});
const CacheSchema = z.object({
auth: z.optional(
z.object({
username: z.string(),
password: z.string()
})
),
enabled: z.optional(z.boolean())
});
const ShopSelectorSchema = z.enum(["path", "domain", "path_or_default"]);
const StorefrontRuntimeConfigSchema = z.object({
/**
* Determines how the application identifies and switches between different shops.
* It influences how the `domain` and `path` properties are used within the `shops` configuration.
*
* @see https://scayle.dev/en/core-documentation/storefront-guide/storefront-application/features/internationalization/configuration#routing-configuration
*/
shopSelector: ShopSelectorSchema,
/**
* Controls how the application handles URL redirects by leveraging the Storefront API,
* which is synchronized with the SCAYLE Panel. When enabled,
* the Storefront Core intercepts requests and checks for potential
* redirects via the Storefront API. If a match is found, a 30x HTTP response
* is returned with the target in the `Location` header and the appropriate
* status code.
*
* @see https://scayle.dev/en/core-documentation/storefront-guide/storefront-application/features/redirects
*/
redirects: z.optional(RedirectsSchema),
/**
* Configures the available shops in the Storefront Application. Each shop represents a specific region or language,
* with each shop identified by its unique shop ID from the SCAYLE tenant.
*
* @see https://scayle.dev/en/core-documentation/storefront-guide/storefront-application/technical-foundation/configuration#shops
*/
shops: z.record(z.string(), ShopConfigSchema),
session: z.optional(SessionSchema),
/**
* @deprecated The `storage` option within `storefront` is deprecated. Consider using nuxt storage configuration instead.
*/
storage: z.optional(
z.object({
session: z.optional(StorageSchema),
cache: z.optional(StorageSchema)
})
),
oauth: OAuthSchema,
appKeys: AppKeysSchema,
cache: z.optional(CacheSchema),
publicShopData: z.optional(z.array(z.string())),
idp: z.optional(IdpSchema),
sapi: SapiSchema,
/**
* Collection of feature flags regarding legacy features and functionalities.
*/
legacy: z.optional(LegacySchema),
/**
* @hidden
* Undocumented property intended for internal usage only
*/
internalAccessHeader: z.optional(z.string())
});
const logLevel = z.enum([
"debug",
"info",
"warn",
"error"
]);
const StorefrontPublicRuntimeConfigSchema = z.object({
log: z.object({
name: z.string(),
level: logLevel,
json: z.optional(z.boolean()),
output: z._default(z.enum(["stdout", "stderr", "auto"]), "auto")
}),
/** Collection of feature flags regarding legacy features and functionalities */
legacy: z.optional(
z.object({
enableDefaultGetCachedDataOverride: z.optional(z.boolean())
})
),
/**
* Controls the default lazy loading behavior for useRpc.
* If set to true,the useRpc composable will not block during data fetching.
*
* @default false
*/
rpcDefaultLazy: z.optional(z.boolean())
});
export const RuntimeConfigSchema = z.object({
storefront: StorefrontRuntimeConfigSchema,
public: z.object({
storefront: StorefrontPublicRuntimeConfigSchema
})
});
const AvailableRpcMethods = z.enum(
Object.keys(rpcMethods)
);
const ModuleOptionSchema = z.object({
/**
* The RPC directory where the custom application RPCs are defined.
* The directory should have an `index.ts` file where all RPCs are exported using their name.
* By default this will be `./rpcMethods`.
*/
rpcDir: z.optional(z.string()),
/**
* The RPC method names which are exported from the `rpcDir`.
* Usually this can just be `Object.keys(rpcMethodsDir)`.
*/
rpcMethodNames: z.optional(z.array(z.string())),
/** Specify explicitly overridden RPC methods that are provided by the Storefront Core package. */
rpcMethodOverrides: z.optional(z.array(AvailableRpcMethods)),
/**
* The base path for API routes.
*/
apiBasePath: z.optional(z.string())
});