Class: BetterAuth::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/endpoint.rb

Defined Under Namespace

Classes: Context, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, method: nil, body_schema: nil, query_schema: nil, params_schema: nil, headers_schema: nil, metadata: {}, use: [], disable_body: false, &handler) ⇒ Endpoint

Returns a new instance of Endpoint.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/better_auth/endpoint.rb', line 19

def initialize(path: nil, method: nil, body_schema: nil, query_schema: nil, params_schema: nil, headers_schema: nil, metadata: {}, use: [], disable_body: false, &handler)
  @path = path
  @methods = Array(method || "*").map { |value| value.to_s.upcase }
  @body_schema = body_schema
  @query_schema = query_schema
  @params_schema = params_schema
  @headers_schema = headers_schema
  @metadata =  || {}
  apply_default_open_api_metadata!
  apply_open_api_defaults!
  apply_open_api_schemas!
  @options = endpoint_options
  @use = Array(use)
  @disable_body = !!disable_body
  @handler = handler || ->(_ctx) {}
end

Instance Attribute Details

#body_schemaObject (readonly)

Returns the value of attribute body_schema.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def body_schema
  @body_schema
end

#disable_bodyObject (readonly)

Returns the value of attribute disable_body.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def disable_body
  @disable_body
end

#handlerObject (readonly)

Returns the value of attribute handler.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def handler
  @handler
end

#headers_schemaObject (readonly)

Returns the value of attribute headers_schema.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def headers_schema
  @headers_schema
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def 
  @metadata
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def options
  @options
end

#params_schemaObject (readonly)

Returns the value of attribute params_schema.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def params_schema
  @params_schema
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def path
  @path
end

#query_schemaObject (readonly)

Returns the value of attribute query_schema.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def query_schema
  @query_schema
end

#useObject (readonly)

Returns the value of attribute use.



8
9
10
# File 'lib/better_auth/endpoint.rb', line 8

def use
  @use
end

Instance Method Details

#call(context) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/better_auth/endpoint.rb', line 44

def call(context)
  use.each do |middleware|
    middleware_result = middleware.call(context)
    return Result.from_value(middleware_result, context) if middleware_result
  end

  apply_schemas!(context)
  Result.from_value(handler.call(context), context)
end

#matches_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/better_auth/endpoint.rb', line 40

def matches_method?(method)
  methods.include?("*") || methods.include?(method.to_s.upcase)
end

#methodsObject



36
37
38
# File 'lib/better_auth/endpoint.rb', line 36

def methods
  @methods.empty? ? ["*"] : @methods
end