mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 07:56:59 +00:00
* feat: init commercetools setup --------- Co-authored-by: Anja-Janina Stiefermann <anja.stiefermann@kernpunkt.de>
41 lines
1.1 KiB
TypeScript
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;
|