Edits history of script submission #18022 for ' Create a database (planetscale)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Planetscale = {
      serviceTokenId: string;
      serviceToken: string;
    };
    /**
     * Create a database
     * 
    ### Authorization
    A service token or OAuth token must have at least one of the following access or scopes in order to use this API endpoint:
    
    **Service Token Accesses**
     `create_databases`
    
    **OAuth Scopes**
    
     | Resource | Scopes |
    | :------- | :---------- |
    | Organization | `create_databases` |
     */
    export async function main(
      auth: Planetscale,
      organization: string,
      body: { name: string; region?: string; cluster_size: string },
    ) {
      const url = new URL(
        `https://api.planetscale.com/v1/organizations/${organization}/databases`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `${auth.serviceTokenId}:${auth.serviceToken}`,
        },
        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 235 days ago