A friendly way to get content types using

I rate Contentful.com as the Content Engine for Angular SPA.

The problem I'm facing is related to retrieving posts by content type (for example, getting all blog posts). As described in the sample documentation , this is done as follows:

/ space / spaceid / records? access_token = token & content_type = cat

So in my case, not "cat", content_type = "blog" or content_type = "news" would be what I would expect to interact with.

The reality I'm encountering is that getting data using content_type does not allow me to use the name of my content type - it expects an identifier that is an ugly GUID, so my queries look like this:

/ space / spaceid / records? access_token = token & content_type = 2wKn6yEnZewu2SCCkus4as

Problems with this:

  • not friendly to developers
  • cannot be transported between environments (i.e. test space and production space)

The only solution that I see at the moment is to pre-extract the content type mapping to display the identifier when the page loads and use it, but it will not be so big for performance.

, , (, cat), .

, , :

  1. , ?
  2. , ?
+5
4

, , if. , . Contentful CDN, .

, : Contentful, API, , curl ( SDK):

curl -H 'Authorization: Bearer <access-token>' \ 
  -X PUT \
  -d '{"name": "Blog", "fields": [{"name":"Name","id":"name","type":"Symbol"}]}' \
  https://api.contentful.com/spaces/<spaceId>/content_types/blog

"blog", "". , .

+3

-, URL-. :

Content_Types/yourContentType

yourContentType - , ( , ). , , .

0

In fact, you can use the SDK to create a contentType with the given id. You must use the method createContentTypeWithId ContentfulSpaceAPI.

His signature looks like

* @property {function(id: string, data: {name: string, fields: Array}): Promise<ContentType>} createContentTypeWithId - creates a ContentType with a specified id
0
source

This is no longer the case; you can get all the records of a certain type of content using a query of the following type:

options = {
    include: 10,
    limit: 1000,
    order: "fields.publishDate",
    "fields.tags[in]": title_case_filter,
    content_type: "blog"
  };

In this case, the “blog” is the contentType identifier assigned when the content type is created.

0
source

Source: https://habr.com/ru/post/1605651/


All Articles