README
Toilmore Javascript API
Installing
Use:
npm install --save-dev @shimmercat/toilmore-sdk
How to use
Simplifies using the toilmore API by wrapping the request state machine. To optimize an image, all you need is a valid API token.
Using the light API
import { LIGHT_API, Toilmore } from '@shimmercat/toilmore-sdk';
import fs from "fs";
import { Readable } from "stream";
let toilmore = new Toilmore(
{
// `LIGHT_API` contains our light api endpoint.
'api_endpoint': LIGHT_API,
// Use a valid API token below:
'api_token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
// Use a valid domain below, as received when
// you created the token
'domain': 'YYYYYYYYYYYYYYYYYYYYYYYYYY'
});
let result_promise = toilmore.optimize("./my_image.jpg", "webp0");
result_promise.then((result) => {
// `result` will be null when the optimization effort fails.
if (result instanceof Readable) {
let optimized_img_path = "./my_image.webp";
let w = fs.createWriteStream(optimized_img_path);
result.pipe(w);
console.log("Successfully image optimized! Find it at: ", optimized_img_path);
} else {
console.log("The image could not be optimized due to: ", result.rejection_notice);
}
});
Using the Lux API
import { LUX_API, Toilmore } from '@shimmercat/toilmore-sdk';
import fs from "fs";
import { Readable } from "stream";
let adjustments = {
"shifter": {
"steps": [
{
"scale-to": {
"width": 90
}
}
]
},
"encoder": {
"quality-measure": "fsim-c",
"qual-threshold": 0.90
}
}
let toilmore = new Toilmore(
{
'api_endpoint': LUX_API,
'api_token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // <-- Use a valid API token here.
'domain': 'YYYYYYYYYYYYYYYYYYYYYYYYYY' // <-- Use a registered domain here, e.g the one you received when you started the trial period.
});
let result_promise = toilmore.optimize(
"./my_image.jpg",
"webp0",
adjustments
);
result_promise.then((result) => {
// `result` will be null when the optimization effort fails.
if (result instanceof Readable) {
let optimized_img_path = "./my_image.webp";
let w = fs.createWriteStream(optimized_img_path);
result.pipe(w);
console.log("Successfully image optimized! Find it at: ", optimized_img_path);
} else {
console.log("The image could not be optimized due to: ", result.rejection_notice);
}
});
Both the light and the lux API are supported as you could se above.