debian-mirror-gitlab/lib/api/pagination_params.rb

48 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module API
# Concern for declare pagination params.
#
# @example
2020-07-28 23:09:34 +05:30
# class CustomApiResource < Grape::API::Instance
2017-08-17 22:00:37 +05:30
# include PaginationParams
#
# params do
# use :pagination
# end
# end
module PaginationParams
extend ActiveSupport::Concern
included do
helpers do
params :pagination do
2023-01-13 00:05:48 +05:30
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 }
2017-08-17 22:00:37 +05:30
end
2022-08-27 11:52:29 +05:30
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
2017-08-17 22:00:37 +05:30
end
end
end
end