commerce/lib/client-builder.ts
2023-11-16 15:40:57 +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;