Table of Contents
Combining our useSearch
hook with a serverless endpoint will let you create different ways to call multiple or individual assets, based off of a handful of different possibilities.
import Image from './Image';import { useSearch } from 'use-cloudinary';export default function Images({ endpoint }) {const { search, data, isLoading, isError, error } = useSearch({ endpoint });if (isLoading) return <p>Loading...</p>;if (isError) return <p>{error.message}</p>;// OPTION 1: To mirror Cloudinary's Node SDK's search expression, we've provided direct support for crafting custom search queriesconst expressionSearch = search({ expression: "resource_type:image"})// OPTION 2: Also included is a few opinionated object configurations to more easily craft search based off of user interaction. This includes publicId search, folder search, and more.const customConfigSearch = search({ resourceType: "image" })return (<div><button onClick={() => expressionSearch()}>Load</button><div>{data && data.resources.map(image => (<Image publicId={image.public_id} transforms={{ height: 0.2, border: "2px_solid_black" }} />)}</div></div >)
Combined with this serverless endpoint example, we can get all of the images in our Cloudinary account, by utilizing search expressions and looking for all images. Cloudinary's Search API let's you search your assets a bunch of different ways!
const cloudinary = require('cloudinary').v2;cloudinary.config({cloud_name: process.env.CLOUD_NAME,api_key: process.env.API_KEY,api_secret: process.env.API_SECRET})exports.handler = async (event) => {const body = JSON.parse(event.body);const res = await cloudinary.search.expression(body.expression).execute().then(result => result);return {statusCode: 200,body: JSON.stringify(res)}}
Args
Key | Type | Required? | Description |
---|---|---|---|
endpoint | String | Yes | An endpoint that will accept your file and options and upload to Cloudinary |
Return values
Key | Type | Description |
---|---|---|
search | Func | An async function that accepts options and returns an array of objects or single object, describing your Cloudinary assets |
data | undefined || String | When `search` is successful, data will return the value of the request. More about React Query's return values here |
isLoading | Boolean | isLoading state returns a boolean if the query is in a "hard" loading state. This means no cached data and the query is currently fetching.here |
isErrro | Boolean | isError returns a boolean (true) if the query attempt resulted in an error (otherwise false). More about React Query return vairables here |
isSuccess | Boolean | isSuccess returns a boolean (true) if the query attempt resulted in an error (otherwise false).More about React Query return vairables here |
isIdle | Boolean | isIdle returns a boolean (true) if the query is initialized with enabled: false and no initial data is available (otherwise false). More about React Query return vairables here |
error | Object | When getVideo is successful, data will return the value of the request, usually a String defining your asset URL. More about React Query's return values here |
search()
Our search
function simply can accept any of the options you would expect to send to the Search API. There's also been added support to setup an opinionated search using a custom object configuration.
Mostly, the search will be handled in your serverless function, so whatever you support there is available to your hook's search
.
Including no parameters in the method call will return the 50 most recently created resources in descending order of creation time.
Otherwise your options are --
Key | Type | Required | Description |
---|---|---|---|
publicId | String | No | The public id of the asset to search for. Supports only exact matches currently. Case sensitive. |
resourceType | String | No | Possible values: image & video. Exact match only. Case sensitive. |
folder | String | No | Search for an exact folder path. Case sensitive. |
tags | String | No | Search for images by tag(s). The tags should be in a single string seperated by spaces. Case sensitive |
aspectRatio | String | No | Specify an image ratio in a string with "Width:Height" Exact match and case sensitive. |
expression | String | No | Describes the query string for filtering the assets in your account. Learn more here |
sort_by | String | No | A key value pair, where the key is the field to sort by and the value is the direction |
max_results | Int | No | The maximum number of results to return. Default is 50, maximum is 500 |
next_cursor | String | No | When a search request returns more results than max_results, the next_cursor value is returned as part of the response. |
with_field | String | No | You can have multiple with_field parameters with possible values of context and tags |