Skip to content

Commit

Permalink
feat(contentful): allow passing transformed locale to CMS
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinol committed Aug 7, 2020
1 parent d240907 commit 8ef9df0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class Contentful implements cms.CMS {
const deliveryApi = new AdaptorDeliveryApi(
options.disableCache
? client
: new CachedClientApi(client, options.cacheTtlMs)
: new CachedClientApi(client, options.cacheTtlMs),
options
)
const delivery = new IgnoreFallbackDecorator(deliveryApi)
this._contents = new ContentsDelivery(delivery)
Expand Down
23 changes: 12 additions & 11 deletions packages/botonic-plugin-contentful/src/contentful/delivery-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export interface DeliveryApi {
* Manages the {@link Context}, parses Content's Id and ContentType from the Contentful entries...
*/
export class AdaptorDeliveryApi implements DeliveryApi {
constructor(readonly client: ReducedClientApi) {}
constructor(
readonly client: ReducedClientApi,
readonly options: ContentfulOptions
) {}

async getAsset(id: string, query?: any): Promise<contentful.Asset> {
return this.client.getAsset(id, query)
Expand All @@ -48,19 +51,14 @@ export class AdaptorDeliveryApi implements DeliveryApi {
context: Context,
query: any = {}
): Promise<contentful.Entry<T>> {
return this.client.getEntry<T>(
id,
AdaptorDeliveryApi.queryFromContext(context, query)
)
return this.client.getEntry<T>(id, this.queryFromContext(context, query))
}

async getEntries<T>(
context: Context,
query: any = {}
): Promise<contentful.EntryCollection<T>> {
return this.client.getEntries<T>(
AdaptorDeliveryApi.queryFromContext(context, query)
)
return this.client.getEntries<T>(this.queryFromContext(context, query))
}

async getContentType(id: string): Promise<contentful.ContentType> {
Expand All @@ -72,9 +70,12 @@ export class AdaptorDeliveryApi implements DeliveryApi {
}
}

private static queryFromContext(context: Context, query: any = {}): any {
if (context.locale) {
query['locale'] = context.locale
private queryFromContext(context: Context, query: any = {}): any {
const locale = this.options.cmsLocale
? this.options.cmsLocale(context.locale)
: context.locale
if (locale) {
query['locale'] = locale
}
return query
}
Expand Down
5 changes: 4 additions & 1 deletion packages/botonic-plugin-contentful/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cms from './cms'
import { KeywordsOptions, Normalizer, StemmingBlackList } from './nlp'
import { KeywordsOptions, Locale, Normalizer, StemmingBlackList } from './nlp'
import { Search } from './search'
import { BotonicMsgConverter } from './render'
import { Contentful } from './contentful/cms-contentful'
Expand Down Expand Up @@ -44,6 +44,9 @@ export interface ContentfulOptions extends OptionsBase, ContentfulCredentials {

contentfulFactory?: (opts: ContentfulOptions) => cms.CMS

/** For locales not supported by the CMS (eg. English on a non-English country) */
cmsLocale?: (locale?: Locale) => Locale | undefined

/**
* If the delivery of a part of a content fails (eg. a referenced content),
* the flag defines whether the content should be partially delivered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TopContentId, ContentType, MESSAGE_CONTENT_TYPES } from '../../src/cms'
import { ContentType, MESSAGE_CONTENT_TYPES, TopContentId } from '../../src/cms'
import { testContentful } from './contentful.helper'
import { ENGLISH, SPANISH } from '../../src/nlp'
import { TEST_POST_FAQ1_ID } from './contents/text.test'

const TEST_IMAGE = '3xjvpC7d7PYBmiptEeygfd'

Expand All @@ -16,3 +18,11 @@ test('TEST: contentful delivery checks that we get the requested message type',
}
}
}, 10000)

test('TEST: contentful cmsLocale', async () => {
const sut = testContentful({ cmsLocale: locale => ENGLISH })

const text = await sut.text(TEST_POST_FAQ1_ID, { locale: SPANISH })

expect(text.text).toEqual('How to find your order')
})

0 comments on commit 8ef9df0

Please sign in to comment.