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

microlinkhq / mql / 8317274257

17 Mar 2024 06:00PM UTC coverage: 74.595%. First build
8317274257

Pull #153

github

web-flow
Merge d4106c84f into 2100c49a4
Pull Request #153: fix(types): expose some methods

153 of 202 branches covered (75.74%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 1 file covered. (0.0%)

860 of 1156 relevant lines covered (74.39%)

6.8 hits per line

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

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

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

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

2✔
14
const parseBody = (input, error, url) => {
2✔
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

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

2✔
52
  const mapRules = rules => {
2✔
53
    if (!isObject(rules)) return
×
54
    const flatRules = flatten(rules)
×
55
    return Object.keys(flatRules).reduce((acc, key) => {
×
56
      acc[`data.${key}`] = flatRules[key].toString()
×
57
      return acc
×
58
    }, {})
×
59
  }
×
60

2✔
61
  const fetchFromApi = async (apiUrl, opts = {}) => {
2✔
62
    try {
×
63
      const response = await got(apiUrl, opts)
×
64
      return opts.responseType === streamResponseType
×
65
        ? response
×
66
        : { ...response.body, response }
×
NEW
67
    } catch (error) {
×
NEW
68
      const { response = {} } = error
×
69
      const {
×
70
        statusCode,
×
71
        body: rawBody,
×
72
        headers = {},
×
73
        url: uri = apiUrl
×
74
      } = response
×
75
      const isBodyBuffer = isBuffer(rawBody)
×
76

×
77
      const body =
×
78
        isObject(rawBody) && !isBodyBuffer
×
79
          ? rawBody
×
NEW
80
          : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri)
×
81

×
82
      throw new MicrolinkError({
×
83
        ...body,
×
84
        message: body.message,
×
85
        url: uri,
×
86
        statusCode,
×
87
        headers
×
88
      })
×
89
    }
×
90
  }
×
91

2✔
92
  const getApiUrl = (
2✔
93
    url,
×
94
    { data, apiKey, endpoint, retry, cache, ...opts } = {},
×
95
    { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
×
96
  ) => {
×
97
    const isPro = !!apiKey
×
98
    const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE']
×
99

×
100
    const apiUrl = `${apiEndpoint}?${new URLSearchParams({
×
101
      url,
×
102
      ...mapRules(data),
×
103
      ...flatten(opts)
×
104
    }).toString()}`
×
105

×
106
    const headers = isPro
×
107
      ? { ...gotHeaders, 'x-api-key': apiKey }
×
108
      : { ...gotHeaders }
×
109

×
110
    if (opts.stream) {
×
111
      responseType = streamResponseType
×
112
    }
×
113
    return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
×
114
  }
×
115

2✔
116
  const createMql = defaultOpts => async (url, opts, gotOpts) => {
2✔
117
    assertUrl(url)
×
118
    const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
×
119
      ...defaultOpts,
×
120
      ...gotOpts
×
121
    })
×
122
    return fetchFromApi(apiUrl, fetchOpts)
×
123
  }
×
124

2✔
125
  const mql = createMql()
2✔
126
  mql.extend = createMql
2✔
127
  mql.MicrolinkError = MicrolinkError
2✔
128
  mql.getApiUrl = getApiUrl
2✔
129
  mql.fetchFromApi = fetchFromApi
2✔
130
  mql.mapRules = mapRules
2✔
131
  mql.version = VERSION
2✔
132
  mql.stream = got.stream
2✔
133

2✔
134
  return mql
2✔
135
}
2✔
136

2✔
137
module.exports = factory
2✔
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