Skip to content

Commit 207f17a

Browse files
committed
feat: conditionally encode response if csm is enabled
1 parent b07f55e commit 207f17a

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

lib/create-contentful-api.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* different kinds of entities present in Contentful (Entries, Assets, etc).
44
*/
55

6+
import { encodeCPAResponse } from '@contentful/content-source-maps'
67
import { AxiosInstance, createRequestConfig, errorHandler } from 'contentful-sdk-core'
78
import { CreateClientParams } from './contentful'
89
import { GetGlobalOptions } from './create-global-options'
@@ -112,12 +113,22 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
112113
return query
113114
}
114115

116+
function maybeEncodeCPAResponse(data: any, config: Record<string, any>): any {
117+
const includeContentSourceMaps = config.query?.includeContentSourceMaps as boolean
118+
119+
if (includeContentSourceMaps) {
120+
return encodeCPAResponse(data)
121+
}
122+
123+
return data
124+
}
125+
115126
async function get<T>({ context, path, config }: GetConfig): Promise<T> {
116127
const baseUrl = getBaseUrl(context)
117128

118129
try {
119130
const response = await http.get(baseUrl + path, config)
120-
return response.data
131+
return maybeEncodeCPAResponse(response.data, config)
121132
} catch (error) {
122133
errorHandler(error)
123134
}

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"prepush": "npm run test:prepush"
7676
},
7777
"dependencies": {
78+
"@contentful/content-source-maps": "^0.4.2",
7879
"@contentful/rich-text-types": "^16.0.2",
7980
"axios": "^1.6.7",
8081
"contentful-resolve-response": "^1.8.1",
@@ -175,4 +176,4 @@
175176
"@semantic-release/github"
176177
]
177178
}
178-
}
179+
}

0 commit comments

Comments
 (0)