@microlink/mql 0.11.6-0 → 0.11.6-1

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.
package/package.json CHANGED
@@ -2,10 +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.6-0",
5
+ "version": "0.11.6-1",
6
6
  "types": "lightweight/index.d.ts",
7
- "main": "src/node.js",
8
- "module": "src/node.mjs",
9
7
  "exports": {
10
8
  "require": "./src/node.js",
11
9
  "default": "./lightweight/index.js"
@@ -88,10 +86,7 @@
88
86
  },
89
87
  "files": [
90
88
  "lightweight",
91
- "src/factory.js",
92
- "src/index.d.ts",
93
- "src/ky.js",
94
- "src/node.js"
89
+ "src"
95
90
  ],
96
91
  "license": "MIT",
97
92
  "ava": {
@@ -124,7 +119,7 @@
124
119
  },
125
120
  "standard": {
126
121
  "ignore": [
127
- "dist",
122
+ "src/node.mjs",
128
123
  "lightweight/index.js"
129
124
  ]
130
125
  },
@@ -139,7 +134,7 @@
139
134
  "scripts": {
140
135
  "build": "rollup -c rollup.config.js --bundleConfigAsCjs",
141
136
  "clean": "rm -rf node_modules",
142
- "clean:build": "rm -rf dist lightweight/index.js",
137
+ "clean:build": "rm -rf lightweight/index.js",
143
138
  "contributors": "(npx git-authors-cli && npx finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
144
139
  "dev": "npm run build -- -w",
145
140
  "lint": "standard && tsd",
@@ -0,0 +1,60 @@
1
+ 'use strict'
2
+
3
+ const urlHttp = require('url-http/lightweight')
4
+ const { flattie: flatten } = require('flattie')
5
+ const factory = require('./factory')
6
+ const { default: ky } = require('ky')
7
+
8
+ class MicrolinkError extends Error {
9
+ constructor (props) {
10
+ super()
11
+ this.name = 'MicrolinkError'
12
+ Object.assign(this, props)
13
+ this.description = this.message
14
+ this.message = this.code
15
+ ? `${this.code}, ${this.description}`
16
+ : this.description
17
+ }
18
+ }
19
+
20
+ const got = async (url, opts) => {
21
+ try {
22
+ if (opts.retry > 0) opts.retry = opts.retry + 1
23
+ if (opts.timeout === undefined) opts.timeout = false
24
+ const response = await ky(url, opts)
25
+ const body = await response.json()
26
+ const { headers, status: statusCode } = response
27
+ return { url: response.url, body, headers, statusCode }
28
+ } catch (err) {
29
+ if (err.response) {
30
+ const { response } = err
31
+ err.response = {
32
+ ...response,
33
+ headers: Array.from(response.headers.entries()).reduce(
34
+ (acc, [key, value]) => {
35
+ acc[key] = value
36
+ return acc
37
+ },
38
+ {}
39
+ ),
40
+ statusCode: response.status,
41
+ body: await response.text()
42
+ }
43
+ }
44
+ throw err
45
+ }
46
+ }
47
+
48
+ const mql = factory({
49
+ MicrolinkError,
50
+ urlHttp,
51
+ got,
52
+ flatten,
53
+ VERSION: '__MQL_VERSION__'
54
+ })
55
+
56
+ module.exports = mql
57
+ module.exports.MicrolinkError = mql.MicrolinkError
58
+ module.exports.getApiUrl = mql.getApiUrl
59
+ module.exports.mapRules = mql.mapRules
60
+ module.exports.version = mql.version
package/src/node.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { MqlPayload, MqlOptions, MicrolinkApiOptions } from '../lightweight'
2
+
3
+ export { MqlError, MqlPayload } from '../lightweight'
4
+
5
+ export type MqlResponse = MqlPayload & {
6
+ response: {
7
+ url: string;
8
+ isFromCache?: boolean;
9
+ statusCode: number;
10
+ headers: { [key: string]: string };
11
+ body: MqlPayload
12
+ };
13
+ }
14
+
15
+ declare function mql(
16
+ url: string,
17
+ opts?: MqlOptions & MicrolinkApiOptions,
18
+ gotOpts?: object
19
+ ): Promise<MqlResponse>;
20
+
21
+ declare namespace mql {
22
+ function stream(
23
+ url: string,
24
+ opts?: MqlOptions & MicrolinkApiOptions,
25
+ gotOpts?: object
26
+ ): NodeJS.ReadableStream;
27
+ }
28
+
29
+ export default mql;
package/src/node.mjs ADDED
@@ -0,0 +1,158 @@
1
+ import require$$0 from 'whoops';
2
+ import require$$1 from 'url-http/lightweight';
3
+ import require$$2 from 'got';
4
+ import * as require$$3 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$$0('MicrolinkError'),
142
+ urlHttp: require$$1,
143
+ got: require$$2.extend({ headers: { 'user-agent': undefined } }),
144
+ flatten: require$$3.flattie,
145
+ VERSION: '0.11.6-0'
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 };