From 980787e0a4a3aa099fbf79a55d778099f8b60483 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sun, 17 Mar 2024 19:06:25 +0100 Subject: [PATCH] fix(types): expose some methods (#153) --- .gitignore | 1 + lightweight/index.d.ts | 8 +++++++- rollup.config.js | 4 ++-- src/factory.js | 6 +++--- src/node.d.ts | 6 ++++-- test/lightweight.test-d.ts | 34 ++++++++++++++++++++++++++++++---- test/node.test-d.ts | 36 ++++++++++++++++++++++++++++++++---- 7 files changed, 79 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index bdeb873..b743a60 100755 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ stats.html examples src/node.mjs lightweight/index.js +lightweight/index.umd.js diff --git a/lightweight/index.d.ts b/lightweight/index.d.ts index 95eab99..ca12769 100644 --- a/lightweight/index.d.ts +++ b/lightweight/index.d.ts @@ -173,12 +173,18 @@ export type MqlError = { export type MqlOptions = MqlClientOptions & MicrolinkApiOptions; +export const MicrolinkError: new (props: object) => MqlError; + +export const version: string; + interface mql { - (url: string, opts?: MqlOptions & { stream: string }, gotOpts?: object): Promise; + (url: string, opts?: MqlOptions & { stream: string }, gotOpts?: object): Promise; (url: string, opts?: MqlOptions, gotOpts?: object): Promise; arrayBuffer: (url: string, opts?: MqlOptions, gotOpts?: object) => Promise; extend: (gotOpts?: object) => mql; stream: (input: RequestInfo, init?: RequestInit) => ReadableStream; + MicrolinkError: new (props: object) => MqlError; + version: string; } declare const mql: mql; diff --git a/rollup.config.js b/rollup.config.js index 45719fc..32adce5 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -44,7 +44,7 @@ const builds = [ plugins: [commonjs(), rewriteFlattie()] }), build({ - compress: true, + compress: false, input: 'src/lightweight.js', output: { file: 'lightweight/index.js', format: 'es' }, plugins: [ @@ -55,7 +55,7 @@ const builds = [ ] }), build({ - compress: true, + compress: false, input: 'src/lightweight.js', output: { name: 'mql', file: 'lightweight/index.umd.js', format: 'umd' }, plugins: [ diff --git a/src/factory.js b/src/factory.js index 10f282d..b6341fc 100644 --- a/src/factory.js +++ b/src/factory.js @@ -64,8 +64,8 @@ const factory = streamResponseType => ({ return opts.responseType === streamResponseType ? response : { ...response.body, response } - } catch (err) { - const { response = {} } = err + } catch (error) { + const { response = {} } = error const { statusCode, body: rawBody, @@ -77,7 +77,7 @@ const factory = streamResponseType => ({ const body = isObject(rawBody) && !isBodyBuffer ? rawBody - : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri) + : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, error, uri) throw new MicrolinkError({ ...body, diff --git a/src/node.d.ts b/src/node.d.ts index 3aea123..e0ecb2c 100644 --- a/src/node.d.ts +++ b/src/node.d.ts @@ -1,6 +1,6 @@ -import { MqlPayload, MqlOptions } from '../lightweight'; +import { MqlError, MqlPayload, MqlOptions } from '../lightweight'; -export { MqlError, MqlPayload } from '../lightweight' +export { MicrolinkError, MqlError, MqlPayload } from '../lightweight' import { Response, Options as GotOpts } from 'got/dist/source/core' @@ -18,6 +18,8 @@ interface Mql { extend: (gotOpts?: GotOpts) => Mql; stream: (url: string, gotOpts?: GotOpts) => NodeJS.ReadableStream; buffer: (url: string, opts?: MqlOptions, gotOpts?: GotOpts) => Promise; + MicrolinkError: new (props: object) => MqlError; + version: string; } declare const mql: Mql; diff --git a/test/lightweight.test-d.ts b/test/lightweight.test-d.ts index b23dc29..0493baf 100644 --- a/test/lightweight.test-d.ts +++ b/test/lightweight.test-d.ts @@ -1,9 +1,29 @@ -import mql from '../lightweight' +import mql, { MicrolinkError } from '../lightweight' import type { MqlError } from '../lightweight' +/** error */ + +;(async () => { + const error = new MicrolinkError({ + status: 'fail', + data: { url: 'something goes wrong' }, + more: 'https://microlink.io/docs/api/api-parameters/url', + code: 'EINVALURLCLIENT', + message: 'something goes wrong', + url: 'https://example.com' + }) + + console.log(error.status) + console.log(error.data) + console.log(error.more) + console.log(error.code) + console.log(error.url) + console.log(error.description) +})() + /** mql */ -;async () => { +;(async () => { const result = await mql('https://example.com', { endpoint: 'https://pro.microlink.io', apiKey: '123', @@ -13,17 +33,20 @@ import type { MqlError } from '../lightweight' console.log(result.status) console.log(result.data) - console.log(result.statusCode) console.log(result.headers) console.log(result.response) -} + console.log(result.response.statusCode) +})() ;(async () => { const response = await mql('https://example.com', { stream: 'screenshot', screenshot: true }) + console.log(response.url) console.log(response.body) + console.log(response.headers) + console.log(response.statusCode) })() /** data */ @@ -106,6 +129,9 @@ mql('https://example.com', { video: true }) +console.log(MicrolinkError) +console.log(mql.version) + /** response */ const result = await mql('https://example.com', { meta: true }) diff --git a/test/node.test-d.ts b/test/node.test-d.ts index 1a1a213..2eac798 100644 --- a/test/node.test-d.ts +++ b/test/node.test-d.ts @@ -1,12 +1,37 @@ -import mql from '../src/node' +import mql, { MicrolinkError } from '../src/node' + +/** error */ + +;(async () => { + const error = new MicrolinkError({ + status: 'fail', + data: { url: 'something goes wrong' }, + more: 'https://microlink.io/docs/api/api-parameters/url', + code: 'EINVALURLCLIENT', + message: 'something goes wrong', + url: 'https://example.com' + }) + + console.log(error.status) + console.log(error.data) + console.log(error.more) + console.log(error.code) + console.log(error.url) + console.log(error.description) +})() /** mql */ ;(async () => { - const result = await mql('https://example.com', { meta: true }) + const result = await mql('https://example.com', { + endpoint: 'https://pro.microlink.io', + apiKey: '123', + retry: 2, + cache: new Map() + }) + console.log(result.status) console.log(result.data) - console.log(result.statusCode) console.log(result.headers) console.log(result.response) console.log(result.response.isFromCache) @@ -14,10 +39,13 @@ import mql from '../src/node' ;(async () => { const response = await mql('https://example.com', { - stream: 'string', + stream: 'screenshot', screenshot: true }) + console.log(response.url) console.log(response.body) + console.log(response.headers) + console.log(response.statusCode) })() /** got options */