Create an approval link with the approve_links app

Script nextcloud Verified

by marcel klehr12 · 9/5/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
  approveCallbackUri: string,
6
  rejectCallbackUri: string,
7
  description: string,
8
) {
9

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

21
  const data = {
22
    approveCallbackUri,
23
    rejectCallbackUri,
24
    description,
25
  }
26

27
  try {
28
    const resp = await await client.POST("/ocs/v2.php/apps/approve_links/api/v1/link", {
29
      params: {
30
        header: {
31
          "OCS-APIRequest": true,
32
        },
33
        query: {
34
          format: "json",
35
        },
36

37
      },
38
      body: data,
39
    });
40
    console.debug('RESPONSE', resp.data)
41
    return {
42
      link: resp.data.ocs.data.link,
43
    }
44
  } catch (e) {
45
    console.debug('error', e)
46
  }
47

48

49
  return {}
50

51
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 568 days ago
    1
    import * as wmill from "npm:windmill-client@1"
    2
    import axios from "npm:axios"
    3
    
    
    4
    export async function main(
    5
      nextcloudResource: string,
    6
      userId: string|null = null,
    7
      approveCallbackUri: string, 
    8
      rejectCallbackUri: string, 
    9
      description: string,
    10
      useAppApiAuth: boolean = false,
    11
    ) {
    12
      const ncResource = await wmill.getResource(
    13
        nextcloudResource,
    14
      );
    15
    
    
    16
      const data = {
    17
        approveCallbackUri,
    18
        rejectCallbackUri,
    19
        description,
    20
      }
    21
      const url = ncResource.baseUrl + '/ocs/v2.php/apps/approve_links/api/v1/link'
    22
      const config = {
    23
        ...(!useAppApiAuth && ({
    24
          auth: {
    25
            username: ncResource.username,
    26
            password: ncResource.password,
    27
          },
    28
        })),
    29
        headers: {
    30
          'content-type': 'application/json',
    31
          'ocs-apirequest': true,
    32
          ...(useAppApiAuth && ({
    33
            "AA-VERSION": "2.3.0",
    34
            "EX-APP-ID": "flow",
    35
            "EX-APP-VERSION": "1.0.0",
    36
            "AUTHORIZATION-APP-API": btoa(
    37
              `${userId || ncResource.username}:${ncResource.password}`,
    38
            ),
    39
        })),
    40
        },
    41
      }
    42
      console.debug('config', config)
    43
      try {
    44
        const resp = await axios.post(url, data, config)
    45
        console.debug('RESPONSE', resp.data)
    46
        return {
    47
          link: resp.data.ocs.data.link,
    48
        }
    49
      } catch(e) {
    50
        console.debug('error', e)
    51
      }
    52
      
    53
      
    54
      return {}
    55
    }