mimid-client v1.0.9
Client library for Mimid API
Node.js client library to access various API endpoints of the https://mimid.io service. The library uses API token that a valid user can obtain from the Mimid dashboard.
Using the library
First install the package with NPM or Yarn:
npm install mimid-client -s
After adding the package to your project, you can require it like this:
const mimid = require('mimid-client');
Setting the user token
The token can be set in the following way:
mimid.token(<TOKEN>);
It is also possible to set the token in an environment variable called MIMID_TOKEN:
export MIMID_TOKEN='<TOKEN>'
Creating a PDF
When you have an existing document on Mimid, you can get its ID from the URL when the document is open in the editor. You should also upload new CSV data that replaces the document default data.
Example:
const fs = require('fs');
const mimid = require('mimid-client');
const data = fs.readFileSync("./test-data.csv", 'utf8');
// Set the user token
mimid.token(process.env.MIMID_TOKEN);
(async () => {
// Generate PDF
const pdf = await mimid.pdf(process.env.MIMID_DOCUMENT_ID, data);
fs.writeFileSync("./doc.pdf", pdf, "binary");
console.log("Done creating PDF");
})();
Creating a text representation of the document
Exporting the document as text is almost identical to exporting a PDF. The biggest difference is that the call returns plain text.
Example:
const fs = require('fs');
const mimid = require('mimid-client');
const data = fs.readFileSync("./test-data.csv", 'utf8');
// Set the user token
mimid.token(process.env.MIMID_TOKEN);
(async () => {
// Generate text
const text = await mimid.text(process.env.MIMID_DOCUMENT_ID, data);
fs.writeFileSync("./doc.txt", text, "utf8");
console.log("Done creating text");
})();
Creating an image
Documents can be exported as PNG images. The current implementation tries to capture the area of the document that contains some visible content. The document margins are not included in the generated image (ie. the margin is always 0px).
Example:
const fs = require('fs');
const mimid = require('mimid-client');
const data = fs.readFileSync("./test-data.csv", 'utf8');
// Set the user token
mimid.token(process.env.MIMID_TOKEN);
(async () => {
// Generate PNG image
const image = await mimid.image(process.env.MIMID_DOCUMENT_ID, data);
fs.writeFileSync("./doc.png", image, "binary");
console.log("Done creating image");
})();
Exporting as HTML
The HTML export creates a simple HTML representation of the document. The HTML content does not include the styles or other properties that might have been set in the editor.
Following elements are supported:
- Headings (converted to H1-H3)
- Paragraphs (converted to p tags)
- Links (converted to plain a tags)
- Images (converted to img tags with the image data inlined in the src attribute)
- Text formatting like b, i, u and strike.
The generated HTML also does not contain the surrounding tags like html or body. Only the content is being included as HTML tags.
Example:
const fs = require('fs');
const mimid = require('mimid-client');
const data = fs.readFileSync("./test-data.csv", 'utf8');
// Set the user token
mimid.token(process.env.MIMID_TOKEN);
(async () => {
// Generate html
const html = await mimid.html(process.env.MIMID_DOCUMENT_ID, data);
fs.writeFileSync("./doc.html", html, "utf8");
console.log("Done creating HTML");
})();
Exporting as Markdown
The Markdown export is based on the HTML export, so it supports the same content elements.
Headings use the atx style format.
Example:
const fs = require('fs');
const mimid = require('mimid-client');
const data = fs.readFileSync("./test-data.csv", 'utf8');
// Set the user token
mimid.token(process.env.MIMID_TOKEN);
(async () => {
// Generate md
const markdown = await mimid.markdown(process.env.MIMID_DOCUMENT_ID, data);
fs.writeFileSync("./doc.md", markdown, "utf8");
console.log("Done creating markdown");
})();
Converting arrays to CSV
The client library contains a utility function to convert array of arrays or array of object to CSV.
Example
// Convert array of arrays to CSV
var arrayData = [
["First item", "Second item"],
["Second row", "Second row second item"]
];
var arrayCsv = mimid.csv(arrayData);
console.log(arrayCsv);
// Convert array of objects to CSV
var objectData = [
{name: "John Doe", age: 41},
{name: "Jane Doe", age: 45}
];
var objectCsv = mimid.csv(objectData);
console.log(objectCsv);