first version which seems to run

This commit is contained in:
Gerhard Scheikl
2026-04-01 19:15:22 +02:00
parent 80e965aa57
commit fc107d576c
25 changed files with 963 additions and 40 deletions

View File

@@ -1,22 +1,23 @@
import { defineConfig } from "tinacms";
import { defineConfig, LocalAuthProvider } from "tinacms";
import { UsernamePasswordAuthJSProvider } from "tinacms-authjs/dist/tinacms";
import { schema } from "./schema";
const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === "true";
export const config = defineConfig({
telemetry: 'disabled',
contentApiUrlOverride: "/api/tina/gql",
authProvider: isLocal
? new LocalAuthProvider()
: new UsernamePasswordAuthJSProvider(),
schema,
clientId: process.env.NEXT_PUBLIC_TINA_CLIENT_ID,
branch:
process.env.TINA_GIT_BRANCH ||
process.env.NEXT_PUBLIC_TINA_BRANCH || // custom branch env override
process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF || // Vercel branch env
process.env.HEAD, // Netlify branch env
process.env.HEAD || // Netlify branch env
"main",
token: process.env.TINA_TOKEN,
media: {
// If you wanted cloudinary do this
// loadCustomStore: async () => {
// const pack = await import("next-tinacms-cloudinary");
// return pack.TinaCloudCloudinaryMediaStore;
// },
// this is the config for the tina cloud media store
tina: {
publicFolder: "public",
mediaRoot: "",
@@ -24,8 +25,8 @@ export const config = defineConfig({
accept: ["image/*", "video/*", "application/json", ".json"],
},
build: {
publicFolder: "public", // The public asset folder for your framework
outputFolder: "admin", // within the public folder
publicFolder: "public",
outputFolder: "admin",
basePath: process.env.TINA_BASE_PATH || "",
},
});

47
tina/database.ts Normal file
View File

@@ -0,0 +1,47 @@
import {
createDatabase,
createLocalDatabase,
FilesystemBridge,
} from "@tinacms/datalayer";
import { RedisLevel } from "upstash-redis-level";
import { Redis } from "@upstash/redis";
import { GiteaGitProvider } from "./gitea-git-provider";
const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === "true";
const branch = process.env.TINA_GIT_BRANCH || "main";
function createProductionDatabase() {
const giteaUrl = process.env.GITEA_URL;
const giteaToken = process.env.GITEA_TOKEN;
const giteaOwner = process.env.GITEA_OWNER;
const giteaRepo = process.env.GITEA_REPO;
const kvUrl = process.env.KV_REST_API_URL;
const kvToken = process.env.KV_REST_API_TOKEN;
if (!giteaUrl || !giteaToken || !giteaOwner || !giteaRepo) {
// During tinacms build (schema generation), env vars may not be available.
// Fall back to local database for the build step.
return createLocalDatabase();
}
return createDatabase({
gitProvider: new GiteaGitProvider({
owner: giteaOwner,
repo: giteaRepo,
token: giteaToken,
branch,
baseUrl: giteaUrl,
}),
databaseAdapter: new RedisLevel<string, Record<string, unknown>>({
redis: new Redis({
url: kvUrl || "http://localhost:8079",
token: kvToken || "example_token",
}) as any,
debug: process.env.DEBUG === "true" || false,
}),
bridge: new FilesystemBridge(process.cwd()),
namespace: branch,
});
}
export default isLocal ? createLocalDatabase() : createProductionDatabase();

123
tina/gitea-git-provider.ts Normal file
View File

@@ -0,0 +1,123 @@
import { Base64 } from "js-base64";
import type { GitProvider } from "@tinacms/datalayer";
export interface GiteaGitProviderOptions {
owner: string;
repo: string;
token: string;
branch: string;
baseUrl: string;
commitMessage?: string;
rootPath?: string;
}
export class GiteaGitProvider implements GitProvider {
owner: string;
repo: string;
branch: string;
baseUrl: string;
commitMessage: string;
rootPath?: string;
private token: string;
constructor(args: GiteaGitProviderOptions) {
this.owner = args.owner;
this.repo = args.repo;
this.branch = args.branch;
this.baseUrl = args.baseUrl.replace(/\/$/, "");
this.commitMessage = args.commitMessage || "Edited with TinaCMS";
this.rootPath = args.rootPath;
this.token = args.token;
}
private getApiUrl(path: string): string {
return `${this.baseUrl}/api/v1/repos/${this.owner}/${this.repo}/contents/${path}`;
}
private getHeaders(): Record<string, string> {
return {
Authorization: `token ${this.token}`,
"Content-Type": "application/json",
Accept: "application/json",
};
}
private getKeyWithPath(key: string): string {
return this.rootPath ? `${this.rootPath}/${key}` : key;
}
private async getFileSha(
keyWithPath: string
): Promise<string | undefined> {
try {
const url = `${this.getApiUrl(keyWithPath)}?ref=${encodeURIComponent(this.branch)}`;
const response = await fetch(url, {
method: "GET",
headers: this.getHeaders(),
});
if (!response.ok) {
return undefined;
}
const data = await response.json();
return data.sha;
} catch {
return undefined;
}
}
async onPut(key: string, value: string): Promise<void> {
const keyWithPath = this.getKeyWithPath(key);
const sha = await this.getFileSha(keyWithPath);
const body: Record<string, unknown> = {
message: this.commitMessage,
content: Base64.encode(value),
branch: this.branch,
};
if (sha) {
body.sha = sha;
}
const response = await fetch(this.getApiUrl(keyWithPath), {
method: sha ? "PUT" : "POST",
headers: this.getHeaders(),
body: JSON.stringify(body),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to save ${keyWithPath}: ${response.status} ${errorText}`
);
}
}
async onDelete(key: string): Promise<void> {
const keyWithPath = this.getKeyWithPath(key);
const sha = await this.getFileSha(keyWithPath);
if (!sha) {
throw new Error(
`Could not find file ${keyWithPath} in repo ${this.owner}/${this.repo}`
);
}
const response = await fetch(this.getApiUrl(keyWithPath), {
method: "DELETE",
headers: this.getHeaders(),
body: JSON.stringify({
message: this.commitMessage,
branch: this.branch,
sha,
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to delete ${keyWithPath}: ${response.status} ${errorText}`
);
}
}
}

View File

@@ -1,4 +1,5 @@
import { type Collection, defineSchema } from "tinacms";
import { TinaUserCollection } from "tinacms-authjs/dist/tinacms";
import API_Schema_Collection from "./collections/API-schema";
import docsCollection from "./collections/docs";
import docsNavigationBarCollection from "./collections/navigation-bar";
@@ -11,5 +12,6 @@ export const schema = defineSchema({
//TODO: Investigate why casting as unknown works
API_Schema_Collection as unknown as Collection,
Settings as unknown as Collection,
TinaUserCollection as Collection,
],
});