From 6fa640ff082ab39d891b5097fdeeeecc3c34d575 Mon Sep 17 00:00:00 2001 From: surmon-china Date: Sat, 28 Mar 2020 19:33:03 +0800 Subject: [PATCH] Init TypeScript --- .babelrc | 13 ---- .editorconfig | 33 ++-------- .gitignore | 3 + .npmiginore | 30 --------- .travis.yml | 5 -- CHANGELOG.md | 6 +- dist/bing.js | 121 ------------------------------------ examples/index.js | 72 ---------------------- package.json | 67 ++++++++++---------- src/bing.js | 154 ---------------------------------------------- test/bing.spec.js | 94 ---------------------------- 11 files changed, 40 insertions(+), 558 deletions(-) delete mode 100644 .babelrc delete mode 100644 .npmiginore delete mode 100644 .travis.yml delete mode 100644 dist/bing.js delete mode 100644 examples/index.js delete mode 100644 src/bing.js delete mode 100644 test/bing.spec.js diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 2975f56..0000000 --- a/.babelrc +++ /dev/null @@ -1,13 +0,0 @@ -{ - "presets": ["es2015", "stage-2"], - "plugins": [ - "transform-es2015-destructuring", - "transform-object-rest-spread" - ], - "comments": false, - "env": { - "test": { - "plugins": [ "istanbul" ] - } - } -} diff --git a/.editorconfig b/.editorconfig index 8291f3a..c6c8b36 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,34 +1,9 @@ -# EditorConfig is awesome: http://EditorConfig.org - -# top-most EditorConfig file root = true -# Unix-style newlines with a newline ending every file [*] -end_of_line = lf -insert_final_newline = true - -# Matches multiple files with brace expansion notation -# Set default charset -[*.{js,py}] -charset = utf-8 - -# 4 space indentation -[*.py] -indent_style = space -indent_size = 4 - -# Tab indentation (no size specified) -[Makefile] -indent_style = tab - -# Indentation override for all JS under lib directory -[*.js] -indent_style = space -indent_size = 2 - -# Matches the exact files either package.json or .travis.yml -[{package.json,.travis.yml}] indent_style = space indent_size = 2 - +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitignore b/.gitignore index ad46b30..5c1117b 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ typings/ # next.js build output .next + +# output +dist diff --git a/.npmiginore b/.npmiginore deleted file mode 100644 index a29e2b7..0000000 --- a/.npmiginore +++ /dev/null @@ -1,30 +0,0 @@ -.github -.DS_Store -.babelrc -.editorconfig -.eslintrc.js -.eslintignore -.gitignore -.travis.yml - -dev/ -test/ -test/unit/coverage -test/e2e/reports -examples/ - -gh-pages/ -static/ -build/ -config/ -node_modules -node_modules/ -npm-debug.log -examples -examples/ - -index.html -config.js -bower.json -package-lock.json -selenium-debug.log diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index dea937a..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: node_js -node_js: - - "7" -# after_success: -# - codeclimate-test-reporter < ./test/unit/coverage/lcov.info \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index b671f05..7356857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,13 @@ -## CHANGELOG +# Changelog +All notable changes to this project will be documented in this file. ### v0.2.0 - - 去除无效的今日故事接口 - 增加 ensearch 接口用于获取英文版的全量数据 ### v0.1.2 - 增加容错处理 ### v0.1.0 - 实现基本功能。 diff --git a/dist/bing.js b/dist/bing.js deleted file mode 100644 index f59a0fc..0000000 --- a/dist/bing.js +++ /dev/null @@ -1,121 +0,0 @@ -'use strict'; - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -var https = require('https'); -var querystring = require('querystring'); - -var WonderfulBingWallpaper = function () { - function WonderfulBingWallpaper() { - var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - - _classCallCheck(this, WonderfulBingWallpaper); - - this.setOptions(options); - } - - _createClass(WonderfulBingWallpaper, [{ - key: 'setOptions', - value: function setOptions(options) { - if (this.options) { - this.options = Object.assign(this.options, options); - } else { - this.options = Object.assign({}, DEFAULT_OPTIONS, options); - } - return this; - } - }, { - key: 'getWallpapers', - value: function getWallpapers(params) { - var _this = this; - - return new Promise(function (resolve, reject) { - var mergeParmas = Object.assign({}, _this.options, params); - var queryParams = { - n: mergeParmas.size, - idx: mergeParmas.day, - format: mergeParmas.format, - mkt: mergeParmas.local, - ensearch: mergeParmas.ensearch, - pid: 'hp' - }; - - var requestOptions = { - port: 443, - method: 'GET', - host: _this.options.host, - path: _this.options.wallpaperApi + '?' + querystring.stringify(queryParams) - }; - - var request = https.request(requestOptions, function (res) { - if (res.statusCode < 200 || res.statusCode >= 300) { - return reject(new Error('WonderfulBingWallpaper getWallpapers error: statusCode=' + res.statusCode)); - } - var body = []; - res.on('data', function (data) { - body.push(data); - }); - res.on('end', function () { - body = Buffer.concat(body).toString(); - if (queryParams.format === 'js') { - body = JSON.parse(body); - } - resolve(body.images || body); - }); - }); - - request.on('error', reject); - request.end(); - }); - } - }, { - key: 'humanizeWallpapers', - value: function humanizeWallpapers(wallpaperJson, resolution) { - var _this2 = this; - - resolution = resolution || this.options.resolution; - var doHumanize = function doHumanize(image) { - var host = 'https://' + _this2.options.host; - var fileFormat = /\.[^\.]+$/.exec(image.url); - fileFormat = fileFormat.length ? fileFormat[0] : '.jpg'; - return { - title: image.title, - bsTitle: image.bsTitle, - caption: image.caption, - desc: image.desc, - date: image.date, - copyright: image.copyright, - copyrightonly: image.copyrightonly, - copyrightlink: image.copyrightlink, - searchUrl: '' + host + image.quiz, - defaultUrl: '' + host + image.url, - humanizeUrl: '' + host + image.urlbase + '_' + resolution + fileFormat - }; - }; - if (wallpaperJson instanceof Array) { - return wallpaperJson.map(doHumanize); - } else { - return doHumanize(wallpaperJson); - } - } - }]); - - return WonderfulBingWallpaper; -}(); - -WonderfulBingWallpaper.resolutions = ['1920x1200', '1920x1080', '1366x768', '1280x768', '1024x768', '800x600', '800x480', '768x1280', '720x1280', '640x480', '480x800', '400x240', '320x240', '240x320']; - -var DEFAULT_OPTIONS = { - size: 1, - day: 0, - format: 'js', - ensearch: 0, - local: 'en-US', - host: 'www.bing.com', - wallpaperApi: '/HPImageArchive.aspx', - resolution: WonderfulBingWallpaper.resolutions[1] -}; - -module.exports = WonderfulBingWallpaper; \ No newline at end of file diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index b0f8824..0000000 --- a/examples/index.js +++ /dev/null @@ -1,72 +0,0 @@ -/* - * @file Example of wonderful-bing-wallpaper - * @author Surmon - */ - -const WonderfulBingWallpaper = require('../src/bing') -const xml2js = require('xml2js') - -const resolutions = WonderfulBingWallpaper.resolutions -const wbw = new WonderfulBingWallpaper({ - size: 8, - day: 7, - resolution: resolutions[2], - host: 'cn.bing.com', - local: 'zh-cn', -}) - -console.log('wallPaper', wbw) -console.log('\nwallPaper resolutions', resolutions, resolutions[3]) - -// default json -wbw.getWallpapers().then(wallpaperJSON => { - console.group('wallpaperJSON-1') - console.log('got wallpaperJSON-1 data', wallpaperJSON) - console.log(`\ngot wallpaperJSON-1 humanizeWallpapers data :url = ${resolutions[2]}\n`, wbw.humanizeWallpapers(wallpaperJSON)) - console.log(`\ngot wallpaperJSON-1 humanizeWallpapers data :url = ${resolutions[3]}\n`, wbw.humanizeWallpapers(wallpaperJSON, resolutions[3])) - console.log(`\ngot wallpaperJSON-1[0] humanizeWallpapers data :url = ${resolutions[2]}\n`, wbw.humanizeWallpapers(wallpaperJSON[0])) - console.log(`\ngot wallpaperJSON-1[0] humanizeWallpapers data :url = ${resolutions[5]}\n`, wbw.humanizeWallpapers(wallpaperJSON[0], resolutions[5])) - console.groupEnd() -}) - -// params json -wbw.getWallpapers({ size: 10, day: 2 }).then(wallpaperJSON => { - console.group('wallpaperJSON-2') - console.log('got wallpaperJSON-2 data', wallpaperJSON) - console.log('got wallpaperJSON-2 humanizeWallpapers data', wbw.humanizeWallpapers(wallpaperJSON)) - console.log('got wallpaperJSON-2[0] humanizeWallpapers data\n', wbw.humanizeWallpapers(wallpaperJSON[0])) - console.groupEnd() -}) - -wbw.setOptions({ ensearch: 1 }) - -// ensearch -wbw.getWallpapers().then(wallpaperJSON => { - console.group('wallpaperJSON-3') - console.log('got wallpaperJSON-3 data', wallpaperJSON) - console.log('got wallpaperJSON-3 humanizeWallpapers data', wbw.humanizeWallpapers(wallpaperJSON)) - console.groupEnd() -}) - -// xml -wbw.getWallpapers({ format: 'xml' }).then(wallpaperXML => { - console.group('wallpaperXML') - console.debug('got wallpaperXML data\n', wallpaperXML) - xml2js.parseString(wallpaperXML, (err, result) => { - console.log('wallpaperXML', result) - }) - console.groupEnd() -}) - -// setOptions -wbw.setOptions({ size: 3 }) - -// rss -wbw.getWallpapers({ format: 'rss' }).then(wallpaperRSS => { - console.group('wallpaperRSS') - console.debug('got wallpaperRSS data\n', wallpaperRSS) - xml2js.parseString(wallpaperRSS, (err, result) => { - console.log('wallpaperRSS', result) - }) - console.groupEnd() -}) diff --git a/package.json b/package.json index ffc7c4b..df38ae4 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,7 @@ { "name": "wonderful-bing-wallpaper", - "version": "0.2.1", - "description": "A simple bing daily wallpaper api lib", - "main": "dist/bing.js", - "bugs": { - "url": "https://github.com/surmon-china/wonderful-bing-wallpaper/issues" - }, - "homepage": "https://github.com/surmon-china/wonderful-bing-wallpaper#readme", - "scripts": { - "test": "mocha", - "dev": "devtool examples/index.js --watch", - "build": "babel src --out-dir dist", - "finish": "npm run build && npm run test", - "publish:npm": "git push && git push --tags && npm publish" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/surmon-china/wonderful-bing-wallpaper.git" - }, + "version": "0.3.0", + "description": "Simple bing daily wallpaper API lib for node.js", "keywords": [ "bing", "bing wallpaper", @@ -29,27 +13,38 @@ "author": { "name": "Surmon", "email": "surmon@foxmail.com", - "url": "https://surmon.me" + "url": "https://github.com/surmon-china" + }, + "bugs": { + "url": "https://github.com/surmon-china/wonderful-bing-wallpaper/issues" + }, + "homepage": "https://github.com/surmon-china/wonderful-bing-wallpaper#readme", + "repository": { + "type": "git", + "url": "https://github.com/surmon-china/wonderful-bing-wallpaper.git" + }, + "main": "dist/bing.cjs.js", + "module": "dist/bing.esm.js", + "files": [ + "src", + "dist", + "types" + ], + "types": "types/bing.d.ts", + "scripts": { + "dev": "node dev/index.js", + "cleanup": "rm -rf ./dist/* && rm -rf ./types/*", + "build": "npm run cleanup && abc build", + "lint": "abc lint --ext .ts,.js src test", + "test": "abc test", + "rebirth": "npm run lint && npm run test && npm run build" }, "private": false, "license": "MIT", "devDependencies": { - "babel-cli": "^6.26.0", - "babel-core": "^6.26.3", - "babel-eslint": "^10.0.0", - "babel-helper-vue-jsx-merge-props": "^2.0.3", - "babel-loader": "^8.0.2", - "babel-plugin-istanbul": "^5.0.1", - "babel-plugin-syntax-jsx": "^6.18.0", - "babel-plugin-transform-es2015-destructuring": "^6.23.0", - "babel-plugin-transform-export-extensions": "^6.22.0", - "babel-plugin-transform-object-rest-spread": "^6.26.0", - "babel-plugin-transform-runtime": "^6.23.0", - "babel-preset-es2015": "^6.24.1", - "babel-preset-stage-2": "^6.24.1", - "babel-register": "^6.26.0", - "chai": "^4.2.0", - "mocha": "^5.2.0", - "xml2js": "^0.4.19" + "@surmon-china/abc-factory": "^0.3.2", + "@types/node": "^12.12.31", + "typescript": "^3.8.3" } } + diff --git a/src/bing.js b/src/bing.js deleted file mode 100644 index f0df892..0000000 --- a/src/bing.js +++ /dev/null @@ -1,154 +0,0 @@ -/* - * @file Main of wonderful-bing-wallpaper - * @author Surmon - */ - -const https = require('https') -const querystring = require('querystring') - -/** Class WonderfulBingWallpaper. */ -class WonderfulBingWallpaper { - - /** - * Create a wbw instance. - * @param {object} options - The instance options. - */ - constructor(options = {}) { - this.setOptions(options) - } - - /** - * Set the instance new options. - * @return {instance} The WonderfulBingWallpaper instance. - */ - setOptions(options) { - if (this.options) { - this.options = Object.assign(this.options, options) - } else { - this.options = Object.assign({}, DEFAULT_OPTIONS, options) - } - return this - } - - /** - * Get the wallpapers by params. - * @param {params} size - wallpapers size. - * @param {params} day - Before days. - * @param {params} local - The location. - * @param {params} format - The result doc format. - */ - getWallpapers(params) { - return new Promise((resolve, reject) => { - - // query - const mergeParmas = Object.assign({}, this.options, params) - const queryParams = { - n: mergeParmas.size, - idx: mergeParmas.day, - format: mergeParmas.format, - mkt: mergeParmas.local, - ensearch: mergeParmas.ensearch, - pid: 'hp' - } - - // options - const requestOptions = { - port: 443, - method: 'GET', - host: this.options.host, - path: this.options.wallpaperApi + '?' + querystring.stringify(queryParams), - } - - - // request - const request = https.request(requestOptions, res => { - if (res.statusCode < 200 || res.statusCode >= 300) { - return reject(new Error('WonderfulBingWallpaper getWallpapers error: statusCode=' + res.statusCode)) - } - let body = [] - res.on('data', data => { body.push(data) }) - res.on('end', () => { - body = Buffer.concat(body).toString() - if (queryParams.format === 'js') { - body = JSON.parse(body) - } - resolve(body.images || body) - }) - }) - - // request - request.on('error', reject) - request.end() - }) - } - - /** - * Get the humanize wallpapers by original wallpapers. - * @param {object} wallpaperJson - original wallpapers. - * @param {string} resolution - wallpaper resolution. - */ - humanizeWallpapers(wallpaperJson, resolution) { - resolution = resolution || this.options.resolution - const doHumanize = image => { - const host = 'https://' + this.options.host - let fileFormat = (/\.[^\.]+$/.exec(image.url)) - fileFormat = fileFormat.length ? fileFormat[0] : '.jpg' - return { - title: image.title, - bsTitle: image.bsTitle, - caption: image.caption, - desc: image.desc, - date: image.date, - copyright: image.copyright, - copyrightonly: image.copyrightonly, - copyrightlink: image.copyrightlink, - searchUrl: `${host}${image.quiz}`, - defaultUrl: `${host}${image.url}`, - humanizeUrl: `${host}${image.urlbase}_${resolution}${fileFormat}` - } - } - if (wallpaperJson instanceof Array) { - return wallpaperJson.map(doHumanize) - } else { - return doHumanize(wallpaperJson) - } - } -} - -/** - * Static property. - * @return {Array} A resolutions array. - */ -WonderfulBingWallpaper.resolutions = [ - '1920x1200', - '1920x1080', - '1366x768', - '1280x768', - '1024x768', - '800x600', - '800x480', - '768x1280', - '720x1280', - '640x480', - '480x800', - '400x240', - '320x240', - '240x320' -] - -/** - * Default options. - * @return {Object} A default options. - */ -const DEFAULT_OPTIONS = { - size: 1, - day: 0, - format: 'js', - ensearch: 0, - local: 'en-US', - host: 'www.bing.com', - wallpaperApi: '/HPImageArchive.aspx', - resolution: WonderfulBingWallpaper.resolutions[1] -} - -module.exports = WonderfulBingWallpaper diff --git a/test/bing.spec.js b/test/bing.spec.js deleted file mode 100644 index 1f0cdca..0000000 --- a/test/bing.spec.js +++ /dev/null @@ -1,94 +0,0 @@ -/* - * @file Mocha test of wonderful-bing-wallpaper - * @author Surmon - */ - -const WonderfulBingWallpaper = require('../dist/bing') -const xml2js = require('xml2js') -const expect = require('chai').expect - -const resolutions = WonderfulBingWallpaper.resolutions -const wbw = new WonderfulBingWallpaper({ - size: 8, - day: 7, - resolution: resolutions[2], - host: 'cn.bing.com', - local: 'zh-cn' -}) - -describe('Test Class WonderfulBingWallpaper', () => { - - it('test instance', () => { - expect(wbw instanceof WonderfulBingWallpaper).to.equal(true) - expect(wbw.options.host).to.equal('cn.bing.com') - expect(wbw.options.local).to.equal('zh-cn') - expect(wbw.options.day).to.equal(7) - }) - - it('test resolutions', () => { - expect(resolutions.length).to.equal(14) - expect(resolutions[6]).to.equal('800x480') - }) - - it('test getWallpapers', done => { - wbw.getWallpapers() - .then(data => { - expect(data.length).to.equal(8) - expect(data[0].url.indexOf('.jpg') > -1).to.be.equal(true) - done() - }) - .catch(err => { - done(err) - }) - }) - - it('test params getWallpapers', done => { - wbw.getWallpapers({ size: 10, day: 2 }) - .then(data => { - expect(data.length).to.equal(8) - expect(data[0].url.indexOf('.jpg') > -1).to.be.equal(true) - expect(data[0].quiz.indexOf('/search') === 0).to.be.equal(true) - done() - }) - .catch(err => { - done(err) - }) - }) - - it('test getWallpapers format', done => { - wbw.getWallpapers({ format: 'xml' }) - .then(wallpaperXML => { - xml2js.parseString(wallpaperXML, (err, wallpaperXMLJSON) => { - expect(wallpaperXML).not.to.be.empty - expect(wallpaperXMLJSON).not.to.be.empty - expect(Object.keys(wallpaperXMLJSON.images).includes('tooltips')).to.be.equal(true) - expect(wallpaperXMLJSON.images.image.length === 8).to.be.equal(true) - expect(wallpaperXMLJSON.images.image[0].copyright[0]).not.to.be.empty - done() - }) - }) - .catch(err => { - done(err) - }) - }) - - it('test setOptions', () => { - wbw.setOptions({ size: 3 }) - expect(wbw.options.size).to.equal(3) - }) - - it('test rss format and options merge', done => { - wbw.getWallpapers({ format: 'rss' }) - .then(data => { - xml2js.parseString(data, (err, wallpaperXMLJSON) => { - expect(wallpaperXMLJSON.rss.channel.length).to.equal(1) - expect(wallpaperXMLJSON.rss.channel[0].link[0] === 'http://bing.com/HPImageArchive.aspx?format=rss').to.equal(true) - expect(wallpaperXMLJSON.rss.channel[0].item.length).to.equal(3) - done() - }) - }) - .catch(e => { - done(e) - }) - }) -})