oembed-parser
Advanced tools
Comparing version 1.3.5 to 1.3.6
@@ -1,18 +0,91 @@ | ||
// Type definitions for oembed-parser 1.2 | ||
// Type definitions for oembed-parser 1.3.5 | ||
// Project: https://www.npmjs.com/package/oembed-parser | ||
// Definitions by: BendingBender <https://github.com/BendingBender> | ||
// CodeBast4rd <https://github.com/CodeBast4rd> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
export function extract(url: string, params: {maxwidth?: number, maxheight?: number}): Promise<OembedData>; | ||
export function extract(url: string): Promise<OembedData>; | ||
export function hasProvider(url: string): boolean; | ||
/** | ||
* Basic data structure of every oembed response see https://oembed.com/ | ||
*/ | ||
export interface OembedData { | ||
type: string; | ||
type: 'rich' | 'video' | 'photo' | 'link'; | ||
version: string; | ||
/** A text title, describing the resource. */ | ||
title?: string; | ||
/** The name of the author/owner of the resource. */ | ||
author_name?: string; | ||
/** A URL for the author/owner of the resource. */ | ||
author_url?: string; | ||
/** The name of the resource provider. */ | ||
provider_name?: string; | ||
/** The url of the resource provider. */ | ||
provider_url?: string; | ||
/** The suggested cache lifetime for this resource, in seconds. Consumers may choose to use this value or not. */ | ||
cache_age?: string; | ||
/** | ||
* A URL to a thumbnail image representing the resource. | ||
* The thumbnail must respect any maxwidth and maxheight parameters. | ||
* If this parameter is present, thumbnail_width and thumbnail_height must also be present. | ||
*/ | ||
thumbnail_url?: string; | ||
/** | ||
* The width of the optional thumbnail. | ||
* If this parameter is present, thumbnail_url and thumbnail_height must also be present. | ||
*/ | ||
thumbnail_width?: number; | ||
/** | ||
* The height of the optional thumbnail. | ||
* If this parameter is present, thumbnail_url and thumbnail_width must also be present. | ||
*/ | ||
thumbnail_height?: number; | ||
} | ||
export interface LinkTypeData extends OembedData { | ||
readonly type: 'link'; | ||
} | ||
export interface PhotoTypeData extends OembedData { | ||
readonly type: 'photo'; | ||
/** | ||
* The source URL of the image. Consumers should be able to insert this URL into an <img> element. | ||
* Only HTTP and HTTPS URLs are valid. | ||
*/ | ||
url: string; | ||
/** The width in pixels of the image specified in the url parameter. */ | ||
width: number; | ||
/** The height in pixels of the image specified in the url parameter. */ | ||
height: number; | ||
} | ||
export interface VideoTypeData extends OembedData { | ||
readonly type: 'video'; | ||
/** | ||
* The HTML required to embed a video player. | ||
* The HTML should have no padding or margins. | ||
* Consumers may wish to load the HTML in an off-domain iframe to avoid XSS vulnerabilities. | ||
*/ | ||
html: string; | ||
provider_url: string; | ||
provider_name: string; | ||
/** The width in pixels required to display the HTML. */ | ||
width: number; | ||
/** The height in pixels required to display the HTML. */ | ||
height: number; | ||
} | ||
export interface RichTypeData extends OembedData { | ||
readonly type: 'rich'; | ||
/** | ||
* The HTML required to display the resource. | ||
* The HTML should have no padding or margins. | ||
* Consumers may wish to load the HTML in an off-domain iframe to avoid XSS vulnerabilities. | ||
* The markup should be valid XHTML 1.0 Basic. | ||
*/ | ||
html: string; | ||
/** The width in pixels required to display the HTML. */ | ||
width: number; | ||
/** The height in pixels required to display the HTML. */ | ||
height: number; | ||
} |
{ | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"name": "oembed-parser", | ||
@@ -29,3 +29,3 @@ "description": "Get oEmbed data from given URL.", | ||
"eslint-config-goes": "^1.1.8", | ||
"tap": "^14.8.2" | ||
"tap": "^14.9.1" | ||
}, | ||
@@ -32,0 +32,0 @@ "keywords": [ |
@@ -11,3 +11,3 @@ # oembed-parser | ||
```bash | ||
npm i oembed-parser | ||
npm install oembed-parser | ||
``` | ||
@@ -24,29 +24,37 @@ | ||
// Promise style | ||
extract(url).then((data) => { | ||
console.log(data); | ||
extract(url).then((oembed) => { | ||
console.log(oembed); | ||
}).catch((err) => { | ||
console.log(err); | ||
console.trace(err); | ||
}); | ||
``` | ||
### APIs | ||
// async/await style | ||
const getArticle = async (link) => { | ||
#### .extract(String url [, Object params]) | ||
Extract oEmbed data from specified url. | ||
Return: a Promise | ||
Optional argument `params` is an object with it we can set `maxwidth` and/or `maxheight` those are used to scale embed size to fit your container size. Please refer [oEmbed/Full Spec/Consumer Request](https://oembed.com/#section2) for more info. | ||
Here is how we can use `oembed-parser` in async/await style: | ||
```js | ||
import { | ||
extract | ||
} from 'oembed-parser'; | ||
const getOembed = async (url) => { | ||
try { | ||
let data = await extract(link); | ||
return data; | ||
const oembed = await extract(url); | ||
return oembed; | ||
} catch (err) { | ||
return err; | ||
console.trace(err); | ||
} | ||
} | ||
}; | ||
console.log(getArticle(url)); | ||
``` | ||
### APIs | ||
#### .extract(String URL) | ||
Return a Promise object. | ||
#### .hasProvider(String URL) | ||
@@ -53,0 +61,0 @@ |
@@ -13,3 +13,3 @@ // main | ||
const extract = async (url, params) => { | ||
const extract = async (url, params = {}) => { | ||
if (!isValidURL(url)) { | ||
@@ -16,0 +16,0 @@ throw new Error('Invalid input URL'); |
@@ -5,4 +5,4 @@ // utils -> fetchEmbed | ||
const fetchEmbed = async (url, provider, params) => { | ||
let { | ||
const fetchEmbed = async (url, provider, params = {}) => { | ||
const { | ||
provider_name, // eslint-disable-line camelcase | ||
@@ -13,9 +13,22 @@ provider_url, // eslint-disable-line camelcase | ||
const baseUrl = resourceUrl.replace(/\{format\}/g, 'json'); | ||
resourceUrl = resourceUrl.replace(/\{format\}/g, 'json'); | ||
const queries = [ | ||
'format=json', | ||
`url=${encodeURIComponent(url)}`, | ||
]; | ||
let link = `${resourceUrl}?format=json&url=${encodeURIComponent(url)}`; | ||
link = params && params.maxwidth ? `${link}&maxwidth=${params.maxwidth}` : link; | ||
link = params && params.maxheight ? `${link}&maxheight=${params.maxheight}` : link; | ||
const { | ||
maxwidth = 0, | ||
maxheight = 0, | ||
} = params; | ||
if (maxwidth > 0) { | ||
queries.push(`maxwidth=${maxwidth}`); | ||
} | ||
if (maxheight > 0) { | ||
queries.push(`maxheight=${maxheight}`); | ||
} | ||
const link = `${baseUrl}?${queries.join('&')}`; | ||
const res = await fetch(link, {mode: 'no-cors'}); | ||
@@ -22,0 +35,0 @@ const json = await res.json(); |
Sorry, the diff of this file is too big to display
168053
6896
90