commerce/lib/client-builder.ts
leonmargaritis feaa87a9c8
feat: init commercetools setup (#1)
* feat: init commercetools setup

---------

Co-authored-by: Anja-Janina Stiefermann <anja.stiefermann@kernpunkt.de>
2023-11-17 11:39:54 +01:00

41 lines
1.1 KiB
TypeScript

import {
ClientBuilder,
type AuthMiddlewareOptions,
type HttpMiddlewareOptions
} from "@commercetools/sdk-client-v2";
import { createApiBuilderFromCtpClient } from "@commercetools/platform-sdk";
const authUrl = process.env.CTP_AUTH_URL as string;
const apiUrl = process.env.CTP_API_URL as string;
const clientId = process.env.CTP_CLIENT_ID as string;
const clientSecret = process.env.CTP_CLIENT_SECRET as string;
const projectKey = process.env.CTP_PROJECT_KEY as string;
const scopes = [process.env.CTP_SCOPES as string];
const authMiddlewareOptions: AuthMiddlewareOptions = {
host: authUrl,
projectKey,
credentials: {
clientId,
clientSecret
},
scopes
};
const httpMiddlewareOptions: HttpMiddlewareOptions = {
host: apiUrl
};
const ctpClient = new ClientBuilder()
.withProjectKey(projectKey)
.withClientCredentialsFlow(authMiddlewareOptions)
.withHttpMiddleware(httpMiddlewareOptions)
.withLoggerMiddleware()
.build();
const apiRoot = createApiBuilderFromCtpClient(ctpClient).withProjectKey({
projectKey
});
export default apiRoot;