Edits history of script submission #18063 for ' List organizations (planetscale)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Planetscale = {
      serviceTokenId: string;
      serviceToken: string;
    };
    /**
     * List organizations
     * When using a service token, returns the list of organizations the service token has access to. When using an OAuth token, returns the list of organizations the user has access to.
    ### Authorization
    A   OAuth token must have at least one of the following   scopes in order to use this API endpoint:
    
    **OAuth Scopes**
    
     | Resource | Scopes |
    | :------- | :---------- |
    | User | `read_organizations` |
     */
    export async function main(
      auth: Planetscale,
      page: string | undefined,
      per_page: string | undefined,
    ) {
      const url = new URL(`https://api.planetscale.com/v1/organizations`);
      for (const [k, v] of [
        ["page", page],
        ["per_page", per_page],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `${auth.serviceTokenId}:${auth.serviceToken}`,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago