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

@astrojs/image: Allow passing undefined to transform options #6008

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-wombats-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Updated error message for missing `widths` prop to provide an example
5 changes: 5 additions & 0 deletions .changeset/strange-olives-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/image": patch
---

Allow passing `undefined` to TransformOptions, this is a types fix for users with `exactOptionalTypes` enabled in their tsconfig
2 changes: 1 addition & 1 deletion packages/integrations/image/src/lib/get-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function getPicture(params: GetPictureParams): Promise<GetPictureRe
}

if (!widths || !Array.isArray(widths)) {
throw new Error('[@astrojs/image] at least one `width` is required');
throw new Error('[@astrojs/image] at least one `width` is required. ex: `widths={[100]}`');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should put this in a separate PR 😓

I've been confused every time I've gotten this error, trying to pass width="100vw" then widths="100vw" until I get to widths=[100] and then widths={[100]} 😅

weird that astro devtools doesn't seem to pick it up as an error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this message isn't great. I'm fine with this being in the same PR, it's just a very simple string change.

}

const aspectRatio = await resolveAspectRatio(params);
Expand Down
16 changes: 8 additions & 8 deletions packages/integrations/image/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,31 @@ export interface TransformOptions {
*
* @default undefined The original image format will be used.
*/
format?: OutputFormat;
format?: OutputFormat | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x?: string and x: string | undefined are different when exactOptionalProperties is true in tsconfig

/**
* The compression quality used during optimization.
*
* @default undefined Allows the image service to determine defaults.
*/
quality?: number;
quality?: number | undefined;
/**
* The desired width of the output image. Combine with `height` to crop the image
* to an exact size, or `aspectRatio` to automatically calculate and crop the height.
*/
width?: number;
width?: number | undefined;
/**
* The desired height of the output image. Combine with `height` to crop the image
* to an exact size, or `aspectRatio` to automatically calculate and crop the width.
*/
height?: number;
height?: number | undefined;
/**
* The desired aspect ratio of the output image. Combine with either `width` or `height`
* to automatically calculate and crop the other dimension.
*
* @example 1.777 - numbers can be used for computed ratios, useful for doing `{width/height}`
* @example "16:9" - strings can be used in the format of `{ratioWidth}:{ratioHeight}`.
*/
aspectRatio?: number | `${number}:${number}`;
aspectRatio?: number | `${number}:${number}` | undefined;
/**
* The background color to use when converting from a transparent image format to a
* non-transparent format. This is useful for converting PNGs to JPEGs.
Expand All @@ -131,19 +131,19 @@ export interface TransformOptions {
* @example "#ffffff" - a hex color
* @example "rgb(255, 255, 255)" - an rgb color
*/
background?: ColorDefinition;
background?: ColorDefinition | undefined;
/**
* How the image should be resized to fit both `height` and `width`.
*
* @default 'cover'
*/
fit?: CropFit;
fit?: CropFit | undefined;
/**
* Position of the crop when fit is `cover` or `contain`.
*
* @default 'centre'
*/
position?: CropPosition;
position?: CropPosition | undefined;
}

export interface HostedImageService<T extends TransformOptions = TransformOptions> {
Expand Down