|
1 | | -const fetch = require('isomorphic-fetch') |
2 | | -const Validator = require('jsonschema').Validator |
3 | | -const CustomV1Schema = require('../../schema/custom-v1.json') |
4 | | -const UrlSchema = require('../../schema/url.json') |
5 | | -const BaseProvider = require('./base-provider') |
| 1 | +const Validator = require("jsonschema").Validator; |
| 2 | +const CustomV1Schema = require("../../schema/custom-v1.json"); |
| 3 | +const UrlSchema = require("../../schema/url.json"); |
| 4 | +const BaseProvider = require("./base-provider"); |
6 | 5 |
|
7 | | -class CustomV1 extends BaseProvider |
8 | | -{ |
9 | | - randomImage(query) { |
10 | | - const providerUrl = this._provider.substring(this._provider.indexOf(':')+1) |
11 | | - return this.validateAndFetchProvider(providerUrl) |
12 | | - .then(imagePool => this.imageFromPool(query, imagePool)) |
13 | | - .catch(err => { throw err }) |
14 | | - } |
| 6 | +class CustomV1 extends BaseProvider { |
| 7 | + randomImage(query) { |
| 8 | + const providerUrl = this._provider.substring( |
| 9 | + this._provider.indexOf(":") + 1, |
| 10 | + ); |
| 11 | + return this.validateAndFetchProvider(providerUrl) |
| 12 | + .then((imagePool) => this.imageFromPool(query, imagePool)) |
| 13 | + .catch((err) => { |
| 14 | + throw err; |
| 15 | + }); |
| 16 | + } |
15 | 17 |
|
16 | | - validateAndFetchProvider(providerUrl) { |
17 | | - const validator = new Validator() |
18 | | - return new Promise((resolve, reject) => { |
19 | | - if (validator.validate(providerUrl, UrlSchema).valid !== true) { |
20 | | - reject(new Error("Invalid URL")) |
21 | | - } |
| 18 | + validateAndFetchProvider(providerUrl) { |
| 19 | + const validator = new Validator(); |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + if (validator.validate(providerUrl, UrlSchema).valid !== true) { |
| 22 | + reject(new Error("Invalid URL")); |
| 23 | + } |
22 | 24 |
|
23 | | - fetch(providerUrl, { |
24 | | - headers: { |
25 | | - Accept: "application/json" |
26 | | - } |
27 | | - }) |
28 | | - .then(res => res.json()) |
29 | | - .catch(err => { |
30 | | - console.error(err) |
31 | | - throw new Error("This is not a valid image pool!") |
32 | | - }) |
33 | | - .then(data => { |
34 | | - if ( |
35 | | - validator.validate(data, CustomV1Schema).valid !== true |
36 | | - || data.version !== 1 |
37 | | - ) { |
38 | | - reject(new Error("Invalid image pool!")) |
39 | | - } |
40 | | - resolve(data) |
41 | | - }) |
42 | | - .catch(reject) |
| 25 | + fetch(providerUrl, { |
| 26 | + headers: { |
| 27 | + Accept: "application/json", |
| 28 | + }, |
| 29 | + }) |
| 30 | + .then((res) => res.json()) |
| 31 | + .catch((err) => { |
| 32 | + console.error(err); |
| 33 | + throw new Error("This is not a valid image pool!"); |
43 | 34 | }) |
44 | | - } |
45 | | - |
46 | | - imageFromPool(query, pool) { |
47 | | - const image = pool.pictures[Math.floor(Math.random()*pool.pictures.length)] |
48 | | - const imageServiceParams = [] |
| 35 | + .then((data) => { |
| 36 | + if ( |
| 37 | + validator.validate(data, CustomV1Schema).valid !== true || |
| 38 | + data.version !== 1 |
| 39 | + ) { |
| 40 | + reject(new Error("Invalid image pool!")); |
| 41 | + } |
| 42 | + resolve(data); |
| 43 | + }) |
| 44 | + .catch(reject); |
| 45 | + }); |
| 46 | + } |
49 | 47 |
|
50 | | - if (query.size) { |
51 | | - if (query.size.width > 0) imageServiceParams.push(`width=${query.size.width}`) |
52 | | - if (query.size.height > 0) imageServiceParams.push(`height=${query.size.height}`) |
53 | | - image.url = `${image.url}?${imageServiceParams.join('&')}` |
54 | | - image.size = query.size |
55 | | - } |
| 48 | + imageFromPool(query, pool) { |
| 49 | + const image = |
| 50 | + pool.pictures[Math.floor(Math.random() * pool.pictures.length)]; |
| 51 | + const imageServiceParams = []; |
56 | 52 |
|
57 | | - return this._normalizeResponse(image) |
| 53 | + if (query.size) { |
| 54 | + if (query.size.width > 0) |
| 55 | + imageServiceParams.push(`width=${query.size.width}`); |
| 56 | + if (query.size.height > 0) |
| 57 | + imageServiceParams.push(`height=${query.size.height}`); |
| 58 | + image.url = `${image.url}?${imageServiceParams.join("&")}`; |
| 59 | + image.size = query.size; |
58 | 60 | } |
| 61 | + |
| 62 | + return this._normalizeResponse(image); |
| 63 | + } |
59 | 64 | } |
60 | 65 |
|
61 | | -module.exports = CustomV1 |
| 66 | +module.exports = CustomV1; |
0 commit comments