-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update api and configure supported environment variables
- Loading branch information
1 parent
b25a649
commit daf124e
Showing
7 changed files
with
25 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
NEXT_PUBLIC_REGION | ||
NEXT_PUBLIC_USER_POOL_ID | ||
NEXT_PUBLIC_IDENTITY_POOL_ID | ||
NEXT_PUBLIC_USER_POOL_CLIENT_ID | ||
NEXT_PUBLIC_MULTI_TENANT_TABLE_NAME | ||
NEXT_PUBLIC_API_URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,34 @@ | ||
import { SignatureV4 } from '@aws-sdk/signature-v4' | ||
import { Sha256 } from '@aws-crypto/sha256-js' | ||
import { HttpRequest } from '@aws-sdk/protocol-http' | ||
import { REGION } from './base' | ||
import { cognitoCredentialProvider } from './cognito' | ||
import * as aws4 from 'aws4' | ||
|
||
const apiSigner = (idToken: string) => | ||
new SignatureV4({ | ||
region: REGION, | ||
credentials: cognitoCredentialProvider(idToken), | ||
service: 'execute-api', | ||
sha256: Sha256, | ||
}) | ||
|
||
export const awsApiGatewayRequest = async <T>( | ||
path: string, | ||
idToken: string | ||
): Promise<T> => { | ||
const url = new URL(String(process.env.NEXT_PUBLIC_API_URL)) | ||
|
||
// TODO: Getting temporary credentials for every API call should not be | ||
// necessary. We should get from cache or refresh. | ||
// | ||
// Potentially just use Amplify API support to avoid this stuff. | ||
const credentials = cognitoCredentialProvider(idToken) | ||
const creds = await credentials() | ||
console.info('Credentials', creds) | ||
|
||
const opts = { | ||
path, | ||
method: 'GET', | ||
hostname: '027zjwioj3.execute-api.eu-west-2.amazonaws.com', | ||
protocol: 'https', | ||
hostname: url.host, | ||
protocol: url.protocol, | ||
body: '', | ||
headers: {}, | ||
} | ||
|
||
aws4.sign(opts, creds) | ||
|
||
console.info('Signed opts', opts) | ||
|
||
const signer = apiSigner(idToken) | ||
|
||
console.info('Signer', signer) | ||
|
||
const request = await signer.sign( | ||
new HttpRequest({ | ||
path, | ||
method: 'GET', | ||
hostname: '027zjwioj3.execute-api.eu-west-2.amazonaws.com', | ||
protocol: 'https', | ||
body: '', | ||
}) | ||
) | ||
|
||
console.info('Signed aws request', request) | ||
|
||
return fetch( | ||
`https://027zjwioj3.execute-api.eu-west-2.amazonaws.com${path}`, | ||
{ | ||
headers: opts.headers, | ||
} | ||
).then(async (response) => { | ||
console.info(response) | ||
return fetch(`${url.origin}${path}`, { | ||
headers: opts.headers, | ||
}).then(async (response) => { | ||
const json = await response.json() | ||
console.info(json) | ||
return json | ||
}) | ||
} |