next.js/examples/cms-sitecore-xmcloud/src/lib/layout-service-factory.ts
layout-service-factory.ts35 lines926 B
import {
  LayoutService,
  RestLayoutService,
  GraphQLLayoutService,
  constants,
} from "@sitecore-jss/sitecore-jss-nextjs";
import config from "temp/config";

/**
 * Factory responsible for creating a LayoutService instance
 */
export class LayoutServiceFactory {
  /**
   * @param {string} siteName site name
   * @returns {LayoutService} service instance
   */
  create(siteName: string): LayoutService {
    return process.env.FETCH_WITH === constants.FETCH_WITH.GRAPHQL
      ? new GraphQLLayoutService({
          endpoint: config.graphQLEndpoint,
          apiKey: config.sitecoreApiKey,
          siteName,
        })
      : new RestLayoutService({
          apiHost: config.sitecoreApiHost,
          apiKey: config.sitecoreApiKey,
          siteName,
          configurationName: "default",
        });
  }
}

/** LayoutServiceFactory singleton */
export const layoutServiceFactory = new LayoutServiceFactory();
Quest for Codev2.0.0
/
SIGN IN