Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fetch method #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ Link Tool supports these configuration parameters:

| Field | Type | Description |
| ---------|-------------|------------------------------------------------|
| endpoint | `string` | **Required:** endpoint for link data fetching. |
| endpoint | `string` | Endpoint for link data fetching. |
| fetcher | `{{fetchLinkDataForUrl: function}}` | Optional custom fetch function. See details below. |

Note that if you don't implement your custom fetch method, the `endpoint` parameter is required.

## Output data

Expand Down Expand Up @@ -131,3 +134,6 @@ Currently title, image and description fields are supported by plugin's design .
}
```
Also, can contain any additional fields you want to store.

## Providing custom fetch method
You have the option to provide your own custom fetch method. Use the `fetcher` config parameter with an object with a `fetchLinkDataForUrl` method that takes the url as a parameter and returns a Promise that resolves with response in the format described above.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export default class LinkTool {
* Tool's initial config
*/
this.config = {
endpoint: config.endpoint || ''
endpoint: config.endpoint || '',
fetcher: config.fetcher || undefined
};

this.nodes = {
Expand Down Expand Up @@ -347,13 +348,18 @@ export default class LinkTool {
this.data = { link: url };

try {
const response = await (ajax.get({
url: this.config.endpoint,
data: {
url
}
}));

let response;

if (this.config.fetcher && typeof this.config.fetcher.fetchLinkDataForUrl === 'function') {
response = await (this.config.fetcher.fetchLinkDataForUrl(url));
} else {
response = await (ajax.get({
url: this.config.endpoint,
data: {
url
}
}));
}
this.onFetch(response);
} catch (error) {
this.fetchingFailed('Haven\'t received data from server');
Expand Down