From 9d6c16551d704dc5206bf3a9c27afa61ccf36faf Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Thu, 27 Dec 2018 19:42:52 +0100 Subject: [PATCH] feat(apis): add new apis for the images in repository --- _redirects | 1 + lib/content.js | 6 ++-- lib/ghLink.js | 77 +++++++++++++++++++++++++++++++++++++++++++ lib/link.js | 22 ------------- lib/parseUrl.js | 39 ++++++++++++++++++++++ package.json | 6 ++-- src/apis/logo.js | 42 +++++++++++++++++++++++ src/apis/personal.js | 32 +++++++++++++----- src/docs/index.doc.js | 1 + 9 files changed, 190 insertions(+), 36 deletions(-) create mode 100644 _redirects create mode 100644 lib/ghLink.js delete mode 100644 lib/link.js create mode 100644 lib/parseUrl.js create mode 100644 src/apis/logo.js create mode 100644 src/docs/index.doc.js diff --git a/_redirects b/_redirects new file mode 100644 index 0000000..ef25d15 --- /dev/null +++ b/_redirects @@ -0,0 +1 @@ +/.netlify/functions/* /v2/:splat 200! \ No newline at end of file diff --git a/lib/content.js b/lib/content.js index a3ab408..5e48d4b 100644 --- a/lib/content.js +++ b/lib/content.js @@ -1,6 +1,8 @@ const axios = require("axios"); -const { Information } = require("../lib/link"); +const { + Information +} = require("../lib/ghLink"); // return promise export const QueryContent = (octokit, username, filename) => { @@ -26,4 +28,4 @@ export const TransformResult = result => { export const MultipleQueryResult = results => { // TODO: implement performance here return Promise.all(results.map(r => axios.get(r).then(v => v.data))); -}; +}; \ No newline at end of file diff --git a/lib/ghLink.js b/lib/ghLink.js new file mode 100644 index 0000000..1366559 --- /dev/null +++ b/lib/ghLink.js @@ -0,0 +1,77 @@ +const repo_owner = "kcnt-info"; +const repo_name = "website"; +const branch = "master"; +const language = 'en' + +export const Information = { + owner: repo_owner, + repo: repo_name, + branch: branch +}; + +/** + * generate github link to query result + * @param {String} path path from root folder + * @param {Object} opts options + * @param {String} opts.branch branch of result + */ +export const GenerateGithubLink = (path, opts = {}) => { + const link = `https://raw.githubusercontent.com/${repo_owner}/${repo_name}/${opts.branch || + branch}/static/resources/${path}`; + console.log(`query to: ${link}`); + return link +} + +/** + * get api link of information file + * @param {String} user username, should be `net` or `prang` + * @param {Object} opts options + * @param {String} opts.branch branch of result + * @param {String} opts.language language of the result + */ +export const PersonalInformationLink = (user, opts = {}) => { + return GenerateGithubLink(`${user}/personal/information-${opts.language || language}.json`, opts) +}; + +/** + * get api link of social file + * @param {String} user username, should be `net` or `prang` + * @param {Object} opts options + * @param {String} opts.branch branch of result + */ +export const PersonalSocialLink = (user, opts = {}) => { + return GenerateGithubLink(`${user}/personal/social.json`, opts) +}; + +export const _logoPNGLink = (color, opts = {}) => { + let postfix = ''; + let size = '1x'; + if (opts.size === "small") { + size = '0.5x' + } else if (opts.size === "high") { + size = "300ppi" + postfix = '-high' + } + + const type = opts.type === "round" ? "-round-icon" : '-icon' + + // https://raw.githubusercontent.com/kcnt-info/website/dev/static/resources/images/icon/1x/dark-icon.png + return GenerateGithubLink(`images/icon/${size}/${color}${type}${postfix}.png`, opts) +} + +export const _logoSVGLink = (color, type, opts = {}) => { + const postfix = type === "round" ? "-round-icon" : '-icon' + // https://raw.githubusercontent.com/kcnt-info/website/dev/static/resources/images/icon/SVG/dark-icon.svg + return GenerateGithubLink(`images/icon/SVG/${color}${postfix}.svg`, opts) +} + +/** + * get api link of social file + * @param {String} user username, should be `net` or `prang` + * @param {Object} opts options + * @param {String} opts.branch branch of result + */ +export const LogoLink = (color, opts = {}) => { + if (opts.extension === "png") return _logoPNGLink(color, opts); + else return _logoSVGLink(color, opts.type, opts) +} \ No newline at end of file diff --git a/lib/link.js b/lib/link.js deleted file mode 100644 index 551a1df..0000000 --- a/lib/link.js +++ /dev/null @@ -1,22 +0,0 @@ -const repo_owner = "kcnt-info"; -const repo_name = "website"; -const branch = "master"; - -export const Information = { - owner: repo_owner, - repo: repo_name, - branch: branch -}; - -export const generateLink = (username, path, opts = {}) => { - return `https://raw.githubusercontent.com/${repo_owner}/${repo_name}/${opts.branch || - branch}/static/resources/${username}/${path}`; -}; - -export const PersonalInformationLink = (user, opts = {}) => { - return generateLink(user, "personal/information.json", opts); -}; - -export const PersonalSocialLink = (user, opts = {}) => { - return generateLink(user, "personal/social.json", opts); -}; diff --git a/lib/parseUrl.js b/lib/parseUrl.js new file mode 100644 index 0000000..7e4eb7f --- /dev/null +++ b/lib/parseUrl.js @@ -0,0 +1,39 @@ +const matchPath = (path, results) => { + const index = results.findIndex(p => path.includes(`/${p}`)) + if (index < 0) return undefined; + return results[index] +} + +const matchQuery = (query, options) => { + if (options === undefined) return query; + return options.includes(query) ? query : undefined +} + +/** + * + * @param {String} name name of the key result and query name + * @param {Array} options possible values + * @param {String} defaultValue default value if value is not exist + */ +export const GenObj = (name, options, + defaultValue = undefined) => { + if ( + defaultValue === undefined || + defaultValue === null) defaultValue = options[0] + + return { + name: name, + default: defaultValue, + options: options + } +} + +export const ParseParameters = (event, ...obj) => { + const query = event.queryStringParameters; + const path = event.path; + const result = {}; + obj.forEach(v => { + result[v.name] = matchPath(path, v.options) || matchQuery(query[v.name], v.options) || v.default + }) + return result +} \ No newline at end of file diff --git a/package.json b/package.json index dc798a8..224aeaf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kcnt-info-apis", - "version": "1.0.0", + "version": "2.0.0", "description": "My Portfolio website", "repository": "https://github.com/kcnt-info/apis.git", "author": "Kamontat Chantrachirathumrong ", @@ -16,7 +16,7 @@ "scripts": { "start": "netlify-lambda serve src/apis", "build": "netlify-lambda build src/apis", - "docs": "apidoc -i src/docs -o docs --verbose", + "docs": "apidoc -i src/docs -o docs --verbose && cp _redirects docs/_redirects", "deploy": "yarn docs && yarn build" }, "apidoc": { @@ -25,4 +25,4 @@ "url": "https://apis.kcnt.info/.netlify/functions", "sampleUrl": "https://apis.kcnt.info/.netlify/functions" } -} \ No newline at end of file +} diff --git a/src/apis/logo.js b/src/apis/logo.js new file mode 100644 index 0000000..5dd27a1 --- /dev/null +++ b/src/apis/logo.js @@ -0,0 +1,42 @@ +const axios = require("axios") + +const { + ParseParameters, + GenObj +} = require('../../lib/parseUrl') + +const { + LogoLink +} = require("../../lib/ghLink"); + +exports.handler = function (event, context, callback) { + const result = ParseParameters(event, + GenObj("branch", ["master", "dev"]), + GenObj("color", ["primary", "light", "dark"]), + GenObj("size", ["normal", "smaller", "high"]), + GenObj("type", ["normal", "round"]), + GenObj("extension", ["png", "svg"])) + + const url = LogoLink(result.color, result); + + axios + .get(url, { + responseType: 'arraybuffer' + }) + .then(response => { + let image = Buffer.from(response.data, 'binary').toString('base64') + + callback(undefined, { + statusCode: response.status, + headers: { + "Content-Type": result.extension === "svg" ? "image/svg+xml" : "image/png", + "Content-Length": response.headers['content-length'], + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Headers": "*" + }, + body: image, + isBase64Encoded: true + }); + }) + .catch(callback); +} \ No newline at end of file diff --git a/src/apis/personal.js b/src/apis/personal.js index 86568f8..498741a 100644 --- a/src/apis/personal.js +++ b/src/apis/personal.js @@ -1,16 +1,30 @@ const axios = require("axios"); -const { PersonalInformationLink, PersonalSocialLink } = require("../../lib/link"); -const { AnalysicEvent, defaultUser, defaultUsers } = require("../../lib/analysic"); +const { + ParseParameters, + GenObj +} = require('../../lib/parseUrl') -exports.handler = function(event, _, callback) { - const result = AnalysicEvent(event, defaultUser, defaultUsers, "information", ["information", "social"]); - console.log(result); +const { + PersonalInformationLink, + PersonalSocialLink +} = require("../../lib/ghLink"); + +exports.handler = function (event, _, callback) { + const result = ParseParameters(event, + GenObj("user", ["net", "prang"]), + GenObj("branch", ["master", "dev"]), + GenObj("lang", ["en", "th"]), + GenObj("path", ["information", "social"])); const url = - result.type === "social" - ? PersonalSocialLink(result.user, { branch: result.branch }) - : PersonalInformationLink(result.user, { branch: result.branch }); + result.path === "social" ? + PersonalSocialLink(result.user, { + branch: result.branch + }) : + PersonalInformationLink(result.user, { + branch: result.branch + }); axios .get(url) @@ -26,4 +40,4 @@ exports.handler = function(event, _, callback) { }); }) .catch(callback); -}; +}; \ No newline at end of file diff --git a/src/docs/index.doc.js b/src/docs/index.doc.js new file mode 100644 index 0000000..83dfca8 --- /dev/null +++ b/src/docs/index.doc.js @@ -0,0 +1 @@ +// TODO: Changes doc generate to https://github.com/danielgtaylor/aglio \ No newline at end of file