This repository has been archived by the owner on Apr 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apis): add personal api and collection api
- Loading branch information
Kamontat Chantrachirathumrong
committed
Dec 18, 2018
1 parent
5119ea0
commit fb8fb95
Showing
10 changed files
with
4,654 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
# Created by https://www.gitignore.io/api/node,macos,visualstudiocode,linux | ||
# Edit at https://www.gitignore.io/?templates=node,macos,visualstudiocode,linux | ||
|
||
### Linux ### | ||
*~ | ||
|
||
# temporary files which can be created if a process still has a handle open of a deleted file | ||
.fuse_hidden* | ||
|
||
# KDE directory preferences | ||
.directory | ||
|
||
# Linux trash folder which might appear on any partition or disk | ||
.Trash-* | ||
|
||
# .nfs files are created when an open file is removed but is still being accessed | ||
.nfs* | ||
|
||
### macOS ### | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### Node ### | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
|
||
# next.js build output | ||
.next | ||
|
||
# nuxt.js build output | ||
.nuxt | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# Serverless directories | ||
.serverless/ | ||
|
||
# FuseBox cache | ||
.fusebox/ | ||
|
||
#DynamoDB Local files | ||
.dynamodb/ | ||
|
||
### VisualStudioCode ### | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
### VisualStudioCode Patch ### | ||
# Ignore all local history of files | ||
.history | ||
|
||
# End of https://www.gitignore.io/api/node,macos,visualstudiocode,linux | ||
|
||
.functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// const axios = require("axios"); | ||
const Octokit = require("@octokit/rest"); | ||
|
||
// const { PersonalInformationLink, PersonalSocialLink } = require("../lib/link"); | ||
const { AnalysicEvent, defaultUser, defaultUsers } = require("../lib/analysic"); | ||
const { QueryContent, TransformResult, MultipleQueryResult } = require("../lib/content"); | ||
|
||
const query = async (octokit, key, user) => { | ||
const raw = await QueryContent(octokit, user, key); | ||
const results = TransformResult(raw); | ||
|
||
const json = {}; | ||
json[key] = await MultipleQueryResult(results); | ||
return json; | ||
}; | ||
|
||
exports.handler = function(event, context, callback) { | ||
const octokit = new Octokit({ | ||
headers: { authorization: event.headers.authorization } | ||
}); | ||
|
||
const params = AnalysicEvent(event, defaultUser, defaultUsers, "projects", [ | ||
"educations", | ||
"interests", | ||
"languages", | ||
"projects", | ||
"references", | ||
"skills", | ||
"volunteers", | ||
"works" | ||
]); | ||
|
||
(async () => { | ||
// TODO: implement performance here | ||
const result = await Promise.all(params.types.map(async type => await query(octokit, type, params.user))); | ||
return result.reduce((p, c) => { | ||
const key = Object.keys(c)[0]; | ||
p[key] = c[key]; | ||
return p; | ||
}, {}); | ||
})() | ||
.then(v => { | ||
callback(undefined, { | ||
statusCode: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(v) | ||
}); | ||
}) | ||
.catch(callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const axios = require("axios"); | ||
|
||
const { PersonalInformationLink, PersonalSocialLink } = require("../lib/link"); | ||
const { AnalysicEvent, defaultUser, defaultUsers } = require("../lib/analysic"); | ||
|
||
/** | ||
* @api {get} /user/:id Request User information | ||
* @apiName GetUser | ||
* @apiGroup User | ||
* | ||
* @apiParam {Number} id Users unique ID. | ||
* | ||
* @apiSuccess {String} firstname Firstname of the User. | ||
* @apiSuccess {String} lastname Lastname of the User. | ||
*/ | ||
exports.handler = function(event, _, callback) { | ||
const result = AnalysicEvent(event, defaultUser, defaultUsers, "information", ["information", "social"]); | ||
console.log(result); | ||
|
||
const url = | ||
result.type === "social" | ||
? PersonalSocialLink(result.user, { branch: result.branch }) | ||
: PersonalInformationLink(result.user, { branch: result.branch }); | ||
|
||
axios | ||
.get(url) | ||
.then(result => { | ||
callback(undefined, { | ||
statusCode: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(result.data) | ||
}); | ||
}) | ||
.catch(callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const defaultUser = "net"; | ||
export const defaultUsers = ["net", "prang"]; | ||
|
||
export const AnalysicEvent = (event, defaultUser, users, defaultPath, paths, defaultBranch = "master") => { | ||
let user = event.queryStringParameters.user || defaultUser; | ||
let branch = event.queryStringParameters.branch || defaultBranch; | ||
let type = event.queryStringParameters.type || defaultPath; | ||
let types = [type]; | ||
|
||
const userIndex = users.findIndex(u => event.path.includes(`/${u}`)); | ||
if (userIndex >= 0) user = users[userIndex]; | ||
|
||
if (defaultPath) { | ||
const matches = paths.filter(p => event.path.includes(`/${p}`)); | ||
if (matches && matches.length > 0) { | ||
type = matches[0]; | ||
types = matches; | ||
} | ||
} | ||
|
||
return { | ||
user: user, | ||
type: type, | ||
types: types, | ||
branch: branch | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const axios = require("axios"); | ||
|
||
const { Information } = require("../lib/link"); | ||
|
||
// return promise | ||
export const QueryContent = (octokit, username, filename) => { | ||
const path = `static/resources/${username}/${filename}`; | ||
|
||
return octokit.repos.getContents({ | ||
owner: Information.owner, | ||
repo: Information.repo, | ||
path, | ||
ref: Information.branch | ||
}); | ||
}; | ||
|
||
export const TransformResult = result => { | ||
const data = result.data || result; | ||
|
||
return data.reduce((p, c) => { | ||
if (c.name.includes(".json")) p.push(c.download_url); | ||
return p; | ||
}, []); | ||
}; | ||
|
||
export const MultipleQueryResult = results => { | ||
// TODO: implement performance here | ||
return Promise.all(results.map(r => axios.get(r).then(v => v.data))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[build] | ||
functions = "apis" | ||
functions = ".functions" | ||
[build.environment] | ||
YARN_VERSION = "1.3.2" | ||
YARN_FLAGS = "--no-ignore-optional" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.