Skip to content

Commit f26a17b

Browse files
committed
bring in the deployment files
1 parent 72672e6 commit f26a17b

File tree

6 files changed

+3664
-0
lines changed

6 files changed

+3664
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_dist/
2+
_functions/
3+
node_modules/

build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
set -x
6+
7+
yarn install
8+
yarn run netlify-lambda build functions
9+
10+
poetry install --no-dev
11+
poetry run ditto transform --base-url='https://pokeapi-prod.netlify.com/' --dest-dir='_dist'

functions/resource-list.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import getJson from 'get-json'
2+
3+
const baseUrl = "https://pokeapi-prod.netlify.com";
4+
5+
function targetUrlForPath(path) {
6+
let target = baseUrl;
7+
target += path;
8+
if (!target.endsWith("/")) {
9+
target += "/";
10+
}
11+
target += "index.json";
12+
return target;
13+
}
14+
15+
function extractParams(query) {
16+
let defaults = {offset: 0, limit: 20};
17+
return {
18+
offset: parseInt(query.offset) || 0,
19+
limit: parseInt(query.limit) || 20,
20+
}
21+
}
22+
23+
function getPageUrl(path, params) {
24+
if (params == null) {
25+
return null;
26+
}
27+
return baseUrl + path + "?offset=" + params.offset + "&limit=" + params.limit;
28+
}
29+
30+
function getPreviousPage(params) {
31+
let newPage = {
32+
begin: params.offset - params.limit,
33+
end: params.offset,
34+
}
35+
36+
if (newPage.begin < 0) {
37+
newPage.begin = 0;
38+
}
39+
40+
// it's a prev page only if we've moved back
41+
if (newPage.begin < params.offset) {
42+
return {
43+
offset: newPage.begin,
44+
limit: newPage.end - newPage.begin,
45+
};
46+
}
47+
48+
return null;
49+
}
50+
51+
function getNextPage(params, count) {
52+
let newPage = {
53+
begin: params.offset + params.limit,
54+
end: params.offset + params.limit * 2,
55+
}
56+
57+
if (newPage.end > count) {
58+
newPage.end = count;
59+
}
60+
61+
// it's a next page only if we've moved forward
62+
if (newPage.end > params.offset + params.limit) {
63+
return {
64+
offset: newPage.begin,
65+
limit: newPage.end - newPage.begin,
66+
}
67+
}
68+
69+
return null;
70+
}
71+
72+
exports.handler = (event, context, callback) => {
73+
if (event.httpMethod !== "GET") {
74+
callback(null, {statusCode: 405, body: event.httpMethod + " not allowed"});
75+
return;
76+
}
77+
78+
let path = "/api/v2/" + event.queryStringParameters.endpoint + "/";
79+
80+
if (!path) {
81+
callback(null, {statusCode: 400, body: "path must not be empty"});
82+
return;
83+
}
84+
85+
let url = targetUrlForPath(path);
86+
87+
getJson(url, (error, response) => {
88+
if (error) {
89+
callback(null, {statusCode: 400, body: "path must be a valid resource list"});
90+
return;
91+
}
92+
93+
let params = extractParams(event.queryStringParameters);
94+
let resultSlice = response.results.slice(params.offset, params.offset + params.limit);
95+
let finalResponse = Object.assign(response, {
96+
next: getPageUrl(path, getNextPage(params, response.count)),
97+
previous: getPageUrl(path, getPreviousPage(params)),
98+
results: resultSlice,
99+
});
100+
101+
callback(null, {statusCode: 200, body: JSON.stringify(finalResponse, null, 4)})
102+
});
103+
};

netlify.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[build]
2+
publish = "_dist"
3+
command = "pip install poetry && bash build.sh"
4+
functions = "_functions"
5+
6+
[[redirects]]
7+
from = "/api/v2/"
8+
to = "/api/v2/index.json"
9+
status = 200
10+
11+
[[redirects]]
12+
from = "/api/v2/:endpoint/"
13+
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset&limit=:limit"
14+
query = {offset = ":offset", limit = ":limit"}
15+
status = 200
16+
17+
[[redirects]]
18+
from = "/api/v2/:endpoint/"
19+
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&offset=:offset"
20+
query = {offset = ":offset"}
21+
status = 200
22+
23+
[[redirects]]
24+
from = "/api/v2/:endpoint/"
25+
to = "/.netlify/functions/resource-list/?endpoint=:endpoint&limit=:limit"
26+
query = {limit = ":limit"}
27+
status = 200
28+
29+
[[redirects]]
30+
from = "/api/v2/:endpoint/"
31+
to = "/.netlify/functions/resource-list/?endpoint=:endpoint"
32+
status = 200
33+
34+
[[redirects]]
35+
from = "/api/v2/:endpoint/:id/"
36+
to = "/api/v2/:endpoint/:id/index.json"
37+
status = 200
38+
39+
[[redirects]]
40+
from = "/api/v2/:endpoint/:id/:extra"
41+
to = "/api/v2/:endpoint/:id/:extra/index.json"
42+
status = 200

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"devDependencies": {
3+
"@babel/core": "^7.0.1",
4+
"babel-loader": "^8.0.2",
5+
"babel-plugin-transform-class-properties": "^6.24.1",
6+
"babel-plugin-transform-object-assign": "^6.22.0",
7+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
8+
"babel-preset-env": "^1.7.0",
9+
"get-json": "^1.0.0",
10+
"netlify-lambda": "^0.4.0"
11+
},
12+
"dependencies": {}
13+
}

0 commit comments

Comments
 (0)