简体   繁体   English

使用SwaggerJS创建API工厂

[英]Creating API factory with SwaggerJS

So I have this long standing APIService factory that creates functions to pass through the swagger functions to the UI. 因此,我拥有这个由来已久的APIService工厂,该工厂创建函数以通过swagger函数传递给UI。 Here's a snippet of the factory: 这是工厂的片段:

'use strict';

angular.module('myApp').factory('APIService', function ($http, $window, $q, swaggerClient, $mdToast) {
    var ApiDoc = {};

    ApiDoc.getAllBookmarks = function () {
        return $q(function (resolve, reject) {
            $http.get('client/components/api/Schema.json')
                    .success(function (data) {
                        var schema = data;
                        _.each(schema.apis, function (b) {
                            b.apiDeclaration.basePath = $window.location.origin;
                        })

                        var api = swaggerClient(schema);
                        api = api.apiBookmarks.getAll();
                        resolve(api);
                     });
        });
    }
    return ApiDoc;
});

And here is a snippet of it's use case in a controller: 以下是控制器中的用例片段:

$scope.getAllDashboards = function () {
    APIService.getAllBookmarks().then(function(data){
        if (data.length > 0){
           $scope.dashboardsList = data;
           $scope.emptyDash = false;
        } else {
           $scope.emptyDash = true;
        }
    })
}

$scope.getAllDashboards();

The inherent problem herein, is that if I have 30 API function calls in a controller, then there are 30 $http requests for schema.json that are un-needed really. 这里的固有问题是,如果我在控制器中有30个API函数调用,那么实际上不需要30个对schema.json的$ http请求。 Problem is that I can't figure out how to request/store that json and call on the functions with swagger the same way as they are now (or else I have to change 200+ methods in controllers, urgh). 问题是我无法弄清楚如何请求/存储该json并以与现在相同的方式昂首阔步地调用函数(否则我必须在控制器中更改200多个方法,urgh)。 I tried this: 我尝试了这个:

// var api = null;
// $http.get('client/components/api/Schema.json')
//     .success(function (data) {
//         var schema = data;
//         _.each(schema.apis, function (b) {
//             b.apiDeclaration.basePath = $window.location.origin;
//         })
//         api = swaggerClient(schema);
//     });

But couldn't get a function after that to read it properly, or return the result of the function call in a promise like the controllers expect. 但是之后无法获得功能以正确读取它,或者无法像控制器期望的那样以承诺的形式返回函数调用的结果。

I have no other JS developers here so I need help from you all! 我这里没有其他JS开发人员,因此我需要大家的帮助! Thanks much! 非常感谢!

That is ugly. 太丑了 If you upgrade your swagger-client to something more modern, you have some options. 如果您将swagger-client升级到更现代的版本,则可以有一些选择。

First off, you can cache your schema as an object, and supply it in your swaggerClient constructor using the argument spec . 首先,您可以将架构作为对象进行缓存,并使用参数spec将其提供给swaggerClient构造函数。 You'll still need to pass the URL of the target host to the client when constructing it. 构造目标主机时,仍然需要将目标主机的URL传递给客户端。 With that, there won't need to be any need to call anything remotely. 这样,就不需要远程调用任何东西了。

Next, you can see about keeping a proper swaggerClient instance around, and use it in each of your calls. 接下来,您将看到有关保留适当的swaggerClient实例的信息,并在每个调用中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM