0

Record an artifacts cache usage event

by
Published Apr 8, 2025

Records an artifacts cache usage event. The body of this request is an array of cache usage events. The supported event types are `HIT` and `MISS`. The source is either `LOCAL` the cache event was on the users filesystem cache or `REMOTE` if the cache event is for a remote cache. When the event is a `HIT` the request also accepts a number `duration` which is the time taken to generate the artifact in the cache.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Record an artifacts cache usage event
7
 * Records an artifacts cache usage event. The body of this request is an array of cache usage events. The supported event types are `HIT` and `MISS`. The source is either `LOCAL` the cache event was on the users filesystem cache or `REMOTE` if the cache event is for a remote cache. When the event is a `HIT` the request also accepts a number `duration` which is the time taken to generate the artifact in the cache.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  teamId: string | undefined,
12
  slug: string | undefined,
13
  body: {
14
    sessionId: string;
15
    source: "LOCAL" | "REMOTE";
16
    event: "HIT" | "MISS";
17
    hash: string;
18
    duration?: number;
19
  }[],
20
  x_artifact_client_ci?: string,
21
  x_artifact_client_interactive?: string,
22
) {
23
  const url = new URL(`https://api.vercel.com/v8/artifacts/events`);
24
  for (const [k, v] of [
25
    ["teamId", teamId],
26
    ["slug", slug],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      ...(x_artifact_client_ci
36
        ? { "x-artifact-client-ci": x_artifact_client_ci }
37
        : {}),
38
      ...(x_artifact_client_interactive
39
        ? { "x-artifact-client-interactive": x_artifact_client_interactive }
40
        : {}),
41
      "Content-Type": "application/json",
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: JSON.stringify(body),
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.text();
51
}
52