Update Supabase version and improve Keystatic's configurations

This commit updates the version of Supabase in multiple packages. It also introduces specificity to environment variables in Keystatic configuration and adds a method to create a 'KeyStatic' reader based on the storage kind. Furthermore, minor adjustments have been made to keep the environment variable names consistent.
This commit is contained in:
giancarlo
2024-04-19 14:40:11 +08:00
parent d733c2e653
commit 19b8cc793e
15 changed files with 124 additions and 63 deletions

View File

@@ -1,17 +1,14 @@
import { Entry, createReader } from '@keystatic/core/reader';
import { Cms, CmsClient } from '@kit/cms';
import config from './keystatic.config';
const reader = createReader('.', config);
type EntryProps = Entry<(typeof config)['collections']['posts']>;
import { createKeystaticReader } from './create-reader';
import { PostEntryProps } from './keystatic.config';
export class KeystaticClient implements CmsClient {
async getContentItems(options: Cms.GetContentItemsOptions) {
const reader = await createKeystaticReader();
const collection =
options.collection as keyof (typeof config)['collections'];
options.collection as keyof (typeof reader)['collections'];
if (!reader.collections[collection]) {
throw new Error(`Collection ${collection} not found`);
@@ -19,6 +16,8 @@ export class KeystaticClient implements CmsClient {
const docs = await reader.collections[collection].all();
console.log(docs);
const startOffset = options?.offset ?? 0;
const endOffset = startOffset + (options?.limit ?? 10);
@@ -87,8 +86,10 @@ export class KeystaticClient implements CmsClient {
}
async getContentItemBySlug(params: { slug: string; collection: string }) {
const reader = await createKeystaticReader();
const collection =
params.collection as keyof (typeof config)['collections'];
params.collection as keyof (typeof reader)['collections'];
if (!reader.collections[collection]) {
throw new Error(`Collection ${collection} not found`);
@@ -127,7 +128,7 @@ export class KeystaticClient implements CmsClient {
private async mapPost<
Type extends {
entry: EntryProps;
entry: PostEntryProps;
slug: string;
},
>(item: Type, children: Type[] = []): Promise<Cms.ContentItem> {

View File

@@ -0,0 +1,41 @@
import { z } from 'zod';
/**
* Create a KeyStatic reader based on the storage kind.
*/
export async function createKeystaticReader() {
switch (process.env.KEYSTATIC_STORAGE_KIND ?? 'local') {
case 'local': {
const { default: config } = await import('./keystatic.config');
const { createReader } = await import('@keystatic/core/reader');
return createReader('.', config);
}
case 'github':
case 'cloud': {
const { default: config } = await import('./keystatic.config');
const githubConfig = z
.object({
token: z.string(),
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(config, githubConfig);
}
default:
throw new Error(`Unknown storage kind`);
}
}

View File

@@ -1,7 +1,7 @@
import { makeRouteHandler } from '@keystatic/next/route-handler';
import keystaticConfig from './keystatic.config';
import config from './keystatic.config';
export const { POST, GET } = makeRouteHandler({
config: keystaticConfig,
config,
});

View File

@@ -1,4 +1,5 @@
import { collection, config, fields } from '@keystatic/core';
import { Entry } from '@keystatic/core/reader';
import { z } from 'zod';
const local = z.object({
@@ -14,19 +15,30 @@ const github = z.object({
kind: z.literal('github'),
repo: z.custom<`${string}/${string}`>(),
branchPrefix: z.string().optional(),
pathPrefix: z.string().optional(),
githubToken: z.string({
required_error: 'Please provide a GitHub token',
}),
});
const storage = z.union([local, cloud, github]).parse({
kind: process.env.KEYSTATIC_STORAGE_KIND ?? 'local',
kind: process.env.KEYSTATIC_STORAGE_KIND,
project: process.env.KEYSTATIC_STORAGE_PROJECT,
repo: process.env.KEYSTATIC_STORAGE_REPO,
branchPrefix: process.env.KEYSTATIC_STORAGE_BRANCH_PREFIX,
githubToken: process.env.KEYSTATIC_GITHUB_TOKEN,
pathPrefix: process.env.KEYSTATIC_PATH_PREFIX,
});
const path = process.env.KEY_STATIC_PATH ?? 'content';
export default createKeyStaticConfig(path);
const keyStaticConfig = createKeyStaticConfig(path);
export default keyStaticConfig;
export type PostEntryProps = Entry<
(typeof keyStaticConfig)['collections']['posts']
>;
function createKeyStaticConfig(path: string) {
return config({