Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch more media data #38

Closed
wants to merge 10 commits into from
Closed

Fetch more media data #38

wants to merge 10 commits into from

Conversation

tonyspiro
Copy link
Member

@tonyspiro tonyspiro commented Sep 3, 2024

What's new

Includes a new options chain method to add the full media to the Objects payload on all metadata fields. This enables easier use of alt_text, metadata and other helpful media data.

Before

So instead of this:

const { object } = await cosmic.objects
  .findOne({
    type: 'shows',
    slug: 'sf-car-show',
  })
  .props('slug,title,metadata')
  .depth(1)

Which returns:

{
  "slug": "sf-car-show",
  "title": "SF Car Show",
  "metadata": {
    "first_car": {
      "url": "https://cdn.cosmicjs.com/04ba2480-dc6c-11ed-bc7a-55cf2d59d704-FrB66Vzuxxc.jpg",
      "imgix_url": "https://imgix.cosmicjs.com/04ba2480-dc6c-11ed-bc7a-55cf2d59d704-FrB66Vzuxxc.jpg"
    },
    "second_car": {
      "url": "https://cdn.cosmicjs.com/00cd0bd0-dc6c-11ed-bc7a-55cf2d59d704-WTN4tbNtNQ.jpg",
      "imgix_url": "https://imgix.cosmicjs.com/00cd0bd0-dc6c-11ed-bc7a-55cf2d59d704-WTN4tbNtNQ.jpg"
    }
  }
}

With this update

You can now do:

const { object } = await cosmic.objects
  .findOne({
    type: 'vehicles',
    slug: 'ferrari',
  })
  .props('slug,title,metadata')
  .depth(1)
  .options({
    media: {
      props: "alt_text" // all | alt_text | name | id ... etc see media model for all available props https://www.cosmicjs.com/docs/api/media#the-media-model
    }
});

Returning all available media data:

{
  "slug": "sf-car-show",
  "title": "SF Car Show",
  "metadata": {
    "first_car": {
      "url": "https://cdn.cosmicjs.com/04ba2480-dc6c-11ed-bc7a-55cf2d59d704-FrB66Vzuxxc.jpg",
      "imgix_url": "https://imgix.cosmicjs.com/04ba2480-dc6c-11ed-bc7a-55cf2d59d704-FrB66Vzuxxc.jpg",
      "alt_text": "Gray Lambo"
    },
    "second_car": {
      "url": "https://cdn.cosmicjs.com/00cd0bd0-dc6c-11ed-bc7a-55cf2d59d704-WTN4tbNtNQ.jpg",
      "imgix_url": "https://imgix.cosmicjs.com/00cd0bd0-dc6c-11ed-bc7a-55cf2d59d704-WTN4tbNtNQ.jpg",
      "alt_text": "Red Ferarri"
    }
  }
}

How it works under the hood

  1. After calling the API for Objects, it checks the metadata response for any url or imgix_url properties.
  2. Then it collects all of the file names (mapped to the object key)
  3. Then it queries all of the names using the media.find method and the intended props declared in the options.
  4. Then it maps the values back to the Object.

Note: this will use another API request media data for each object and may slow down the response time.

@tonyspiro tonyspiro changed the title RFC: Add full media data options RFC: Add media props as an option Sep 9, 2024
@tonyspiro tonyspiro changed the title RFC: Add media props as an option RFC: Fetch more media data using props Sep 9, 2024
@tonyspiro tonyspiro changed the title RFC: Fetch more media data using props RFC: Fetch more media data Sep 9, 2024
@tonyspiro tonyspiro changed the base branch from tony/alt-text to main September 10, 2024 21:31
@tonyspiro tonyspiro marked this pull request as ready for review September 17, 2024 16:25
@tonyspiro tonyspiro changed the title RFC: Fetch more media data Fetch more media data Sep 17, 2024
@tonyspiro tonyspiro closed this Sep 19, 2024
@tonyspiro
Copy link
Member Author

Merged with #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant