DEV Community

Atsushi
Atsushi

Posted on

Introduction to GROWI TypeScript/JavaScript SDK (Part 3: manipulating attachments)

A REST API is available on GROWI, an open source in-house wiki. We are developing a TypeScript/JavaScript SDK to make this API easier to use. It is still in progress, but I will gradually add more functions.

This article explains how to operate attachments.

Notes.

This is a community SDK. Please refrain from contacting the official.

Source code

The source code of the GROWI TypeScript/JavaScript SDK is available on GitHub. The licence is MIT.

goofmint/growi-sdk-alpha

Installation.

Installation is done via npm/yarn.

$ npm install @goofmint/growi-js
# or
$ yarn add @goofmint/growi-js
Enter fullscreen mode Exit fullscreen mode

Usage

First import the SDK.

const { GROWI } = require('@goofmint/growi-js');
// or
import { GROWI } from '@goofmint/growi-js';.
Enter fullscreen mode Exit fullscreen mode

Then, initialise. At this point, specify the API token for GROWI.

const growi = new GROWI({ apiToken: 'your-api-token' });
Enter fullscreen mode Exit fullscreen mode

In addition to this, the following parameters are provided.

  • url : Specifies the URL of GROWI. Default is http://localhost:3000.
  • path : specifies the GROWI page path. Defaults to ''. Specify when installing in a subdirectory.

Get page

Attachments are tied to pages, so first retrieve the page. To retrieve a page from root, do the following. The return value is a Page object.

const page = await growi.root();
Enter fullscreen mode Exit fullscreen mode

Retrieving by page name

If you know the page name (path), use the page method.

const page = await growi.page({path: '/page name'});
Enter fullscreen mode Exit fullscreen mode

Upload attachments

Use the upload method for attachments. The easiest way is to specify the path to the file.

const fileName = 'logo.png';.
const attachment = await page.upload(path.resolve('path', 'to', fileName));
Enter fullscreen mode Exit fullscreen mode

After retrieval, the URL of the image can be retrieved with attachment.filePathProxied. It can be added to the body as follows.

page.set(body, `${page.body}\n\n![](${attachment.filePathProxied})`);
Enter fullscreen mode Exit fullscreen mode

Getting a list of attachments

You can use the list method to get a list of attachments.

const res = await Attachment.list(page);
res.attachments // array of Attachment instances
res.limit // 10
res.page // 1
res.totalDocs // 20
Enter fullscreen mode Exit fullscreen mode

Check if a file can be uploaded

You can check whether a file can be uploaded by specifying a file size. Note that the default value for GROWI is unlimited size.

const bol = await Attachment.limit(1024 * 1024 * 10);
bol // true
Enter fullscreen mode Exit fullscreen mode

Retrieving attachments

To retrieve an attachment, use the find method with the ID of the attachment.

const a = await Attachment.find(id);
a // Attachment instance
Enter fullscreen mode Exit fullscreen mode

Summary.

As an internal wiki, GROWI is likely to have data linkage needs with internal systems. Please use the Node.js SDK to operate GROWI from your system.

GROWI, an OSS development wiki tool | comfortable information sharing for all

Top comments (0)