commerce/lib/client-builder.ts
2023-11-22 17:30:52 +01:00

40 lines
1.0 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)
.build();
const apiRoot = createApiBuilderFromCtpClient(ctpClient).withProjectKey({
projectKey
});
export default apiRoot;