Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktimsaikia committed Sep 12, 2020
0 parents commit 012698b
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/package-lock.json
/yarn-lock.json
/pnpm-lock.yaml
/dist
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js

node_js:
- "12"
- "10"

before_install:
- npm install -g pnpm

install:
- pnpm install

script:
- pnpm run test

notifications:
email: false
12 changes: 12 additions & 0 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {fetchMetaData} from '../src';

test('check meta-data', async () => {
const response = await fetchMetaData('https://microtip.now.sh');
const keys = Object.keys(response);

expect(keys).toHaveLength(4);
expect(response).toHaveProperty('basic_metadata');
expect(response).toHaveProperty('opengraph');
expect(response).toHaveProperty('opengraph_social');
expect(response).toHaveProperty('favicons');
});
13 changes: 13 additions & 0 deletions __tests__/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {createValidUri} from '../src/util';

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

test('returns the path', () => {
const result: string = createValidUri('https://github.com/', 'https://github.com/rocktimsaikia');
expect(result).toBe('https://github.com/rocktimsaikia');
});
});
9 changes: 9 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {fetchMetaData} = require('../dist');

(async () => {
const result = await fetchMetaData('https://hoppscotch.io/', {
userAgent: 'Rocktim',
fromEmail: '[email protected]'
});
console.log(result);
})();
11 changes: 11 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Example script using url-fetch
'use strict';
import {fetchMetaData} from '../src';

(async () => {
const result = await fetchMetaData('https://microtip.now.sh', {
userAgent: 'Rocktim',
fromEmail: '[email protected]'
});
console.log(result);
})();
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Rocktim Saikia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "meta-fetch",
"version": "1.0.0",
"description": "Tiny URL meta-data fetcher that scraps the meta-data of a given URL string",
"keywords": [
"meta-man",
"url-metadata",
"url-metadata-scrapper",
"metadata-scrapper",
"website-metadata",
"website-metadata-scrapper",
"metadata",
"scrapper"
],
"bugs": {
"url": "https://github.com/rocktimsaikia/meta-fetch/issues"
},
"license": "MIT",
"author": {
"name": "Rocktim Saikia",
"email": "[email protected]",
"url": "https://rocktim.xyz"
},
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"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"
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@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",
"ts-node": "^9.0.0",
"typescript": "^4.0.2",
"xo": "^0.33.1"
},
"engines": {
"node": ">=12"
}
}
104 changes: 104 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# meta-fetch :mag_right:

> Tiny URL meta-data fetcher that scraps the meta-data of a given `URL` string.
![Travis (.com) branch](https://img.shields.io/travis/com/rocktimsaikia/meta-fetcher/master)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)

Under the hood it uses [node-fetch](https://github.com/node-fetch/node-fetch) to fetch the metadata, parses it and returns it as json object.

## Install

```sh
npm install meta-fetch
```

## Basic Usage
```js
const {fetchMetaData} = require('meta-fetch');

(async () => {
const result = await fetchMetaData('https://hoppscotch.io/');
console.log(result);

/*
{
basic_metadata: {
website: 'https://hoppscotch.io/',
title: 'Hoppscotch • A free, fast and beautiful API request builder',
description: 'A free, fast and beautiful API request builder'
},
opengraph: {
'og:image': 'https://hoppscotch.io/banner.jpg',
'og:type': 'website',
'og:title': 'Hoppscotch',
'og:site_name': 'Hoppscotch',
'og:description': 'A free, fast and beautiful API request builder',
'og:url': 'https://hoppscotch.io/'
},
opengraph_social: {
'twitter:card': 'summary_large_image',
'twitter:site': '@liyasthomas',
'twitter:creator': '@liyasthomas'
},
favicons: [
'https://hoppscotch.io/icon.png',
'https://hoppscotch.io/icon.png',
'https://hoppscotch.io/_nuxt/icons/icon_64x64.9834b3.png'
]
}
*/
})();

```

## Advanced Usage (with options)
You can optionally set the `userAgent` and `fromEmail` options in request `Header` while fetching the meta-data.

```js
const {fetchMetaData} = require('meta-fetch');

(async () => {
const result = await fetchMetaData('https://hoppscotch.io/', {
userAgent: 'Rocktim',
fromEmail: '[email protected]'
});
console.log(result);
})();
```
<br>
It can also fetch meta-data from `shortened-url` .For example:
```js
const {fetchMetaData} = require('meta-fetch');

(async () => {
const result = await fetchMetaData('https://bit.ly/2Fj9sNF');
console.log(result);
})();
```

## Options
You can set these options in Header while fetching the data if needed.

| Option | Required | Default Value |
| :------------- | :----------: | -----------: |
| `userAgent` | No | `meta-fetch` |
| `fromEMail` | No | `[email protected]` |

## API

**metaDataFetch(url, options)**

#### url
Type: `string`
url string that you want to fetch the meta-data from.

#### options
Type: `object`
Optional `Header` paramerter you can set if needed.

## Contribute
For any new feature request or bug report, please open an issue or pull request in GitHub.

## License
MIT &copy; [Rocktim Saikia](https://rocktim.xyz)
Loading

0 comments on commit 012698b

Please sign in to comment.