Skip to content

Commit

Permalink
feat: conditionally encode response if csm is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
2wce committed May 7, 2024
1 parent b07f55e commit 207f17a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/create-contentful-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* different kinds of entities present in Contentful (Entries, Assets, etc).
*/

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

function maybeEncodeCPAResponse(data: any, config: Record<string, any>): any {
const includeContentSourceMaps = config.query?.includeContentSourceMaps as boolean

if (includeContentSourceMaps) {
return encodeCPAResponse(data)
}

return data
}

async function get<T>({ context, path, config }: GetConfig): Promise<T> {
const baseUrl = getBaseUrl(context)

try {
const response = await http.get(baseUrl + path, config)
return response.data
return maybeEncodeCPAResponse(response.data, config)
} catch (error) {
errorHandler(error)
}
Expand Down
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"prepush": "npm run test:prepush"
},
"dependencies": {
"@contentful/content-source-maps": "^0.4.2",
"@contentful/rich-text-types": "^16.0.2",
"axios": "^1.6.7",
"contentful-resolve-response": "^1.8.1",
Expand Down Expand Up @@ -175,4 +176,4 @@
"@semantic-release/github"
]
}
}
}

0 comments on commit 207f17a

Please sign in to comment.