Skip to content

Commit 8e7fe15

Browse files
committed
feat: ensure relevant content source maps fields are selected
1 parent b600e3f commit 8e7fe15

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/create-contentful-api.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from './utils/validate-params'
3737
import validateSearchParameters from './utils/validate-search-parameters'
3838
import validateTimestamp from './utils/validate-timestamp'
39+
import getQuerySelectionSet from './utils/query-selection-set'
3940

4041
const ASSET_KEY_MAX_LIFETIME = 48 * 60 * 60
4142

@@ -107,6 +108,18 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
107108

108109
if (areAllowed) {
109110
query.includeContentSourceMaps = true
111+
112+
// Ensure that content source maps related attributes are selected
113+
if (query.select) {
114+
const selection = getQuerySelectionSet(query)
115+
116+
if (!selection.has('sys')) {
117+
selection.add('sys.contentSourceMaps')
118+
selection.add('sys.contentSourceMapsLookup')
119+
}
120+
121+
query.select = [...selection].join(',')
122+
}
110123
}
111124

112125
return query

lib/utils/normalize-select.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import getQuerySelectionSet from './query-selection-set'
2+
13
/*
24
* sdk relies heavily on sys metadata
35
* so we cannot omit the sys property on sdk level entirely
@@ -9,13 +11,7 @@ export default function normalizeSelect(query) {
911
return query
1012
}
1113

12-
// The selection of fields for the query is limited
13-
// Get the different parts that are listed for selection
14-
const allSelects = Array.isArray(query.select)
15-
? query.select
16-
: query.select.split(',').map((q) => q.trim())
17-
// Move the parts into a set for easy access and deduplication
18-
const selectedSet = new Set(allSelects)
14+
const selectedSet = getQuerySelectionSet(query)
1915

2016
// If we already select all of `sys` we can just return
2117
// since we're anyway fetching everything that is needed

lib/utils/query-selection-set.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function getQuerySelectionSet(query: Record<string, any>): Set<string> {
2+
if (!query.select) {
3+
return new Set()
4+
}
5+
6+
// The selection of fields for the query is limited
7+
// Get the different parts that are listed for selection
8+
const allSelects = Array.isArray(query.select)
9+
? query.select
10+
: query.select.split(',').map((q) => q.trim())
11+
12+
// Move the parts into a set for easy access and deduplication
13+
return new Set(allSelects)
14+
}

0 commit comments

Comments
 (0)