restql 1.1.7 → 1.1.8
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/README.md +24 -69
- package/dist/cjs/index.js +8 -12
- package/dist/esm/index.js +7 -11
- package/dist/umd/index.js +232 -11952
- package/dist/umd/index.min.js +1 -7
- package/package.json +4 -6
package/README.md
CHANGED
|
@@ -25,33 +25,27 @@ npm install restql
|
|
|
25
25
|
<script src="https://unpkg.com/restql/dist/umd/index.min.js"></script>
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
##
|
|
29
|
-
|
|
30
|
-
### `resource`
|
|
28
|
+
## Usage
|
|
31
29
|
|
|
32
|
-
`
|
|
30
|
+
### `restql(resource, resolver[, options])`
|
|
33
31
|
|
|
34
|
-
|
|
32
|
+
#### Parameters
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
- `resource` (`string`): The resource to fetch.
|
|
35
|
+
- Self-explanatory.
|
|
37
36
|
|
|
38
|
-
|
|
37
|
+
##### Example
|
|
39
38
|
|
|
40
39
|
```js
|
|
41
40
|
'https://pokeapi.co/api/v2/pokemon/1/'
|
|
42
41
|
```
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
**Description**
|
|
43
|
+
- `resolver` (`Object`): The resolver to apply.
|
|
44
|
+
- At every level, each property describes a path to the nested resources within the current one.
|
|
45
|
+
- RestQL resolves the sames and call the subsequent resolver against them.
|
|
46
|
+
- Until the base case (`null`) is reached; from which it returns back the merged responses.
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
RestQL resolves the sames and call the subsequent resolver against them.
|
|
52
|
-
Until the base case (`null`) is reached; from which it returns back the merged responses.
|
|
53
|
-
|
|
54
|
-
**Quantifiers**
|
|
48
|
+
##### Quantifiers
|
|
55
49
|
|
|
56
50
|
Following is a table of the quantifiers you can use:
|
|
57
51
|
|
|
@@ -60,7 +54,7 @@ Following is a table of the quantifiers you can use:
|
|
|
60
54
|
| `[]` | Collection of properties. |
|
|
61
55
|
| `?` | Optional property. |
|
|
62
56
|
|
|
63
|
-
|
|
57
|
+
##### Example
|
|
64
58
|
|
|
65
59
|
```js
|
|
66
60
|
{
|
|
@@ -77,66 +71,27 @@ Following is a table of the quantifiers you can use:
|
|
|
77
71
|
}
|
|
78
72
|
```
|
|
79
73
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
`{Object} The options to bypass.`
|
|
83
|
-
|
|
84
|
-
**Description**
|
|
85
|
-
|
|
86
|
-
[`Request Config`](https://github.com/axios/axios/tree/v1.13.6#request-config)
|
|
74
|
+
- `[options]` (`Object`): The options to bypass.
|
|
75
|
+
- [See `RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
|
|
87
76
|
|
|
88
|
-
|
|
77
|
+
##### Example
|
|
89
78
|
|
|
90
79
|
```js
|
|
91
|
-
{
|
|
92
|
-
// ...
|
|
93
|
-
}
|
|
80
|
+
{ ... }
|
|
94
81
|
```
|
|
95
82
|
|
|
96
|
-
|
|
83
|
+
#### Returns
|
|
97
84
|
|
|
98
|
-
|
|
99
|
-
import restql from 'restql'
|
|
85
|
+
(`Promise<Object>`): A promise which resolves into an object.
|
|
100
86
|
|
|
101
|
-
|
|
102
|
-
* @constant {string} resource The resource to fetch.
|
|
103
|
-
*/
|
|
104
|
-
const resource = 'https://pokeapi.co/api/v2/pokemon/1/'
|
|
87
|
+
## Try It
|
|
105
88
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
*/
|
|
109
|
-
const resolver = {
|
|
110
|
-
'abilities[]?.ability.url': {
|
|
111
|
-
'generation.url': {
|
|
112
|
-
'main_region.url': null,
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
'stats[].stat.url?': {
|
|
116
|
-
'affecting_natures.increase[].url': null,
|
|
117
|
-
'affecting_natures.decrease[].url': null,
|
|
118
|
-
},
|
|
119
|
-
'moves[].move?.url': null,
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @constant {Object} options The options to bypass.
|
|
124
|
-
*/
|
|
125
|
-
const options = {
|
|
126
|
-
// ...
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
;(async () => {
|
|
130
|
-
try {
|
|
131
|
-
const data = await restql(resource, resolver, options)
|
|
132
|
-
|
|
133
|
-
console.log(data)
|
|
134
|
-
} catch (error) {
|
|
135
|
-
console.error(error.message)
|
|
136
|
-
}
|
|
137
|
-
})()
|
|
89
|
+
```sh
|
|
90
|
+
npm run playground
|
|
138
91
|
```
|
|
139
92
|
|
|
93
|
+
[See Playground](https://github.com/relztic/restql/blob/main/playground/index.js)
|
|
94
|
+
|
|
140
95
|
## Roadmap
|
|
141
96
|
|
|
142
97
|
- ~~Support for authentication~~
|
|
@@ -145,4 +100,4 @@ const options = {
|
|
|
145
100
|
- ~~Ability to cache responses~~
|
|
146
101
|
- Support for recursive resolvers
|
|
147
102
|
|
|
148
|
-
Take 🎂, Folks! 🌮 🐴 💨
|
|
103
|
+
> Take 🎂, Folks! 🌮 🐴 💨
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var merge = require('lodash/merge');
|
|
5
|
-
var axios = require('axios');
|
|
6
|
-
var md5 = require('md5');
|
|
7
|
-
var isURL = require('validator/lib/isURL');
|
|
3
|
+
var esToolkit = require('es-toolkit');
|
|
8
4
|
|
|
9
5
|
/**
|
|
10
6
|
* @constant {Object} responses The responses to cache.
|
|
@@ -19,9 +15,9 @@ const responses = {};
|
|
|
19
15
|
* @returns {Promise<Object>} A promise which resolves into an object.
|
|
20
16
|
*/
|
|
21
17
|
async function fetchResource(resource, options) {
|
|
22
|
-
const key =
|
|
18
|
+
const key = `${resource}-${JSON.stringify(options)}`;
|
|
23
19
|
if (!(key in responses)) {
|
|
24
|
-
responses[key] =
|
|
20
|
+
responses[key] = fetch(resource, options).then(response => {
|
|
25
21
|
responses[key] = response;
|
|
26
22
|
return response;
|
|
27
23
|
});
|
|
@@ -36,7 +32,7 @@ async function fetchResource(resource, options) {
|
|
|
36
32
|
* @returns {boolean} Whether or not a resource is valid.
|
|
37
33
|
*/
|
|
38
34
|
function isResource(resource) {
|
|
39
|
-
return
|
|
35
|
+
return Boolean(URL.parse(resource));
|
|
40
36
|
}
|
|
41
37
|
|
|
42
38
|
/**
|
|
@@ -118,11 +114,11 @@ async function resolve(resource, resolver, options) {
|
|
|
118
114
|
if (!isResource(resource)) {
|
|
119
115
|
throw new Error(`InvalidArgumentError: invalid resource \`${resource}\``);
|
|
120
116
|
}
|
|
121
|
-
const response =
|
|
122
|
-
if (!response.
|
|
117
|
+
const response = (await fetchResource(resource, options)).clone();
|
|
118
|
+
if (!response.ok) {
|
|
123
119
|
throw new Error(`RuntimeError: could not fetch resource \`${resource}\``);
|
|
124
120
|
}
|
|
125
|
-
const obj = response.
|
|
121
|
+
const obj = await response.json();
|
|
126
122
|
if (!resolver) {
|
|
127
123
|
return obj;
|
|
128
124
|
}
|
|
@@ -138,7 +134,7 @@ async function resolve(resource, resolver, options) {
|
|
|
138
134
|
const data = Array.isArray(resources) ? await Promise.all(resources.map(async nextResource => resolve(nextResource, nextResolver, options))) : await resolve(resources, nextResolver, options);
|
|
139
135
|
return [props, data];
|
|
140
136
|
}));
|
|
141
|
-
return responses.reduce((result, [props, data]) => merge(result, objectSet(data, props)), obj);
|
|
137
|
+
return responses.reduce((result, [props, data]) => esToolkit.merge(result, objectSet(data, props)), obj);
|
|
142
138
|
}
|
|
143
139
|
|
|
144
140
|
/**
|
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import merge from 'lodash/merge';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
import md5 from 'md5';
|
|
5
|
-
import isURL from 'validator/lib/isURL';
|
|
1
|
+
import { merge } from 'es-toolkit';
|
|
6
2
|
|
|
7
3
|
/**
|
|
8
4
|
* @constant {Object} responses The responses to cache.
|
|
@@ -17,9 +13,9 @@ const responses = {};
|
|
|
17
13
|
* @returns {Promise<Object>} A promise which resolves into an object.
|
|
18
14
|
*/
|
|
19
15
|
async function fetchResource(resource, options) {
|
|
20
|
-
const key =
|
|
16
|
+
const key = `${resource}-${JSON.stringify(options)}`;
|
|
21
17
|
if (!(key in responses)) {
|
|
22
|
-
responses[key] =
|
|
18
|
+
responses[key] = fetch(resource, options).then(response => {
|
|
23
19
|
responses[key] = response;
|
|
24
20
|
return response;
|
|
25
21
|
});
|
|
@@ -34,7 +30,7 @@ async function fetchResource(resource, options) {
|
|
|
34
30
|
* @returns {boolean} Whether or not a resource is valid.
|
|
35
31
|
*/
|
|
36
32
|
function isResource(resource) {
|
|
37
|
-
return
|
|
33
|
+
return Boolean(URL.parse(resource));
|
|
38
34
|
}
|
|
39
35
|
|
|
40
36
|
/**
|
|
@@ -116,11 +112,11 @@ async function resolve(resource, resolver, options) {
|
|
|
116
112
|
if (!isResource(resource)) {
|
|
117
113
|
throw new Error(`InvalidArgumentError: invalid resource \`${resource}\``);
|
|
118
114
|
}
|
|
119
|
-
const response =
|
|
120
|
-
if (!response.
|
|
115
|
+
const response = (await fetchResource(resource, options)).clone();
|
|
116
|
+
if (!response.ok) {
|
|
121
117
|
throw new Error(`RuntimeError: could not fetch resource \`${resource}\``);
|
|
122
118
|
}
|
|
123
|
-
const obj = response.
|
|
119
|
+
const obj = await response.json();
|
|
124
120
|
if (!resolver) {
|
|
125
121
|
return obj;
|
|
126
122
|
}
|