Edits history of script submission #12006 for ' Get a list of organization invitations (clerk)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Clerk = {
      apiKey: string;
    };
    /**
     * Get a list of organization invitations
     * This request returns the list of organization invitations.
    Results can be paginated using the optional `limit` and `offset` query parameters.
    You can filter them by providing the 'status' query parameter, that accepts multiple values.
    The organization invitations are ordered by descending creation date.
    Most recent invitations will be returned first.
    Any invitations created as a result of an Organization Domain are not included in the results.
     */
    export async function main(
      auth: Clerk,
      organization_id: string,
      limit: string | undefined,
      offset: string | undefined,
      status: "pending" | "accepted" | "revoked" | undefined,
    ) {
      const url = new URL(
        `https://api.clerk.com/v1/organizations/${organization_id}/invitations`,
      );
      for (const [k, v] of [
        ["limit", limit],
        ["offset", offset],
        ["status", status],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago