Skip to content

Commit 0bd54a9

Browse files
committed
feat: add alpha modifier [TOL-2043]
1 parent 8b81413 commit 0bd54a9

File tree

7 files changed

+146
-20
lines changed

7 files changed

+146
-20
lines changed

lib/create-contentful-api.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ import {
1313
ContentfulClientApi,
1414
ContentType,
1515
ContentTypeCollection,
16-
LocaleCollection,
16+
EntryCollection,
17+
EntrySkeletonType,
1718
LocaleCode,
19+
LocaleCollection,
1820
Space,
21+
SyncOptions,
22+
SyncQuery,
1923
Tag,
2024
TagCollection,
21-
EntryCollection,
22-
SyncQuery,
23-
SyncOptions,
24-
EntrySkeletonType,
2525
} from './types'
26+
import { ChainOptions, ModifiersFromOptions } from './utils/client-helpers'
2627
import normalizeSearchParameters from './utils/normalize-search-parameters'
2728
import normalizeSelect from './utils/normalize-select'
2829
import resolveCircular from './utils/resolve-circular'
29-
import validateTimestamp from './utils/validate-timestamp'
30-
import { ChainOptions, ModifiersFromOptions } from './utils/client-helpers'
3130
import {
3231
validateLocaleParam,
3332
validateRemoveUnresolvedParam,
3433
validateResolveLinksParam,
3534
} from './utils/validate-params'
3635
import validateSearchParameters from './utils/validate-search-parameters'
36+
import validateTimestamp from './utils/validate-timestamp'
3737

3838
const ASSET_KEY_MAX_LIFETIME = 48 * 60 * 60
3939

@@ -75,6 +75,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
7575
context: Context
7676
path: string
7777
config?: any
78+
queryParams?: string
7879
}
7980

8081
interface PostConfig extends GetConfig {
@@ -96,11 +97,14 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
9697
return baseUrl
9798
}
9899

99-
async function get<T>({ context, path, config }: GetConfig): Promise<T> {
100+
async function get<T>({ context, path, config, queryParams }: GetConfig): Promise<T> {
100101
const baseUrl = getBaseUrl(context)
101102

103+
const url = baseUrl + path + (queryParams ?? '')
104+
105+
console.log('get', { url, queryParams, config })
102106
try {
103-
const response = await http.get(baseUrl + path, config)
107+
const response = await http.get(url, config)
104108
return response.data
105109
} catch (error) {
106110
errorHandler(error)
@@ -148,6 +152,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
148152
id: string,
149153
query,
150154
options: ChainOptions = {
155+
alpha_withContentSourceMaps: false,
151156
withAllLocales: false,
152157
withoutLinkResolution: false,
153158
withoutUnresolvableLinks: false,
@@ -193,6 +198,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
193198
async function makeGetEntries<EntrySkeleton extends EntrySkeletonType>(
194199
query,
195200
options: ChainOptions = {
201+
alpha_withContentSourceMaps: false,
196202
withAllLocales: false,
197203
withoutLinkResolution: false,
198204
withoutUnresolvableLinks: false,
@@ -224,13 +230,14 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
224230
query: Record<string, any>,
225231
options: Options,
226232
): Promise<EntryCollection<EntrySkeleton, ModifiersFromOptions<Options>, Locales>> {
227-
const { withoutLinkResolution, withoutUnresolvableLinks } = options
233+
const { withoutLinkResolution, withoutUnresolvableLinks, alpha_withContentSourceMaps } = options
228234

229235
try {
230236
const entries = await get({
231237
context: 'environment',
232238
path: 'entries',
233239
config: createRequestConfig({ query: normalizeSearchParameters(normalizeSelect(query)) }),
240+
queryParams: alpha_withContentSourceMaps ? '?includeContentSourceMaps=true' : undefined,
234241
})
235242

236243
return resolveCircular(entries, {
@@ -253,6 +260,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
253260
async function makeGetAssets(
254261
query: Record<string, any>,
255262
options: ChainOptions = {
263+
alpha_withContentSourceMaps: false,
256264
withAllLocales: false,
257265
withoutLinkResolution: false,
258266
withoutUnresolvableLinks: false,
@@ -287,6 +295,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
287295
id: string,
288296
query: Record<string, any>,
289297
options: ChainOptions = {
298+
alpha_withContentSourceMaps: false,
290299
withAllLocales: false,
291300
withoutLinkResolution: false,
292301
withoutUnresolvableLinks: false,
@@ -369,7 +378,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
369378
async function makePagedSync<EntrySkeleton extends EntrySkeletonType = EntrySkeletonType>(
370379
query: SyncQuery,
371380
syncOptions: SyncOptions,
372-
options: ChainOptions = {
381+
options: Omit<ChainOptions, 'alpha_withContentSourceMaps'> = {
373382
withAllLocales: false,
374383
withoutLinkResolution: false,
375384
withoutUnresolvableLinks: false,
@@ -397,6 +406,7 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
397406
function makeParseEntries<EntrySkeleton extends EntrySkeletonType>(
398407
data,
399408
options: ChainOptions = {
409+
alpha_withContentSourceMaps: false,
400410
withAllLocales: false,
401411
withoutLinkResolution: false,
402412
withoutUnresolvableLinks: false,

lib/make-client.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import createContentfulApi, { CreateContentfulApiParams } from './create-contentful-api'
2+
import { ContentfulClientApi } from './types'
23
import {
4+
ChainOption,
35
ChainOptions,
46
DefaultChainOption,
5-
ChainOption,
67
ModifiersFromOptions,
78
} from './utils/client-helpers'
8-
import { ContentfulClientApi } from './types'
99

1010
function create<OptionsType extends ChainOptions>(
1111
{ http, getGlobalOptions }: CreateContentfulApiParams,
@@ -29,6 +29,9 @@ function create<OptionsType extends ChainOptions>(
2929
Object.defineProperty(response, 'withoutUnresolvableLinks', {
3030
get: () => makeInnerClient({ ...options, withoutUnresolvableLinks: true }),
3131
})
32+
Object.defineProperty(response, 'alpha_withContentSourceMaps', {
33+
get: () => makeInnerClient({ ...options, alpha_withContentSourceMaps: true }),
34+
})
3235
return Object.create(response) as ContentfulClientApi<ModifiersFromOptions<OptionsType>>
3336
}
3437

@@ -48,6 +51,7 @@ export const makeClient = ({
4851
withoutLinkResolution: false,
4952
withAllLocales: false,
5053
withoutUnresolvableLinks: false,
54+
alpha_withContentSourceMaps: false,
5155
},
5256
)
5357

@@ -58,20 +62,32 @@ export const makeClient = ({
5862
withAllLocales: true,
5963
withoutLinkResolution: false,
6064
withoutUnresolvableLinks: false,
65+
alpha_withContentSourceMaps: false,
6166
})
6267
},
6368
get withoutLinkResolution() {
6469
return makeInnerClient<ChainOption<'WITHOUT_LINK_RESOLUTION'>>({
6570
withAllLocales: false,
6671
withoutLinkResolution: true,
6772
withoutUnresolvableLinks: false,
73+
alpha_withContentSourceMaps: false,
6874
})
6975
},
7076
get withoutUnresolvableLinks() {
7177
return makeInnerClient<ChainOption<'WITHOUT_UNRESOLVABLE_LINKS'>>({
7278
withAllLocales: false,
7379
withoutLinkResolution: false,
7480
withoutUnresolvableLinks: true,
81+
alpha_withContentSourceMaps: false,
82+
})
83+
},
84+
get alpha_withContentSourceMaps() {
85+
console.log('alpha_withContentSourceMaps')
86+
return makeInnerClient<ChainOption<'ALPHA_WITH_CONTENT_SOURCE_MAPS'>>({
87+
withAllLocales: false,
88+
withoutLinkResolution: false,
89+
withoutUnresolvableLinks: false,
90+
alpha_withContentSourceMaps: true,
7591
})
7692
},
7793
}

lib/types/client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Asset, AssetCollection, AssetFields } from './asset'
2+
import { AssetKey } from './asset-key'
13
import { ContentType, ContentTypeCollection } from './content-type'
2-
import { Space } from './space'
4+
import { Entry, EntryCollection } from './entry'
35
import { LocaleCode, LocaleCollection } from './locale'
46
import {
57
AssetQueries,
@@ -9,18 +11,17 @@ import {
911
EntrySkeletonType,
1012
TagQueries,
1113
} from './query'
14+
import { Space } from './space'
1215
import { SyncCollection, SyncOptions, SyncQuery } from './sync'
1316
import { Tag, TagCollection } from './tag'
14-
import { AssetKey } from './asset-key'
15-
import { Entry, EntryCollection } from './entry'
16-
import { Asset, AssetCollection, AssetFields } from './asset'
1717

1818
/**
1919
* Client chain modifiers used in all types that depend on the client configuration.
2020
* @category Client
2121
* @internal
2222
*/
2323
export type ChainModifiers =
24+
| 'ALPHA_WITH_CONTENT_SOURCE_MAPS'
2425
| 'WITH_ALL_LOCALES'
2526
| 'WITHOUT_LINK_RESOLUTION'
2627
| 'WITHOUT_UNRESOLVABLE_LINKS'
@@ -364,6 +365,13 @@ export interface ContentfulClientApi<Modifiers extends ChainModifiers> {
364365
query?: AssetsQueries<AssetFields, Modifiers>,
365366
): Promise<AssetCollection<Modifiers, Locales>>
366367

368+
/**
369+
* A client that will fetch assets and entries with Content Source Maps. Only available if not already enabled.
370+
*/
371+
alpha_withContentSourceMaps: 'ALPHA_WITH_CONTENT_SOURCE_MAPS' extends Modifiers
372+
? never
373+
: ContentfulClientApi<AddChainModifier<Modifiers, 'ALPHA_WITH_CONTENT_SOURCE_MAPS'>>
374+
367375
/**
368376
* A client that will fetch assets and entries with all locales. Only available if not already enabled.
369377
*/

lib/types/sys.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EntryFields } from './entry'
2-
import { SpaceLink, EnvironmentLink } from './link'
2+
import { EnvironmentLink, SpaceLink } from './link'
33

44
/**
55
* Definition of common part of system managed metadata
@@ -21,4 +21,20 @@ export interface EntitySys extends BaseSys {
2121
space: { sys: SpaceLink }
2222
environment: { sys: EnvironmentLink }
2323
locale?: string
24+
contentSourceMaps?: ContentSourceMaps
25+
}
26+
27+
export type ContentSourceMaps = {
28+
sys: {
29+
type: 'ContentSourceMaps'
30+
}
31+
mappings: Record<
32+
string,
33+
{
34+
source: {
35+
fieldType: number
36+
editorInterface: number
37+
}
38+
}
39+
>
2440
}

lib/utils/client-helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export type ChainOption<Modifiers extends ChainModifiers = ChainModifiers> = {
1616
: 'WITHOUT_UNRESOLVABLE_LINKS' extends Modifiers
1717
? true
1818
: false
19+
alpha_withContentSourceMaps: ChainModifiers extends Modifiers
20+
? boolean
21+
: 'ALPHA_WITH_CONTENT_SOURCE_MAPS' extends Modifiers
22+
? true
23+
: false
1924
}
2025

2126
export type DefaultChainOption = ChainOption<undefined>

test/integration/getEntry.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { EntryFields, EntrySkeletonType } from '../../lib'
12
import * as contentful from '../../lib/contentful'
23
import { localeSpaceParams, params, previewParams } from './utils'
3-
import { EntryFields, EntrySkeletonType } from '../../lib'
44

55
if (process.env.API_INTEGRATION_TESTS) {
66
params.host = '127.0.0.1:5000'
@@ -148,6 +148,41 @@ describe('getEntry via client chain modifiers', () => {
148148
})
149149
})
150150

151+
describe('client has alpha_withContentSourceMaps modifier', () => {
152+
test.only('client.alpha_withContentSourceMaps', async () => {
153+
const response = await client.alpha_withContentSourceMaps.getEntry(entryWithResolvableLink, {
154+
include: 2,
155+
})
156+
157+
console.log({ response })
158+
expect(response.fields.color).toHaveProperty('en-US')
159+
expect(response.fields.bestFriend).not.toHaveProperty('[en-US].sys.type', 'Link')
160+
})
161+
162+
test('client.alpha_withContentSourceMaps.withoutLinkResolution', async () => {
163+
const response = await client.alpha_withContentSourceMaps.withoutLinkResolution.getEntry(
164+
entryWithResolvableLink,
165+
{
166+
include: 2,
167+
},
168+
)
169+
expect(response.fields.color).toHaveProperty('en-US')
170+
expect(response.fields.bestFriend).toHaveProperty('[en-US].sys.type', 'Link')
171+
})
172+
173+
test('client.alpha_withContentSourceMaps.withoutUnresolvableLinks', async () => {
174+
const response = await client.alpha_withContentSourceMaps.withoutUnresolvableLinks.getEntry(
175+
entryWithUnresolvableLink,
176+
{
177+
include: 2,
178+
},
179+
)
180+
181+
expect(response.fields.color).toHaveProperty('en-US')
182+
expect(response.fields.bestFriend).toEqual({})
183+
})
184+
})
185+
151186
describe('client has withoutLinkResolution modifier', () => {
152187
test('client.withoutLinkResolution', async () => {
153188
const response = await client.withoutLinkResolution.getEntry(entryWithResolvableLink)

0 commit comments

Comments
 (0)