Skip to content

Commit

Permalink
Merge 4deebb0 into 44fba5f
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaidong committed Sep 19, 2022
2 parents 44fba5f + 4deebb0 commit 92689e1
Show file tree
Hide file tree
Showing 28 changed files with 11,171 additions and 25,450 deletions.
52 changes: 5 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ Extract oEmbed content from given URL.
import { extract } from 'oembed-parser'

// with CommonJS environments

// const { extract } = require('oembed-parser')

// or specify exactly path to cjs variant
// const { extract } = require('oembed-parser/dist/cjs/oembed-parser.js')

const url = 'https://www.youtube.com/watch?v=8jPQjjsBbIc'
const url = 'https://www.youtube.com/watch?v=x2bqscVkGxk'

extract(url).then((oembed) => {
console.log(oembed)
Expand Down Expand Up @@ -85,40 +89,6 @@ Here are several popular params:
Note that some params are supported by these providers but not by the others.
Please see the provider's oEmbed API docs carefully for exact information.

### `.hasProvider(String URL)`

Check if a URL matches with any provider in the list.

Examples:

```js
import { hasProvider } from 'oembed-parser'

hasProvider('https://www.youtube.com/watch?v=ciS8aCrX-9s') // return true
hasProvider('https://trello.com/b/BO3bg7yn/notes') // return false
```

### `.findProvider(String URL)`

Get the provider which is relevant to given URL.

For example:

```js
import { findProvider } from 'oembed-parser'

findProvider('https://www.facebook.com/video.php?v=999999999')
```

Result looks like below:

```json
{
fetchEndpoint: 'https://graph.facebook.com/v10.0/oembed_video',
providerName: 'Facebook',
providerUrl: 'https://www.facebook.com/'
}
```

### `.setProviderList(Array providers)`

Expand Down Expand Up @@ -154,18 +124,6 @@ const providers = [
setProviderList(providers)
```

### `.setRequestOptions(Object requestOptions)`

Define options to call oembed HTTP request.

`oembed-parser` is using [axios](https://github.com/axios/axios) to send HTTP requests. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info.

### `.getRequestOptions()`

Return current request options.

Default values can be found [here](https://github.com/ndaidong/oembed-parser/blob/main/src/config.js#L5).

## Facebook and Instagram

In order to work with the links from Facebook and Instagram, you need a [reviewed Facebook's app](https://developers.facebook.com/docs/app-review) with [oEmbed Read](https://developers.facebook.com/docs/features-reference/oembed-read) permission.
Expand Down
36 changes: 26 additions & 10 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
* @ndaidong
**/

import { readFileSync, writeFileSync, copyFileSync } from 'fs'
import { execSync } from 'child_process'
import { readFileSync, writeFileSync, copyFileSync, rmSync, mkdirSync } from 'fs'

import { buildSync } from 'esbuild'

const pkg = JSON.parse(readFileSync('./package.json'))
const pkg = JSON.parse(readFileSync('./package.json', { encoding: 'utf-8' }))

execSync('rm -rf dist')
execSync('mkdir dist')
rmSync('dist', {
force: true,
recursive: true
})
mkdirSync('dist')

const buildTime = (new Date()).toISOString()
const comment = [
Expand All @@ -25,10 +27,25 @@ const baseOpt = {
bundle: true,
charset: 'utf8',
target: ['es2020', 'node14'],
pure: ['console.log', 'debug', 'alert'],
legalComments: 'none',
minify: false,
sourcemap: false,
write: true
}

const esmVersion = {
...baseOpt,
platform: 'browser',
format: 'esm',
mainFields: ['module'],
outfile: `dist/${pkg.name}.esm.js`,
banner: {
js: comment
}
}
buildSync(esmVersion)

const cjsVersion = {
...baseOpt,
platform: 'node',
Expand All @@ -42,17 +59,16 @@ const cjsVersion = {
buildSync(cjsVersion)

const cjspkg = {
name: pkg.name + '-cjs',
name: pkg.name,
version: pkg.version,
main: `./${pkg.name}.js`
}

writeFileSync(
'dist/cjs/package.json',
JSON.stringify(cjspkg, null, ' '),
'utf8'
)

copyFileSync(
'src/utils/providers.latest.js',
'dist/cjs/providers.latest.js'
)
// copy types definition to cjs dir
copyFileSync('./index.d.ts', 'dist/cjs/index.d.ts')
18 changes: 16 additions & 2 deletions build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,32 @@ import {

const pkg = JSON.parse(readFileSync('./package.json'))

const esmFile = `./dist/${pkg.name}.esm.js`
const cjsFile = `./dist/cjs/${pkg.name}.js`
const cjsPkg = JSON.parse(readFileSync('./dist/cjs/package.json'))

describe('Validate commonjs version output', () => {
test(`Check if ${cjsFile} created`, () => {
expect(existsSync(cjsFile)).toBeTruthy()
})
const constent = readFileSync(cjsFile, 'utf8')
const lines = constent.split('\n')
test('Check if file meta contains package info', () => {
expect(lines[0].includes(`${pkg.name}@${pkg.version}`)).toBeTruthy()
expect(lines[0].includes(pkg.author)).toBeTruthy()
expect(lines[0].includes(pkg.license)).toBeTruthy()
})
test('Check if cjs package info updated', () => {
expect(cjsPkg.name).toEqual(`${pkg.name}-cjs`)
expect(cjsPkg.name).toEqual(pkg.name)
expect(cjsPkg.version).toEqual(pkg.version)
})
const constent = readFileSync(cjsFile, 'utf8')
})

describe('Validate ESM version output', () => {
test(`Check if ${esmFile} created`, () => {
expect(existsSync(esmFile)).toBeTruthy()
})
const constent = readFileSync(esmFile, 'utf8')
const lines = constent.split('\n')
test('Check if file meta contains package info', () => {
expect(lines[0].includes(`${pkg.name}@${pkg.version}`)).toBeTruthy()
Expand Down
2 changes: 1 addition & 1 deletion cjs-eval.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// cjs-eval.js
// to quickly test with a single url or file

const { extract } = require('./dist/cjs/oembed-parser')
const { extract } = require('./dist/oembed-parser')

const run = async (url) => {
try {
Expand Down
115 changes: 115 additions & 0 deletions dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Type definitions for oembed-parser
// Project: https://www.npmjs.com/package/oembed-parser
// Definitions by: BendingBender <https://github.com/BendingBender>
// CodeBast4rd <https://github.com/CodeBast4rd>
// Marc McIntosh <https://github.com/MarcMcIntosh>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

export function extract(url: string, params?: any): Promise<OembedData>;

export function hasProvider(url: string): boolean

export function findProvider(url: string): FindProviderResult

export function setProviderList(providers: Provider[]): void

export interface Endpoint {
schemes?: string[];
url: string;
formats?: string[]; // "json" "xml"
discovery?: boolean;
}

export interface Provider {
"provider_name": string;
"provider_url": string;
"endpoints": Endpoint[];
}

export interface FindProviderResult {
"fetchEndpoint": string;
"provider_name": string;
"provider_url": string;
}

/**
* Basic data structure of every oembed response see https://oembed.com/
*/
export interface OembedData {
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 | number;
/**
* 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;
/** 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;
}
Loading

0 comments on commit 92689e1

Please sign in to comment.