2017-08-17 47 views
1

我已經創建了一個API網關API,並且想從打字稿中的nodejs項目中使用它。爲此,我想包含生成的SDK。如何在打字稿nodejs項目中包含生成的api網關sdk

生成的SDK由不導出任何內容的js文件組成。例如主文件「apigClient」基本上是這樣的:

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

要在JavaScript項目中使用這一點,就必須做到:

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

我該怎麼做同樣的事情在一個nodejs項目寫在打字稿?

回答

0

我遇到了同樣的問題。

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

然而與此庫,你將必須實現自己的要求。從那裏README改編:

// 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 
 
    });

我希望這有助於。

相關問題