diff --git a/lib/contentful.ts b/lib/contentful.ts index f1646ba99..e0f67fc60 100644 --- a/lib/contentful.ts +++ b/lib/contentful.ts @@ -109,15 +109,18 @@ export interface CreateClientParams { * Interceptor called on every response. Takes Axios response object as an arg. */ responseLogger?: (response: AxiosResponse | Error) => unknown - + /** + * Enable Content Source Maps. + * @remarks + * This feature is only available when using the Content Preview API. + */ + includeContentSourceMaps?: boolean /** * Enable alpha features. */ alphaFeatures?: { /** - * Enable Content Source Maps. - * @remarks - * This feature is only available when using the Content Preview API. + * @deprecated Use the `includeContentSourceMaps` option directly instead. */ includeContentSourceMaps?: boolean } diff --git a/lib/create-contentful-api.ts b/lib/create-contentful-api.ts index 6c74c4d56..e10177a21 100644 --- a/lib/create-contentful-api.ts +++ b/lib/create-contentful-api.ts @@ -101,11 +101,13 @@ export default function createContentfulApi( } function maybeEnableSourceMaps(query: Record = {}): Record { - const alphaFeatures = (http.httpClientParams as any as CreateClientParams)?.alphaFeatures + const params = http.httpClientParams as CreateClientParams + const includeContentSourceMaps = + params?.includeContentSourceMaps ?? params?.alphaFeatures?.includeContentSourceMaps - const host = http.httpClientParams?.host + const host = params?.host - const areAllowed = checkIncludeContentSourceMapsParamIsAllowed(host, alphaFeatures) + const areAllowed = checkIncludeContentSourceMapsParamIsAllowed(host, includeContentSourceMaps) if (areAllowed) { query.includeContentSourceMaps = true diff --git a/lib/utils/validate-params.ts b/lib/utils/validate-params.ts index 77d567c03..22687293e 100644 --- a/lib/utils/validate-params.ts +++ b/lib/utils/validate-params.ts @@ -47,39 +47,24 @@ export function validateRemoveUnresolvedParam(query) { return } -export function checkIncludeContentSourceMapsParamIsValid(alphaFeatures?: Record) { - if (!alphaFeatures) { +export function checkIncludeContentSourceMapsParamIsAllowed( + host?: string, + includeContentSourceMaps?: boolean, +) { + if (includeContentSourceMaps === undefined) { return false } - const isValidIncludeContentSourceMaps = - 'includeContentSourceMaps' in alphaFeatures && - typeof alphaFeatures.includeContentSourceMaps === 'boolean' - - if (!isValidIncludeContentSourceMaps) { + if (typeof includeContentSourceMaps !== 'boolean') { throw new ValidationError( 'includeContentSourceMaps', `The 'includeContentSourceMaps' parameter must be a boolean.`, ) } - return true -} - -export function checkIncludeContentSourceMapsParamIsAllowed( - host?: string, - alphaFeatures?: Record, -) { - if (!alphaFeatures || Object.keys(alphaFeatures).length === 0) { - return false - } - const includeContentSourceMapsIsAllowed = host === 'preview.contentful.com' - if ( - checkIncludeContentSourceMapsParamIsValid(alphaFeatures) && - !includeContentSourceMapsIsAllowed - ) { + if (includeContentSourceMaps && !includeContentSourceMapsIsAllowed) { throw new ValidationError( 'includeContentSourceMaps', `The 'includeContentSourceMaps' parameter can only be used with the CPA. Please set host to 'preview.contentful.com' to include Content Source Maps. @@ -87,5 +72,5 @@ export function checkIncludeContentSourceMapsParamIsAllowed( ) } - return alphaFeatures.includeContentSourceMaps as boolean + return includeContentSourceMaps as boolean } diff --git a/test/integration/getAsset.test.ts b/test/integration/getAsset.test.ts index 878251cf7..6c56a6a17 100644 --- a/test/integration/getAsset.test.ts +++ b/test/integration/getAsset.test.ts @@ -16,7 +16,7 @@ if (process.env.API_INTEGRATION_TESTS) { const client = contentful.createClient(params) const invalidClient = contentful.createClient({ ...params, - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, }) const previewClient = contentful.createClient(previewParamsWithCSM) @@ -37,7 +37,7 @@ describe('getAsset', () => { expect(typeof response.fields.title).toBe('object') }) - describe('has (alpha) includeContentSourceMaps enabled', () => { + describe('has includeContentSourceMaps enabled', () => { test('cdn client', async () => { await expect(invalidClient.getAsset(asset)).rejects.toThrow( `The 'includeContentSourceMaps' parameter can only be used with the CPA. Please set host to 'preview.contentful.com' to include Content Source Maps.`, diff --git a/test/integration/getAssets.test.ts b/test/integration/getAssets.test.ts index 8e532902b..342caf611 100644 --- a/test/integration/getAssets.test.ts +++ b/test/integration/getAssets.test.ts @@ -16,7 +16,7 @@ if (process.env.API_INTEGRATION_TESTS) { const client = contentful.createClient(params) const invalidClient = contentful.createClient({ ...params, - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, }) const previewClient = contentful.createClient(previewParamsWithCSM) @@ -45,7 +45,7 @@ describe('getAssets', () => { }) }) - describe('has (alpha) includeContentSourceMaps enabled', () => { + describe('has includeContentSourceMaps enabled', () => { test('cdn client', async () => { await expect(invalidClient.getAssets()).rejects.toThrow( `The 'includeContentSourceMaps' parameter can only be used with the CPA. Please set host to 'preview.contentful.com' to include Content Source Maps.`, diff --git a/test/integration/getEntries.test.ts b/test/integration/getEntries.test.ts index dc7d230ad..ca01a8565 100644 --- a/test/integration/getEntries.test.ts +++ b/test/integration/getEntries.test.ts @@ -11,7 +11,7 @@ if (process.env.API_INTEGRATION_TESTS) { const client = contentful.createClient(params) const invalidClient = contentful.createClient({ ...params, - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, }) const previewClient = contentful.createClient(previewParamsWithCSM) @@ -375,7 +375,7 @@ describe('getEntries via client chain modifiers', () => { }) }) - describe('has (alpha) includeContentSourceMaps enabled', () => { + describe('has includeContentSourceMaps enabled', () => { test('invalid client', async () => { await expect(invalidClient.getEntries()).rejects.toThrow( `The 'includeContentSourceMaps' parameter can only be used with the CPA. Please set host to 'preview.contentful.com' to include Content Source Maps.`, diff --git a/test/integration/getEntry.test.ts b/test/integration/getEntry.test.ts index b5eeb07a7..821c42274 100644 --- a/test/integration/getEntry.test.ts +++ b/test/integration/getEntry.test.ts @@ -11,7 +11,7 @@ if (process.env.API_INTEGRATION_TESTS) { const client = contentful.createClient(params) const invalidClient = contentful.createClient({ ...params, - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, }) const previewClient = contentful.createClient(previewParamsWithCSM) const localeClient = contentful.createClient(localeSpaceParams) @@ -177,7 +177,7 @@ describe('getEntry via client chain modifiers', () => { }) }) - describe('preview client has (alpha) includeContentSourceMaps enabled', () => { + describe('preview client has includeContentSourceMaps enabled', () => { test('invalid client', async () => { await expect(invalidClient.getEntry(entryWithResolvableLink)).rejects.toThrow( `The 'includeContentSourceMaps' parameter can only be used with the CPA. Please set host to 'preview.contentful.com' to include Content Source Maps.`, diff --git a/test/integration/utils.ts b/test/integration/utils.ts index 0852bebc4..323dac267 100644 --- a/test/integration/utils.ts +++ b/test/integration/utils.ts @@ -28,7 +28,7 @@ export const previewParams = { export const previewParamsWithCSM = { ...previewParams, - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, } export type Mappings = Record< diff --git a/test/unit/contentful.test.ts b/test/unit/contentful.test.ts index a03eaa5e0..9a3a7fda1 100644 --- a/test/unit/contentful.test.ts +++ b/test/unit/contentful.test.ts @@ -117,15 +117,14 @@ describe('contentful', () => { ) }) - test('Initializes API with alpha features', () => { + test('Initializes API with includeContentSourceMaps option', () => { createClient({ accessToken: 'accessToken', space: 'spaceId', - alphaFeatures: { includeContentSourceMaps: true }, + includeContentSourceMaps: true, }) const callConfig = createHttpClientMock.mock.calls[0] - const alphaFeatures = callConfig[1].alphaFeatures - expect(alphaFeatures).toEqual({ includeContentSourceMaps: true }) + expect(callConfig[1].includeContentSourceMaps).toBe(true) }) }) diff --git a/test/unit/utils/validate-params.test.ts b/test/unit/utils/validate-params.test.ts index 6462e6616..677967887 100644 --- a/test/unit/utils/validate-params.test.ts +++ b/test/unit/utils/validate-params.test.ts @@ -1,44 +1,33 @@ -import { - checkIncludeContentSourceMapsParamIsAllowed, - checkIncludeContentSourceMapsParamIsValid, -} from '../../../lib/utils/validate-params' +import { checkIncludeContentSourceMapsParamIsAllowed } from '../../../lib/utils/validate-params' import { ValidationError } from '../../../lib/utils/validation-error' -describe('checkIncludeContentSourceMapsParamIsValid', () => { - it('returns false if host/alphaFeatures is not provided', () => { - expect(checkIncludeContentSourceMapsParamIsValid()).toBe(false) +describe('checkIncludeContentSourceMapsParamIsAllowed', () => { + it('returns false if includeContentSourceMaps is not provided', () => { + expect(checkIncludeContentSourceMapsParamIsAllowed('http://example.com')).toBe(false) + expect(checkIncludeContentSourceMapsParamIsAllowed('http://example.com', undefined)).toBe(false) }) it('throws ValidationError if includeContentSourceMaps is not a boolean', () => { expect(() => - checkIncludeContentSourceMapsParamIsValid({ includeContentSourceMaps: 'not a boolean' }), + checkIncludeContentSourceMapsParamIsAllowed('http://example.com', 'not a boolean' as any), + ).toThrow(ValidationError) + expect(() => + checkIncludeContentSourceMapsParamIsAllowed('http://example.com', 1 as any), ).toThrow(ValidationError) }) - it('returns true if includeContentSourceMaps is a boolean', () => { - expect(checkIncludeContentSourceMapsParamIsValid({ includeContentSourceMaps: true })).toBe(true) + it('throws ValidationError if includeContentSourceMaps is true but host is not preview.contentful.com', () => { + expect(() => checkIncludeContentSourceMapsParamIsAllowed('cdn.contentful.com', true)).toThrow( + ValidationError, + ) }) -}) -describe('checkIncludeContentSourceMapsParamIsAllowed', () => { - it('returns false if alphaFeatures is not provided', () => { - expect(checkIncludeContentSourceMapsParamIsAllowed('http://example.com')).toBe(false) - expect(checkIncludeContentSourceMapsParamIsAllowed('http://example.com', {})).toBe(false) - }) - - it('throws ValidationError if includeContentSourceMaps is valid but baseUrl does not include preview.contentful.com', () => { - expect(() => - checkIncludeContentSourceMapsParamIsAllowed('cdn.contentful.com', { - includeContentSourceMaps: true, - }), - ).toThrow(ValidationError) + it('returns true if includeContentSourceMaps is true and host is preview.contentful.com', () => { + expect(checkIncludeContentSourceMapsParamIsAllowed('preview.contentful.com', true)).toBe(true) }) - it('returns true if includeContentSourceMaps is valid and baseUrl includes preview.contentful.com', () => { - expect( - checkIncludeContentSourceMapsParamIsAllowed('preview.contentful.com', { - includeContentSourceMaps: true, - }), - ).toBe(true) + it('returns false if includeContentSourceMaps is false, regardless of host', () => { + expect(checkIncludeContentSourceMapsParamIsAllowed('preview.contentful.com', false)).toBe(false) + expect(checkIncludeContentSourceMapsParamIsAllowed('cdn.contentful.com', false)).toBe(false) }) })