The goal of Swagger™ is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interfaces have done for lower-level programming, Swager removes the guesswork in calling the service.
Check out Swagger-Spec for additional information about the Swagger project, including additional libraries with support for other languages and more.
npm install swagger-client
Then let swagger do the work!
var client = require"swagger-client"var swagger =url: ''swaggerapispetgetPetByIdpetId:1;;
That's it! You'll get a JSON response with the default callback handler:
Need to pass an API key? Configure one as a querystring:
...or with a header:
shred.bundle.js into your lib folder
var pet =id: 100name: "dog";swaggerapispetaddPetbody: pet;
var pet = "<Pet><id>2</id><name>monster</name></Pet>";swaggerapispetaddPetbody: pet requestContentType:"application/xml";
You can easily write your own request signing code for Swagger. For example:
varthisname = name;;var hashFunction = this_btoa;var hash = hashFunctionobjurl;objheaders"signature" = hash;return true;;
In the above simple example, we're creating a new request signer that simply
base 64 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.
sapispetgetPetByIdhelp'* petId (required) - ID of pet that needs to be fetched'
The HTTP requests themselves are handled by the excellent shred library, which has a ton of features itself. But it runs on both node and the browser.
Please fork the code and help us improve swagger-client.js. Send us a pull request and we'll mail you a wordnik T-shirt!
swagger-js use gulp for Node.js.
# Install the gulp client on the pathnpm install -g gulp# Install all project dependenciesnpm install
# List all tasks.gulp -T# Run lint (will not fail if there are errors/warnings), tests (without coverage) and builds the browser binariesgulp# 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 directorygulp build# Continuously run the test suite:gulp watch# Run jshint reportgulp lint# Run a coverage report based on running the unit testsgulp coverage
Copyright 2011-2015 Reverb Technologies, Inc.
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
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.