Coder Social home page Coder Social logo

garryyao / swagger-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swagger-api/swagger-js

0.0 1.0 0.0 942 KB

Javascript library to connect to swagger-enabled APIs via browser or nodejs

Home Page: http://swagger.wordnik.com

License: Other

CoffeeScript 9.87% JavaScript 87.98% CSS 2.15%

swagger-js's Introduction

Swagger JS library

This is the Wordnik Swagger javascript client for use with swagger enabled APIs. It's written in CoffeeScript and tested with Jasmine, and is the fastest way to enable a javascript client to communicate with a swagger-enabled server.

What's Swagger?

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.

Calling an API with swagger + node.js!

Install swagger-client:

npm install swagger-client

Then let swagger do the work!

var client = require("swagger-client")

var swagger = new client.SwaggerApi({
  url: 'http://petstore.swagger.wordnik.com/api/api-docs',
  success: function() {
    if(swagger.ready === true) {
      swagger.apis.pet.getPetById({petId:1});
    }
  }
});

That's it! You'll get a JSON response with the default callback handler:

{
  "id": 1,
  "category": {
    "id": 2,
    "name": "Cats"
  },
  "name": "Cat 1",
  "photoUrls": [
    "url1",
    "url2"
  ],
  "tags": [
    {
      "id": 1,
      "name": "tag1"
    },
    {
      "id": 2,
      "name": "tag2"
    }
  ],
  "status": "available"
}

Need to pass an API key? Configure one as a querystring:

client.authorizations.add("apiKey", new client.ApiKeyAuthorization("api_key","special-key","query"));

...or with a header:

client.authorizations.add("apiKey", new client.ApiKeyAuthorization("api_key","special-key","header"));

Calling an API with swagger + the browser!

Download swagger.js and shred.bundle.js into your lib folder

<script src='lib/shred.bundle.js' type='text/javascript'></script>
<script src='lib/swagger.js' type='text/javascript'></script>
<script type="text/javascript">
  // initialize swagger, point to a resource listing
  window.swagger = new SwaggerApi({
    url: "http://petstore.swagger.wordnik.com/api/api-docs",
    success: function() {
      if(swagger.ready === true) {
        // upon connect, fetch a pet and set contents to element "mydata"
        swagger.apis.pet.getPetById({petId:1}, function(data) {
          document.getElementById("mydata").innerHTML = data.content.data;
        });
      }
    }
  });

</script>

Need to send an object to your API via POST or PUT?

var body = {
  id: 100,
  name: "dog"};

swagger.apis.pet.addPet({body: JSON.stringify(body)});

Sending XML in as a payload to your API?

var body = "<Pet><id>2</id><name>monster</name></Pet>";

swagger.apis.pet.addPet({body: body},{requestContentType:"application/xml"});

Need XML response?

swagger.apis.pet.getPetById({petId:1},{responseContentType:"application/xml"});

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:

s.apis.pet.getPetById.help()
'* 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.

Development

Please fork the code and help us improve swagger.js. Send us a pull request and we'll mail you a wordnik T-shirt!

Swagger.js is written in CoffeeScript, so you'll need Node.js and the CoffeeScript compiler. For more detailed installation instructions, see coffeescript.org/#installation.

# generate the javascript libraries and put them in the `lib` folder

npm run-script build
# The 'dev' task will:
# 1. Open source files in your $EDITOR
# 2. Open and run the Jasmine specs in your browser.
# 3. Watch for changes to CoffeeScript files and auto-compile them to Javascript.

npm run-script dev

# List all cake tasks:
cake

License

Copyright 2011-2014 Wordnik, 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.

swagger-js's People

Contributors

fehguy avatar zeke avatar ayush avatar pose avatar webron avatar danieleli avatar flipside avatar nickpadilla avatar pdegeus avatar valdemon avatar sr3d avatar chytreg avatar leedm777 avatar dilipkrish avatar prantlf avatar filirom1 avatar garryyao avatar maxpmagee avatar pconnelly15 avatar rintcius avatar tillig avatar vlad avatar vfeskov avatar lbovet avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.