Edits history of script submission #15887 for ' Wait for State (fly)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Fly = {
      token: string;
    };
    /**
     * Wait for State
     * Wait for a Machine to reach a specific state. Specify the desired state with the state parameter. See the [Machine states table](https://fly.io/docs/machines/working-with-machines/#machine-states) for a list of possible states. The default for this parameter is `started`.
    
    This request will block for up to 60 seconds. Set a shorter timeout with the timeout parameter.
    
     */
    export async function main(
      auth: Fly,
      app_name: string,
      machine_id: string,
      instance_id: string | undefined,
      timeout: string | undefined,
      state: "started" | "stopped" | "suspended" | "destroyed" | undefined,
    ) {
      const url = new URL(
        `https://api.machines.dev/v1/apps/${app_name}/machines/${machine_id}/wait`,
      );
      for (const [k, v] of [
        ["instance_id", instance_id],
        ["timeout", timeout],
        ["state", state],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 235 days ago