Skip to content

Commit

Permalink
feat: add support for GET published asset endpoint [] (#2134)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishelgert authored Jan 25, 2024
1 parent 9b812e1 commit 2ee1ebf
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/adapters/REST/endpoints/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ export const get: RestEndpoint<'Asset', 'get'> = (
)
}

export const getPublished: RestEndpoint<'Asset', 'getPublished'> = (
http: AxiosInstance,
params: GetSpaceEnvironmentParams & QueryParams,
rawData?: unknown,
headers?: RawAxiosRequestHeaders
) => {
return raw.get<CollectionProp<AssetProps>>(
http,
`/spaces/${params.spaceId}/environments/${params.environmentId}/public/assets`,
{
params: normalizeSelect(params.query),
headers: headers ? { ...headers } : undefined,
}
)
}

export const getMany: RestEndpoint<'Asset', 'getMany'> = (
http: AxiosInstance,
params: GetSpaceEnvironmentParams & QueryParams,
Expand Down
6 changes: 6 additions & 0 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type MRInternal<UA extends boolean> = {
>

(opts: MROpts<'Asset', 'getMany', UA>): MRReturn<'Asset', 'getMany'>
(opts: MROpts<'Asset', 'getPublished', UA>): MRReturn<'Asset', 'getPublished'>
(opts: MROpts<'Asset', 'get', UA>): MRReturn<'Asset', 'get'>
(opts: MROpts<'Asset', 'update', UA>): MRReturn<'Asset', 'update'>
(opts: MROpts<'Asset', 'delete', UA>): MRReturn<'Asset', 'delete'>
Expand Down Expand Up @@ -933,6 +934,11 @@ export type MRActions = {
}
}
Asset: {
getPublished: {
params: GetSpaceEnvironmentParams & QueryParams
headers?: RawAxiosRequestHeaders
return: CollectionProp<AssetProps>
}
getMany: {
params: GetSpaceEnvironmentParams & QueryParams
headers?: RawAxiosRequestHeaders
Expand Down
30 changes: 30 additions & 0 deletions lib/create-environment-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,36 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
},
}).then((data) => wrapAssetCollection(makeRequest, data))
},
/**
* Gets a collection of published Assets
* @param query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
* @return Promise for a collection of published Assets
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment-id>'))
* .then((environment) => environment.getPublishedAssets())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getPublishedAssets(query: QueryOptions = {}) {
const raw = this.toPlainObject() as EnvironmentProps
return makeRequest({
entityType: 'Asset',
action: 'getPublished',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
query: createRequestConfig({ query: query }).params,
},
}).then((data) => wrapAssetCollection(makeRequest, data))
},
/**
* Creates a Asset. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
* @param data - Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.
Expand Down
5 changes: 5 additions & 0 deletions lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ export type PlainClientAPI = {
): Promise<EntryReferenceProps>
}
asset: {
getPublished(
params: OptionalDefaults<GetSpaceEnvironmentParams & QueryParams>,
rawData?: unknown,
headers?: RawAxiosRequestHeaders
): Promise<CollectionProp<AssetProps>>
getMany(
params: OptionalDefaults<GetSpaceEnvironmentParams & QueryParams>,
rawData?: unknown,
Expand Down
1 change: 1 addition & 0 deletions lib/plain/plain-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const createPlainClient = (
references: wrap(wrapParams, 'Entry', 'references'),
},
asset: {
getPublished: wrap(wrapParams, 'Asset', 'getPublished'),
getMany: wrap(wrapParams, 'Asset', 'getMany'),
get: wrap(wrapParams, 'Asset', 'get'),
update: wrap(wrapParams, 'Asset', 'update'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const TEST_IMAGE_SOURCE_URL =
import { expect } from 'chai'
import { after, before, describe, test } from 'mocha'
import { initClient, getDefaultSpace, createTestSpace } from '../helpers'
import type { Environment, Space } from '../../lib/export-types'

describe('Asset api', function () {
describe('Read', () => {
let space
let environment
let space: Space
let environment: Environment

before(async () => {
space = await getDefaultSpace()
Expand Down Expand Up @@ -37,6 +38,12 @@ describe('Asset api', function () {
expect(response.items, 'items').to.be.ok
})
})

test('Gets published assets', async () => {
return environment.getPublishedAssets().then((response) => {
expect(response.items, 'items').to.be.ok
})
})
})

// Write test seems currently broken
Expand Down

0 comments on commit 2ee1ebf

Please sign in to comment.