|
| 1 | +import { GraphQLClient, GraphQLRequestClient } from '../graphql'; |
| 2 | +import debug from '../debug'; |
| 3 | +import { CacheClient, CacheOptions, MemoryCacheClient } from '../cache-client'; |
| 4 | +import { headlessSiteGroupingTemplate } from '../constants'; |
| 5 | + |
| 6 | +const defaultQuery = /* GraphQL */ ` |
| 7 | + { |
| 8 | + search( |
| 9 | + where: { name: "_templates", value: "${headlessSiteGroupingTemplate}", operator: CONTAINS } |
| 10 | + ) { |
| 11 | + results { |
| 12 | + ... on Item { |
| 13 | + name |
| 14 | + hostName: field(name: "Hostname") { |
| 15 | + value |
| 16 | + } |
| 17 | + virtualFolder: field(name: "VirtualFolder") { |
| 18 | + value |
| 19 | + } |
| 20 | + language: field(name: "Language") { |
| 21 | + value |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | +`; |
| 28 | + |
| 29 | +export type SiteInfo = { |
| 30 | + name: string; |
| 31 | + hostName: string; |
| 32 | + virtualFolder: string; |
| 33 | + language: string; |
| 34 | +}; |
| 35 | + |
| 36 | +export type GraphQLSiteInfoServiceConfig = CacheOptions & { |
| 37 | + /** |
| 38 | + * Your Graphql endpoint |
| 39 | + */ |
| 40 | + endpoint: string; |
| 41 | + /** |
| 42 | + * The API key to use for authentication |
| 43 | + */ |
| 44 | + apiKey: string; |
| 45 | +}; |
| 46 | + |
| 47 | +type GraphQLSiteInfoResponse = { |
| 48 | + search: { |
| 49 | + results: GraphQLSiteInfoResult[]; |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +type GraphQLSiteInfoResult = { |
| 54 | + name: string; |
| 55 | + hostName: { |
| 56 | + value: string; |
| 57 | + }; |
| 58 | + virtualFolder: { |
| 59 | + value: string; |
| 60 | + }; |
| 61 | + language: { |
| 62 | + value: string; |
| 63 | + }; |
| 64 | +}; |
| 65 | + |
| 66 | +export class GraphQLSiteInfoService { |
| 67 | + private graphQLClient: GraphQLClient; |
| 68 | + private cache: CacheClient<SiteInfo[]>; |
| 69 | + |
| 70 | + protected get query(): string { |
| 71 | + return defaultQuery; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates an instance of graphQL service to retrieve site configuration list from Sitecore |
| 76 | + * @param {GraphQLSiteInfoServiceConfig} config instance |
| 77 | + */ |
| 78 | + constructor(private config: GraphQLSiteInfoServiceConfig) { |
| 79 | + this.graphQLClient = this.getGraphQLClient(); |
| 80 | + this.cache = this.getCacheClient(); |
| 81 | + } |
| 82 | + |
| 83 | + async fetchSiteInfo(): Promise<SiteInfo[]> { |
| 84 | + const cachedResult = this.cache.getCacheValue(this.getCacheKey()); |
| 85 | + if (cachedResult) { |
| 86 | + return cachedResult; |
| 87 | + } |
| 88 | + |
| 89 | + const response = await this.graphQLClient.request<GraphQLSiteInfoResponse>(this.query); |
| 90 | + const result = response?.search?.results?.reduce<SiteInfo[]>((result, current) => { |
| 91 | + result.push({ |
| 92 | + name: current.name, |
| 93 | + hostName: current.hostName.value, |
| 94 | + virtualFolder: current.virtualFolder.value, |
| 95 | + language: current.language.value, |
| 96 | + }); |
| 97 | + return result; |
| 98 | + }, []); |
| 99 | + this.cache.setCacheValue(this.getCacheKey(), result); |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets cache client implementation |
| 105 | + * Override this method if custom cache needs to be used |
| 106 | + * @returns CacheClient instance |
| 107 | + */ |
| 108 | + protected getCacheClient(): CacheClient<SiteInfo[]> { |
| 109 | + return new MemoryCacheClient<SiteInfo[]>({ |
| 110 | + cacheEnabled: this.config.cacheEnabled ?? true, |
| 111 | + cacheTimeout: this.config.cacheTimeout ?? 10, |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default |
| 117 | + * library for fetching graphql data (@see GraphQLRequestClient). Override this method if you |
| 118 | + * want to use something else. |
| 119 | + * @returns {GraphQLClient} implementation |
| 120 | + */ |
| 121 | + protected getGraphQLClient(): GraphQLClient { |
| 122 | + return new GraphQLRequestClient(this.config.endpoint, { |
| 123 | + apiKey: this.config.apiKey, |
| 124 | + debugger: debug.multisite, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + private getCacheKey(): string { |
| 129 | + return 'siteinfo-service-cache'; |
| 130 | + } |
| 131 | +} |
0 commit comments