Edits history of script submission #13849 for ' Submit Billing Data (vercel)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Vercel = {
      token: string;
    };
    /**
     * Submit Billing Data
     * Sends the billing and usage data. The partner should do this at least once a day and ideally once per hour.  Use the `credentials.access_token` we provided in the [Upsert Installation](#upsert-installation) body to authorize this request.
     */
    export async function main(
      auth: Vercel,
      integrationConfigurationId: string,
      body: {
        timestamp: string;
        eod: string;
        period: { start: string; end: string };
        billing:
          | {
              billingPlanId: string;
              resourceId?: string;
              start?: string;
              end?: string;
              name: string;
              details?: string;
              price: string;
              quantity: number;
              units: string;
              total: string;
            }[]
          | {
              items: {
                billingPlanId: string;
                resourceId?: string;
                start?: string;
                end?: string;
                name: string;
                details?: string;
                price: string;
                quantity: number;
                units: string;
                total: string;
              }[];
              discounts?: {
                billingPlanId: string;
                resourceId?: string;
                start?: string;
                end?: string;
                name: string;
                details?: string;
                amount: string;
              }[];
            };
        usage: {
          resourceId?: string;
          name: string;
          type: "total" | "interval" | "rate";
          units: string;
          dayValue: number;
          periodValue: number;
          planValue?: number;
        }[];
      },
    ) {
      const url = new URL(
        `https://api.vercel.com/v1/installations/${integrationConfigurationId}/billing`,
      );
    
      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.text();
    }
    

    Submitted by hugo697 428 days ago