Skip to content

Commit

Permalink
IMAGES-1258: Don't pollute global scope with generally named types
Browse files Browse the repository at this point in the history
  • Loading branch information
ns476 committed Aug 21, 2024
1 parent 8cf4628 commit 3138a98
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
14 changes: 9 additions & 5 deletions src/cloudflare/internal/images-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type RawInfoResponse =
height: number;
};

class TransformationResultImpl implements TransformationResult {
class TransformationResultImpl implements ImageTransformationResult {
public constructor(private readonly bindingsResponse: Response) {}

public contentType(): string {
Expand Down Expand Up @@ -52,7 +52,7 @@ async function streamToBlob(stream: ReadableStream<Uint8Array>): Promise<Blob> {
}

class ImageTransformerImpl implements ImageTransformer {
private transforms: Transform[];
private transforms: ImageTransform[];
private consumed: boolean;

public constructor(
Expand All @@ -63,12 +63,14 @@ class ImageTransformerImpl implements ImageTransformer {
this.consumed = false;
}

public transform(transform: Transform): ImageTransformerImpl {
public transform(transform: ImageTransform): ImageTransformerImpl {
this.transforms.push(transform);
return this;
}

public async output(options: OutputOptions): Promise<TransformationResult> {
public async output(
options: ImageOutputOptions
): Promise<ImageTransformationResult> {
if (this.consumed) {
throw new ImagesErrorImpl(
'IMAGES_TRANSFORM_ERROR 9525: ImageTransformer consumed; you may only call .output() once',
Expand Down Expand Up @@ -107,7 +109,9 @@ class ImageTransformerImpl implements ImageTransformer {
class ImagesBindingImpl implements ImagesBinding {
public constructor(private readonly fetcher: Fetcher) {}

public async info(stream: ReadableStream<Uint8Array>): Promise<InfoResponse> {
public async info(
stream: ReadableStream<Uint8Array>
): Promise<ImageInfoResponse> {
const body = new FormData();
body.append('image', await streamToBlob(stream));

Expand Down
14 changes: 7 additions & 7 deletions src/cloudflare/internal/images.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

type InfoResponse =
type ImageInfoResponse =
| { format: 'image/svg+xml' }
| {
format: string;
Expand All @@ -11,7 +11,7 @@ type InfoResponse =
height: number;
};

type Transform = {
type ImageTransform = {
fit?: 'scale-down' | 'contain' | 'pad' | 'squeeze' | 'cover' | 'crop';
gravity?:
| 'left'
Expand Down Expand Up @@ -62,7 +62,7 @@ type Transform = {
zoom?: number;
};

type OutputOptions = {
type ImageOutputOptions = {
format:
| 'image/jpeg'
| 'image/png'
Expand All @@ -81,7 +81,7 @@ interface ImagesBinding {
* @throws {@link ImagesError} with code 9412 if input is not an image
* @param stream The image bytes
*/
info(stream: ReadableStream<Uint8Array>): Promise<InfoResponse>;
info(stream: ReadableStream<Uint8Array>): Promise<ImageInfoResponse>;
/**
* Begin applying a series of transformations to an image
* @param stream The image bytes
Expand All @@ -96,16 +96,16 @@ interface ImageTransformer {
* You can then apply more transformations or retrieve the output.
* @param transform
*/
transform(transform: Transform): ImageTransformer;
transform(transform: ImageTransform): ImageTransformer;
/**
* Retrieve the image that results from applying the transforms to the
* provided input
* @param options Options that apply to the output e.g. output format
*/
output(options: OutputOptions): Promise<TransformationResult>;
output(options: ImageOutputOptions): Promise<ImageTransformationResult>;
}

interface TransformationResult {
interface ImageTransformationResult {
/**
* The image as a response, ready to store in cache or return to users
*/
Expand Down
50 changes: 25 additions & 25 deletions types/defines/images.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

type InfoResponse =
| { format: "image/svg+xml" }
type ImageInfoResponse =
| { format: 'image/svg+xml' }
| {
format: string;
fileSize: number;
width: number;
height: number;
};

type Transform = {
fit?: "scale-down" | "contain" | "pad" | "squeeze" | "cover" | "crop";
type ImageTransform = {
fit?: 'scale-down' | 'contain' | 'pad' | 'squeeze' | 'cover' | 'crop';
gravity?:
| "left"
| "right"
| "top"
| "bottom"
| "center"
| "auto"
| "entropy"
| "face"
| 'left'
| 'right'
| 'top'
| 'bottom'
| 'center'
| 'auto'
| 'entropy'
| 'face'
| {
x?: number;
y?: number;
mode: "remainder" | "box-center";
mode: 'remainder' | 'box-center';
};
trim?: {
top?: number;
Expand Down Expand Up @@ -62,15 +62,15 @@ type Transform = {
zoom?: number;
};

type OutputOptions = {
type ImageOutputOptions = {
format:
| "image/jpeg"
| "image/png"
| "image/gif"
| "image/webp"
| "image/avif"
| "rgb"
| "rgba";
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/webp'
| 'image/avif'
| 'rgb'
| 'rgba';
quality?: number;
background?: string;
};
Expand All @@ -81,7 +81,7 @@ interface ImagesBinding {
* @throws {@link ImagesError} with code 9412 if input is not an image
* @param stream The image bytes
*/
info(stream: ReadableStream<Uint8Array>): Promise<InfoResponse>;
info(stream: ReadableStream<Uint8Array>): Promise<ImageInfoResponse>;
/**
* Begin applying a series of transformations to an image
* @param stream The image bytes
Expand All @@ -96,16 +96,16 @@ interface ImageTransformer {
* You can then apply more transformations or retrieve the output.
* @param transform
*/
transform(transform: Transform): ImageTransformer;
transform(transform: ImageTransform): ImageTransformer;
/**
* Retrieve the image that results from applying the transforms to the
* provided input
* @param options Options that apply to the output e.g. output format
*/
output(options: OutputOptions): Promise<TransformationResult>;
output(options: ImageOutputOptions): Promise<ImageTransformationResult>;
}

interface TransformationResult {
interface ImageTransformationResult {
/**
* The image as a response, ready to store in cache or return to users
*/
Expand Down

0 comments on commit 3138a98

Please sign in to comment.