Skip to content

Commit 51417da

Browse files
committed
Handle missing giphy image sizes
1 parent 209bb27 commit 51417da

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/backend/src/api-tests/giphy.test.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ describe('API giphy', () => {
1414
data: [
1515
{
1616
images: {
17-
fixed_height: { url: 'normal.gif?extra' },
17+
original: { url: 'original.gif?extra' },
18+
fixed_height: { url: 'medium.gif?extra' },
1819
fixed_height_small: { url: 'small.gif?extra' },
1920
},
2021
},
22+
{
23+
images: {
24+
original: { url: 'original2.gif' },
25+
},
26+
},
2127
],
2228
});
2329
});
@@ -48,7 +54,10 @@ describe('API giphy', () => {
4854
.expect('Content-Type', /application\/json/);
4955

5056
expect(response.body).toEqual({
51-
gifs: [{ small: 'small.gif', medium: 'normal.gif' }],
57+
gifs: [
58+
{ small: 'small.gif', medium: 'medium.gif' },
59+
{ small: 'original2.gif', medium: 'original2.gif' },
60+
],
5261
});
5362
});
5463

src/backend/src/services/GiphyService.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ interface GifInfo {
1414
}
1515

1616
interface GiphyResponseResource {
17-
url: string;
17+
url?: string;
1818
}
1919

2020
interface GiphyResponseGif {
2121
images: {
22-
fixed_height: GiphyResponseResource;
23-
fixed_height_small: GiphyResponseResource;
22+
original?: GiphyResponseResource;
23+
fixed_height?: GiphyResponseResource;
24+
fixed_height_small?: GiphyResponseResource;
2425
};
2526
}
2627

@@ -75,10 +76,15 @@ export class GiphyService {
7576
throw new Error(`Unknown Giphy API response: ${resultJson.status}`);
7677
}
7778

78-
return resultJson.data.map((gif) => ({
79-
small: gif.images.fixed_height_small.url.split('?')[0] ?? '',
80-
medium: gif.images.fixed_height.url.split('?')[0] ?? '',
81-
}));
79+
return resultJson.data.map((gif) => {
80+
const original = gif.images.original?.url ?? '';
81+
const fixed = gif.images.fixed_height?.url ?? original;
82+
const small = gif.images.fixed_height_small?.url ?? fixed;
83+
return {
84+
small: small.split('?')[0] ?? '',
85+
medium: fixed.split('?')[0] ?? '',
86+
};
87+
});
8288
},
8389
(c) => c.length >= limit,
8490
);

0 commit comments

Comments
 (0)