Skip to content

Commit 6fa01d1

Browse files
committed
adding types to project and adding better support for both cjs and esm
1 parent 70ca2bb commit 6fa01d1

12 files changed

Lines changed: 493 additions & 12 deletions

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
"description": "Javascript scraper module for Open Graph and Twitter Card info",
44
"version": "2.0.0",
55
"license": "MIT",
6-
"main": "./dist/index.js",
7-
"types": "./dist/index.d.ts",
6+
"main": "./dist/cjs/index.js",
7+
"types": "./types/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./types/index.d.ts",
11+
"import": "./dist/esm/index.js",
12+
"require": "./dist/cjs/index.js"
13+
}
14+
},
815
"scripts": {
9-
"build": "rm -rf dist/ && tsc",
10-
"ci": "npm run eslint && npm run build && npm run test",
16+
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir dist/cjs/",
17+
"build:declaration": "tsc --project tsconfig.declaration.json --module node16 --moduleResolution node16",
18+
"build:esm": "tsc --project tsconfig.build.json --module node16 --moduleResolution node16 --outDir dist/esm/",
19+
"build": "rm -rf dist/ && npm run build:cjs && npm run build:esm",
20+
"ci": "npm run eslint && npm run build && npm run build:declaration && npm run test",
1121
"eslint:fix": "eslint . --ext .js,.ts --fix",
1222
"eslint": "eslint . --ext .js,.ts",
1323
"mocha:unit": "nyc --reporter=html --reporter=text --exclude=tests/ ts-mocha --recursive \"./tests/unit/**/*.spec.ts\"",

tsconfig.build.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"include": [
3+
"index.ts",
4+
"lib/**/*"
5+
],
6+
"exclude": [
7+
"tests/**/*"
8+
],
9+
"compilerOptions": {
10+
"esModuleInterop": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"lib": ["es2023"],
13+
"skipLibCheck": true,
14+
"strict": true,
15+
"target": "es2022",
16+
}
17+
}

tsconfig.declaration.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"include": [
3+
"index.ts",
4+
"lib/**/*"
5+
],
6+
"exclude": [
7+
"tests/**/*"
8+
],
9+
"compilerOptions": {
10+
"declaration": true,
11+
"declarationDir": "./types",
12+
"emitDeclarationOnly": true,
13+
"esModuleInterop": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"lib": ["es2023"],
16+
"skipLibCheck": true,
17+
"strict": true,
18+
"target": "es2022",
19+
}
20+
}

tsconfig.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
"tests/**/*"
88
],
99
"compilerOptions": {
10-
"allowJs": true,
11-
"declaration": true,
1210
"esModuleInterop": true,
1311
"forceConsistentCasingInFileNames": true,
14-
"strict": true,
15-
"lib": ["es2017"],
16-
"module": "commonjs",
17-
"moduleResolution": "node",
18-
"outDir": "./dist",
12+
"lib": ["es2023"],
13+
"module": "node16",
14+
"moduleResolution": "node16",
1915
"skipLibCheck": true,
20-
"target": "es2017"
16+
"strict": true,
17+
"target": "es2022",
2118
}
2219
}

types/index.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { OpenGraphScraperOptions, OgObject } from './lib/types';
2+
/**
3+
* `open-graph-scraper` uses [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for http requests
4+
* for scraping Open Graph and Twitter Card info off a website.
5+
*
6+
* @param {object} options - The options used by Open Graph Scraper
7+
* @param {boolean} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on anything else.
8+
* @param {object} [options.customMetaTags] - Here you can define custom meta tags you want to scrape.
9+
* @param {string} options.html - You can pass in an HTML string to run ogs on it. (use without options.url)
10+
* @returns {Promise} Promise Object with the Open Graph results
11+
*/
12+
export default function run(options: OpenGraphScraperOptions): Promise<ErrorResult | SuccessResult>;
13+
export interface SuccessResult {
14+
error: false;
15+
html: string;
16+
result: OgObject;
17+
}
18+
export interface ErrorResult {
19+
error: true;
20+
html: undefined;
21+
result: OgObject;
22+
}
23+
24+
// adding this line in to fix ESM imports for `module/moduleResolution = NodeNext` projects
25+
export = run;

types/lib/extract.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
2+
/**
3+
* extract all of the meta tags needed for ogs
4+
*
5+
* @param {sting} body - the body of the fetch request
6+
* @param {object} options - options for ogs
7+
* @return {object} object with ogs results
8+
*
9+
*/
10+
export default function extractMetaTags(body: string, options: OpenGraphScraperOptions): OgObjectInteral;

types/lib/fallback.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { CheerioAPI } from 'cheerio';
2+
import type { OpenGraphScraperOptions, OgObjectInteral } from './types';
3+
/**
4+
* ogs fallbacks
5+
*
6+
* @param {object} ogObject - the current ogObject
7+
* @param {object} options - options for ogs
8+
* @param {object} $ - cheerio.load() of the current html
9+
* @return {object} object with ogs results with updated fallback values
10+
*
11+
*/
12+
export declare function fallback(ogObject: OgObjectInteral, options: OpenGraphScraperOptions, $: CheerioAPI, body: string): OgObjectInteral;
13+
export default fallback;

types/lib/fields.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { OgObjectInteral } from './types';
2+
type Fields = {
3+
multiple: boolean;
4+
property: string;
5+
fieldName: keyof OgObjectInteral;
6+
}[];
7+
/**
8+
* array of meta tags ogs is looking for
9+
*
10+
* @return {array} array of meta tags
11+
*
12+
*/
13+
declare const fields: Fields;
14+
export default fields;

types/lib/media.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { OgObjectInteral } from './types';
2+
/**
3+
* formats the multiple media values
4+
*
5+
* @param {object} ogObject - the current ogObject
6+
* @param {object} options - options for ogs
7+
* @return {object} object with ogs results with updated media values
8+
*
9+
*/
10+
export declare function mediaSetup(ogObject: OgObjectInteral): OgObjectInteral;
11+
export default mediaSetup;

types/lib/openGraphScraper.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { OpenGraphScraperOptions } from './types';
2+
/**
3+
* sets up options for the fetch request and calls extract on html
4+
*
5+
* @param {object} options - options for ogs
6+
* @return {object} object with ogs results
7+
*
8+
*/
9+
export default function setOptionsAndReturnOpenGraphResults(ogsOptions: OpenGraphScraperOptions): Promise<{
10+
ogObject: import("./types").OgObjectInteral;
11+
response: {
12+
body: string;
13+
};
14+
html: string;
15+
}>;

0 commit comments

Comments
 (0)