Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pagination_params.rb « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3505780396cc024a9faa5ce6f0153b2c7c37131 (plain)
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
# frozen_string_literal: true

module API
  # Concern for declare pagination params.
  #
  # @example
  #   class CustomApiResource < Grape::API::Instance
  #     include PaginationParams
  #
  #     params do
  #       use :pagination
  #     end
  #   end
  module PaginationParams
    extend ActiveSupport::Concern

    included do
      helpers do
        params :pagination do
          optional :page, type: Integer, default: 1, desc: 'Current page number', documentation: { example: 1 }
          optional :per_page, type: Integer, default: 20,
                              desc: 'Number of items per page', except_values: [0], documentation: { example: 20 }
        end

        def verify_pagination_params!
          return if Feature.disabled?(:only_positive_pagination_values)

          page = begin
            Integer(params[:page])
          rescue ArgumentError, TypeError
            nil
          end

          return render_structured_api_error!({ error: 'page does not have a valid value' }, 400) if page&.< 1

          per_page = begin
            Integer(params[:per_page])
          rescue ArgumentError, TypeError
            nil
          end

          return render_structured_api_error!({ error: 'per_page does not have a valid value' }, 400) if per_page&.< 1
        end
      end
    end
  end
end