Skip to content

Commit

Permalink
Typescript Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Franco committed Jan 7, 2020
1 parent f9eab1a commit a9a567f
Show file tree
Hide file tree
Showing 17 changed files with 1,063 additions and 571 deletions.
52 changes: 15 additions & 37 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"env": {
"browser": true,
"es6": true,
"node": true,
"jest": true
},
"globals": {
Expand All @@ -11,57 +12,34 @@
"__API_URL__": true,
"__ROOT_URL__": true,
"__VERSION_DATE___": true,
"__MIXPANEL_TOKEN__":true,
"__MIXPANEL_TOKEN__": true,
"module": true
},
"extends": "airbnb",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"import/no-extraneous-dependencies": 0,
"import/no-unresolved": 0,
"import/extensions": 0,
"import/no-named-as-default": 0,
"import/first": 0,
"no-else-return": 0,
"prefer-template": 0,
"arrow-body-style": 0,
"no-underscore-dangle": 0,
"no-plusplus": 0,
"no-unused-vars": [1, {
"vars": "local",
"args": "none"
}],
"indent": [
2,
2
],
"react/display-name": [0],
"no-console": [0],
"linebreak-style": [
2,
"unix"
],
"quotes": [
2,
"single"
],
"semi": [
2,
"always"
"no-unused-vars": [
1,
{
"vars": "local",
"args": "none"
}
],
"max-len": 0,
"indent": [2, 2],
"linebreak-style": [2, "unix"],
"quotes": [2, "single"],
"semi": [2, "always"],
"class-methods-use-this": 0,
"react/jsx-no-bind": [0],
"react/forbid-prop-types": [0],
"react/no-string-refs": [0],
"no-shadow": [0],
"comma-dangle": ["error", {"functions": "never"}],
"arrow-parens": 0,
"react/prefer-stateless-function": [0],
"no-param-reassign": 0
"space-before-function-paren": 0,
"func-names": 0,
"prefer-arrow-callback": 0
}
}
62 changes: 33 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# link-preview-js

