Nextcloud Tables: Get all columns of a table

Get all columns of the given table in the Nextcloud Tables app

Script nextcloud Verified

by marcel klehr12 · 7/23/2024

The script

Submitted by nextcloud Bun
Verified 86 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3
export async function main(
4
  nextcloud: RT.Nextcloud,
5
  tableId: number,
6
) {
7

8
  const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
9
  const authMiddleware: Middleware = {
10
    async onRequest({ request, options }) {
11
      // fetch token, if it doesn’t exist
12
      // add Authorization header to every request
13
      request.headers.set("Authorization", `Basic ${btoa((nextcloud.userId) + ':' + nextcloud.token)}`);
14
      return request;
15
    },
16
  };
17
  client.use(authMiddleware);
18

19
  const resp = await client.GET("/index.php/apps/tables/api/1/tables/{tableId}/columns", {
20
    params: {
21
      header: {
22
        "OCS-APIRequest": true,
23
      },
24
      query: {
25
        format: "json",
26
      },
27
      path: {
28
        tableId: tableId,
29
      },
30

31
    },
32
  });
33

34
  return resp.data;
35

36
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 568 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import * as tb from "https://raw.githubusercontent.com/marcelklehr/nextcloud-client-deno/1629b11dc919b99d8dfadf2870a02128a24333fa/tables/index.ts";
    3
    
    
    4
    // fill the type, or use the +Resource type to get a type-safe reference to a resource
    5
    // type Postgresql = object
    6
    
    
    7
    export async function main(
    8
      nextcloudResource: string,
    9
      userId: string | null = null,
    10
      tableId: number,
    11
      limit: number | null = null,
    12
      offset: number | null = null,
    13
      useAppApiAuth: boolean = false,
    14
    ) {
    15
      const ncResource = await wmill.getResource(
    16
        nextcloudResource,
    17
      );
    18
      const config = new tb.Configuration({
    19
        username: userId || ncResource.username,
    20
        password: ncResource.password,
    21
        basePath: ncResource.baseUrl,
    22
        middleware: [{
    23
          async pre(context) {
    24
            if (!context.url.includes("?")) {
    25
              context.url += "?";
    26
            } else {
    27
              context.url += "&";
    28
            }
    29
            context.url += "format=json";
    30
            return context;
    31
          },
    32
        }],
    33
        ...(useAppApiAuth && ({
    34
          headers: {
    35
            "AA-VERSION": "2.3.0",
    36
            "EX-APP-ID": "flow",
    37
            "EX-APP-VERSION": "1.0.0",
    38
            "AUTHORIZATION-APP-API": btoa(
    39
              `${userId || ncResource.username}:${ncResource.password}`,
    40
            ),
    41
          },
    42
        })),
    43
      });
    44
      const api = new tb.Api1Api(config);
    45
    
    
    46
      return await api.api1ListTableColumns({
    47
        tableId,
    48
        ...(limit && ({ limit })),
    49
        ...(offset && ({ offset })),
    50
      });
    51
    }
    52