mirror of
https://github.com/PokeAPI/api-data.git
synced 2024-11-22 15:42:30 +01:00
bring in the deployment files
This commit is contained in:
parent
72672e63b1
commit
f26a17bf29
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
_dist/
|
||||
_functions/
|
||||
node_modules/
|
11
build.sh
Normal file
11
build.sh
Normal file
@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -o pipefail
|
||||
set -x
|
||||
|
||||
yarn install
|
||||
yarn run netlify-lambda build functions
|
||||
|
||||
poetry install --no-dev
|
||||
poetry run ditto transform --base-url='https://pokeapi-prod.netlify.com/' --dest-dir='_dist'
|
103
functions/resource-list.js
Normal file
103
functions/resource-list.js
Normal file
@ -0,0 +1,103 @@
|
||||
import getJson from 'get-json'
|
||||
|
||||
const baseUrl = "https://pokeapi-prod.netlify.com";
|
||||
|
||||
function targetUrlForPath(path) {
|
||||
let target = baseUrl;
|
||||
target += path;
|
||||
if (!target.endsWith("/")) {
|
||||
target += "/";
|
||||
}
|
||||
target += "index.json";
|
||||
return target;
|
||||
}
|
||||
|
||||
function extractParams(query) {
|
||||
let defaults = {offset: 0, limit: 20};
|
||||
return {
|
||||
offset: parseInt(query.offset) || 0,
|
||||
limit: parseInt(query.limit) || 20,
|
||||
}
|
||||
}
|
||||
|
||||
function getPageUrl(path, params) {
|
||||
if (params == null) {
|
||||
return null;
|
||||
}
|
||||
return baseUrl + path + "?offset=" + params.offset + "&limit=" + params.limit;
|
||||
}
|
||||
|
||||
function getPreviousPage(params) {
|
||||
let newPage = {
|
||||
begin: params.offset - params.limit,
|
||||
end: params.offset,
|
||||
}
|
||||
|
||||
if (newPage.begin < 0) {
|
||||
newPage.begin = 0;
|
||||
}
|
||||
|
||||
// it's a prev page only if we've moved back
|
||||
if (newPage.begin < params.offset) {
|
||||
return {
|
||||
offset: newPage.begin,
|
||||
limit: newPage.end - newPage.begin,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getNextPage(params, count) {
|
||||
let newPage = {
|
||||
begin: params.offset + params.limit,
|
||||
end: params.offset + params.limit * 2,
|
||||
}
|
||||
|
||||
if (newPage.end > count) {
|
||||
newPage.end = count;
|
||||
}
|
||||
|
||||
// it's a next page only if we've moved forward
|
||||
if (newPage.end > params.offset + params.limit) {
|
||||
return {
|
||||
offset: newPage.begin,
|
||||
limit: newPage.end - newPage.begin,
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
exports.handler = (event, context, callback) => {
|
||||
if (event.httpMethod !== "GET") {
|
||||
callback(null, {statusCode: 405, body: event.httpMethod + " not allowed"});
|
||||
return;
|
||||
}
|
||||
|
||||
let path = "/api/v2/" + event.queryStringParameters.endpoint + "/";
|
||||
|
||||
if (!path) {
|
||||
callback(null, {statusCode: 400, body: "path must not be empty"});
|
||||
return;
|
||||
}
|
||||
|
||||
let url = targetUrlForPath(path);
|
||||
|
||||
getJson(url, (error, response) => {
|
||||
if (error) {
|
||||
callback(null, {statusCode: 400, body: "path must be a valid resource list"});
|
||||
return;
|
||||
}
|
||||
|
||||
let params = extractParams(event.queryStringParameters);
|
||||
let resultSlice = response.results.slice(params.offset, params.offset + params.limit);
|
||||
let finalResponse = Object.assign(response, {
|
||||
next: getPageUrl(path, getNextPage(params, response.count)),
|
||||
previous: getPageUrl(path, getPreviousPage(params)),
|
||||
results: resultSlice,
|
||||
});
|
||||
|
||||
callback(null, {statusCode: 200, body: JSON.stringify(finalResponse, null, 4)})
|
||||
});
|
||||
};
|
42
netlify.toml
Normal file
42
netlify.toml
Normal file
@ -0,0 +1,42 @@
|
||||
[build]
|
||||
publish = "_dist"
|
||||
command = "pip install poetry && bash build.sh"
|
||||
functions = "_functions"
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/"
|
||||
to = "/api/v2/index.json"
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/"
|
||||
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset&limit=:limit"
|
||||
query = {offset = ":offset", limit = ":limit"}
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/"
|
||||
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset"
|
||||
query = {offset = ":offset"}
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/"
|
||||
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&limit=:limit"
|
||||
query = {limit = ":limit"}
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/"
|
||||
to = "/.netlify/functions/resource-list/?endpoint=:endpoint"
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/:id/"
|
||||
to = "/api/v2/:endpoint/:id/index.json"
|
||||
status = 200
|
||||
|
||||
[[redirects]]
|
||||
from = "/api/v2/:endpoint/:id/:extra"
|
||||
to = "/api/v2/:endpoint/:id/:extra/index.json"
|
||||
status = 200
|
13
package.json
Normal file
13
package.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.0.1",
|
||||
"babel-loader": "^8.0.2",
|
||||
"babel-plugin-transform-class-properties": "^6.24.1",
|
||||
"babel-plugin-transform-object-assign": "^6.22.0",
|
||||
"babel-plugin-transform-object-rest-spread": "^6.26.0",
|
||||
"babel-preset-env": "^1.7.0",
|
||||
"get-json": "^1.0.0",
|
||||
"netlify-lambda": "^0.4.0"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user