• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microlinkhq / mql / 6418009246

05 Oct 2023 10:47AM UTC coverage: 69.897% (-3.3%) from 73.2%
6418009246

push

github

Kikobeats
fix(types): expose MqlError

139 of 184 branches covered (0.0%)

Branch coverage included in aggregate %.

200 of 301 relevant lines covered (66.45%)

3.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.23
/src/factory.js
1
const ENDPOINT = {
1✔
2
  FREE: 'https://api.microlink.io/',
1✔
3
  PRO: 'https://pro.microlink.io/'
1✔
4
}
1✔
5

1✔
6
const isObject = input => input !== null && typeof input === 'object'
1✔
7

1✔
8
const isBuffer = input =>
1✔
9
  input != null &&
×
10
  input.constructor != null &&
×
11
  typeof input.constructor.isBuffer === 'function' &&
×
12
  input.constructor.isBuffer(input)
×
13

1✔
14
const parseBody = (input, error, url) => {
1✔
15
  try {
×
16
    return JSON.parse(input)
×
17
  } catch (_) {
×
18
    const message = input || error.message
×
19

×
20
    return {
×
21
      status: 'error',
×
22
      data: { url: message },
×
23
      more: 'https://microlink.io/efatalclient',
×
24
      code: 'EFATALCLIENT',
×
25
      message,
×
26
      url
×
27
    }
×
28
  }
×
29
}
×
30

1✔
31
const factory = ({ VERSION, MicrolinkError, urlHttp, got, flatten }) => {
1✔
32
  const assertUrl = (url = '') => {
1✔
33
    if (!urlHttp(url)) {
×
34
      const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`
×
35
      throw new MicrolinkError({
×
36
        status: 'fail',
×
37
        data: { url: message },
×
38
        more: 'https://microlink.io/docs/api/api-parameters/url',
×
39
        code: 'EINVALURLCLIENT',
×
40
        message,
×
41
        url
×
42
      })
×
43
    }
×
44
  }
×
45

1✔
46
  const mapRules = rules => {
1✔
47
    if (!isObject(rules)) return
×
48
    const flatRules = flatten(rules)
×
49
    return Object.keys(flatRules).reduce((acc, key) => {
×
50
      acc[`data.${key}`] = flatRules[key].toString()
×
51
      return acc
×
52
    }, {})
×
53
  }
×
54

1✔
55
  const fetchFromApi = async (apiUrl, opts = {}) => {
1✔
56
    try {
×
57
      const response = await got(apiUrl, opts)
×
58
      return opts.responseType === 'buffer'
×
59
        ? { body: response.body, response }
×
60
        : { ...response.body, response }
×
61
    } catch (err) {
×
62
      const { response = {} } = err
×
63
      const {
×
64
        statusCode,
×
65
        body: rawBody,
×
66
        headers = {},
×
67
        url: uri = apiUrl
×
68
      } = response
×
69
      const isBodyBuffer = isBuffer(rawBody)
×
70

×
71
      const body =
×
72
        isObject(rawBody) && !isBodyBuffer
×
73
          ? rawBody
×
74
          : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri)
×
75

×
76
      throw new MicrolinkError({
×
77
        ...body,
×
78
        message: body.message,
×
79
        url: uri,
×
80
        statusCode,
×
81
        headers
×
82
      })
×
83
    }
×
84
  }
×
85

1✔
86
  const getApiUrl = (
1✔
87
    url,
×
88
    { data, apiKey, endpoint, retry, cache, ...opts } = {},
×
89
    { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
×
90
  ) => {
×
91
    const isPro = !!apiKey
×
92
    const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE']
×
93

×
94
    const apiUrl = `${apiEndpoint}?${new URLSearchParams({
×
95
      url,
×
96
      ...mapRules(data),
×
97
      ...flatten(opts)
×
98
    }).toString()}`
×
99

×
100
    const headers = isPro
×
101
      ? { ...gotHeaders, 'x-api-key': apiKey }
×
102
      : { ...gotHeaders }
×
103
    return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
×
104
  }
×
105

1✔
106
  const createMql = defaultOpts => async (url, opts, gotOpts) => {
1✔
107
    assertUrl(url)
×
108
    const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
×
109
      ...defaultOpts,
×
110
      ...gotOpts
×
111
    })
×
112
    return fetchFromApi(apiUrl, fetchOpts)
×
113
  }
×
114

1✔
115
  const mql = createMql()
1✔
116
  mql.MicrolinkError = MicrolinkError
1✔
117
  mql.getApiUrl = getApiUrl
1✔
118
  mql.fetchFromApi = fetchFromApi
1✔
119
  mql.mapRules = mapRules
1✔
120
  mql.version = VERSION
1✔
121
  mql.stream = got.stream
1✔
122
  mql.buffer = createMql({ responseType: 'buffer' })
1✔
123

1✔
124
  return mql
1✔
125
}
1✔
126

1✔
127
module.exports = factory
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc