1010 # :scan-to-strings)
1111 (:import-from :tiny-routes.request
1212 # :request-append
13+ # :request-get
1314 # :path-info)
1415 (:import-from :tiny-routes.util
1516 # :compose)
16- (:export # :wrap-request-path-info-matcher
17+ (:export # :path-parameter
18+ # :with-path-parameters
19+ # :wrap-request-path-info-matcher
1720 # :wrap-request-matches-path-template))
1821
1922(in-package :tiny-routes.middleware.path-template )
@@ -87,15 +90,16 @@ then it is made available to the request under `:path-parameters'."
8790 " Wrap HANDLER such that it is called only if the request path matches
8891the PATH-TEMPLATE.
8992
90- If PATH-TEMPLATE is t, nil, or an empty string, then return HANDLER
91- unchanged.
93+ If PATH-TEMPLATE is t, nil, the empty string, or \" * \" , then return
94+ HANDLER unchanged.
9295
9396If REGEX is non-nil, then interpret path-template as a regular
9497expression."
9598 (check-type path-template (or symbol string ))
9699 (cond ((or (null path-template)
97100 (eq path-template t )
98- (string= path-template " " ))
101+ (string= path-template " " )
102+ (string= path-template " *" ))
99103 handler)
100104 ; ; If regex is non-nil, then interpret path-info as a regex
101105 (regex
@@ -105,3 +109,15 @@ expression."
105109 (wrap-request-path-info-matcher handler (make-path-template-keyword-matcher path-template)))
106110 (t
107111 (wrap-request-path-info-matcher handler (make-path-template-exact-matcher path-template)))))
112+
113+ (defun path-parameter (request path-parameter &optional default)
114+ " Return the value mapped to PATH-PARAMETER from REQUEST or DEFAULT."
115+ (getf (getf request :path-parameters ) path-parameter default))
116+
117+ (defmacro with-path-parameters (vars path-parameters &body body)
118+ " Bind the variables in VARS to the corresponding values present in
119+ PATH-PARAMETERS."
120+ (let ((gpath-parameters (gensym " path-parameters" )))
121+ ` (let* ((, gpath-parameters , path-parameters))
122+ (destructuring-bind (&key ,@ vars &allow-other-keys ) , gpath-parameters
123+ ,@ body))))
0 commit comments