Skip to content

Commit

Permalink
Support for default export (#5)
Browse files Browse the repository at this point in the history
* switch to ava from jest

* add commonjs support export

* remove pnpm

* remove redundant packages
  • Loading branch information
rocktimsaikia authored Oct 14, 2020
1 parent 7b84952 commit 6130342
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 102 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/node_modules
/package-lock.json
/yarn-lock.json
/pnpm-lock.yaml
/yarn.lock
/dist
13 changes: 0 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
language: node_js

node_js:
- "node"
- "12"
- "10"

before_install:
- npm install -g pnpm

install:
- pnpm install

script:
- pnpm run test

notifications:
email: false
12 changes: 0 additions & 12 deletions __tests__/index.ts

This file was deleted.

13 changes: 0 additions & 13 deletions __tests__/util.ts

This file was deleted.

2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {fetchMetaData} = require('../dist');
const fetchMetaData = require('../dist');

(async () => {
const result = await fetchMetaData('https://hoppscotch.io/', {
Expand Down
2 changes: 1 addition & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Example script using url-fetch
'use strict';
import {fetchMetaData} from '../src';
import fetchMetaData from '../src';

(async () => {
const result = await fetchMetaData('https://microtip.now.sh', {
Expand Down
57 changes: 25 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,42 @@
"scrapper"
],
"scripts": {
"prepublishOnly": "pnpm run prebuild && pnpm run test:dev && pnpm run build",
"prebuild": "rimraf dist",
"build": "pnpm run build:esm && pnpm run build:cjs",
"build:cjs": "tsc --module commonjs",
"build:esm": "tsc --module esnext && cpy dist/index.js dist --rename index.esm.js",
"dev": "ts-node example/index.ts",
"dev:js": "node --trace-warnings example/index.js",
"test:dev": "xo && pnpm run test",
"test": "jest --verbose"
},
"xo": {
"extensions": [
"ts"
],
"ignores": [
"__tests__/**/*.ts"
],
"rules": {
"import/no-anonymous-default-export": 0,
"quote-props": 0
}
},
"jest": {
"preset": "ts-jest"
"build": "del dist && tsc",
"test": "xo && ava",
"prepublishOnly": "npm run build"
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^0.7.0",
"@types/cheerio": "^0.22.21",
"@types/jsdom": "^16.2.4",
"@types/node": "^14.6.4",
"@types/node-fetch": "^2.5.7",
"cpy-cli": "^3.1.1",
"jest": "^26.4.2",
"rimraf": "^3.0.2",
"ts-jest": "^26.3.0",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"ava": "^3.13.0",
"del-cli": "^3.0.1",
"eslint-config-xo-typescript": "^0.33.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.2",
"typescript": "^4.0.3",
"xo": "^0.33.1"
},
"xo": {
"extends": "xo-typescript",
"ignores": [
"example/**/*"
]
},
"ava": {
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
},
"engines": {
"node": ">=12"
}
}
}
38 changes: 23 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import fetch from 'node-fetch';
import * as cheerio from 'cheerio';
import {createValidUri} from './util';

type Metadata = Record<string | number | symbol, unknown>;

/**
* Fetches meta data of a given website url
* @param url | the website url to fetch the metadata from
*/
export const fetchMetaData = async (url: string, _options?: Options): Promise<Record<string, unknown>> => {
const fetchMetaData = async (url: string, _options?: Options): Promise<Metadata | undefined> => {
try {
const urlString: string = url.trim();

Expand All @@ -25,7 +27,7 @@ export const fetchMetaData = async (url: string, _options?: Options): Promise<Re
method: 'GET',
headers: {
'User-Agent': options.userAgent,
'From': options.fromEmail
From: options.fromEmail
}
};

Expand All @@ -37,7 +39,7 @@ export const fetchMetaData = async (url: string, _options?: Options): Promise<Re
const head = $('head');

// Basic site meta-data
const basicMeta = (): Record<string, unknown> => {
const basicMeta = (): Metadata => {
const website = response.url;
const title = head.find('title').text();
const desc = head.find('meta[name=description]').attr('content');
Expand All @@ -50,32 +52,32 @@ export const fetchMetaData = async (url: string, _options?: Options): Promise<Re
};

// Open graph basic
const fetchMeta = (): Record<string, unknown> => {
const fetchMeta = (): Metadata => {
const openGraphsArray = head.find('meta[property]');
const openGraphs = {};
const openGraphs: Metadata = {};
openGraphsArray.each((_, element) => {
const property = $(element)
.attr('property');
const content = $(element).attr('content');
if (!property.includes('twitter')) {
openGraphs[property] = content;
if (!property!.includes('twitter')) {
openGraphs[property!] = content;
}
});

return openGraphs;
};

// Open graph social
const fetchMetaSocial = (): Record<string, unknown> => {
const fetchMetaSocial = (): Metadata => {
const openGraphsArray = head.find('meta[name]');
const socials = {};
const socials: Metadata = {};

openGraphsArray.each((_, element) => {
const property = $(element).attr('name');
const content = $(element).attr('content');

if (property.includes('twitter')) {
socials[property] = content;
if (property!.includes('twitter')) {
socials[property!] = content;
}
});
return socials;
Expand All @@ -89,8 +91,8 @@ export const fetchMetaData = async (url: string, _options?: Options): Promise<Re
faviconArray.each((_, element) => {
const href = $(element).attr('href');

if (href.includes('shortcut icon') || href.includes('icon') || href.includes('apple-touch-startup-image') || href.includes('apple-touch-icon')) {
const validUri = createValidUri(response.url, href);
if (href!.includes('shortcut icon') || href!.includes('icon') || href!.includes('apple-touch-startup-image') || href!.includes('apple-touch-icon')) {
const validUri = createValidUri(response.url, href!);
favicons.push(validUri);
}
});
Expand All @@ -103,15 +105,15 @@ export const fetchMetaData = async (url: string, _options?: Options): Promise<Re
const openGraph_social = fetchMetaSocial();
const favicons = fetchFavicons();

const metaData: Record<string, unknown> = {
const metaData: Metadata = {
basic_metadata: basicMetaData,
opengraph: openGraphs,
opengraph_social: openGraph_social,
favicons
};

return metaData;
} catch (error) {
} catch (error: unknown) {
console.error(error);
}
};
Expand All @@ -121,3 +123,9 @@ interface Options{
userAgent?: string;
fromEmail?: string;
}

export default fetchMetaData;

// For CommonJS default export support
module.exports = fetchMetaData;
module.exports.default = fetchMetaData;
11 changes: 11 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'ava';
import fetchMetaData from '../src';

test('check meta-data', async t => {
const response = await fetchMetaData('https://google.com');

t.true(Object.prototype.hasOwnProperty.call(response, 'basic_metadata'));
t.true(Object.prototype.hasOwnProperty.call(response, 'opengraph'));
t.true(Object.prototype.hasOwnProperty.call(response, 'opengraph_social'));
t.true(Object.prototype.hasOwnProperty.call(response, 'favicons'));
});
12 changes: 12 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava';
import {createValidUri} from '../src/util';

test('creates valid URI', t => {
const result: string = createValidUri('https://github.com/', '/rocktimsaikia');
t.is(result, 'https://github.com/rocktimsaikia');
});

test('returns the path', t => {
const result: string = createValidUri('https://github.com/', 'https://github.com/rocktimsaikia');
t.is(result, 'https://github.com/rocktimsaikia');
});
27 changes: 14 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@

{
"compilerOptions": {
"outDir": "dist",
"target": "es2018",
"sourceMap": false,
"incremental": false,
"skipLibCheck": true,
"declaration": true,
"esModuleInterop": true,
"lib": [
"es2018"
]
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "dist",
"target": "es2018",
"lib": [
"es2018"
],
"noImplicitReturns": false
},
"include": [
"src/**/*"
"include": [
"src"
],
"exclude": [
"example/**/*"
]
}

0 comments on commit 6130342

Please sign in to comment.