Skip to content

Commit

Permalink
Add GetIDParamFromCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Jun 30, 2024
1 parent 5a5cd5e commit 15c6134
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package babyapi

import (
"bytes"
"context"
"errors"
"fmt"
"html/template"
Expand All @@ -22,6 +23,11 @@ func GetIDParam(r *http.Request, name string) string {
return chi.URLParam(r, IDParamKey(name))
}

// GetIDParamFromCtx gets resource ID from the request URL for a resource by name
func GetIDParamFromCtx(ctx context.Context, name string) string {
return chi.URLParamFromCtx(ctx, IDParamKey(name))
}

// IDParamKey gets the chi URL param key used for this API
func (a *API[T]) IDParamKey() string {
return IDParamKey(a.name)
Expand All @@ -36,6 +42,11 @@ func (a *API[T]) GetIDParam(r *http.Request) string {
return param
}

// GetIDParamFromCtx gets resource ID from the request URL for this API's resource
func (a *API[T]) GetIDParamFromCtx(ctx context.Context) string {
return GetIDParamFromCtx(ctx, a.name)
}

// findIDParam will loop through the whole path to manually find the ID parameter that follows this
// API's base path name. This is used when a parent API has a middleware which applies to child APIs
// and attempts to get the child's ID, but the middleware is not aware of child ID URL parameters
Expand Down

0 comments on commit 15c6134

Please sign in to comment.