Skip to content

Commit

Permalink
feat: [] Locale-based publish support in CMA client (#2510)
Browse files Browse the repository at this point in the history
* feat: add client locale-based publish support

* feat: add assets locale-based publish support

* feat: update types for public methods
  • Loading branch information
colomolo authored Jan 17, 2025
1 parent 840086e commit 69dea66
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
31 changes: 24 additions & 7 deletions lib/adapters/REST/endpoints/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ export const del: RestEndpoint<'Asset', 'delete'> = (

export const publish: RestEndpoint<'Asset', 'publish'> = (
http: AxiosInstance,
params: GetSpaceEnvironmentParams & { assetId: string },
params: GetSpaceEnvironmentParams & { assetId: string; locales?: string[] },
rawData: AssetProps
) => {
const payload = params.locales?.length ? { add: { fields: { '*': params.locales } } } : null

return raw.put<AssetProps>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`,
null,
payload,
{
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
Expand All @@ -114,12 +116,27 @@ export const publish: RestEndpoint<'Asset', 'publish'> = (

export const unpublish: RestEndpoint<'Asset', 'unpublish'> = (
http: AxiosInstance,
params: GetSpaceEnvironmentParams & { assetId: string }
params: GetSpaceEnvironmentParams & { assetId: string; locales?: string[] },
rawData?: AssetProps
) => {
return raw.del<AssetProps>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`
)
if (params.locales?.length) {
const payload = { remove: { fields: { '*': params.locales } } }
return raw.put<AssetProps>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`,
payload,
{
headers: {
'X-Contentful-Version': rawData?.sys.version,
},
}
)
} else {
return raw.del<AssetProps>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/assets/${params.assetId}/published`
)
}
}

export const archive: RestEndpoint<'Asset', 'archive'> = (
Expand Down
31 changes: 24 additions & 7 deletions lib/adapters/REST/endpoints/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ export const del: RestEndpoint<'Entry', 'delete'> = (

export const publish: RestEndpoint<'Entry', 'publish'> = <T extends KeyValueMap = KeyValueMap>(
http: AxiosInstance,
params: GetSpaceEnvironmentParams & { entryId: string },
params: GetSpaceEnvironmentParams & { entryId: string; locales?: string[] },
rawData: EntryProps<T>
) => {
const payload = params.locales?.length ? { add: { fields: { '*': params.locales } } } : null

return raw.put<EntryProps<T>>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/entries/${params.entryId}/published`,
null,
payload,
{
headers: {
'X-Contentful-Version': rawData.sys.version,
Expand All @@ -134,12 +136,27 @@ export const publish: RestEndpoint<'Entry', 'publish'> = <T extends KeyValueMap

export const unpublish: RestEndpoint<'Entry', 'unpublish'> = <T extends KeyValueMap = KeyValueMap>(
http: AxiosInstance,
params: GetSpaceEnvironmentParams & { entryId: string }
params: GetSpaceEnvironmentParams & { entryId: string; locales?: string[] },
rawData?: EntryProps<T>
) => {
return raw.del<EntryProps<T>>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/entries/${params.entryId}/published`
)
if (params.locales?.length) {
const payload = { remove: { fields: { '*': params.locales } } }
return raw.put<EntryProps<T>>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/entries/${params.entryId}/published`,
payload,
{
headers: {
'X-Contentful-Version': rawData?.sys.version,
},
}
)
} else {
return raw.del<EntryProps<T>>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/entries/${params.entryId}/published`
)
}
}

export const archive: RestEndpoint<'Entry', 'archive'> = <T extends KeyValueMap = KeyValueMap>(
Expand Down
1 change: 1 addition & 0 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export interface EntityMetaSysProps extends MetaSysProps {
firstPublishedAt?: string
publishedCounter?: number
locale?: string
fieldStatus: { '*': Record<string, 'draft' | 'changed' | 'published'> }
}

export interface EntryMetaSysProps extends EntityMetaSysProps {
Expand Down
8 changes: 4 additions & 4 deletions lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ export type PlainClientAPI = {
): Promise<EntryProps<T>>
delete(params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string }>): Promise<any>
publish<T extends KeyValueMap = KeyValueMap>(
params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string }>,
params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string; locales?: string[] }>,
rawData: EntryProps<T>
): Promise<EntryProps<T>>
unpublish<T extends KeyValueMap = KeyValueMap>(
params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string }>
params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string; locales?: string[] }>
): Promise<EntryProps<T>>
archive<T extends KeyValueMap = KeyValueMap>(
params: OptionalDefaults<GetSpaceEnvironmentParams & { entryId: string }>
Expand Down Expand Up @@ -345,11 +345,11 @@ export type PlainClientAPI = {
): Promise<AssetProps>
delete(params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string }>): Promise<any>
publish(
params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string }>,
params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string; locales?: string[] }>,
rawData: AssetProps
): Promise<AssetProps>
unpublish(
params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string }>
params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string; locales?: string[] }>
): Promise<AssetProps>
archive(
params: OptionalDefaults<GetSpaceEnvironmentParams & { assetId: string }>
Expand Down

0 comments on commit 69dea66

Please sign in to comment.