Skip to content

Commit

Permalink
feat: add .extend and buffer/arrayBuffer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 29, 2023
1 parent e0b81ab commit 44bf208
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ const factory = ({ VERSION, MicrolinkError, urlHttp, got, flatten }) => {
}

const mql = createMql()
mql.extend = createMql
mql.MicrolinkError = MicrolinkError
mql.getApiUrl = getApiUrl
mql.fetchFromApi = fetchFromApi
mql.mapRules = mapRules
mql.version = VERSION
mql.stream = got.stream
mql.buffer = createMql({ responseType: 'buffer' })

return mql
}
Expand Down
6 changes: 4 additions & 2 deletions src/lightweight.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class MicrolinkError extends Error {
}
}

const got = async (url, opts) => {
const got = async (url, { responseType, ...opts }) => {
try {
if (opts.retry > 0) opts.retry = opts.retry + 1
if (opts.timeout === undefined) opts.timeout = false
const response = await ky(url, opts)
const body = await response.json()
const body = await response[responseType]()
const { headers, status: statusCode } = response
return { url: response.url, body, headers, statusCode }
} catch (err) {
Expand Down Expand Up @@ -54,6 +54,8 @@ const mql = factory({
})

module.exports = mql
module.exports.arrayBuffer = mql.extend({ responseType: 'arrayBuffer' })
module.exports.extend = mql.extend
module.exports.fetchFromApi = mql.fetchFromApi
module.exports.getApiUrl = mql.getApiUrl
module.exports.mapRules = mql.mapRules
Expand Down
4 changes: 3 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = require('./factory')({
const mql = require('./factory')({
MicrolinkError: require('whoops')('MicrolinkError'),
urlHttp: require('url-http/lightweight'),
got: require('got').extend({ headers: { 'user-agent': undefined } }),
flatten: require('flattie').flattie,
VERSION: require('../package.json').version
})

module.exports = mql
module.exports.buffer = mql.extend({ responseType: 'buffer' })
module.exports.render = (input, { width = '650px' } = {}) => {
if (input && input.url && input.type) {
return `<img width="${width}" src="${input.url}" />`
Expand Down
5 changes: 3 additions & 2 deletions test/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test('cjs', async t => {
t.deepEqual(methods,
[
'buffer',
'extend',
'fetchFromApi',
'getApiUrl',
'mapRules',
Expand All @@ -35,14 +36,14 @@ test('esm', async t => {

t.deepEqual(methods,
[
'arrayBuffer',
'default',
'extend',
'fetchFromApi',
'getApiUrl',
'mapRules',
'MicrolinkError',
'version'
// render
// sstream
]
)
})

0 comments on commit 44bf208

Please sign in to comment.