You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,6 +95,7 @@ Additionally, you can pass an options object which should add more functionality
95
95
| followRedirects (**optional**) (default 'error') | For security reasons, the library does not automatically follow redirects ('error' value), a malicious agent can exploit redirects to steal data, posible values: ('error', 'follow', 'manual') |
96
96
| handleRedirects (**optional**) (with followRedirects 'manual') | When followRedirects is set to 'manual' you need to pass a function that validates if the redirectinon is secure, below you can find an example |
97
97
| resolveDNSHost (**optional**) | Function that resolves the final address of the detected/parsed URL to prevent SSRF attacks |
98
+
| onResponse (**optional**) | Function that handles the response object to allow for managing special cases |
There may be situations where you need to provide your own logic for population properties in the response object. For example, if the library is unable to detect a description because the website does not provide OpenGraph data, you might want to use the text value of the first paragraph instead. This callback gives you access to the Cheerio doc instance, as well as the URL object so you could handle cases on a site-by-site basis, if you need to. This callback must return the modified response object
165
+
166
+
```javascript
167
+
awaitgetLinkPreview(`https://example.com/`, {
168
+
onResponse: (response, doc, URL) => {
169
+
if (URL.hostname=='example.com') {
170
+
response.siteName='Example Website';
171
+
}
172
+
173
+
if (!response.description) {
174
+
response.description=doc('p').first().text();
175
+
}
176
+
177
+
return response;
178
+
},
179
+
});
180
+
```
181
+
162
182
## Response
163
183
164
184
Returns a Promise that resolves with an object describing the provided link.
0 commit comments