Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devrel 1232/netlify functions #2

Merged
merged 6 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

"overrides": [
{
"files": ["src/**/*.ts"],
"files": ["src/**/*.ts", "netlify/**/*.ts"],
"excludedFiles": ["src/log.ts", "vite.config.ts"]
}
],

"parserOptions": {
"project": ["tsconfig.app.json"]
"project": ["tsconfig.app.json", "tsconfig.node.json"]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# Local Netlify folder
.netlify
26 changes: 13 additions & 13 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"extends": "https://raw.githubusercontent.com/kontent-ai/dprint-config/main/dprint.json",
"plugins": [
"https://plugins.dprint.dev/g-plane/markup_fmt-v0.11.0.wasm"
],
"includes": [
"src/**/*.{ts,tsx,js,jsx,json,html}",
"tests/**/*.{ts,tsx,js,jsx,json,html}"
],
"excludes": [
"tests/**/*.snap.html"
]
}
"extends": "https://raw.githubusercontent.com/kontent-ai/dprint-config/main/dprint.json",
"plugins": [
"https://plugins.dprint.dev/g-plane/markup_fmt-v0.11.0.wasm"
],
"includes": [
"src/**/*.{ts,tsx,js,jsx,json,html}",
"tests/**/*.{ts,tsx,js,jsx,json,html}",
"netlify/**/*.{ts,tsx,js,jsx,json,html}"
],
"excludes": [
"tests/**/*.snap.html"
]
}
1 change: 0 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[build]
command = "npm run build"
publish = "dist"
functions = "src/functions"

[[redirects]]
from = "/*"
Expand Down
68 changes: 68 additions & 0 deletions netlify/functions/diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { syncDiff } from "@kontent-ai/data-ops";
import { Handler } from "@netlify/functions";
import { z } from "zod";
import { fromError } from "zod-validation-error";

const diffParamsSchema = z.strictObject({
sourceEnvironmentId: z.string(),
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) => {
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: "Method not allowed",
};
}

const contentType = event.headers["content-type"] || event.headers["Content-Type"];
if (!contentType || !contentType.includes("application/json")) {
return {
statusCode: 400,
body: "Content-Type must be application/json",
};
}

const result = diffParamsSchema.safeParse(JSON.parse(event.body ?? ""));

if (!result.success) {
return {
statusCode: 400,
body: fromError(result.error).message,
};
}

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: JSON.stringify({ html: diffHtml }),
};
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify(e, Object.getOwnPropertyNames(e)),
};
}
};
66 changes: 66 additions & 0 deletions netlify/functions/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { syncRun } from "@kontent-ai/data-ops";
import { Handler } from "@netlify/functions";
import { z } from "zod";
import { fromError } from "zod-validation-error";

const diffParamsSchema = z.strictObject({
sourceEnvironmentId: z.string(),
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) => {
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: "Method not allowed",
};
}

const contentType = event.headers["content-type"] || event.headers["Content-Type"];
if (!contentType || !contentType.includes("application/json")) {
return {
statusCode: 400,
body: "Content-Type must be application/json",
};
}

const result = diffParamsSchema.safeParse(JSON.parse(event.body ?? ""));

if (!result.success) {
return {
statusCode: 400,
body: fromError(result.error).message,
};
}

try {
await syncRun({
...result.data,
entities: Object.fromEntries(
result.data.entities.map(key => [key, key == "webSpotlight" ? true : () => true]),
),
});

return {
statusCode: 200,
};
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify(e, Object.getOwnPropertyNames(e)),
};
}
};
Loading
Loading