export default async function main(args) {
let { language, searchTerm, apiKey, limit } = args.inputVars;
try {
if (!language) { language = 'en' }
if (!searchTerm) { throw new Error("missing a value for `searchTerm`"); }
if (!apiKey) { throw new Error("missing API key"); }
if (!limit) { limit = 1; }
} catch (err) {
return {
next: {
path: 'Error_Fetching'
},
trace: [
{
type: "debug",
payload: {
message: `Could not search for a .gif, ${err.message}`
}
}
]
};
}
const apiUrl = `https://api.giphy.com/v1/gifs/search?api_key=${apiKey}&q=${searchTerm}&limit=${limit}&lang=${language}`;
const response = await fetch(apiUrl);
const responseBody = response.json;
return {
next: {
path: 'Success'
},
trace: responseBody.data.map(properties => ({
type: "cardV2",
payload: {
imageUrl: properties.images.fixed_height.url,
title: properties.title,
description: {
text: properties.title,
}
}
}))
};
}