Skip to content

Commit

Permalink
chore(init): initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
geocine committed Mar 26, 2019
0 parents commit db97b25
Show file tree
Hide file tree
Showing 12 changed files with 4,630 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
1 change: 1 addition & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maintained node versions
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: [
"airbnb-base",
// Must go last so that it can override others.
"prettier"
]
};
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/gatsby-node.js
/gatsby-node.js.map

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Sonarlint
.sonarlint

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/carbon
65 changes: 65 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/gatsby-node.js
/gatsby-node.js.map

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Sonarlint
.sonarlint

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: node_js
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) 2019 Aivan Monceller

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.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# gatsby-source-dev

[![NPM version](https://badgen.net/npm/v/gatsby-source-dev)](https://www.npmjs.com/package/gatsby-source-dev) [![Build status](https://badgen.net/travis/geocine/gatsby-source-dev)](https://travis-ci.com/danoc/gatsby-source-dev)

A Gatsby plugin that fetches user articles from Dev.to’s `/articles?username` endpoint.

## Usage

Install `gatsby-source-dev` in your project:

```
yarn add gatsby-source-dev
npm install gatsby-source-dev
```

Then add the plugin to your `gatsby-config.js` file:

```js
{
resolve: "gatsby-source-dev",
options: {
// This is your username on Dev.to
username: ''
}
}
```

The plugin will store the Dev.to API response in Gatsby. Here's an example of a query that fetches an articles `title`, `id`, and `description`.

```js
{
allDevArticles {
edges {
node {
article {
name
id
distance
}
}
}
}
}
```

The node contains the entire response from Dev.to’s endpoint.
50 changes: 50 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "gatsby-source-dev",
"version": "0.0.1",
"description": "Gatsby plugin that fetches user articles from Dev.to",
"files": [
"gatsby-node.js",
"gatsby-node.js.map"
],
"scripts": {
"test": "yarn test:eslint && yarn test:prettier",
"test:eslint": "eslint . --ignore-path .gitignore --ext .jsx --ext .js",
"test:prettier": "prettier \"**/*.{js,jsx,css,scss,json,md,mdx,html}\" --list-different",
"prepublishOnly": "babel src --out-dir . --source-maps",
"format": "prettier \"**/*.{js,jsx,css,scss,json,md,mdx,html}\" --write",
"release": "np"
},
"repository": "https://github.com/geocine/gatsby-source-dev",
"author": "Aivan Monceller",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.2.0",
"query-string": "^6.1.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"eslint": "^5.6.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.14.0",
"husky": "^1.0.0-rc.15",
"np": "^4.0.2",
"prettier": "1.16.4",
"pretty-quick": "^1.10.0"
},
"peerDependencies": {
"gatsby": "^2.0.0"
},
"keywords": [
"gatsby",
"dev.to",
"gatsby-plugin"
],
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
59 changes: 59 additions & 0 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fetch = require("node-fetch");
const crypto = require("crypto");
const queryString = require("query-string");

exports.sourceNodes = async ({ actions }, { username }) => {
if (!username) {
throw new Error(
"You must provide a `username` to `gatsby-source-dev`."
);
}

const { createNode } = actions;

const articles = [];
let numResults = null;
let page = 1;

do {
const params = queryString.stringify({
username,
page: 1
});

// eslint-disable-next-line no-await-in-loop
const res = await fetch(
`https://dev.to/api/articles?${params}`
);

// eslint-disable-next-line no-await-in-loop
const data = await res.json();

numResults = data.length;
page += 1;

data.forEach(d => {
articles.push(d);
});
} while (numResults > 0);

articles.forEach(article => {
const jsonString = JSON.stringify(article);

const gatsbyNode = {
article: Object.assign({}, article),
id: `${article.id}`,
parent: "__SOURCE__",
children: [],
internal: {
type: "DevArticles",
contentDigest: crypto
.createHash("md5")
.update(jsonString)
.digest("hex")
}
};

createNode(gatsbyNode);
});
};
Loading

0 comments on commit db97b25

Please sign in to comment.