|
| 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 | +}; |
0 commit comments