@adastradev/aws-api-gateway-client 中文文档教程
Overview
基于自动生成的 JavaScript SDK 的 AWS API 网关客户端模块。 这个模块不仅可以用于 Node.js,也可以用于前端。 此外,它概括了原始 SDK 的端点特定方法。
参考资料:
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html
Prerequisites
要使 JavaScript SDK 正常工作,您的 API 需要支持 CORS。 Amazon API Gateway 开发人员指南解释了如何为端点设置 CORS .
Install
npm install @adastradev/aws-api-gateway-client
Use the SDK in your project
需要模块
var apigClientFactory = require('aws-api-gateway-client').default;
将 invokeUrl 设置为配置并创建客户端。 对于授权,需要如下所述的额外信息。
config = {invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com'}
var apigClient = apigClientFactory.newClient(config);
对 API 的调用采用下面概述的形式。 每个 API 调用都会返回一个承诺,该承诺会调用成功或失败回调
var pathParams = {
//This is where path request params go.
userId: '1234',
};
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = '/users/{userID}/profile'
var method = 'GET';
var additionalParams = {
//If there are query parameters or headers that need to be sent with the request you can add them here
headers: {
param0: '',
param1: ''
},
queryParams: {
param0: '',
param1: ''
}
};
var body = {
//This is where you define the body of the request
};
apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
.then(function(result){
//This is where you would put a success callback
}).catch( function(result){
//This is where you would put an error callback
});
Using AWS IAM for authorization
要使用 AWS 凭证初始化 SDK,请使用以下代码。 请注意,如果您使用凭据,则对 API 的所有请求都将被签名。 这意味着您必须为每个请求设置适当的 CORS accept-* 标头。
var apigClient = apigClientFactory.newClient({
invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com', // REQUIRED
region: 'eu-west-1', // REQUIRED: The region where the API is deployed.
accessKey: 'ACCESS_KEY', // REQUIRED
secretKey: 'SECRET_KEY', // REQUIRED
sessionToken: 'SESSION_TOKEN', // OPTIONAL: If you are using temporary credentials
you must include the session token.
systemClockOffset: 0, // OPTIONAL: An offset value in milliseconds to apply to signing time
retries: 4, // OPTIONAL: Number of times to retry before failing. Uses axios-retry plugin.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried.
return err.response && err.response.status === 500; // Uses axios-retry plugin.
},
retryDelay: 100 || 'exponential' || (retryCount, error) => { // OPTIONAL: Define delay (in ms) as a number, a callback, or
return retryCount * 100 // 'exponential' to use the in-built exponential backoff
} // function. Uses axios-retry plugin. Default is no delay.
});
Using API Keys
要在客户端 SDK 中使用 API 密钥,您可以将密钥作为参数传递给工厂对象。 请注意,如果您使用 apiKey,它将作为标头“x-api-key”附加到将对 API 的所有请求进行签名。 这意味着您必须为每个请求设置适当的 CORS accept-* 标头。
var apigClient = apigClientFactory.newClient({
invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com', // REQUIRED
apiKey: 'API_KEY', // REQUIRED
region: 'eu-west-1' // REQUIRED
});
Overview
A module for AWS API gateway client based on auto-generated JavaScript SDK. This module can be used not only for Node.js but for front-end. In addition, it generalizes original SDK's endpoint specific methods.
Reference:
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html
Prerequisites
For the JavaScript SDK to work your APIs need to support CORS. The Amazon API Gateway developer guide explains how to setup CORS for an endpoint.
Install
npm install @adastradev/aws-api-gateway-client
Use the SDK in your project
Require module
var apigClientFactory = require('aws-api-gateway-client').default;
Set invokeUrl to config and create a client. For authorization, additional information is required as explained below.
config = {invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com'}
var apigClient = apigClientFactory.newClient(config);
Calls to an API take the form outlined below. Each API call returns a promise, that invokes either a success and failure callback
var pathParams = {
//This is where path request params go.
userId: '1234',
};
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = '/users/{userID}/profile'
var method = 'GET';
var additionalParams = {
//If there are query parameters or headers that need to be sent with the request you can add them here
headers: {
param0: '',
param1: ''
},
queryParams: {
param0: '',
param1: ''
}
};
var body = {
//This is where you define the body of the request
};
apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
.then(function(result){
//This is where you would put a success callback
}).catch( function(result){
//This is where you would put an error callback
});
Using AWS IAM for authorization
To initialize the SDK with AWS Credentials use the code below. Note, if you use credentials all requests to the API will be signed. This means you will have to set the appropiate CORS accept-* headers for each request.
var apigClient = apigClientFactory.newClient({
invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com', // REQUIRED
region: 'eu-west-1', // REQUIRED: The region where the API is deployed.
accessKey: 'ACCESS_KEY', // REQUIRED
secretKey: 'SECRET_KEY', // REQUIRED
sessionToken: 'SESSION_TOKEN', // OPTIONAL: If you are using temporary credentials
you must include the session token.
systemClockOffset: 0, // OPTIONAL: An offset value in milliseconds to apply to signing time
retries: 4, // OPTIONAL: Number of times to retry before failing. Uses axios-retry plugin.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried.
return err.response && err.response.status === 500; // Uses axios-retry plugin.
},
retryDelay: 100 || 'exponential' || (retryCount, error) => { // OPTIONAL: Define delay (in ms) as a number, a callback, or
return retryCount * 100 // 'exponential' to use the in-built exponential backoff
} // function. Uses axios-retry plugin. Default is no delay.
});
Using API Keys
To use an API Key with the client SDK you can pass the key as a parameter to the Factory object. Note, if you use an apiKey it will be attached as the header 'x-api-key' to all requests to the API will be signed. This means you will have to set the appropiate CORS accept-* headers for each request.
var apigClient = apigClientFactory.newClient({
invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com', // REQUIRED
apiKey: 'API_KEY', // REQUIRED
region: 'eu-west-1' // REQUIRED
});