From 454f1a113181200898b46e2623df6502488d1ac0 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Mon, 21 Oct 2024 12:44:02 +0200 Subject: [PATCH] updata to data-ops 2.0 --- .eslintrc.json | 2 +- netlify.toml | 1 - {src => netlify}/functions/diff.ts | 41 ++++++++++++++++++++++-------- {src => netlify}/functions/sync.ts | 9 +++++-- tsconfig.app.json | 2 +- 5 files changed, 39 insertions(+), 16 deletions(-) rename {src => netlify}/functions/diff.ts (54%) rename {src => netlify}/functions/sync.ts (84%) diff --git a/.eslintrc.json b/.eslintrc.json index cb2b3e9..e839f52 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,7 @@ "overrides": [ { - "files": ["src/**/*.ts"], + "files": ["src/**/*.ts", "netlify/**/*.ts"], "excludedFiles": ["src/log.ts", "vite.config.ts"] } ], diff --git a/netlify.toml b/netlify.toml index 7845e3d..df9f053 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,7 +1,6 @@ [build] command = "npm run build" publish = "dist" - functions = "src/functions" [[redirects]] from = "/*" diff --git a/src/functions/diff.ts b/netlify/functions/diff.ts similarity index 54% rename from src/functions/diff.ts rename to netlify/functions/diff.ts index e34a837..0a37600 100644 --- a/src/functions/diff.ts +++ b/netlify/functions/diff.ts @@ -1,4 +1,4 @@ -// import { diffEnvironments } from "@kontent-ai/data-ops"; +import { syncDiff } from "@kontent-ai/data-ops"; import { Handler } from "@netlify/functions"; import { z } from "zod"; import { fromError } from "zod-validation-error"; @@ -8,6 +8,17 @@ const diffParamsSchema = z.strictObject({ sourceApiKey: z.string(), targetEnvironmentId: z.string(), targetApiKey: z.string(), + entities: z.array(z.enum([ + "contentTypes", + "contentTypeSnippets", + "taxonomies", + "collections", + "assetFolders", + "spaces", + "languages", + "webSpotlight", + "workflows", + ])), }); export const handler: Handler = async (event) => { @@ -35,15 +46,23 @@ export const handler: Handler = async (event) => { }; } - // const diffHtml = await diffEnvironments({ - // sourceEnvironmentId: result.data.sourceEnvironmentId, - // sourceApiKey: result.data.sourceApiKey, - // targetEnvironmentId: result.data.targetEnvironmentId, - // targetApiKey: result.data.targetApiKey, - // }); + try { + const diffHtml = await syncDiff({ + sourceEnvironmentId: result.data.sourceEnvironmentId, + sourceApiKey: result.data.sourceApiKey, + targetEnvironmentId: result.data.targetEnvironmentId, + targetApiKey: result.data.targetApiKey, + entities: result.data.entities, + }); - return { - statusCode: 200, - body: "diffHtml", - }; + return { + statusCode: 200, + body: JSON.stringify({ html: diffHtml }), + }; + } catch (e) { + return { + statusCode: 500, + body: JSON.stringify(e, Object.getOwnPropertyNames(e)), + }; + } }; diff --git a/src/functions/sync.ts b/netlify/functions/sync.ts similarity index 84% rename from src/functions/sync.ts rename to netlify/functions/sync.ts index a2b4f4c..ed9cb4c 100644 --- a/src/functions/sync.ts +++ b/netlify/functions/sync.ts @@ -1,4 +1,4 @@ -import { syncModelRun } from "@kontent-ai/data-ops"; +import { syncRun } from "@kontent-ai/data-ops"; import { Handler } from "@netlify/functions"; import { z } from "zod"; import { fromError } from "zod-validation-error"; @@ -47,7 +47,12 @@ export const handler: Handler = async (event) => { } try { - await syncModelRun(result.data); + await syncRun({ + ...result.data, + entities: Object.fromEntries( + result.data.entities.map(key => [key, key == "webSpotlight" ? true : () => true]), + ), + }); return { statusCode: 200, diff --git a/tsconfig.app.json b/tsconfig.app.json index f0a2350..d582820 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -20,5 +20,5 @@ "noUnusedParameters": true, "noFallthroughCasesInSwitch": true }, - "include": ["src"] + "include": ["src", "netlify"] }