Edits history of script submission #15837 for ' Create Lease (fly)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Fly = {
      token: string;
    };
    /**
     * Create Lease
     * Create a lease for a specific Machine within an app using the details provided in the request body. Machine leases can be used to obtain an exclusive lock on modifying a Machine.
    
     */
    export async function main(
      auth: Fly,
      app_name: string,
      machine_id: string,
      body: { description?: string; ttl?: number },
      fly_machine_lease_nonce?: string,
    ) {
      const url = new URL(
        `https://api.machines.dev/v1/apps/${app_name}/machines/${machine_id}/lease`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          ...(fly_machine_lease_nonce
            ? { "fly-machine-lease-nonce": fly_machine_lease_nonce }
            : {}),
          "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 235 days ago