Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
feat(apis): add new apis for the images in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamontat Chantrachirathumrong committed Dec 27, 2018
1 parent 4d5249a commit 9d6c165
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 36 deletions.
1 change: 1 addition & 0 deletions _redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.netlify/functions/* /v2/:splat 200!
6 changes: 4 additions & 2 deletions lib/content.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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)));
};
};
77 changes: 77 additions & 0 deletions lib/ghLink.js
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 0 additions & 22 deletions lib/link.js

This file was deleted.

39 changes: 39 additions & 0 deletions lib/parseUrl.js
Original file line number Diff line number Diff line change
@@ -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<String>} 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
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <kamontat_c@hotmail.com>",
Expand All @@ -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": {
Expand All @@ -25,4 +25,4 @@
"url": "https://apis.kcnt.info/.netlify/functions",
"sampleUrl": "https://apis.kcnt.info/.netlify/functions"
}
}
}
42 changes: 42 additions & 0 deletions src/apis/logo.js
Original file line number Diff line number Diff line change
@@ -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);
}
32 changes: 23 additions & 9 deletions src/apis/personal.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -26,4 +40,4 @@ exports.handler = function(event, _, callback) {
});
})
.catch(callback);
};
};
1 change: 1 addition & 0 deletions src/docs/index.doc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: Changes doc generate to https://github.com/danielgtaylor/aglio

0 comments on commit 9d6c165

Please sign in to comment.