Edits history of script submission #23060 for ' List Org Units (gworkspace)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Org Units
     * List the organizational units of the Workspace account: all of them, or only the direct children of `org_unit_path`. Needs admin.directory.orgunit or admin.directory.orgunit.readonly on the connection.
     */
    export async function main(
      auth: RT.Gworkspace,
      type: "all" | "children" | "allIncludingParent" = "all",
      org_unit_path: string | undefined,
    ) {
      const url = new URL(
        "https://admin.googleapis.com/admin/directory/v1/customer/my_customer/orgunits",
      )
      url.searchParams.append("type", type)
      if (org_unit_path !== undefined && org_unit_path !== "") {
        url.searchParams.append("orgUnitPath", org_unit_path)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 1 hour ago