├── .babelrc ├── .gitignore ├── .npmignore ├── README.md ├── circle.yml ├── index.js ├── package.json ├── samples ├── list.js └── upload.js ├── src └── index.js └── tests ├── test.jpg ├── test_gyazo.js └── test_helper.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ "es2015", "stage-0" ] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *#* 3 | .DS_Store 4 | *.log 5 | node_modules 6 | tmp 7 | lib/ 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | samples/ 3 | tests/ 4 | circle.yml 5 | .babelrc 6 | .eslintrc 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gyazo-API 2 | ========= 3 | [Gyazo API](https://gyazo.com/api/docs) wrapper for Node.js 4 | 5 | - https://github.com/shokai/node-gyazo-api 6 | - https://www.npmjs.org/package/gyazo-api 7 | 8 | [![Circle CI](https://circleci.com/gh/shokai/node-gyazo-api.svg?style=svg)](https://circleci.com/gh/shokai/node-gyazo-api) 9 | 10 | 11 | Usage 12 | ----- 13 | 14 | Register new application and get [ACCESS TOKEN](https://gyazo.com/oauth/applications), then 15 | 16 | ### upload("filepath") or upload("stream") 17 | 18 | ```javascript 19 | var Gyazo = require('gyazo-api'); 20 | var client = new Gyazo('ACCESS_TOKEN'); 21 | 22 | client.upload('/path/to/file.jpg', { 23 | title: "my picture", 24 | desc: "upload from nodejs" 25 | }) 26 | .then(function(res){ 27 | console.log(res.data.image_id); 28 | console.log(res.data.permalink_url); 29 | }) 30 | .catch(function(err){ 31 | console.error(err); 32 | }); 33 | ``` 34 | 35 | ### list 36 | 37 | ```javascript 38 | client.list({page: 1, per_page: 50}) 39 | .then(function(res){ 40 | console.log(res.data[0]); 41 | console.log(res.response.headers['x-current-page']); // => 1 42 | console.log(res.response.headers['x-per-page']); // => 50 43 | }) 44 | .catch(function(err){ 45 | console.error(err); 46 | }); 47 | ``` 48 | 49 | ### delete 50 | 51 | ```javascript 52 | client.delete(image_id) 53 | .then(function(res){ 54 | console.log(res.data.image_id); 55 | }); 56 | ``` 57 | 58 | 59 | Test 60 | ---- 61 | 62 | setup 63 | 64 | % npm install 65 | % export GYAZO_TOKEN=a1b2cdef3456 ## set your API Token 66 | 67 | run test 68 | 69 | % npm test 70 | 71 | or 72 | 73 | % npm run watch 74 | 75 | 76 | Contributing 77 | ------------ 78 | 1. Fork it 79 | 2. Create your feature branch (`git checkout -b my-new-feature`) 80 | 3. Commit your changes (`git commit -am 'Add some feature'`) 81 | 4. Push to the branch (`git push origin my-new-feature`) 82 | 5. Create new Pull Request 83 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 6.8 4 | test: 5 | override: 6 | - npm run build 7 | - npm run test:mocha 8 | - npm run test:standard 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/').default 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gyazo-api", 3 | "private": false, 4 | "version": "0.3.1", 5 | "description": "API Client for Gyazo.com", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "run-s test:*", 9 | "test:mocha": "NODE_ENV=test mocha tests/test_*.js -r babel-polyfill --compilers js:babel-register", 10 | "test:standard": "standard", 11 | "build": "run-s build:*", 12 | "build:babel": "babel src/ --out-dir lib/ --source-maps inline", 13 | "watch": "run-p watch:*", 14 | "watch:babel": "npm run build:babel -- --watch", 15 | "clean": "rm -rf lib/" 16 | }, 17 | "devDependencies": { 18 | "babel-cli": "^6.16.0", 19 | "babel-polyfill": "^6.16.0", 20 | "babel-preset-es2015": "^6.16.0", 21 | "babel-preset-stage-0": "^6.16.0", 22 | "chai": "^3.5.0", 23 | "chokidar-cli": "^1.2.0", 24 | "mocha": "^3.1.2", 25 | "npm-run-all": "^3.1.1", 26 | "standard": "8.4.0" 27 | }, 28 | "dependencies": { 29 | "request": "*" 30 | }, 31 | "keywords": [ 32 | "gyazo", 33 | "image", 34 | "upload" 35 | ], 36 | "author": "Sho Hashimoto ", 37 | "license": "MIT", 38 | "repository": { 39 | "type": "git", 40 | "url": "https://github.com/shokai/node-gyazo-api.git" 41 | }, 42 | "bugs": { 43 | "url": "https://github.com/shokai/node-gyazo-api/issues" 44 | }, 45 | "homepage": "https://github.com/shokai/node-gyazo-api" 46 | } 47 | -------------------------------------------------------------------------------- /samples/list.js: -------------------------------------------------------------------------------- 1 | var Gyazo = require('../') // load this repos 2 | 3 | // var Gyazo = require('gyazo-api') // load from npm 4 | 5 | var client = new Gyazo(process.env.GYAZO_TOKEN) 6 | 7 | client.list({page: 1, per_page: 50}) 8 | .then(function (res) { 9 | console.log(res.data[0]) 10 | console.log(res.response.headers['x-current-page']) // => 1 11 | console.log(res.response.headers['x-per-page']) // => 50 12 | }) 13 | .catch(function (err) { 14 | console.error(err) 15 | }) 16 | -------------------------------------------------------------------------------- /samples/upload.js: -------------------------------------------------------------------------------- 1 | var Gyazo = require('../') // load this repos 2 | // var Gyazo = require('gyazo-api') // load from npm 3 | 4 | var client = new Gyazo(process.env.GYAZO_TOKEN) 5 | 6 | var imgPath = process.argv[2] 7 | 8 | client.upload(imgPath, { 9 | title: 'test upload', 10 | desc: 'upload from node gyazo api' 11 | }).then(function (res) { 12 | console.log(res.data) 13 | console.log(res.data.permalink_url) 14 | }).catch(function (err) { 15 | console.error(err) 16 | }) 17 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs' 2 | import request from 'request' 3 | 4 | export default class Gyazo { 5 | 6 | constructor (accessToken = null) { 7 | this.accessToken = accessToken 8 | } 9 | 10 | upload (image, params = {}) { 11 | return new Promise((resolve, reject) => { 12 | if (!image) throw new Error('image is undefined') 13 | if (typeof image === 'string') image = fs.createReadStream(image) 14 | const url = 'https://upload.gyazo.com/api/upload' 15 | const req = request.post({ 16 | url: url 17 | }, (err, res, body) => { 18 | if (err) return reject(err) 19 | if (res.statusCode !== 200) return reject(res.body) 20 | resolve({ 21 | response: res, 22 | data: JSON.parse(body) 23 | }) 24 | }) 25 | const form = req.form() 26 | form.append('imagedata', image) 27 | form.append('access_token', this.accessToken) 28 | for (let k in params) { 29 | form.append(k, params[k]) 30 | } 31 | }) 32 | } 33 | 34 | list (query = {}) { 35 | return new Promise((resolve, reject) => { 36 | query.access_token = this.accessToken 37 | const url = 'https://api.gyazo.com/api/images' 38 | request.get({ 39 | url: url, 40 | qs: query 41 | }, (err, res, body) => { 42 | if (err) return reject(err) 43 | if (res.statusCode !== 200) return reject(res.body) 44 | resolve({ 45 | response: res, 46 | data: JSON.parse(res.body) 47 | }) 48 | }) 49 | }) 50 | } 51 | 52 | delete (imageId) { 53 | return new Promise((resolve, reject) => { 54 | if (!imageId) throw new Error('imageId is undefined') 55 | const url = `https://api.gyazo.com/api/images/${imageId}` 56 | request.del({ 57 | url: url, 58 | qs: { 59 | access_token: this.accessToken 60 | } 61 | }, (err, res, body) => { 62 | if (err) return reject(err) 63 | if (res.statusCode !== 200) return reject(res.body) 64 | resolve({ 65 | response: res, 66 | data: JSON.parse(res.body) 67 | }) 68 | }) 69 | }) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tests/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shokai/node-gyazo-api/531b73c3c95f2cd2013b5aa952c0666c26479a78/tests/test.jpg -------------------------------------------------------------------------------- /tests/test_gyazo.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | 3 | import './test_helper' 4 | import fs from 'fs' 5 | import {assert} from 'chai' 6 | import Gyazo from '../src/' 7 | 8 | const imgPath = `${__dirname}/test.jpg` 9 | 10 | describe('GYAZO_TOKEN', function () { 11 | it('should have ENV var GYAZO_TOKEN', function () { 12 | assert.isString(process.env.GYAZO_TOKEN) 13 | }) 14 | }) 15 | 16 | describe('"upload" method', function () { 17 | const gyazo = new Gyazo(process.env.GYAZO_TOKEN) 18 | 19 | it('should upload image', async function () { 20 | this.timeout(10000) 21 | const res = await gyazo.upload(imgPath) 22 | assert.isObject(res.response) 23 | assert.isString(res.data.image_id) 24 | assert.isUrl(res.data.permalink_url) 25 | assert.isUrl(res.data.url) 26 | }) 27 | 28 | it('should upload image from stream', async function () { 29 | this.timeout(10000) 30 | const res = await gyazo.upload(fs.createReadStream(imgPath)) 31 | assert.isObject(res.response) 32 | assert.isString(res.data.image_id) 33 | assert.isUrl(res.data.permalink_url) 34 | assert.isUrl(res.data.url) 35 | }) 36 | }) 37 | 38 | describe('"list" method', function () { 39 | const gyazo = new Gyazo(process.env.GYAZO_TOKEN) 40 | 41 | it('should return list of images', async function () { 42 | this.timeout(3000) 43 | const res = await gyazo.list({ page: 1, per_page: 5 }) 44 | assert.isObject(res.response) 45 | assert.isArray(res.data) 46 | assert.lengthOf(res.data, 5) 47 | assert.isString(res.data[0].image_id) 48 | assert.isUrl(res.data[0].permalink_url) 49 | assert.isUrl(res.data[0].url) 50 | assert.equal(res.response.headers['x-current-page'], 1) 51 | assert.equal(res.response.headers['x-per-page'], 5) 52 | }) 53 | }) 54 | 55 | describe('"delete" method', function () { 56 | const gyazo = new Gyazo(process.env.GYAZO_TOKEN) 57 | 58 | it('should delete uploaded image', async function () { 59 | this.timeout(10000) 60 | const upRes = await gyazo.upload(imgPath) 61 | const delRes = await gyazo.delete(upRes.data.image_id) 62 | assert.equal(upRes.data.image_id, delRes.data.image_id) 63 | }) 64 | }) 65 | -------------------------------------------------------------------------------- /tests/test_helper.js: -------------------------------------------------------------------------------- 1 | if (!process.env.GYAZO_TOKEN) { 2 | console.error('ENV Var "GYAZO_TOKEN" required') 3 | process.exit(1) 4 | } 5 | 6 | import {assert} from 'chai' 7 | assert.isUrl = function (str) { 8 | return assert.match(str, /^https?:\/\/.+/, 'should be URL String') 9 | } 10 | --------------------------------------------------------------------------------