Changed: DB Params
This commit is contained in:
70
templ/turbo/stream.go
Normal file
70
templ/turbo/stream.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package turbo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
// Append adds an append action to the output stream.
|
||||
func Append(w http.ResponseWriter, target string, template templ.Component) error {
|
||||
return AppendWithContext(context.Background(), w, target, template)
|
||||
}
|
||||
|
||||
// AppendWithContext adds an append action to the output stream.
|
||||
func AppendWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
|
||||
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
|
||||
return actionTemplate("append", target).Render(templ.WithChildren(ctx, template), w)
|
||||
}
|
||||
|
||||
// Prepend adds a prepend action to the output stream.
|
||||
func Prepend(w http.ResponseWriter, target string, template templ.Component) error {
|
||||
return PrependWithContext(context.Background(), w, target, template)
|
||||
}
|
||||
|
||||
// PrependWithContext adds a prepend action to the output stream.
|
||||
func PrependWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
|
||||
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
|
||||
return actionTemplate("prepend", target).Render(templ.WithChildren(ctx, template), w)
|
||||
}
|
||||
|
||||
// Replace adds a replace action to the output stream.
|
||||
func Replace(w http.ResponseWriter, target string, template templ.Component) error {
|
||||
return ReplaceWithContext(context.Background(), w, target, template)
|
||||
}
|
||||
|
||||
// ReplaceWithContext adds a replace action to the output stream.
|
||||
func ReplaceWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
|
||||
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
|
||||
return actionTemplate("replace", target).Render(templ.WithChildren(ctx, template), w)
|
||||
}
|
||||
|
||||
// Update adds an update action to the output stream.
|
||||
func Update(w http.ResponseWriter, target string, template templ.Component) error {
|
||||
return UpdateWithContext(context.Background(), w, target, template)
|
||||
}
|
||||
|
||||
// UpdateWithContext adds an update action to the output stream.
|
||||
func UpdateWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
|
||||
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
|
||||
return actionTemplate("update", target).Render(templ.WithChildren(ctx, template), w)
|
||||
}
|
||||
|
||||
// Remove adds a remove action to the output stream.
|
||||
func Remove(w http.ResponseWriter, target string) error {
|
||||
return RemoveWithContext(context.Background(), w, target)
|
||||
}
|
||||
|
||||
// RemoveWithContext adds a remove action to the output stream.
|
||||
func RemoveWithContext(ctx context.Context, w http.ResponseWriter, target string) error {
|
||||
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
|
||||
return removeTemplate("remove", target).Render(ctx, w)
|
||||
}
|
||||
|
||||
// IsTurboRequest returns true if the incoming request is able to receive a Turbo stream.
|
||||
// This is determined by checking the request header for "text/vnd.turbo-stream.html"
|
||||
func IsTurboRequest(r *http.Request) bool {
|
||||
return strings.Contains(r.Header.Get("accept"), "text/vnd.turbo-stream.html")
|
||||
}
|
13
templ/turbo/stream.templ
Normal file
13
templ/turbo/stream.templ
Normal file
@@ -0,0 +1,13 @@
|
||||
package turbo
|
||||
|
||||
templ actionTemplate(action string, target string) {
|
||||
<turbo-stream action={ action } target={ target }>
|
||||
<template>
|
||||
{ children... }
|
||||
</template>
|
||||
</turbo-stream>
|
||||
}
|
||||
|
||||
templ removeTemplate(action string, target string) {
|
||||
<turbo-stream action={ action } target={ target }></turbo-stream>
|
||||
}
|
129
templ/turbo/stream_templ.go
Normal file
129
templ/turbo/stream_templ.go
Normal file
@@ -0,0 +1,129 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.833
|
||||
package turbo
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func actionTemplate(action string, target string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<turbo-stream action=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(action)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/turbo/stream.templ`, Line: 4, Col: 30}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" target=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(target)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/turbo/stream.templ`, Line: 4, Col: 48}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"><template>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</template></turbo-stream>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func removeTemplate(action string, target string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var4 == nil {
|
||||
templ_7745c5c3_Var4 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<turbo-stream action=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var5 string
|
||||
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(action)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/turbo/stream.templ`, Line: 12, Col: 30}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "\" target=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var6 string
|
||||
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(target)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/turbo/stream.templ`, Line: 12, Col: 48}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\"></turbo-stream>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
118
templ/turbo/stream_test.go
Normal file
118
templ/turbo/stream_test.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package turbo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
var contentTemplate = templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
|
||||
_, err := io.WriteString(w, "content")
|
||||
return err
|
||||
})
|
||||
|
||||
func TestStreamReplace(t *testing.T) {
|
||||
// Arrange.
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
if err := Replace(w, "replaceTarget", contentTemplate); err != nil {
|
||||
t.Fatalf("replace failed: %v", err)
|
||||
}
|
||||
|
||||
// Act.
|
||||
bdy := w.Body.Bytes()
|
||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(bdy))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
// Assert.
|
||||
selection := doc.Find(`turbo-stream[action="replace"][target="replaceTarget"]`)
|
||||
if selection.Length() != 1 {
|
||||
t.Error("expected to find a replace action, but didn't")
|
||||
}
|
||||
text := selection.First().Text()
|
||||
if text != "content" {
|
||||
t.Errorf("expected 'content' to be the text, but got %q\n%s", text, bdy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStream(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// Append.
|
||||
expectedAppends := 2
|
||||
for i := 0; i < expectedAppends; i++ {
|
||||
if err := Append(w, "appendTarget", contentTemplate); err != nil {
|
||||
t.Fatalf("append failed: %v", err)
|
||||
}
|
||||
}
|
||||
// Prepend.
|
||||
expectedPrepends := 3
|
||||
for i := 0; i < expectedPrepends; i++ {
|
||||
if err := Prepend(w, "prependTarget", contentTemplate); err != nil {
|
||||
t.Fatalf("prepend failed: %v", err)
|
||||
}
|
||||
}
|
||||
// Replace.
|
||||
expectedReplaces := 4
|
||||
for i := 0; i < expectedReplaces; i++ {
|
||||
if err := Replace(w, "replaceTarget", contentTemplate); err != nil {
|
||||
t.Fatalf("replace failed: %v", err)
|
||||
}
|
||||
}
|
||||
// Update.
|
||||
expectedUpdates := 5
|
||||
for i := 0; i < expectedUpdates; i++ {
|
||||
if err := Update(w, "updateTarget", contentTemplate); err != nil {
|
||||
t.Fatalf("update failed: %v", err)
|
||||
}
|
||||
}
|
||||
// Remove.
|
||||
expectedRemoves := 6
|
||||
for i := 0; i < expectedRemoves; i++ {
|
||||
if err := Remove(w, "removeTarget"); err != nil {
|
||||
t.Fatalf("remove failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(w.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
if appendCount := doc.Find(`turbo-stream[action="append"][target="appendTarget"]`).Length(); appendCount != expectedAppends {
|
||||
t.Errorf("expected %d append actions, but got %d", expectedAppends, appendCount)
|
||||
}
|
||||
if prependCount := doc.Find(`turbo-stream[action="prepend"][target="prependTarget"]`).Length(); prependCount != expectedPrepends {
|
||||
t.Errorf("expected %d prepend actions, but got %d", expectedPrepends, prependCount)
|
||||
}
|
||||
if replaceCount := doc.Find(`turbo-stream[action="replace"][target="replaceTarget"]`).Length(); replaceCount != expectedReplaces {
|
||||
t.Errorf("expected %d replace actions, but got %d", expectedReplaces, replaceCount)
|
||||
}
|
||||
if updateCount := doc.Find(`turbo-stream[action="update"][target="updateTarget"]`).Length(); updateCount != expectedUpdates {
|
||||
t.Errorf("expected %d update actions, but got %d", expectedUpdates, updateCount)
|
||||
}
|
||||
if removeCount := doc.Find(`turbo-stream[action="remove"][target="removeTarget"]`).Length(); removeCount != expectedRemoves {
|
||||
t.Errorf("expected %d remove actions, but got %d", expectedRemoves, removeCount)
|
||||
}
|
||||
if w.Result().Header.Get("Content-Type") != "text/vnd.turbo-stream.html" {
|
||||
t.Errorf("expected Content-Type %q, got %q", "text/vnd.turbo-stream.html", w.Result().Header.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTurboRequest(t *testing.T) {
|
||||
turboRequest := httptest.NewRequest("GET", "/", nil)
|
||||
if IsTurboRequest(turboRequest) {
|
||||
t.Error("request was incorrectly recognised as a Turbo stream request")
|
||||
}
|
||||
turboRequest.Header.Add("accept", "text/vnd.turbo-stream.html")
|
||||
if !IsTurboRequest(turboRequest) {
|
||||
t.Error("request not correctly recognised as a Turbo stream request")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user