@microlink/mql 0.11.0-5 → 0.11.0-6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  {
2
- "main": "index.js",
2
+ "main": "lightweight.mjs",
3
3
  "type": "module",
4
4
  "types": "index.d.ts"
5
5
  }
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "name": "@microlink/mql",
3
3
  "description": "Microlink Query Language. The official HTTP client to interact with Microlink API for Node.js, browsers & Deno.",
4
4
  "homepage": "https://microlink.io/mql",
5
- "version": "0.11.0-5",
6
- "types": "index.d.ts",
5
+ "version": "0.11.0-6",
6
+ "types": "src/index.d.ts",
7
7
  "browser": "src/lightweight.js",
8
8
  "umd:main": "dist/mql.js",
9
9
  "unpkg": "dist/mql.js",
@@ -92,10 +92,13 @@
92
92
  "node": ">= 12"
93
93
  },
94
94
  "files": [
95
- "dist",
96
- "index.d.ts",
97
- "lightweight",
98
- "src"
95
+ "src/factory.js",
96
+ "src/index.d.ts",
97
+ "src/ky.js",
98
+ "src/node.js",
99
+ "dist/lightweight.mjs",
100
+ "dist/index.d.ts",
101
+ "dist/package.json"
99
102
  ],
100
103
  "license": "MIT",
