Edits history of script submission #10724 for ' Create Trigger (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Create Trigger
     * Creates a new trigger for a given function in a namespace. To create a trigger, send a POST request to `/v2/functions/namespaces/$NAMESPACE_ID/triggers` with the `name`, `function`, `type`, `is_enabled` and `scheduled_details` properties.
     */
    export async function main(
      auth: Digitalocean,
      namespace_id: string,
      body: {
        name: string;
        function: string;
        type: string;
        is_enabled: false | true;
        scheduled_details: { cron: string; body?: { name?: string } };
      },
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/functions/namespaces/${namespace_id}/triggers`,
      );
    
      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 537 days ago