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.
Installation.
Installation is done via npm/yarn.
$ npm install @goofmint/growi-js
# or
$ yarn add @goofmint/growi-js
Usage
First import the SDK.
const { GROWI } = require('@goofmint/growi-js');
// or
import { GROWI } from '@goofmint/growi-js';.
Then, initialise. At this point, specify the API token for GROWI.
const growi = new GROWI({ apiToken: 'your-api-token' });
In addition to this, the following parameters are provided.
-
url
: Specifies the URL of GROWI. Default ishttp://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();
Retrieving by page name
If you know the page name (path), use the page
method.
const page = await growi.page({path: '/page name'});
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));
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})`);
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
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
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
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)