Back

0% Statements 0/1
100% Branches 0/0
0% Functions 0/1
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
import { apiCall, apiPath, toIntRewindId } from './_helpers';
 
/**
 * A single entry in WPCOM's `/rewind/backup/ls` `contents` map.
 *
 * The endpoint returns `contents` as an object keyed by filename, so the
 * entry value itself has no `name` field — the parent's key carries it.
 * Type discriminators: `'dir'` (folder), `'file'` (regular file),
 * `'wordpress'` (virtual core-version marker that has no children).
 */
export type WpcomFileNode = {
	type: 'dir' | 'file' | 'wordpress' | string;
	manifest_path?: string;
	has_children?: boolean;
	total_items?: number;
	period?: string;
	id?: string;
	wordpress_version?: string;
	sort?: number;
};
 
export type WpcomLsResponse = {
	ok?: boolean;
	error?: string;
	contents?: Record< string, WpcomFileNode >;
};
 
/**
 * List the children of a folder inside a backup.
 *
 * @param rewindId   - The backup's rewind id (decimal suffix stripped here).
 * @param folderPath - Folder to list, relative to backup root.
 * @return The decoded WPCOM ls response.
 */
export async function fetchFileTree(
	rewindId: string,
	folderPath: string
): Promise< WpcomLsResponse > {
	return apiCall< WpcomLsResponse >( {
		path: apiPath( '/rewind/backup/ls' ),
		method: 'POST',
		data: {
			rewind_id: toIntRewindId( rewindId ),
			path: folderPath,
		},
	} );
}