Skip to content

Commit

Permalink
ImageSource: Convert ImageURISource to Interface
Browse files Browse the repository at this point in the history
Summary:
Changes the `ImageURISource` Flow type into an interface.

- This enables both objects and class instances to be subtypes of `ImageURISource`.
- This makes it invalid to spread `ImageURISource` as a type, so `interface X extends ImageURISource` can instead be used.
- This makes it invalid to spread `ImageURISource` as a value, so `getImageSourceProperties(x)` can instead be used.
- This makes it invalid to use `$Exact` with `ImageURISource`.

Changelog:
[General][Changed] - `ImageURISource` Flow type is now an interface instead of an object.

Reviewed By: timrc

Differential Revision: D29323508

fbshipit-source-id: 647c2f9b0bfead6d7e56bdb7108e623cbf8b6c89
  • Loading branch information
yungsters authored and facebook-github-bot committed Jun 23, 2021
1 parent fa3697b commit 123d184
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions Libraries/Image/ImageSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@
* This type is intentinoally inexact in order to permit call sites that supply
* extra properties.
*/
export type ImageURISource = $ReadOnly<{
export interface ImageURISource {
/**
* `uri` is a string representing the resource identifier for the image, which
* could be an http address, a local file path, or the name of a static image
* resource (which should be wrapped in the `require('./path/to/image.png')`
* function).
*/
uri?: ?string,
+uri?: ?string;

/**
* `bundle` is the iOS asset bundle which the image is included in. This
* will default to [NSBundle mainBundle] if not set.
* @platform ios
*/
bundle?: ?string,
+bundle?: ?string;

/**
* `method` is the HTTP Method to use. Defaults to GET if not specified.
*/
method?: ?string,
+method?: ?string;

/**
* `headers` is an object representing the HTTP headers to send along with the
* request for a remote image.
*/
headers?: ?{[string]: string},
+headers?: ?{[string]: string};

/**
* `body` is the HTTP body to send with the request. This must be a valid
* UTF-8 string, and will be sent exactly as specified, with no
* additional encoding (e.g. URL-escaping or base64) applied.
*/
body?: ?string,
+body?: ?string;

/**
* `cache` determines how the requests handles potentially cached
Expand All @@ -70,25 +70,68 @@ export type ImageURISource = $ReadOnly<{
*
* @platform ios
*/
cache?: ?('default' | 'reload' | 'force-cache' | 'only-if-cached'),
+cache?: ?('default' | 'reload' | 'force-cache' | 'only-if-cached');

/**
* `width` and `height` can be specified if known at build time, in which case
* these will be used to set the default `<Image/>` component dimensions.
*/
width?: ?number,
height?: ?number,
+width?: ?number;
+height?: ?number;

/**
* `scale` is used to indicate the scale factor of the image. Defaults to 1.0 if
* unspecified, meaning that one image pixel equates to one display point / DIP.
*/
scale?: ?number,

...
}>;
+scale?: ?number;
}

export type ImageSource =
| number
| ImageURISource
| $ReadOnlyArray<ImageURISource>;

export function getImageSourceProperties(
imageSource: ImageURISource,
): $ReadOnly<{
body?: ?string,
bundle?: ?string,
cache?: ?('default' | 'reload' | 'force-cache' | 'only-if-cached'),
headers?: ?{[string]: string},
height?: ?number,
method?: ?string,
scale?: ?number,
uri?: ?string,
width?: ?number,
...
}> {
const object = {};
if (imageSource.body != null) {
object.body = imageSource.body;
}
if (imageSource.bundle != null) {
object.bundle = imageSource.bundle;
}
if (imageSource.cache != null) {
object.cache = imageSource.cache;
}
if (imageSource.headers != null) {
object.headers = imageSource.headers;
}
if (imageSource.height != null) {
object.height = imageSource.height;
}
if (imageSource.method != null) {
object.method = imageSource.method;
}
if (imageSource.scale != null) {
object.scale = imageSource.scale;
}
if (imageSource.uri != null) {
object.uri = imageSource.uri;
}
if (imageSource.width != null) {
object.width = imageSource.width;
}
return object;
}

0 comments on commit 123d184

Please sign in to comment.