Edits history of script submission #13711 for ' Assign an Alias (vercel)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Vercel = {
      token: string;
    };
    /**
     * Assign an Alias
     * Creates a new alias for the deployment with the given deployment ID. The authenticated user or team must own this deployment. If the desired alias is already assigned to another deployment, then it will be removed from the old deployment and assigned to the new one.
     */
    export async function main(
      auth: Vercel,
      id: string,
      teamId: string | undefined,
      slug: string | undefined,
      body: { alias?: string; redirect?: string },
    ) {
      const url = new URL(`https://api.vercel.com/v2/deployments/${id}/aliases`);
      for (const [k, v] of [
        ["teamId", teamId],
        ["slug", slug],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago