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

Commit

Permalink
feat(package): update apis for package and apis version
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamontat Chantrachirathumrong committed Dec 28, 2018
1 parent d91b284 commit e25f73b
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 15 deletions.
10 changes: 8 additions & 2 deletions lib/ghLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ export const FetchUrl = (url, opts = {}) => {
* @param {String} path path from root folder
* @param {Object} opts options
* @param {String} opts.branch branch of result
* @param {String} opts.owner repo owner default: Information.owner
* @param {String} opts.repo name of the repo default: Information.repo
* @param {Boolean} opts.noPrefix remove `/static/resources` path
*/
export const GenerateGithubLink = (path, opts = {}) => {
const link = `https://raw.githubusercontent.com/${repo_owner}/${repo_name}/${opts.branch ||
branch}/static/resources/${path}`;
let prefixPath = '/static/resources'
if (opts.noPrefix) prefixPath = ''

const link = `https://raw.githubusercontent.com/${opts.owner || repo_owner}/${opts.repo || repo_name}/${opts.branch ||
branch}${prefixPath}/${path}`;
console.log(`query to: ${link}`);
return link
}
Expand Down
1 change: 1 addition & 0 deletions lib/parseUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ export const ParseParameters = (event, ...obj) => {
result[v.name] = matchPath(path, v.options) || matchQuery(query[v.name], v.options) || v.default
}
})
console.log(result);
return result
}
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,5 @@
"prepush": "yarn dump-version",
"push": "git push && git push --tag",
"deploy": "yarn docs:compile && yarn build"
},
"apidoc": {
"title": "kcnt content APIs",
"description": "This is a content APIs",
"url": "https://apis.kcnt.info/.netlify/functions",
"sampleUrl": "https://apis.kcnt.info/.netlify/functions"
}
}
1 change: 0 additions & 1 deletion src/apis/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ exports.handler = function (event, context, callback) {
"works"
], ["projects"]));

console.log(result);
const apis = new GHAPIs(event);

apis.queryAll(result.type, result).then(v => {
Expand Down
1 change: 0 additions & 1 deletion src/apis/logo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ exports.handler = function (event, context, callback) {
GenObj("type", ["normal", "round"]),
GenObj("extension", ["png", "svg"]))

console.log(result);
const url = LogoLink(result.color, result);

FetchUrl(url, {
Expand Down
66 changes: 66 additions & 0 deletions src/apis/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const {
ParseParameters,
GenObj
} = require('../../lib/parseUrl')

const {
FetchUrl,
GenerateGithubLink
} = require("../../lib/ghLink")

exports.handler = function (event, _, callback) {
const result = ParseParameters(event,
GenObj("type", ["all", "name", "description", "author", "version", "license", "dependencies", "devDependencies", "repository", "scripts"], ["all"]),
GenObj("format", ["json", "text", "text-head"]),
GenObj("repo", ["apis", "web", "docs"]),
GenObj("branch", ["master", "dev"]));

let repo = result.repo
if (repo === "web") repo = "website"
else if (repo === "docs") repo = "documents"

const url = GenerateGithubLink("package.json", {
repo,
branch: result.branch,
noPrefix: true
})

FetchUrl(url).then(pkg => {
// case all
if (result.type.includes("all")) return callback(undefined, {
statusCode: 200,
headers: {
"Content-Type": result.format === "json" ? "application/json" : "text/plain",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*"
},
body: JSON.stringify(pkg)
});

let response = result.type.reduce((p, c) => {
p[c] = pkg[c];
return p
}, {})

// transform to json
if (result.format === "text" || result.format === "text-head") {
response = Object.keys(response).reduce((p, key) => {
const v = result.format === "text" ? response[key] : `${key}: ${response[key]}`

if (p)
return `${p}\n${v}`
else return v
}, undefined)
}

callback(undefined, {
statusCode: 200,
headers: {
"Content-Type": result.format === "json" ? "application/json" : "text/plain",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*"
},
body: result.format === "json" ? JSON.stringify(response) : response
});
})
}
1 change: 0 additions & 1 deletion src/apis/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ exports.handler = function (event, _, callback) {
GenObj("lang", ["en", "th"]),
GenObj("type", ["information", "social"]));

console.log(result);
const url =
result.type === "social" ?
PersonalSocialLink(result.user, {
Expand Down
23 changes: 23 additions & 0 deletions src/apis/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pkg = require("../../package.json")

const {
ParseParameters,
GenObj
} = require('../../lib/parseUrl')

exports.handler = function (event, _, callback) {
const result = ParseParameters(event,
GenObj("format", ["json", "text"]))

callback(undefined, {
statusCode: 200,
headers: {
"Content-Type": result.format === "json" ? "application/json" : "text/plain",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*"
},
body: result.format === "json" ? JSON.stringify({
version: pkg.version
}) : pkg.version
});
}
102 changes: 98 additions & 4 deletions src/docs/index.apib
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ This APIs is a wrapper of Github APIs so if you query too much Github will block

# Group Images

## Logo collection [/logo{branch}{color}{size}{type}{extension}{?branch,color,size,type,extension}]
## Logo collection [/logo/{branch}/{color}/{size}/{type}/{extension}{?branch,color,size,type,extension}]

### get logo [GET]

Expand All @@ -183,7 +183,7 @@ This APIs is a wrapper of Github APIs so if you query too much Github will block
+ Default: `master`
+ Members
+ `master`
+ `dev
+ `dev`
+ color (optional, enum[string]) - color of the logo
+ Default: `primary`
+ Members
Expand Down Expand Up @@ -215,6 +215,100 @@ This APIs is a wrapper of Github APIs so if you query too much Github will block
+ `png`
+ `svg`

+ Response 200 (application/svg)
+ Response 200 (image/svg)

+ Response 200 (image/png)

# Group Repository

## Current version [/version{?format}]

### Get current version [GET]

+ Parameters
+ format (optional, enum[string]) - format of the response
+ Default: `json`
+ Members
+ `json`
+ `text`

+ Response 200 (application/json)

{
version: "2.0.0"
}

+ Response 200 (text/plain)

2.0.0

## Package JSON [/package/{type}/{format}/{repo}/{branch}{?type,format,repo,branch}]

### Get package.json information [GET]

+ Parameters
+ type (optional, enum[string]) - value type
+ Default: `all`
+ Members
+ `all`
+ `name`
+ `description`
+ `author`
+ `version`
+ `license`
+ `dependencies`
+ `devDependencies`
+ `repository`
+ `scripts`
+ format (optional, enum[string]) - format of the response
+ Default: `json`
+ Members
+ `json`
+ `text`
+ `text-head`
+ repo (optional, enum[string]) - repository of the package.json
+ Default: `apis`
+ Members
+ `apis`
+ `docs`
+ `web`
+ branch (optional, enum[string]) - branch to query the information
+ Default: `master`
+ Members
+ `master`
+ `dev`

+ Response 200 (application/json)

{
"name":"kcnt-info-apis",
"version":"2.0.0",
"description":"My Portfolio website",
"repository":"https://github.com/kcnt-info/apis.git",
"author":"Kamontat Chantrachirathumrong <kamontat_c@hotmail.com>",
"license":"MIT",
"dependencies":{
"@octokit/rest":"16.2.0",
"aglio":"2.3.0",
"axios":"0.18.0",
"bluebird":"3.5.3"
},
"devDependencies":{
"apidoc":"0.17.7",
"netlify-lambda":"1.1.1"
},
"scripts":{
"dump-version":"git tag \"$(node ./scripts/dump-version.js)\"",
"start":"netlify-lambda serve src/apis",
"build":"netlify-lambda build src/apis",
"docs:compile":"mkdir docs 2>/dev/null; aglio --theme-variables slate -i src/docs/index.apib -o docs/docs.html && cp _redirects docs/_redirects && cp 404.html docs/404.html",
"docs:start":"aglio --theme-variables slate -i src/docs/index.apib -o docs/docs.html -s",
"prepush":"yarn dump-version",
"push":"git push && git push --tag",
"deploy":"yarn docs:compile && yarn build"
}
}

+ Response 200 (text/plain)

+ Response 200 (application/png)
2.0.0

0 comments on commit e25f73b

Please sign in to comment.