Skip to content

Commit

Permalink
fix(types): expose some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 17, 2024
1 parent 2100c49 commit 40a955d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ stats.html
examples
src/node.mjs
lightweight/index.js
lightweight/index.umd.js
8 changes: 7 additions & 1 deletion lightweight/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>;
(url: string, opts?: MqlOptions & { stream: string }, gotOpts?: object): Promise<HTTPResponseRaw>;
(url: string, opts?: MqlOptions, gotOpts?: object): Promise<MqlResponse>;
arrayBuffer: (url: string, opts?: MqlOptions, gotOpts?: object) => Promise<HTTPResponseRaw>;
extend: (gotOpts?: object) => mql;
stream: (input: RequestInfo, init?: RequestInit) => ReadableStream;
MicrolinkError: new (props: object) => MqlError;
version: string;
}

declare const mql: mql;
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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: [
Expand Down
6 changes: 3 additions & 3 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MqlPayload, MqlOptions } from '../lightweight';
import { version, 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'

Expand All @@ -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<HTTPResponseRaw>;
MicrolinkError: new (props: object) => MqlError;
version: string;
}

declare const mql: Mql;
Expand Down
34 changes: 30 additions & 4 deletions test/lightweight.test-d.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 */
Expand Down Expand Up @@ -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 })
Expand Down
36 changes: 32 additions & 4 deletions test/node.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
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)
})()

;(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 */
Expand Down

0 comments on commit 40a955d

Please sign in to comment.