Skip to content

Commit

Permalink
feat: remove alpha for content source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesRijckaert committed Aug 20, 2024
1 parent 15b7d71 commit ee2909c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 72 deletions.
11 changes: 7 additions & 4 deletions lib/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ export interface CreateClientParams {
* Interceptor called on every response. Takes Axios response object as an arg.
*/
responseLogger?: (response: AxiosResponse<any> | 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
}
Expand Down
8 changes: 5 additions & 3 deletions lib/create-contentful-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
}

function maybeEnableSourceMaps(query: Record<string, any> = {}): Record<string, any> {
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
Expand Down
31 changes: 8 additions & 23 deletions lib/utils/validate-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,30 @@ export function validateRemoveUnresolvedParam(query) {
return
}

export function checkIncludeContentSourceMapsParamIsValid(alphaFeatures?: Record<string, any>) {
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<string, any>,
) {
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.
`,
)
}

return alphaFeatures.includeContentSourceMaps as boolean
return includeContentSourceMaps as boolean
}
4 changes: 2 additions & 2 deletions test/integration/getAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.`,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/getAssets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.`,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/getEntries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.`,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/getEntry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.`,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const previewParams = {

export const previewParamsWithCSM = {
...previewParams,
alphaFeatures: { includeContentSourceMaps: true },
includeContentSourceMaps: true,
}

export type Mappings = Record<
Expand Down
7 changes: 3 additions & 4 deletions test/unit/contentful.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
47 changes: 18 additions & 29 deletions test/unit/utils/validate-params.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit ee2909c

Please sign in to comment.