-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
384 lines (295 loc) · 9.23 KB
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
(function vectorExpress() {
/*
Basic API :
https://vector.express/api/v2/public/convert/svg/auto/pdf/ - paths
https://vector.express/api/v2/public/convert/dxf/cadlib/svg/svgo/svg - convert
https://vector.express/api/v2/public/convert/svg/librsvg/pdf?use-file=4b89f780-0092-4de6-af11-8fb7f2467a28.svg - convert using existing file
https://vector.express/api/v2/public/convert/ai/gs/pdf/pdf2svg/svg/svgo/svg?pdf2svg-page=1&svgo-pretty=true - convert with config
*/
const fs = require( 'fs' );
const axios = require( 'axios' );
const APIWrapper = Object.create( null );
APIWrapper.basePath = 'https://vector.express';
APIWrapper.publicPath = 'https://vector.express/api/v2/public';
APIWrapper.meteredPath = 'https://vector.express/api/v2/metered';
/**
* Sends request to a url, returns Promise.
*
* @param {String} url
* @return {Promise} - contains info about request, response
*/
APIWrapper.get = async function ( url, format )
{
if( !url )
throw new Error( 'Please provide all obligatory args : url, format' );
if( typeof url !== 'string' )
throw new Error( 'Please provide url string as a parameter' );
const res = await axios.get( url );
if( format === 'full' )
return res;
return res.data;
}
/* */
/**
* Sends data to the url, returns Promise.
*
* @param {String} url
* @return {Promise} - contains info about request, response
*/
APIWrapper.post = async function ( url, data )
{
if( !url )
throw new Error( 'Please provide all obligatory args : url, data' );
if( typeof url !== 'string' )
throw new Error( 'Please provide url string as a parameter' );
const res = await axios({ url, method : 'post', data });
return res;
}
/* */
APIWrapper.convert = async function ( inputFormat, outputFormat, options )
{
/*
obligatory :
inputFormat
outputFormat
optional :
options
options.file
options.transformers
options.params
options.token
options.save
options.path
either options.file or options.params[ 'use-file' ] must be specified
*/
if( !inputFormat || !outputFormat )
throw new Error( 'Please provide all obligatory args : inputFormat, outputFormat' );
if( !options )
{
options = Object.create( null );
options.transformers = null;
options.params = null;
options.token = null;
options.save = null;
options.path = null;
options.file = null;
}
if( !options.file && ( !options.params || !options.params[ 'use-file' ] ) )
throw new Error( 'Please provide a file or specify file on a server through 3d arg : options( options.params[ \'use-file\' ] field )' );
let url;
let transformers = options.transformers;
if( !transformers )
{
let paths, res;
try
{
if( options.token )
{
paths = await axios
({
url : `${this.meteredPath}/convert/${inputFormat}/auto/${outputFormat}/`,
method : 'get',
headers : { Authorization : `Bearer ${options.token}` }
});
}
else
{
paths = await axios.get( `${this.publicPath}/convert/${inputFormat}/auto/${outputFormat}/` );
}
} catch ( error )
{
console.error( error );
throw new Error( error );
}
paths = paths.data.alternatives.map( ( el ) => el.path );
url = `${this.basePath}${paths[ 0 ]}`;
}
else
{
transformers = transformers.join( '/' );
url = `${options.token ? this.meteredPath : this.publicPath}/convert/${inputFormat}/${transformers}/${outputFormat}`;
}
/* - config - */
let config = Object.create( null );
config.url = url;
config.method = 'post';
if( options.params )
config.params = options.params;
if( options.params && options.params[ 'use-file' ] )
config.data = undefined;
else
config.data = options.file;
if( options.token )
config.headers = { Authorization: `Bearer ${options.token}` };
/* - config - */
try
{
res = await axios( config );
} catch ( error )
{
console.error( error );
throw new Error( error );
}
if( options.save )
{
let path = options.path || `${__dirname}/file.${res.data.format}`;
return this.downloadAndSave( res.data.resultUrl, path );
}
else
{
return res.data.resultUrl;
}
}
/* */
APIWrapper.analyze = async function ( format, analyzers, options )
{
/*
obligatory :
format
analyzers
optional :
options
options.file
options.params
options.token
either options.file or options.params[ 'use-file' ] must be specified
*/
if( !format || !analyzers )
throw new Error( 'Please provide all mandatory args : format, analyzers' );
if( Object.prototype.toString.call( analyzers ) === '[object Array]' )
analyzers = analyzers.join( '/' );
else if( !( Object.prototype.toString.call( analyzers ) === '[object String]' ) )
throw new Error( 'Analyzers can either be an array or string' );
if( !options )
{
options = Object.create( null );
options.params = null;
options.token = null;
options.file = null;
}
if( !options.file && ( !options.params || !options.params[ 'use-file' ] ) )
throw new Error( 'Please provide a file or specify file on a server through 3d arg : options( options.params[ \'use-file\' ] field )' );
/* - config - */
let config = Object.create( null );
config.method = 'post';
if( options.params )
config.params = options.params;
if( options.params && options.params[ 'use-file' ] )
config.data = undefined;
else
config.data = options.file;
if( options.token )
{
config.headers = { Authorization: `Bearer ${options.token}` };
url = `${this.meteredPath}/analyze/${format}/${analyzers}`;
}
else
{
url = `${this.publicPath}/analyze/${format}/${analyzers}`;
}
config.url = url;
/* - config - */
let res, analytics;
try
{
res = await axios( config );
analytics = await axios.get( res.data.resultUrl );
} catch ( error )
{
console.error( error );
throw new Error( error );
}
return analytics.data;
}
/* */
APIWrapper.process = async function ( format, processors, options )
{
/*
obligatory :
format
processors
optional :
options
options.file
options.params
options.token
options.save
options.path
either options.file or options.params[ 'use-file' ] must be specified
*/
if( !format || !processors )
throw new Error( 'Please provide all obligatory args : file, format, processors' );
if( Object.prototype.toString.call( processors ) === '[object Array]' )
processors = processors.join( '/' );
else if( !( Object.prototype.toString.call( processors ) === '[object String]' ) )
throw new Error( 'Processors can either be an array or string' )
if( !options )
{
options = Object.create( null );
options.params = null;
options.token = null;
options.save = null;
options.path = null;
options.file = null;
}
if( !options.file && ( !options.params || !options.params[ 'use-file' ] ) )
throw new Error( 'Please provide a file or specify file on a server through 3d arg : options( options.params[ \'use-file\' ] field )' );
let res, url;
/* - config - */
let config = Object.create( null );
config.method = 'post';
if( options.params )
config.params = options.params;
if( options.params && options.params[ 'use-file' ] )
config.data = undefined;
else
config.data = options.file;
if( options.token )
{
config.headers = { Authorization: `Bearer ${options.token}` };
url = `${this.meteredPath}/process/${format}/${processors}`;
}
else
{
url = `${this.publicPath}/process/${format}/${processors}`;
}
config.url = url;
/* - config - */
try
{
res = await axios( config )
} catch ( error )
{
console.error( error );
throw new Error( error );
}
if( options.save )
{
let path = options.path || `${__dirname}/file.${res.data.format}`;
return this.downloadAndSave( res.data.resultUrl, path );
}
else
{
return res.data.resultUrl;
}
}
/* */
APIWrapper.downloadAndSave = async ( url, path ) =>
{
if( !url )
throw new Error( 'Please provide all obligatory args : url' );
if( !path )
path = __dirname + '/file';
let response = await axios({ url, method : 'get', responseType : 'stream' });
// create directories
let dirPath = path.split( '/' ).slice( 0, -1 ).join( '/' ) + '/';
await fs.mkdir( dirPath, { recursive: true }, () => {} );
response.data.pipe( fs.createWriteStream( path ) );
return new Promise( ( resolve, reject ) =>
{
response.data.on( 'end', () => { console.log( 'File is saved!' ); resolve() });
response.data.on( 'error', ( err ) => { reject( err ) });
})
}
module.exports = APIWrapper;
})();