Provide a public variable "NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND" to allow Keystatic to detect the storage client-side. This is fundamental to make the Admin work.

This commit is contained in:
gbuomprisco
2024-09-09 11:17:02 +02:00
parent 2931f6bb1d
commit c275526b07
3 changed files with 97 additions and 66 deletions

View File

@@ -1,18 +1,16 @@
import { z } from 'zod';
import { KeystaticStorage } from './keystatic-storage';
import { keyStaticConfig } from './keystatic.config';
/**
* The kind of storage to use for the Keystatic reader.
*/
const STORAGE_KIND = process.env.KEYSTATIC_STORAGE_KIND ?? 'local';
/**
* Creates a new Keystatic reader instance.
* @name createKeystaticReader
* @description Creates a new Keystatic reader instance.
*/
export async function createKeystaticReader() {
switch (STORAGE_KIND) {
switch (KeystaticStorage.kind) {
case 'local': {
// we need to import this dynamically to avoid parsing the package in edge environments
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { createReader } = await import('@keystatic/core/reader');
@@ -26,28 +24,34 @@ export async function createKeystaticReader() {
case 'github':
case 'cloud': {
const githubConfig = z
.object({
token: z.string({
description: 'The GitHub token to use for authentication.',
}),
repo: z.custom<`${string}/${string}`>(),
pathPrefix: z.string().optional(),
})
.parse({
token: process.env.KEYSTATIC_GITHUB_TOKEN,
repo: process.env.KEYSTATIC_STORAGE_REPO,
pathPrefix: process.env.KEYSTATIC_PATH_PREFIX,
});
const { createGitHubReader } = await import(
'@keystatic/core/reader/github'
);
return createGitHubReader(keyStaticConfig, githubConfig);
return createGitHubReader(
keyStaticConfig,
getKeystaticGithubConfiguration(),
);
}
default:
throw new Error(`Unknown storage kind`);
}
}
function getKeystaticGithubConfiguration() {
return z
.object({
token: z.string({
description:
'The GitHub token to use for authentication. Please provide the value through the "KEYSTATIC_GITHUB_TOKEN" environment variable.',
}),
repo: z.custom<`${string}/${string}`>(),
pathPrefix: z.string().optional(),
})
.parse({
token: process.env.KEYSTATIC_GITHUB_TOKEN,
repo: process.env.KEYSTATIC_STORAGE_REPO,
pathPrefix: process.env.KEYSTATIC_PATH_PREFIX,
});
}