[![CircleCI](https://circleci.com/gh/ospfranco/link-preview-js/tree/master.svg?style=svg)](https://circleci.com/gh/ospfranco/link-preview-js/tree/master) [![npm version](https://badge.fury.io/js/link-preview-js.svg)](https://badge.fury.io/js/link-preview-js)
Expand All @@ -7,63 +6,64 @@ Pure js library that allows you to extract information from a URL or parse text

# WARNING: THIS LIBRARY DOES NOT WORK ON CORS PROTECTED ENVIRONEMNTS, RUNNING IT ON BROWSERS WON'T WORK

Chrome, Firefox, Safari, etc DO NOT ALLOW YOU TO DO CROSS SITE REQUESTS therefore you cannot use this library or even manually request another domain from your web application, read more about [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
Chrome, Firefox, Safari, etc DO NOT ALLOW YOU TO DO CROSS SITE REQUESTS therefore you cannot use this library or even manually request another domain from your web application, read more about [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). However you can use this library on React-Native or on your back-end to fetch link-information to your app.

## Known issues for React-Native

Apparently the fetch especification breaks on some older samsung devices, this is not patchable on this library.

# 2.X.X

The api for version 2.X.X changed slightly, there is no longer a default unnamed export, only a named method export `getLinkPreview`, the library has also been completely re-written on typescript so you now have types and some minor bugs have been fixed.

## Usage
Install the library by

`$ yarn add link-preview-js`

Library exposes just one method: getPreview, you have to pass a string (doesn't matter if it is just a URL or a piece of text that contains a URL), the library will take care of parsing it and returning the info of first valid URL info it finds.
Library exposes just one method `getLinkPreview`, you have to pass a string, doesn't matter if it is just a URL or a piece of text that contains a URL, the library will take care of parsing it and returning the info of first valid HTTP(S) URL info it finds.

URL parsing is done via: https://gist.github.com/dperini/729294

```javascript
import LinkPreview from 'link-preview-js';
```typescript
import {getLinkPreview} from 'link-preview-js';

...

LinkPreview.getPreview('https://www.youtube.com/watch?v=MejbOFk7H6c')
.then(data => console.debug(data));
getLinkPreview('https://www.youtube.com/watch?v=MejbOFk7H6c')
.then((data: ILinkPreviewResponse) => console.debug(data));

LinkPreview.getPreview('This is a text supposed to be parsed and the first link displayed https://www.youtube.com/watch?v=MejbOFk7H6c')
.then(data => console.debug(data));
getLinkPreview('This is a text supposed to be parsed and the first link displayed https://www.youtube.com/watch?v=MejbOFk7H6c')
.then((data: ILinkPreviewResponse) => console.debug(data));
```

## Options
Additionally you can pass an options object which should add more functionality to the parsing of the link

| Property Name | Result |
| ------------- |:-------------:|
| imagesPropertyType (**optional**) (ex: 'og') | Fetches images only with the specified property, `meta[property='${imagesPropertyType}:image']` |
| language (**optional**) (ex: 'de', 'en-US') | Fetch content with specific language |
| headers (**optional**) (ex: { 'user-agent': 'googlebot' }) | Add request headers to fetch call |
Additionally you can pass an options object which should add more functionality to the parsing of the link

| Property Name | Result |
| -------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------: |
| imagesPropertyType (**optional**) (ex: 'og') | Fetches images only with the specified property, `meta[property='${imagesPropertyType}:image']` |
| headers (**optional**) (ex: { 'user-agent': 'googlebot', 'Accept-Language': 'en-US' }) | Add request headers to fetch call |

```javascript
LinkPreview.getPreview(
'https://www.youtube.com/watch?v=MejbOFk7H6c',
{
imagesPropertyType: 'og', // fetches only open-graph images
language: 'fr-CA', // fetches site for French language
headers: {
'user-agent': 'googlebot', // fetches with googlebot crawler user agent
// ...other optional request headers
},
})
.then(data => console.debug(data));
getLinkPreview("https://www.youtube.com/watch?v=MejbOFk7H6c", {
imagesPropertyType: "og", // fetches only open-graph images
headers: {
"user-agent": "googlebot" // fetches with googlebot crawler user agent
"Accept-Language": "fr-CA", // fetches site for French language
// ...other optional HTTP request headers
}
}).then(data => console.debug(data));
```


## Returns

Returns a Promise that resolves with an object describing the provided link.
The info object returned varies depending on the content type (MIME type) returned
in the HTTP response (see below for variations of response). Rejects with an error if response can not be parsed or if there was no URL in the text provided.
in the HTTP response (see below for variations of response). Rejects with an error if response can not be parsed or if there was no URL in the text provided.

### Text/HTML URL

```
{
url: "https://www.youtube.com/watch?v=MejbOFk7H6c",
Expand All @@ -79,6 +79,7 @@ in the HTTP response (see below for variations of response). Rejects with an er
```

### Image URL

```
{
url: "https://media.npr.org/assets/img/2018/04/27/gettyimages-656523922nunes-4bb9a194ab2986834622983bb2f8fe57728a9e5f-s1100-c15.jpg",
Expand All @@ -89,6 +90,7 @@ in the HTTP response (see below for variations of response). Rejects with an er
```

### Audio URL

```
{
url: "https://ondemand.npr.org/anon.npr-mp3/npr/atc/2007/12/20071231_atc_13.mp3",
Expand All @@ -99,6 +101,7 @@ in the HTTP response (see below for variations of response). Rejects with an er
```

### Video URL

```
{
url: "https://www.w3schools.com/html/mov_bbb.mp4",
Expand All @@ -109,6 +112,7 @@ in the HTTP response (see below for variations of response). Rejects with an er
```

### Application URL

```
{
url: "https://assets.curtmfg.com/masterlibrary/56282/installsheet/CME_56282_INS.pdf",
Expand Down
7 changes: 7 additions & 0 deletions __tests__/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`link preview hould handle empty strings gracefully 1`] = `"link-preview-js did not receive either a url or text"`;

exports[`link preview no link in text should fail gracefully 1`] = `"link-preview-js did not receive either a url or text"`;

exports[`link preview should handle malformed urls gracefully 1`] = `"link-preview-js did not receive either a url or text"`;
158 changes: 0 additions & 158 deletions __tests__/index.spec.js

This file was deleted.

Loading

0 comments on commit a9a567f

Please sign in to comment.