Skip to content

Instantly share code, notes, and snippets.

@namiwang
Last active April 16, 2022 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namiwang/e1df58fea2015642cadcd1dd4da3798b to your computer and use it in GitHub Desktop.
Save namiwang/e1df58fea2015642cadcd1dd4da3798b to your computer and use it in GitHub Desktop.
grape extends desc auth header

instead of

desc 'some endpoint', {
  headers: { Authorization: {
    description: 'Bearer JWT',
    required: true
  }
}}
get do
end

on every endpoint, we can share one extender like this

module API::DescHeaderAuth
  def self.included(base)
    base.class_eval do
      def self.desc_header_auth required: true
        return unless setting = inheritable_setting.route
        return unless desc = setting[:description]
        desc[:headers] = {
          Authorization: {
            description: 'Bearer <JWT>',
            required: required
          }
        }
      end
    end
  end
end

Grape::API::Instance.include API::DescHeaderAuth

# your api

require_relative './extends/desc_header_auth'

# your endpoints

desc 'some endpoint'
desc_header_auth
get do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment