Skip to content

Commit

Permalink
feat: support extended media API response (#326)
Browse files Browse the repository at this point in the history
Co-authored-by: lihbr <lihbr@users.noreply.github.com>
Co-authored-by: Angelo Ashmore <angeloashmore@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 6, 2023
1 parent 26d5b0f commit 5e5d057
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@
"publishConfig": {
"access": "public"
}
}
}
2 changes: 2 additions & 0 deletions src/helpers/asImageWidthSrcSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ export const asImageWidthSrcSet = <
const {
url,
dimensions,
id: _id,
alt: _alt,
copyright: _copyright,
edit: _edit,
...responsiveViews
} = field;

Expand Down
9 changes: 9 additions & 0 deletions src/types/value/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ export type ImageFieldImage<State extends FieldState = FieldState> =
State extends "empty" ? EmptyImageFieldImage : FilledImageFieldImage;

export interface FilledImageFieldImage {
id: string;
url: string;
dimensions: {
width: number;
height: number;
};
edit: {
x: number;
y: number;
zoom: number;
background: string;
};
alt: string | null;
copyright: string | null;
}

export interface EmptyImageFieldImage {
id?: null;
url?: null;
dimensions?: null;
edit?: null;
alt?: null;
copyright?: null;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/value/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type LinkToMediaField<State extends FieldState = FieldState> =
* A link that points to media.
*/
export interface FilledLinkToMediaField {
id: string;
link_type: typeof LinkType.Media;
name: string;
kind: string;
Expand Down
7 changes: 7 additions & 0 deletions src/types/value/richText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,20 @@ export interface RTLabelNode extends RTSpanNodeBase {
*/
export type RTImageNode = {
type: typeof RichTextNodeType.image;
id: string;
url: string;
alt: string | null;
copyright: string | null;
dimensions: {
width: number;
height: number;
};
edit: {
x: number;
y: number;
zoom: number;
background: string;
};
linkTo?:
| FilledContentRelationshipField
| FilledLinkToWebField
Expand Down
2 changes: 1 addition & 1 deletion src/types/value/sharedSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export type SharedSlice<
> = {
slice_type: SliceType;
slice_label: null;
id?: string;
id: string;
} & Variations;
2 changes: 1 addition & 1 deletion src/types/value/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Slice<
> {
slice_type: SliceType;
slice_label: string | null;
id?: string;
id: string;
primary: PrimaryFields;
items: ItemsFields[];
}
4 changes: 2 additions & 2 deletions test/__testutils__/createRichTextFixtures.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { RichTextField } from "@prismicio/client";

import cnRichTextJSON from "../__fixtures__/cnRichText.json";
import emojiRichTextJSON from "../__fixtures__/emojiRichText.json";
import enRichTextJSON from "../__fixtures__/enRichText.json";
import koRichTextJSON from "../__fixtures__/koRichText.json";
import overlappedRichTextJSON from "../__fixtures__/overlappedRichText.json";
import xssRichTextJSON from "../__fixtures__/xssRichText.json";

import type { RichTextField } from "../../src";

const deepCloneJSON = <T>(json: T): T => {
return JSON.parse(JSON.stringify(json));
};
Expand Down
21 changes: 21 additions & 0 deletions test/helpers-asImagePixelDensitySrcSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ it("returns null for nullish inputs", () => {

it("returns an image field pixel-density-based srcset with [1, 2, 3] pxiel densities by default", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -26,6 +33,13 @@ it("returns an image field pixel-density-based srcset with [1, 2, 3] pxiel densi

it("supports custom pixel densities", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -47,6 +61,13 @@ it("supports custom pixel densities", () => {

it("applies given Imgix URL parameters", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand Down
14 changes: 14 additions & 0 deletions test/helpers-asImageSrc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ it("returns null for nullish inputs", () => {

it("returns an image field URL", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -20,6 +27,13 @@ it("returns an image field URL", () => {

it("applies given Imgix URL parameters", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand Down
82 changes: 76 additions & 6 deletions test/helpers-asImageWidthSrcSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ it("returns null for nullish inputs", () => {

it("returns an image field src and width-based srcset with [640, 750, 828, 1080, 1200, 1920, 2048, 3840] widths by default", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -28,6 +35,13 @@ it("returns an image field src and width-based srcset with [640, 750, 828, 1080,

it("supports custom widths", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -49,6 +63,13 @@ it("supports custom widths", () => {

it("applies given Imgix URL parameters", () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -70,19 +91,40 @@ it("applies given Imgix URL parameters", () => {
});
});

it('if widths is "auto", returns a srcset of responsive views if the field contains responsive views', () => {
const field = {
it('if widths is "thumbnails", returns a srcset of responsive views if the field contains responsive views', () => {
const field: ImageField<"foo" | "bar"> = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
dimensions: { width: 1000, height: 800 },
foo: {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
dimensions: { width: 500, height: 400 },
},
bar: {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -99,8 +141,15 @@ it('if widths is "auto", returns a srcset of responsive views if the field conta
});
});

it('if widths is "auto", but the field does not contain responsive views, uses the default widths', () => {
const field = {
it('if widths is "thumbnails", but the field does not contain responsive views, uses the default widths', () => {
const field: ImageField = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand All @@ -118,19 +167,40 @@ it('if widths is "auto", but the field does not contain responsive views, uses t
});
});

it('if widths is not "auto", but the field contains responsive views, uses the default widths and ignores thumbnails', () => {
const field = {
it('if widths is not "thumbnails", but the field contains responsive views, uses the default widths and ignores thumbnails', () => {
const field: ImageField<"foo" | "bar"> = {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
dimensions: { width: 1000, height: 800 },
foo: {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
dimensions: { width: 500, height: 400 },
},
bar: {
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "https://images.prismic.io/qwerty/image.png?auto=compress%2Cformat",
alt: null,
copyright: null,
Expand Down
21 changes: 19 additions & 2 deletions test/helpers-isFilled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ it("image", (ctx) => {
expect(isFilled.image(null)).toBe(false);
expect(isFilled.image(undefined)).toBe(false);
expect(isFilled.image({})).toBe(false);
expect(isFilled.image(ctx.mock.value.image())).toBe(true);
expect(
isFilled.image(
// @ts-expect-error Remove this comment after v7.3.0 is published.
ctx.mock.value.image(),
),
).toBe(true);
});

it("image thumbnail", () => {
Expand All @@ -69,6 +74,13 @@ it("image thumbnail", () => {
expect(isFilled.imageThumbnail({})).toBe(false);
expect(
isFilled.imageThumbnail({
id: "id",
edit: {
x: 0,
y: 0,
zoom: 1,
background: "background",
},
url: "url",
alt: null,
copyright: null,
Expand Down Expand Up @@ -124,7 +136,12 @@ it("rich text", (ctx) => {
expect(isFilled.richText([{ type: "paragraph", text: "", spans: [] }])).toBe(
false,
);
expect(isFilled.richText(ctx.mock.value.richText())).toBe(true);
expect(
isFilled.richText(
// @ts-expect-error Remove this comment after v7.3.0 is published.
ctx.mock.value.richText(),
),
).toBe(true);
});

it("select", (ctx) => {
Expand Down
Loading

0 comments on commit 5e5d057

Please sign in to comment.