Skip to content

Commit

Permalink
feat: add .extend and buffer/arrayBuffer methods (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Nov 30, 2023
1 parent e0b81ab commit 3e49bf2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 52 deletions.
30 changes: 18 additions & 12 deletions lightweight/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,18 @@ export type MqlPayload = {
headers: { [key: string]: string };
}

export type MqlResponse = MqlPayload & {
response: {
url: string;
statusCode: number;
headers: Headers;
body: MqlPayload
};
type HTTPResponse = {
url: string;
statusCode: number;
headers: Headers;
}

type HTTPResponseWithBody = HTTPResponse & { body: MqlPayload };

type HTTPResponseRaw = HTTPResponse & { body: ArrayBuffer };

export type MqlResponse = MqlPayload & { response: HTTPResponseWithBody };

export type MqlError = {
headers: { [key: string]: string };
data?: MqlResponseData;
Expand All @@ -176,10 +179,13 @@ export type MqlError = {

export type MqlOptions = MqlClientOptions & MicrolinkApiOptions;

declare function mql(
url: string,
opts?: MqlOptions,
gotOpts?: object
): Promise<MqlResponse>;
interface mql {
(url: string, opts?: MqlOptions, gotOpts?: object): Promise<MqlResponse>;
extend: (gotOpts?: object) => mql
stream: typeof fetch
arrayBuffer: (url: string, opts?: MqlOptions, gotOpts?: object) => Promise<HTTPResponseRaw>;
}

declare const mql: mql;

export default mql;
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
8 changes: 6 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 All @@ -45,6 +45,8 @@ const got = async (url, opts) => {
}
}

got.stream = ky

const mql = factory({
MicrolinkError,
urlHttp,
Expand All @@ -54,6 +56,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
42 changes: 20 additions & 22 deletions src/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { MqlPayload, MqlOptions } from '../lightweight'
import { MqlPayload, MqlOptions } from '../lightweight';

export { MqlError, MqlPayload } from '../lightweight'

export type MqlResponse = MqlPayload & {
response: {
url: string;
isFromCache?: boolean;
statusCode: number;
headers: { [key: string]: string };
body: MqlPayload
};
}
type HTTPResponse = {
url: string;
isFromCache?: boolean;
statusCode: number;
headers: { [key: string]: string };
};

type HTTPResponseWithBody = HTTPResponse & { body: MqlPayload };

type HTTPResponseRaw = HTTPResponse & { body: Buffer };

declare function mql(
url: string,
opts?: MqlOptions,
gotOpts?: object
): Promise<MqlResponse>;

declare namespace mql {
function stream(
url: string,
opts?: MqlOptions,
gotOpts?: object
): NodeJS.ReadableStream;
type MqlResponse = MqlPayload & { response: HTTPResponseWithBody };

interface Mql {
(url: string, opts?: MqlOptions, gotOpts?: object): Promise<MqlResponse>;
extend: (gotOpts?: object) => Mql;
stream: (url: string, gotOpts?: object) => NodeJS.ReadableStream;
buffer: (url: string, opts?: MqlOptions, gotOpts?: object) => Promise<HTTPResponseRaw>;
}

declare const mql: Mql;

export default mql;
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
]
)
})
37 changes: 25 additions & 12 deletions test/lightweight.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import mql from '../lightweight'
import type { MqlError } from '../lightweight'

/** mql */

mql('https://example.com', {
endpoint: 'https://pro.microlink.io',
apiKey: '123',
retry: 2,
cache: new Map()
})

/**
* data
*/
/** data */

mql('https://example.com', {
data: {
version: {
Expand Down Expand Up @@ -44,16 +44,14 @@ mql('https://github.com/microlinkhq', {
}
})

/**
* meta
*/
/** meta */

mql('https://example.com')
mql('https://example.com', { meta: true })
mql('https://example.com', { meta: { logo: { square: true } } })

/**
* pdf
*/
/** pdf */

mql('https://example.com', { pdf: true })
mql('https://example.com', {
pdf: {
Expand All @@ -68,9 +66,8 @@ mql('https://example.com', {
}
})

/**
* screenshot
*/
/** screenshot */

mql('https://example.com', { screenshot: true })
mql('https://example.com', {
screenshot: {
Expand All @@ -84,6 +81,7 @@ mql('https://example.com', {
})

/** others */

mql('https://example.com', { click: ['div'] })
mql('https://example.com', {
adblock: true,
Expand Down Expand Up @@ -146,3 +144,18 @@ console.log(result.headers)
message: 'EAUTH, Invalid API key. Make sure you are attaching your API key as `x-api-key` header.',
description: 'Invalid API key. Make sure you are attaching your API key as `x-api-key` header.'
}) as MqlError

/* extend */

mql.extend({ responseType: 'text' })

/* stream */

mql.stream('https://example.com', { headers: { 'user-agent': 'foo' }})

/* arrraBuffer */

{
const response = await mql.arrayBuffer('https://example.com', { meta: false })
console.log(response.body)
}

1 comment on commit 3e49bf2

@vercel
Copy link

@vercel vercel bot commented on 3e49bf2 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mql – ./

mql-git-master-microlink.vercel.app
mql-microlink.vercel.app
mql.microlink.io

Please sign in to comment.