aws-sigv4-fetch
Version:
SignatureV4 fetch function implemented with the official AWS SDK
46 lines (43 loc) • 2.01 kB
text/typescript
import { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@aws-sdk/types';
type SignedFetcherOptions = {
/**
* The AWS service to sign requests for, e.g. `execute-api` for AWS API Gateway or `lambda` for Lambda Function URLs.
*/
service: string;
/**
* The AWS region to sign requests for, e.g. `us-east-1` or `eu-west-2`.
* Defaults to `us-east-1` if not provided.
*/
region?: string;
/**
* The AWS credentials to use when signing requests.
* Optional in Node.js environments where they will be retrieved from the environment using [`@aws-sdk/credential-provider-node`](https://www.npmjs.com/package/@aws-sdk/credential-provider-node).
* In browser environments, credentials are **required** and must be provided explicitly.
*
* @see {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-types/Interface/AwsCredentialIdentity/ | AwsCredentialIdentity}
* @see {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-types/Interface/AwsCredentialIdentityProvider/ | AwsCredentialIdentityProvider}
*/
credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider;
/**
* The `fetch` function to use when signing requests.
* Defaults to the native `fetch` function.
*/
fetch?: typeof fetch;
};
type CreateSignedFetcher = (init: SignedFetcherOptions) => typeof fetch;
/**
* Creates a signed fetcher function that automatically signs requests using AWS Signature V4.
*
* @example
* ```ts
* const signedFetcher = createSignedFetcher({ service: 'lambda', region: 'eu-west-1' });
*
* const response = await signedFetcher('https://mylambda.lambda-url.eu-west-1.on.aws/', {
* method: 'POST',
* body: JSON.stringify({ a: 1 }),
* headers: { 'Content-Type': 'application/json' }
* });
* ```
*/
declare const createSignedFetcher: CreateSignedFetcher;
export { type CreateSignedFetcher, type SignedFetcherOptions, createSignedFetcher };