Blame view

node_modules/swagger-client/README.md 10.1 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  # Swagger JS library
  
  [![Build Status](https://travis-ci.org/swagger-api/swagger-js.svg?branch=master)](https://travis-ci.org/swagger-api/swagger-js)
  [![NPM version](https://badge.fury.io/js/swagger-client.svg)](http://badge.fury.io/js/swagger-client)
  
  This is the Swagger javascript client for use with [swagger](http://swagger.io) enabled APIs.
  It's written in javascript and tested with mocha, and is the fastest way to enable a javascript client to communicate with a swagger-enabled server.
  
  Check out [Swagger-Spec](https://github.com/swagger-api/swagger-spec) for additional information about the Swagger project, including additional libraries with support for other languages and more.
  
  
  ### Calling an API with swagger + node.js!
  
  Install swagger-client:
  ```
  npm install swagger-client
  ```
  
  or:
  
  ```
  bower install swagger-js
  ```
  
  Then let swagger do the work!
  ```js
  var Swagger = require('swagger-client');
  
  var client = new Swagger({
    url: 'http://petstore.swagger.io/v2/swagger.json',
    success: function() {
      client.pet.getPetById({petId:7},{responseContentType: 'application/json'},function(pet){
        console.log('pet', pet);
      });
    }
  });
  ```
  
  NOTE: we're explicitly setting the responseContentType, because we don't want you getting stuck when 
  there is more than one content type available.
  
  That's it!  You'll get a JSON response with the default callback handler:
  
  ```json
  {
    "id": 1,
    "category": {
      "id": 2,
      "name": "Cats"
    },
    "name": "Cat 1",
    "photoUrls": [
      "url1",
      "url2"
    ],
    "tags": [
      {
        "id": 1,
        "name": "tag1"
      },
      {
        "id": 2,
        "name": "tag2"
      }
    ],
    "status": "available"
  }
  ```
  
  ### Handling success and failures
  
  You need to pass success and error functions to do anything reasonable with the responses:
  
  ```js
  var Swagger = require('swagger-client');
  
  var client = new Swagger({
    url: 'http://petstore.swagger.io/v2/swagger.json',
    success: function() {
      client.pet.getPetById({petId:7}, function(success){
        console.log('succeeded and returned this object: ' + success.obj);
      },
      function(error) {
        console.log('failed with the following: ' + error.statusText);
      });
    }
  });
  ```
  
  You can use promises, too, by passing the `usePromise: true` option:
  
  ```js
  var Swagger = require('swagger-client');
  
  new Swagger({
    url: 'http://petstore.swagger.io/v2/swagger.json',
    usePromise: true
  })
  .then(function(client) {
    client.pet.getPetById({petId:7})
      .then(function(pet) {
        console.log(pet.obj);
      })
      .catch(function(error) {
        console.log('Oops!  failed with message: ' + error.statusText);
      });
  });
  ```
  
  Need to pass an API key?  Configure one in your client instance as a query string:
  
  ```js
  client.clientAuthorizations.add("apiKey", new Swagger.ApiKeyAuthorization("api_key","special-key","query"));
  ```
  
  ...or with a header:
  
  ```js
  client.clientAuthorizations.add("apiKey", new Swagger.ApiKeyAuthorization("api_key","special-key","header"));
  ```
  
  ...or with the swagger-client constructor:
  
  ```js
  var client = new Swagger({
    url: 'http://example.com/spec.json',
    success: function() {},
    authorizations : {
      easyapi_basic: new Swagger.PasswordAuthorization('<username>', '<password>'),
      someHeaderAuth: new Swagger.ApiKeyAuthorization('<nameOfHeader>', '<value>', 'header'),
      someQueryAuth: new Swagger.ApiKeyAuthorization('<nameOfQueryKey>', '<value>', 'query'),
      someCookieAuth: new Swagger.CookieAuthorization('<cookie>'),
    }
  });
  ```
  
  Note the authorization nickname, such as `easyapi_basic` in the above example, must match the `security` requirement in the specification (see the [OAI Specification](https://github.com/OAI/OpenAPI-Specification/blob/master/README.md) for details).
  
  You can also pass authorzations on a _per-request_ basis, in the event that you're reusing a `swagger-client` object across multiple connections:
  
  ```
  client.pet.addPet({pet: {
      name: 'doggie'
    }}, {
      clientAuthorizations: {
        api_key: new Swagger.ApiKeyAuthorization('foo', 'bar', 'header')
      }
    })
    .then(function(pet) {
      console.log(pet.obj);
    });
  ```
  
  
  ### Calling an API with swagger + the browser!
  
  Download [`browser/swagger-client.min.js`](https://raw.githubusercontent.com/swagger-api/swagger-js/master/browser/swagger-client.min.js) and place it into your webapp:
  
  ```html
  <script src='browser/swagger-client.js' type='text/javascript'></script>
  <script type="text/javascript">
    // initialize swagger client, point to a resource listing
    window.client = new SwaggerClient({
      url: "http://petstore.swagger.io/v2/swagger.json",
      success: function() {
        // upon connect, fetch a pet and set contents to element "mydata"
        client.pet.getPetById({petId:1},{responseContentType: 'application/json'}, function(data) {
          document.getElementById("mydata").innerHTML = JSON.stringify(data.obj);
        });
      }
    });
  </script>
  
  <body>
    <div id="mydata"></div>
  </body>
  ```
  
  ### Need to send an object to your API via POST or PUT?
  ```js
  var pet = {
    id: 100,
    name: "dog"};
  
  // note: the parameter for `addPet` is named `body` in the example below
  client.pet.addPet({body: pet});
  ```
  
  ### Sending XML in as a payload to your API?
  ```js
  var pet = "<Pet><id>2</id><name>monster</name></Pet>";
  
  client.pet.addPet({body: pet}, {requestContentType:"application/xml"});
  ```
  
  ### Need XML response? (assuming your server can produce it)
  ```js
  client.pet.getPetById({petId:1}, {responseContentType:"application/xml"});
  ```
  
  ### Custom request signing
  You can easily write your own request signing code for Swagger.  For example:
  
  ```js
  var CustomRequestSigner = function(name) {
    this.name = name;
  };
  
  CustomRequestSigner.prototype.apply = function(obj, authorizations) {
    var hashFunction = this._btoa;
    var hash = hashFunction(obj.url);
  
    obj.headers["signature"] = hash;
    return true;
  };
  ```
  
  In the above simple example, we're creating a new request signer that simply
  Base64 encodes the URL.  Of course you'd do something more sophisticated, but
  after encoding it, a header called `signature` is set before sending the request.
  
  You can add it to the swagger-client like such:
  
  ```js
  client.clientAuthorizations.add('my-auth', new CustomRequestSigner());
  ```
  
  ### Setting headers
  
  Headers are a type of `parameter`, and can be passed with the other parameters. For example, if you supported translated pet details via the `Accept-Language` header:
  
  ```js
  "parameters": [
    {
      "name": "petId",
      "description": "ID of pet that needs to be fetched",
      "required": true,
      "type": "integer",
      "format": "int64",
      "paramType": "path",
      "minimum": "1.0",
      "defaultValue": 3,
      "maximum": "100000.0"
    },
    "LanguageHeader": {
      "name": "Accept-Language",
      "in": "header",
      "description": "Specify the user's language",
      "required": false,
      "type": "string"
    }
  ...
  ```
  
  Then you would pass the header value via the parameters ([header parameters are case-insenstive](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)):
  
  ```js
  
  client.pet.getPetById({
    petId: 7,
    'accept-language': 'fr'
  }, function(pet){
    console.log('pet', pet);
  });
  
  ```
  
  ### Using your own HTTP client
  
  Don't like [superagent](https://github.com/visionmedia/superagent)? Despise [JQuery](https://github.com/jquery/jquery)?  Well, you're in luck.  You can plug your own HTTP library easily:
  
  ```js
  var myHttpClient = {
    // implment an execute function
    execute: function(obj) {
      var httpMethod = obj.method;
      var requestHeaders = obj.headers;
      var body = obj.body;
      var url = obj.url;
      // do your thing, and call `obj.on.response`
      if(itWorked) {
        obj.on.response('horray');
      }
      else {
        obj.on.error('boo');
      }
    }
  };
  
  var client = new SwaggerClient({
    spec: petstoreRaw,
    client: myHttpClient,
    success: function () {
      client.pet.getPetById({petId: 3}, function(data){
        expect(data).toBe('ok');
        done();
      });
    }
  });
  ```
  
  ### How does it work?
  The swagger javascript client reads the swagger api definition directly from the server.  As it does, it constructs a client based on the api definition, which means it is completely dynamic.  It even reads the api text descriptions (which are intended for humans!) and provides help if you need it:
  
  ```js
  s.apis.pet.getPetById.help()
  '* petId (required) - ID of pet that needs to be fetched'
  ```
  
  The HTTP requests themselves are handled by the excellent [superagent](https://github.com/visionmedia/superagent) library, which has a ton of features itself.  But it runs on both node and the browser.
  
  
  Development
  -----------
  
  Please [fork the code](https://github.com/swagger-api/swagger-js) and help us improve swagger-js. Send us a pull request to the `master` branch!  Tests make merges get accepted more quickly.
  
  Note!  We _will not_ merge pull requests for features not supported in the OAI Specification!  Add an issue there instead!
  
  swagger-js use gulp for Node.js.
  
  ```bash
  # Install the gulp client on the path
  npm install -g gulp
  
  # Install all project dependencies
  npm install
  ```
  
  ```bash
  # List all tasks.
  gulp -T
  
  # Run lint (will not fail if there are errors/warnings), tests (without coverage) and builds the browser binaries
  gulp
  
  # Run the test suite (without coverage)
  gulp test
  
  # Build the browser binaries (One for development with source maps and one that is minified and without source maps) in the browser directory
  gulp build
  
  # Continuously run the test suite:
  gulp watch
  
  # Run jshint report
  gulp lint
  
  # Run a coverage report based on running the unit tests
  gulp coverage
  ```
  
  License
  -------
  
  Copyright 2016 SmartBear Software
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  
  ---
  <img src="http://swagger.io/wp-content/uploads/2016/02/logo.jpg"/>