typescript - 如何在 typescript nodejs 项目中包含生成的 api 网关 sdk

标签 typescript aws-api-gateway

我已经创建了一个 API 网关 API,我想在我用 typescript 编写的 nodejs 项目中使用它。为此,我想包含生成的 SDK。

生成的 SDK 由不导出任何内容的 js 文件组成。例如主文件“apigClient”基本上是这样的:

var apigClientFactory = {};
apigClientFactory.newClient = function (config) {
...
}

要在 javascript 项目中使用它,必须这样做:

<script type="text/javascript" src="apigClient.js"></script>

如何在用 typescript 编写的 nodejs 项目中做同样的事情?

最佳答案

我遇到了同样的问题。

SDK 不适用于 Node。为了在 Node 上使用 SDK,您必须将代码更改为模块化。

我找到了一个允许我通过节点与 API 网关通信的库:aws-api-gateway-client

NPM:https://www.npmjs.com/package/aws-api-gateway-client 存储库:https://github.com/kndt84/aws-api-gateway-client

但是对于这个库,你必须自己实现请求。改编自那里的自述文件:

// Require module
var apigClientFactory = require('aws-api-gateway-client').default;
// ES6:
// import apigClientFactory from 'aws-api-gateway-client';
// Set invokeUrl to config and create a client.
// For authorization, additional information is 
// required as explained below.

// you can find the following url on 
// AWS Console/Gateway in the API Gateway Stage session
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 params = {
    //This is where any header, path, or querystring 
    // request params go. The key is the parameter 
    // named as defined in the API
    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 any unmodeled 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(params, 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
    });

希望对您有所帮助。

关于typescript - 如何在 typescript nodejs 项目中包含生成的 api 网关 sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45739180/

相关文章:

angular - 可配置的 Angular 9 模块

aws-api-gateway - API 网关 HTTP 代理与 aws-sam 集成(不是 Lambda 代理)

aws-api-gateway - AWS API网关的安全性

node.js - 如何在 API Gateway AWS 的 Lambda/Node JS 中添加 CORS header

javascript - Typescript reducer 问题

typescript - 在带有初始化值的 Typescript 中使用 Map 构造函数

amazon-web-services - 如何将 Amazon AWS API Gateway 连接到 Amazon RDS?

swift - AWS APIGateway 和 Lambda - 如何在 iOS 应用程序终止之前立即调用函数?

并非所有属性都匹配时不显示 typescript 类型检查错误

typescript - 函数的书写类型冗余