101
104
  "ava": {
@@ -1,6 +1,6 @@
1
- import { MqlPayload, MqlOptions, MicrolinkApiOptions } from './lightweight'
1
+ import { MqlPayload, MqlOptions, MicrolinkApiOptions } from '../lightweight'
2
2
 
3
- export { MqlPayload } from './lightweight'
3
+ export { MqlPayload } from '../lightweight'
4
4
 
5
5
  export type MqlResponse = MqlPayload & {
6
6
  response: {
package/dist/node.mjs DELETED
@@ -1,158 +0,0 @@
1
- import require$$1 from 'whoops';
2
- import require$$2 from 'url-http/lightweight';
3
- import require$$3 from 'got';
4
- import * as require$$4 from 'flattie';
5
-
6
- function getDefaultExportFromCjs (x) {
7
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
8
- }
9
-
10
- var node$1 = {exports: {}};
11
-
12
- const ENDPOINT = {
13
- FREE: 'https://api.microlink.io/',
14
- PRO: 'https://pro.microlink.io/'
15
- };
16
-
17
- const isObject = input => input !== null && typeof input === 'object';
18
-
19
- const isBuffer = input =>
20
- input != null &&
21
- input.constructor != null &&
22
- typeof input.constructor.isBuffer === 'function' &&
23
- input.constructor.isBuffer(input);
24
-
25
- const parseBody = (input, error, url) => {
26
- try {
27
- return JSON.parse(input)
28
- } catch (_) {
29
- const message = input || error.message;
30
-
31
- return {
32
- status: 'error',
33
- data: { url: message },
34
- more: 'https://microlink.io/efatalclient',
35
- code: 'EFATALCLIENT',
36
- message,
37
- url
38
- }
39
- }
40
- };
41
-
42
- const factory = ({ VERSION, MicrolinkError, urlHttp, got, flatten }) => {
43
- const assertUrl = (url = '') => {
44
- if (!urlHttp(url)) {
45
- const message = `The \`url\` as \`${url}\` is not valid. Ensure it has protocol (http or https) and hostname.`;
46
- throw new MicrolinkError({
47
- status: 'fail',
48
- data: { url: message },
49
- more: 'https://microlink.io/docs/api/api-parameters/url',
50
- code: 'EINVALURLCLIENT',
51
- message,
52
- url
53
- })
54
- }
55
- };
56
-
57
- const mapRules = rules => {
58
- if (!isObject(rules)) return
59
- const flatRules = flatten(rules);
60
- return Object.keys(flatRules).reduce((acc, key) => {
61
- acc[`data.${key}`] = flatRules[key].toString();
62
- return acc
63
- }, {})
64
- };
65
-
66
- const fetchFromApi = async (apiUrl, opts = {}) => {
67
- try {
68
- const response = await got(apiUrl, opts);
69
- return opts.responseType === 'buffer'
70
- ? { body: response.body, response }
71
- : { ...response.body, response }
72
- } catch (err) {
73
- const { response = {} } = err;
74
- const {
75
- statusCode,
76
- body: rawBody,
77
- headers = {},
78
- url: uri = apiUrl
79
- } = response;
80
- const isBodyBuffer = isBuffer(rawBody);
81
-
82
- const body =
83
- isObject(rawBody) && !isBodyBuffer
84
- ? rawBody
85
- : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri);
86
-
87
- throw new MicrolinkError({
88
- ...body,
89
- message: body.message,
90
- url: uri,
91
- statusCode,
92
- headers
93
- })
94
- }
95
- };
96
-
97
- const getApiUrl = (
98
- url,
99
- { data, apiKey, endpoint, retry, cache, ...opts } = {},
100
- { responseType = 'json', headers: gotHeaders, ...gotOpts } = {}
101
- ) => {
102
- const isPro = !!apiKey;
103
- const apiEndpoint = endpoint || ENDPOINT[isPro ? 'PRO' : 'FREE'];
104
-
105
- const apiUrl = `${apiEndpoint}?${new URLSearchParams({
106
- url,
107
- ...mapRules(data),
108
- ...flatten(opts)
109
- }).toString()}`;
110
-
111
- const headers = isPro
112
- ? { ...gotHeaders, 'x-api-key': apiKey }
113
- : { ...gotHeaders };
114
- return [apiUrl, { ...gotOpts, responseType, cache, retry, headers }]
115
- };
116
-
117
- const createMql = defaultOpts => async (url, opts, gotOpts) => {
118
- assertUrl(url);
119
- const [apiUrl, fetchOpts] = getApiUrl(url, opts, {
120
- ...defaultOpts,
121
- ...gotOpts
122
- });
123
- return fetchFromApi(apiUrl, fetchOpts)
124
- };
125
-
126
- const mql = createMql();
127
- mql.MicrolinkError = MicrolinkError;
128
- mql.getApiUrl = getApiUrl;
129
- mql.fetchFromApi = fetchFromApi;
130
- mql.mapRules = mapRules;
131
- mql.version = VERSION;
132
- mql.stream = got.stream;
133
- mql.buffer = createMql({ responseType: 'buffer' });
134
-
135
- return mql
136
- };
137
-
138
- var factory_1 = factory;
139
-
140
- node$1.exports = factory_1({
141
- MicrolinkError: require$$1('MicrolinkError'),
142
- urlHttp: require$$2,
143
- got: require$$3.extend({ headers: { 'user-agent': undefined } }),
144
- flatten: require$$4.flattie,
145
- VERSION: '0.11.0-4'
146
- });
147
-
148
- var render = node$1.exports.render = (input, { width = '650px' } = {}) => {
149
- if (input && input.url && input.type) {
150
- return `<img width="${width}" src="${input.url}" />`
151
- }
152
- return input
153
- };
154
-
155
- var nodeExports = node$1.exports;
156
- var node = /*@__PURE__*/getDefaultExportFromCjs(nodeExports);
157
-
158
- export { node as default, render };
@@ -1,55 +0,0 @@
1
- 'use strict'
2
-
3
- const urlHttp = require('url-http/lightweight')
4
- const { flattie: flatten } = require('flattie')
5
-
6
- const factory = require('./factory')
7
- const { default: ky } = require('ky')
8
-
9
- class MicrolinkError extends Error {
10
- constructor (props) {
11
- super()
12
- this.name = 'MicrolinkError'
13
- Object.assign(this, props)
14
- this.description = this.message
15
- this.message = this.code
16
- ? `${this.code}, ${this.description}`
17
- : this.description
18
- }
19
- }
20
-
21
- const got = async (url, opts) => {
22
- try {
23
- if (opts.retry > 0) opts.retry = opts.retry + 1
24
- if (opts.timeout === undefined) opts.timeout = false
25
- const response = await ky(url, opts)
26
- const body = await response.json()
27
- const { headers, status: statusCode } = response
28
- return { url: response.url, body, headers, statusCode }
29
- } catch (err) {
30
- if (err.response) {
31
- const { response } = err
32
- err.response = {
33
- ...response,
34
- headers: Array.from(response.headers.entries()).reduce(
35
- (acc, [key, value]) => {
36
- acc[key] = value
37
- return acc
38
- },
39
- {}
40
- ),
41
- statusCode: response.status,
42
- body: await response.text()
43
- }
44
- }
45
- throw err
46
- }
47
- }
48
-
49
- module.exports = factory({
50
- MicrolinkError,
51
- urlHttp,
52
- got,
53
- flatten,
54
- VERSION: require('../package.json').version
55
- })
File without changes