Search code examples
javascriptangulartypescriptcontentful

Modifying Angular Contentful API response


I have a contentful service like so..

import { Injectable } from '@angular/core';
import { createClient, Entry } from 'contentful';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';


const CONFIG = {
   space: '<spaceid>',
   accessToken: '<accesstoken>',

   contentTypeIds: {
      programItems: 'programItem'
   }
};

@Injectable()
export class ContentfulService {
private cdaClient = createClient({
    space: CONFIG.space,
    accessToken: CONFIG.accessToken
});

public weekNumber = new BehaviorSubject<any>(1);

constructor() { }
// Get all the program items
getProgramItems(query?: object): Promise<Entry<any>[]> {
   return this.cdaClient.getEntries(Object.assign({
     content_type: CONFIG.contentTypeIds.programItems
   }, query))
   .then(res => res.items);
}

}

but I only want to bring in the programItems sys.ids in the contentful documentation.. you can modify api calls and return only certain values like this modify api calls

https://cdn.contentful.com/spaces/<space_id>/entries/ ?select=fields.productName,fields.price &content_type=<content_type_id>

but Im not sure how I would implement the same thing, the way they do angular calls.. I could just do a http request but I would prefer to keep it the same way as I have done above

any help would be appreciated


Solution

  • You add a select property to your getEntries call.

    // Get all the program items
    getProgramItems(query?: object): Promise<Entry<any>[]> {
       return this.cdaClient.getEntries(Object.assign({
         content_type: CONFIG.contentTypeIds.programItems,
         select: 'sys.id'
       }, query))
       .then(res => res.items);
    }
    

    You can read the full documentation, including javascript snippets, here: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/select-operator/query-entries/console/js