From c297377c7f89199555f35ed619c55dedda3a8838 Mon Sep 17 00:00:00 2001 From: Zander Martineau Date: Sun, 24 Sep 2017 07:34:22 +0100 Subject: [PATCH] initial commit :tada: add API --- .gitignore | 1 + index.js | 13 +++++++ now.json | 4 +++ package.json | 22 ++++++++++++ readme.md | 35 +++++++++++++++++++ yarn.lock | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 now.json create mode 100644 package.json create mode 100644 readme.md create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.js b/index.js new file mode 100644 index 0000000..daba079 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +const axios = require('axios') +const { json, send } = require('micro') +const DOMAIN = 'https://xkcd.com/' +const PATH = 'info.0.json' + +module.exports = async function(req, res) { + const id = req.url.replace('/', '') + const comicId = id ? `${id}/` : '' + const path = `${DOMAIN}${comicId}${PATH}` + const response = await axios(path) + res.setHeader('Access-Control-Allow-Origin', '*') + send(res, 200, response.data) +} diff --git a/now.json b/now.json new file mode 100644 index 0000000..9e4687d --- /dev/null +++ b/now.json @@ -0,0 +1,4 @@ +{ + "name": "xkcd", + "alias": "xkcd.now.sh" +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..92b98a4 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "xkcd", + "version": "1.0.0", + "description": "XKCD API", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "nodemon --exec npm start", + "start": "micro", + "deploy": "now" + }, + "keywords": [ + "xkcd", + "api" + ], + "author": "Zander Martineau", + "license": "MIT", + "dependencies": { + "axios": "^0.16.2", + "micro": "^9.0.0" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..687017a --- /dev/null +++ b/readme.md @@ -0,0 +1,35 @@ +

+ XKCD API +

+ +> CORS enabled API for the [XKCD](https://xkcd.com) comic + +## Latest comic +``` +https://xkcd.now.sh +``` + +## Comic #1000 +``` +https://xkcd.now.sh/1000 +``` + +## Example response (comic #1892) +This is the same exact response that the official [XKCD API](https://xkcd.com/json.html) +```json +{ + "month": "9", + "num": 1892, + "link": "", + "year": "2017", + "news": "", + "safe_title": "USB Cables", + "transcript": "", + "alt": "Tag yourself, I'm \"frayed.\"", + "img": "https://imgs.xkcd.com/comics/usb_cables.png", + "title": "USB Cables", + "day": "20" +} +``` + +> Made by [ZΛNDΞR :zap:](https://github.com/mrmartineau/) by using [Micro](https://github.com/zeit/micro) & hosted on [now](https://zeit.co/now) by [zeit](https://zeit.co) diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..d0bf3f9 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,97 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +axios@^0.16.2: + version "0.16.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.16.2.tgz#ba4f92f17167dfbab40983785454b9ac149c3c6d" + dependencies: + follow-redirects "^1.2.3" + is-buffer "^1.1.5" + +bytes@3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" + +debug@^2.4.5: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + dependencies: + ms "2.0.0" + +depd@1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359" + +follow-redirects@^1.2.3: + version "1.2.4" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.2.4.tgz#355e8f4d16876b43f577b0d5ce2668b9723214ea" + dependencies: + debug "^2.4.5" + +http-errors@1.6.2: + version "1.6.2" + resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.2.tgz#0a002cc85707192a7e7946ceedc11155f60ec736" + dependencies: + depd "1.1.1" + inherits "2.0.3" + setprototypeof "1.0.3" + statuses ">= 1.3.1 < 2" + +iconv-lite@0.4.19: + version "0.4.19" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" + +inherits@2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + +is-buffer@^1.1.5: + version "1.1.5" + resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc" + +is-stream@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" + +media-typer@0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" + +micro@^9.0.0: + version "9.0.0" + resolved "https://registry.yarnpkg.com/micro/-/micro-9.0.0.tgz#c07006a4297099bef9c7a4a318711a77209a4b72" + dependencies: + is-stream "1.1.0" + media-typer "0.3.0" + mri "1.1.0" + raw-body "2.3.2" + +mri@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/mri/-/mri-1.1.0.tgz#5c0a3f29c8ccffbbb1ec941dcec09d71fa32f36a" + +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + +raw-body@2.3.2: + version "2.3.2" + resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.3.2.tgz#bcd60c77d3eb93cde0050295c3f379389bc88f89" + dependencies: + bytes "3.0.0" + http-errors "1.6.2" + iconv-lite "0.4.19" + unpipe "1.0.0" + +setprototypeof@1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.0.3.tgz#66567e37043eeb4f04d91bd658c0cbefb55b8e04" + +"statuses@>= 1.3.1 < 2": + version "1.3.1" + resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.3.1.tgz#faf51b9eb74aaef3b3acf4ad5f61abf24cb7b93e" + +unpipe@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"