File size: 972 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type ContentPushUploadErrorCode =
	| "content_push_http_status"
	| "content_push_invalid_ref"
	| "content_push_missing_page_token";

export type ContentPushUploadError = Error & {
	code: ContentPushUploadErrorCode;
	status?: number;
	protocol?: string;
};

export function contentPushUploadError(
	code: ContentPushUploadErrorCode,
	message: string,
	meta: { status?: number; protocol?: string } = {},
): ContentPushUploadError {
	const err = new Error(message) as ContentPushUploadError;
	err.code = code;
	if (meta.status !== undefined) err.status = meta.status;
	if (meta.protocol) err.protocol = meta.protocol;
	return err;
}

export function validateContentPushFileRef(
	raw: unknown,
	protocol: string,
): string {
	const fileRef = String(raw || "").trim();
	if (!fileRef.startsWith("/")) {
		throw contentPushUploadError(
			"content_push_invalid_ref",
			`invalid ${protocol} file ref: ${fileRef.slice(0, 120)}`,
			{ protocol },
		);
	}
	return fileRef;
}