Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
_dist/
_functions/

node_modules/

.idea/

*.py[cod]
Expand Down
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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.netlify.com/' --dest-dir='_dist'
103 changes: 103 additions & 0 deletions functions/resource-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import getJson from 'get-json'

const baseUrl = "https://pokeapi.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)})
});
};
22 changes: 10 additions & 12 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
[Settings]
ID = "pokeapi"

[build]
publish = "dist"
command = "pip install poetry && poetry install --no-dev && poetry run ditto transform --base-url='https://pokeapi.nelify.com/'"
publish = "_dist"
command = "pip install poetry && bash build.sh"
functions = "_functions"

[[redirects]]
from = "/api/v2/"
Expand All @@ -12,25 +10,25 @@ status = 200

[[redirects]]
from = "/api/v2/:endpoint/"
to = "https://pokeapi.co/api/v2/:endpoint/?limit=:limit&offset=:offset"
status = 200
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset&limit=:limit"
query = {offset = ":offset", limit = ":limit"}
status = 200

[[redirects]]
from = "/api/v2/:endpoint/"
to = "https://pokeapi.co/api/v2/:endpoint/?offset=:offset"
status = 200
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset"
query = {offset = ":offset"}
status = 200

[[redirects]]
from = "/api/v2/:endpoint/"
to = "https://pokeapi.co/api/v2/:endpoint/?limit=:limit"
status = 200
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&limit=:limit"
query = {limit = ":limit"}
status = 200

[[redirects]]
from = "/api/v2/:endpoint/"
to = "/api/v2/:endpoint/index.json"
to = "/.netlify/functions/resource-list/?endpoint=:endpoint"
status = 200

[[redirects]]
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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