All files / services/labelary API.js

0% Statements 0/9
0% Branches 0/8
0% Functions 0/1
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
import axios from 'axios';
 
async function ZPLToPNG(widthLabel, heightLabel, zpl) {
  try {
    const widthInches = Number(widthLabel || 10) / 2.54; // MM to Inches
    const heightInches = Number(heightLabel || 6) / 2.54; // MM to Inches
    const url = `http://api.labelary.com/v1/printers/8dpmm/labels/${widthInches}x${heightInches}/0/`;
 
    const response = await axios({
      method: 'post',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Accept: 'image/png',
      },
      data: zpl,
      responseType: 'arraybuffer',
      url,
    });
 
    if (response && response.data) {
      return await `data:image/png;base64,${Buffer.from(
        response.data,
        'binary'
      ).toString('base64')}`;
    }
    return null;
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log('Error API labelary', err);
  }
}
 
export default {
  ZPLToPNG,
};