File size: 1,613 Bytes
01e6679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { RepoDesignation, RepoId } from "../types/public";

export function toRepoId(repo: RepoDesignation): RepoId {
	if (typeof repo !== "string") {
		return repo;
	}

	if (repo.startsWith("model/") || repo.startsWith("models/")) {
		throw new TypeError(
			"A repo designation for a model should not start with 'models/', directly specify the model namespace / name",
		);
	}

	if (repo.startsWith("space/")) {
		throw new TypeError("Spaces should start with 'spaces/', plural, not 'space/'");
	}

	if (repo.startsWith("dataset/")) {
		throw new TypeError("Datasets should start with 'datasets/', plural, not 'dataset/'");
	}

	if (repo.startsWith("bucket/")) {
		throw new TypeError("Buckets should start with 'buckets/', plural, not 'bucket/'");
	}

	const slashes = repo.split("/").length - 1;

	if (repo.startsWith("spaces/")) {
		if (slashes !== 2) {
			throw new TypeError("Space Id must include namespace and name of the space");
		}

		return {
			type: "space",
			name: repo.slice("spaces/".length),
		};
	}

	if (repo.startsWith("datasets/")) {
		if (slashes > 2) {
			throw new TypeError("Too many slashes in repo designation: " + repo);
		}

		return {
			type: "dataset",
			name: repo.slice("datasets/".length),
		};
	}

	if (repo.startsWith("buckets/")) {
		if (slashes !== 2) {
			throw new TypeError("Bucket Id must include namespace and name of the bucket");
		}

		return {
			type: "bucket",
			name: repo.slice("buckets/".length),
		};
	}

	if (slashes > 1) {
		throw new TypeError("Too many slashes in repo designation: " + repo);
	}

	return {
		type: "model",
		name: repo,
	};
}