Skip to content

Commit 95e6d90

Browse files
robhoganfacebook-github-bot
authored andcommitted
Auto-generate TypeScript types for metro-cache (#1576)
Summary: Pull Request resolved: #1576 Auto-generate TypeScript defs for the `metro-cache` package using `flow-api-translator`, and enforce that they remain in sync. ## Why `metro-cache`? - It's relatively small, so easy to review these changes by eye while this is a new process. - Its types have no dependencies on any other package (Metro, Node or otherwise) - It's a dependency of `metro-config`, which is the most important to start auto-generating in terms of user impact - all RN users interact with `metro-config`'s types. Changelog: Internal Reviewed By: vzaidman Differential Revision: D81125128 fbshipit-source-id: d33cef3bb91dc74ecac0ad7cf83d260a0fae9c32
1 parent d0c89e5 commit 95e6d90

11 files changed

Lines changed: 101 additions & 35 deletions

File tree

packages/metro-cache/types/Cache.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
* @oncall react_native
99
*/
1010

11-
import {CacheStore} from './types';
12-
11+
import type {CacheStore} from './types';
1312
/**
1413
* Main cache class. Receives an array of cache instances, and sequentially
1514
* traverses them to return a previously stored value. It also ensures setting
1615
* the value in all instances.
1716
*
1817
* All get/set operations are logged via Metro's logger.
1918
*/
20-
export default class Cache<T> {
19+
declare class Cache<T> {
2120
constructor(stores: ReadonlyArray<CacheStore<T>>);
22-
get(key: Buffer): Promise<T | null>;
21+
get(key: Buffer): Promise<null | undefined | T>;
2322
set(key: Buffer, value: T): Promise<void>;
2423
get isDisabled(): boolean;
2524
}
25+
export default Cache;

packages/metro-cache/types/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ export {
2626
HttpStore,
2727
stableHash,
2828
};
29-
3029
export interface MetroCache {
31-
AutoCleanFileStore: typeof AutoCleanFileStore;
32-
Cache: typeof Cache;
33-
FileStore: typeof FileStore;
34-
HttpGetStore: typeof HttpGetStore;
35-
HttpStore: typeof HttpStore;
36-
stableHash: typeof stableHash;
30+
readonly AutoCleanFileStore: typeof AutoCleanFileStore;
31+
readonly Cache: typeof Cache;
32+
readonly FileStore: typeof FileStore;
33+
readonly HttpGetStore: typeof HttpGetStore;
34+
readonly HttpStore: typeof HttpStore;
35+
readonly stableHash: typeof stableHash;
3736
}
38-
3937
/**
4038
* Backwards-compatibility with CommonJS consumers using interopRequireDefault.
4139
* Do not add to this list.
4240
*
4341
* @deprecated Default import from 'metro-cache' is deprecated, use named exports.
4442
*/
4543
declare const $$EXPORT_DEFAULT_DECLARATION$$: MetroCache;
44+
declare type $$EXPORT_DEFAULT_DECLARATION$$ =
45+
typeof $$EXPORT_DEFAULT_DECLARATION$$;
4646
export default $$EXPORT_DEFAULT_DECLARATION$$;

packages/metro-cache/types/stableHash.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
* @oncall react_native
99
*/
1010

11-
export default function stableHash(value: unknown): Buffer;
11+
declare function stableHash(value: unknown): Buffer;
12+
export default stableHash;

packages/metro-cache/types/stores/AutoCleanFileStore.d.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
* @oncall react_native
99
*/
1010

11-
import type FileStore from './FileStore';
11+
import type {Options} from './FileStore';
1212

13+
import FileStore from './FileStore';
14+
15+
type CleanOptions = Readonly<
16+
Omit<Options, keyof {intervalMs?: number; cleanupThresholdMs?: number}> & {
17+
intervalMs?: number;
18+
cleanupThresholdMs?: number;
19+
}
20+
>;
1321
/**
1422
* A FileStore that, at a given interval, stats the content of the cache root
1523
* and deletes any file last modified a set threshold in the past.
@@ -18,4 +26,7 @@ import type FileStore from './FileStore';
1826
* redundant I/O when caches are large. Prefer your own cleanup scripts, or a
1927
* custom Metro cache that uses watches, hooks get/set, and/or implements LRU.
2028
*/
21-
export default class AutoCleanFileStore<T> extends FileStore<T> {}
29+
declare class AutoCleanFileStore<T> extends FileStore<T> {
30+
constructor(opts: CleanOptions);
31+
}
32+
export default AutoCleanFileStore;

packages/metro-cache/types/stores/FileStore.d.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
* @oncall react_native
99
*/
1010

11-
export interface Options {
12-
root: string;
13-
}
14-
15-
export default class FileStore<T> {
11+
export type Options = Readonly<{root: string}>;
12+
declare class FileStore<T> {
1613
constructor(options: Options);
17-
get(key: Buffer): Promise<T | null>;
14+
get(key: Buffer): Promise<null | undefined | T>;
1815
set(key: Buffer, value: T): Promise<void>;
1916
clear(): void;
2017
}
18+
export default FileStore;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
declare class HttpError extends Error {
11+
code: number;
12+
constructor(message: string, code: number);
13+
}
14+
export default HttpError;

packages/metro-cache/types/stores/HttpGetStore.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
* @oncall react_native
99
*/
1010

11-
import type {Options} from './HttpStore';
11+
import type {Options as HttpOptions} from './HttpStore';
1212

13-
export default class HttpGetStore<T> {
14-
constructor(options: Options);
15-
get(key: Buffer): Promise<T | null>;
16-
set(key: Buffer, value: T): Promise<void>;
17-
clear(): void;
13+
import HttpStore from './HttpStore';
14+
15+
declare class HttpGetStore<T> extends HttpStore<T> {
16+
constructor(options: HttpOptions);
17+
get(key: Buffer): Promise<null | undefined | T>;
18+
set(_key: Buffer, _value: T): Promise<void>;
1819
}
20+
export default HttpGetStore;

packages/metro-cache/types/stores/HttpStore.d.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,41 @@
88
* @oncall react_native
99
*/
1010

11-
export interface Options {
11+
import HttpError from './HttpError';
12+
import NetworkError from './NetworkError';
13+
14+
export type Options =
15+
| EndpointOptions
16+
| {getOptions: EndpointOptions; setOptions: EndpointOptions};
17+
type EndpointOptions = {
1218
endpoint: string;
1319
family?: 4 | 6;
1420
timeout?: number;
1521
key?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
1622
cert?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
1723
ca?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
18-
}
19-
20-
export default class HttpStore<T> {
24+
params?: URLSearchParams;
25+
headers?: {[$$Key$$: string]: string};
26+
additionalSuccessStatuses?: ReadonlyArray<number>;
27+
/**
28+
* Whether to include additional debug information in error messages.
29+
*/
30+
debug?: boolean;
31+
/**
32+
* Retry configuration
33+
*/
34+
maxAttempts?: number;
35+
retryNetworkErrors?: boolean;
36+
retryStatuses?: ReadonlySet<number>;
37+
socketPath?: string;
38+
proxy?: string;
39+
};
40+
declare class HttpStore<T> {
41+
static HttpError: typeof HttpError;
42+
static NetworkError: typeof NetworkError;
2143
constructor(options: Options);
22-
get(key: Buffer): Promise<T | null>;
44+
get(key: Buffer): Promise<null | undefined | T>;
2345
set(key: Buffer, value: T): Promise<void>;
2446
clear(): void;
2547
}
48+
export default HttpStore;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
declare class NetworkError extends Error {
11+
code: string;
12+
constructor(message: string, code: string);
13+
}
14+
export default NetworkError;

packages/metro-cache/types/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010

1111
export interface CacheStore<T> {
12-
get(key: Buffer): T | undefined | Promise<T> | Promise<undefined>;
12+
name?: string;
13+
get(key: Buffer): (null | undefined | T) | Promise<null | undefined | T>;
1314
set(key: Buffer, value: T): void | Promise<void>;
1415
clear(): void | Promise<void>;
1516
}

0 commit comments

Comments
 (0)