Skip to content

Commit

Permalink
Update example code to use latest deps
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart committed Jul 20, 2024
1 parent 12db98b commit 0846064
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 336 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
example/worker.js
package-lock.json
.wrangler
1 change: 1 addition & 0 deletions example/.dev.vars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AWS_SECRET_ACCESS_KEY = "EXAMPLEKEY..."
26 changes: 17 additions & 9 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@ The relevant portions of the code are the import/setup:
```js
import { AwsClient } from 'aws4fetch'

// Assume AWS_* vars have been uploaded via `npx wrangler secret put ...`
// https://developers.cloudflare.com/workers/configuration/secrets/
const aws = new AwsClient({ accessKeyId: AWS_ACCESS_KEY_ID, secretAccessKey: AWS_SECRET_ACCESS_KEY })
// ...

const AWS_REGION = 'us-east-1'
const LAMBDA_FN = 'my-api-function'
// Assume AWS_* vars have added to your environment
// https://developers.cloudflare.com/workers/reference/apis/environment-variables/#secrets
aws = new AwsClient({
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
})

// https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
const LAMBDA_INVOKE_URL = `https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/${LAMBDA_FN}/invocations`

// https://docs.aws.amazon.com/lambda/latest/api/API_Invoke.html
const LAMBDA_INVOKE_URL = `https://lambda.${AWS_REGION}.amazonaws.com/2015-03-31/functions/${LAMBDA_FN}/invocations`
const lambdaResponse = await aws.fetch(LAMBDA_INVOKE_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(await toLambdaEvent(request)),
})
```

The conversion from the incoming [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
to an API-Gateway-style Lambda event:

```js
async function toLambdaEvent(request) {
const url = new URL(request.url);
const url = new URL(request.url)
return {
httpMethod: request.method,
path: url.pathname,
Expand Down Expand Up @@ -54,7 +62,7 @@ if (!lambdaResponse.ok) {

const { statusCode: status, headers, body } = await lambdaResponse.json()

const response = new Response(body, { status, headers })
return new Response(body, { status, headers })
```

## Deploying to Cloudflare
Expand Down
Loading

0 comments on commit 0846064

Please sign in to comment.