如何从AWS Api网关获取根path“/”资源?

我一直在尝试使用NodeJs AWS SDK从AWS Api Gateway的path“/”(root)检索资源。 我知道天真的解决办法就是这样做:

var AWS = require('aws-sdk'); var __ = require('lodash'); var Promise = require('bluebird'); var resources = []; var apiGateway = Promise.promisifyAll(new AWS.APIGateway({apiVersion: '2015-07-09', region: 'us-west-2'})); var _finishRetrievingResources = function (resources) { var orderedResources = __.sortBy(resources, function (res) { return res.path.split('/').length; }); var firstResource = orderedResources[0]; }; var _retrieveNextPage = function (resp) { resources = resources.concat(resp.data.items); if (resp.hasNextPage()) { resp.nextPage().on('success', _retrieveNextPage).send(); } else { _finishRetrievingResources(resources); } }; var foo = apiGateway.getResources({restApiId: 'mah_rest_api_id'}).on('success', _retrieveNextPage).send(); 

但是,有人知道另一种方法吗? 我宁愿知道,我将永远不得不做最多一个电话,而不是做多个。

PS:我知道有几个优化可以做(例如检查每个响应的根path),我真的想知道是否有一个SDK调用可以解决这个问题。

没有一个调用,但是如果你有less于500个资源。 作为一个安慰奖,这是最好的做法,如果有超过500个资源,用position来防止意外的失误。 如果资源less于500个,则只能使用一个呼叫:

https://github.com/andrew-templeton/cfn-api-gateway-restapi/blob/bd964408bcb4bc6fc8ec91b5e1f0387c8f11691a/index.js#L77-L102