Git Product home page Git Product logo

wp-api-swaggerui's Introduction

SwaggerUI for WordPress REST API

Build Status

Description

SwaggerUI used to make WordPress REST API endpoint have a interactive UI, so we can check our API endpoint directly from the website it self.

Features

  • Support for GET, POST, PUT, PATCH and DELETE request methods
  • Support for Auth Basic authorization method
  • Choose which namespace API that will be used on the SwaggerUI

alt text

Requirements

  • Your website should not block support of WordPress default REST API
  • Works for WordPress REST API Version 2
  • PHP Version should be greater than 5.4

Installation

Manual Installation

  • Clone this repository
  • Run npm install
  • Run npm run build
  • Copy all files into your wp-content/plugins/wp-api-swaggerui folder

Automatic Installation

  1. Log in and navigate to Plugins > Add New.
  2. Type “WP API SwaggerUI” into the Search and hit Enter.
  3. Locate the WP API SwaggerUI plugin in the list of search results and click Install Now.
  4. Once installed, click the Activate link.

Usage

After installing and activating the plugin, from your WordPress admin dashboard, access settings and then you will be able to see the Swagger option. Click that to access the Swagger Setting page.

alt text

Changing API Basepath

From the Swagger Setting page, choose between the API Basepath options displayed on the select menu, then click on the Save Changes button. Accessing the Docs URL you will be able to see the Swagger UI configured for the chosen endpoint.

Access Swagger UI

You can see Swagger Docs URL accessing:

REST API Customization

To customize how your created endpoints are shown at Swagger, here is an example with all the possible arguments you can add to your route:

GET

register_rest_route(
    'pet',
    '/(?P<petId>\d+)',
    [
        'methods'              => 'GET',
        'callback'             => $service->get_callback(),
        'summary'              => 'Find pet by ID',
        'description'          => 'Returns a single pet',
        'produces'             => ['application/json', 'application/xml'],
        'responses'            => [
            '200' => [
                'description' => 'successful operation',
                'schema'      => [
                    'type'       => 'object',
                    'required'   => ['name', 'photoUrls'],
                    'properties' => [
                        'id' => [
                            'type'   => 'integer',
                            'format' => 'int64',
                        ],
                        'name' => [
                            'type'    => 'string',
                            'example' => 'doggie',
                        ],
                        'status' => [
                            'type'        => 'string',
                            'description' => 'pet status in the store',
                            'enum'        => ['available', 'pending', 'sold']
                        ]
                    ],
                    'example'    => [
                        'id'     => 1,
                        'name'   => 'doggie',
                        'status' => ' available'
                    ],
                    'xml' => [
                        'name' => 'Pet'
                    ]
                ]
            ],
            '400' => [
                'description' => 'Invalid ID supplied'
            ],
            '404' => [
                'description' => 'Pet not found'
            ]

        ],
        'args'                 => [
            'petId'   => [
                'in'          => 'path',
                'description' => 'ID of pet to return',
                'required'    => true,
                'type'        => 'integer',
                'format'      => 'int64'
            ]
        ],
        'permission_callback'  => '__return_true'
    ]
)

POST

register_rest_route(
    'user',
    '/',
[
  'methods'              => 'POST',
  'callback'             => $service->get_callback(),
  'summary'              => 'Create user',
  'description'          => 'This can only be done by the logged in user.',
  'consumes'             => ['application/json'],
  'produces'             => ['application/json', 'application/xml'],
  'responses'            => [
      'default' => [
          'description' => 'successful operation',
      ]
  ],
  'args'                 => [
      'body'   => [
          'in'          => 'body',
          'description' => 'Created user object',
          'required'    => true,
          'type'        => 'object',
          'schema'      => [
              'type'       => 'object',
              'properties' => [
                  'id' => [
                      'type'   => 'integer',
                      'format' => 'int64',
                  ],
                  'username' => [
                      'type' => 'string'
                  ],
                  'email' => [
                      'type' => 'string'
                  ],
                  'password' => [
                      'type' => 'string'
                  ],
              ],
              'example' => [
                  'id'       => 0,
                  'username' => 'string',
                  'email'    => 'string',
                  'password' => 'string',
              ]
          ]
      ]
  ],
  'permission_callback'  => '__return_true'
]
)

These examples were based on the defaults presented at Swagger Editor

Guide

If you need help undesrtadning any of of the parameters used on this documentation, please refer to Swagger Documentation.
Here is some useful links:

wp-api-swaggerui's People

Contributors

5t111111 avatar agussuroyo avatar kazunao-anahara avatar lbeghini avatar mahjouba91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wp-api-swaggerui's Issues

Does not work on Bedrock boilerplate.

We are using Bedrock as boilerplates of WordPress sites. This plugin is very useful but unfortunately it does not work correctly because API endpoints become wrong.

When using bedrock, the WordPress address and the site address are not the same. For example, on typical Bedrock site the WordPress address is http://example.com/wp and the site address is http://example.com, then the API endpoints are kind of http://example.com/wp-json/<namespace>/.... Since wp-api-swaggerui tries to find endpoint using WordPress address via site_url API, it fails to get correct endpoints.

Although I know that using Bedrock is not general WordPress installation, for your information, I had to replace all usage of site_url with home_url to get it work with Bedrock.

Support for Authorization header

Is it possible to change authorizations option to use Bearer token via Authorization header?

If not, I would love to see this option added.

Thanks!

JS script crashes the browser

Hi, whenever I request an endpoint that takes longer to load the data, it crashes the browser.

Any ideas about what may be causing this? It seems like after 3 seconds the loading animation freezes and then the browser crashes. I have tried in various browsers and all have the same issue. This occurs when I'm on the API docs page and I do a simple GET request Authorized.

Support for OpenAPI version 3.0

Hi, could you please develop a new version that is compatible with openapi version 3?

Or at least share how we could do it.

Also I can see some minified JS files, I suppose you're using a gulp or webpack to build the assets, can you commit these files so we might be able to fork the plugin and implement some changes please?

PHP Notice: Undefined index: type

PHP Notice: Undefined index: type in /usr/local/wordpress/wp-content/plugins/wp-api-swaggerui/wp-api-swaggerui.php on line 327

if (isset($detail['items'])) {
    $params['items'] = array(
        'type' => $detail['items']['type']
    );
}

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.