Skip to content
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
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MAPI_KEY=
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm ci
Expand All @@ -27,9 +26,6 @@ jobs:

- name: Lint
run: npm run lint

- name: Security audit
run: npm audit

- name: Build
run: npm run build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ The loaded entities are memoized for the app's lifecycle, only triggering a re-r
1. Clone the repository
2. Install dependencies:
```bash
npm i
npm ci
```
3. Create a `.env` file in the root directory with:
```
Expand Down
75 changes: 43 additions & 32 deletions netlify/functions/mapiProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,61 @@ type RequestPayload = {
action: Action;
};

const createResponse = (statusCode: number, body: string) => ({
const createResponse = (statusCode: number, data: object) => ({
statusCode,
body,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});

export const handler: Handler = async (event: HandlerEvent) => {
if (event.httpMethod !== "POST") {
return createResponse(405, JSON.stringify({ body: `Method Not Allowed: ${event.httpMethod}`, statusCode: 405 }));
return createResponse(405, { message: `Method Not Allowed: ${event.httpMethod}`, errorCode: 405 });
}

try {
const { environmentId, action }: RequestPayload = JSON.parse(event.body || "{}");
if (!environmentId || !action) {
return createResponse(400, JSON.stringify({ body: "Missing environmentId or action", statusCode: 400 }));
}
const { environmentId, action }: RequestPayload = JSON.parse(event.body || "{}");

const apiKey = process.env.MAPI_KEY;
if (!apiKey) {
console.error("API key is missing from environment variables.");
return createResponse(500, JSON.stringify({ body: "Server misconfiguration", statusCode: 500 }));
}
if (!environmentId || !action) {
return createResponse(400, { message: "Missing environmentId or action", errorCode: 400 });
}

const client = new ManagementClient({ environmentId, apiKey });
const apiKey = process.env.MAPI_KEY;
if (!apiKey) {
console.error("API key is missing from environment variables.");
return createResponse(500, { message: "Missing MAPI key", errorCode: 500 });
}

const actions: ActionMap = {
listContentTypes: () => client.listContentTypes(),
listContentTypeSnippets: () => client.listContentTypeSnippets(),
listTaxonomies: () => client.listTaxonomies(),
};
const client = new ManagementClient({ environmentId, apiKey });
const actions: ActionMap = {
listContentTypes: () => client.listContentTypes(),
listContentTypeSnippets: () => client.listContentTypeSnippets(),
listTaxonomies: () => client.listTaxonomies(),
};

const actionFn = actions[action];
if (!actionFn) {
return createResponse(400, JSON.stringify({ body: `Unsupported action: "${action}"`, statusCode: 400 }));
}
const actionFn = actions[action];
if (!actionFn) {
return createResponse(400, { message: `Unsupported action: "${action}"`, errorCode: 400 });
}

try {
const response = await actionFn().toAllPromise();
return createResponse(200, JSON.stringify(response.data.items));
} catch (error) {
console.error("Error:", error);
return error instanceof SharedModels.ContentManagementBaseKontentError
? createResponse(error.errorCode, JSON.stringify({ body: error.message, statusCode: error.errorCode }))
: createResponse(
500,
JSON.stringify({ body: error instanceof Error ? error.message : "An unknown error occurred", statusCode: 500 }),
);
return createResponse(200, response.data.items);
} catch (apiError) {
console.error("Kontent.ai management API Error:", apiError);

if (apiError instanceof SharedModels.ContentManagementBaseKontentError) {
return createResponse(400, {
message: apiError.message,
errorCode: apiError.errorCode,
details: apiError.validationErrors,
requestId: apiError.requestId,
});
}

// Fallback for unknown errors.
return createResponse(500, {
message: apiError.message || "Unknown API error",
errorCode: 500,
details: typeof apiError === "object" ? JSON.stringify(apiError) : apiError,
});
}
};
Loading
Loading