Skip to content

Commit

Permalink
Merge 03cca9f into 815e516
Browse files Browse the repository at this point in the history
  • Loading branch information
skeemer committed Oct 19, 2019
2 parents 815e516 + 03cca9f commit 12b0f5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ In addition to the required `AIRTABLE_API_KEY` and `AIRTABLE_API_BASE_ID` variab
- `AIRTABLE_API_VERSION` - Defaults to `v0`.
- `PROXY_PREFIX` - Use this if your Cloudflare worker's routes are prefixed by something before the Airtable resource name. For example, you may want to call `mycustomdomain.com/api/posts` instead of `mycustomdomain.com/posts`. In this example, you would add `api` as a prefix.
- `ALLOWED_TARGETS` - Use this to lock down your Airtable API to specific resources and methods. For example, a stringified JSON object like this: `'[{"resource":"posts","method":"GET,PUT"},{"resource":"comments","method":"*"}]'` will allow `GET` and `PUT` requests on the `posts` resource and all request methods on the `comments` resource. Allows all methods for all resources by default.
- `PROXY_CACHE_TIME` - Defaults to `0`. The number of seconds set on the `Cache-Control` header to use Cloudflare's caching.


## Contributing
Expand Down
16 changes: 15 additions & 1 deletion src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,28 @@ export async function handleRequest(req) {
});
}

return fetch(target.airtableRequestUrl, {
const response = await fetch(target.airtableRequestUrl, {
headers: {
Authorization: `Bearer ${config.airtableApiKey}`,
"Content-type": "application/json"
},
method: method,
body: req.body
});

const body = await response.body;

const headers = new Headers();
for (const kv of response.headers.entries()) {
headers.append(kv[0], kv[1]);
}
headers.set("Cache-Control", "max-age=" + config.cacheTime);

return new Response(body, {
status: response.status,
statusText: response.statusText,
headers: headers
});
} catch (e) {
console.error(e);

Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
airtableBaseId: JSON.stringify(process.env.AIRTABLE_API_BASE_ID || ''),
airtableApiVersion: JSON.stringify(process.env.AIRTABLE_API_VERSION || 'v0'),
airtableApiKey: JSON.stringify(process.env.AIRTABLE_API_KEY || ''),
cacheTime: JSON.stringify(process.env.PROXY_CACHE_TIME || 0),
prefix: JSON.stringify(process.env.PROXY_PREFIX || ''),
allowedTargets: JSON.stringify(process.env.ALLOWED_TARGETS || '*'),
},
Expand Down

0 comments on commit 12b0f5a

Please sign in to comment.