2023-12-16 02:25:49 +00:00
|
|
|
package transformers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2024-01-24 16:09:39 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-12-16 02:25:49 +00:00
|
|
|
)
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
var ReplacePath = Transformer{
|
2023-12-16 03:42:14 +00:00
|
|
|
ModifyRequest: func(options any, ctx *fiber.Ctx) error {
|
2023-12-16 02:25:49 +00:00
|
|
|
opts := DeserializeOptions[struct {
|
2024-01-24 16:09:39 +00:00
|
|
|
Pattern string `json:"pattern" toml:"pattern"`
|
|
|
|
Value string `json:"value" toml:"value"`
|
|
|
|
Repl string `json:"repl" toml:"repl"` // Use when complex mode(regexp) enabled
|
|
|
|
Complex bool `json:"complex" toml:"complex"`
|
2023-12-16 02:25:49 +00:00
|
|
|
}](options)
|
|
|
|
path := string(ctx.Request().URI().Path())
|
|
|
|
if !opts.Complex {
|
|
|
|
ctx.Path(strings.ReplaceAll(path, opts.Pattern, opts.Value))
|
|
|
|
} else if ex := regexp.MustCompile(opts.Pattern); ex != nil {
|
|
|
|
ctx.Path(ex.ReplaceAllString(path, opts.Repl))
|
|
|
|
}
|
2023-12-16 03:42:14 +00:00
|
|
|
return nil
|
2023-12-16 02:25:49 +00:00
|
|
|
},
|
|
|
|
}
|