Skip to content

Commit

Permalink
rename Flash Data Provider to Session Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
romsar committed Oct 19, 2024
1 parent b40fce0 commit 37c8d30
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 52 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,39 +282,39 @@ i, err := inertia.New(
)
```

#### Set flash provider
#### Set session provider

Unfortunately (or fortunately) we do not have the advantages of such a framework as Laravel in terms of session management.
In this regard, we have to do some things manually that are done automatically in frameworks.

One of them is displaying validation errors after redirects.
You have to write your own implementation of `gonertia.FlashProvider` which will have to store error data into the user's session and return this data (you can get the session ID from the context depending on your application).
You have to write your own implementation of `gonertia.SessionProvider` which will have to store error data into the user's session and return this data (you can get the session ID from the context depending on your application).

```go
i, err := inertia.New(
/* ... */
inertia.WithFlashProvider(flashProvider),
inertia.WithSessionProvider(session),
)
```

Simple inmemory implementation of flash provider:
Simple inmemory implementation of session provider:

```go
type InmemFlashProvider struct {
type InmemSessionProvider struct {
errors map[string]inertia.ValidationErrors
}

func NewInmemFlashProvider() *InmemFlashProvider {
return &InmemFlashProvider{errors: make(map[string]inertia.ValidationErrors)}
func NewInmemSessionProvider() *InmemSessionProvider {
return &InmemSessionProvider{errors: make(map[string]inertia.ValidationErrors)}
}

func (p *InmemFlashProvider) FlashErrors(ctx context.Context, errors ValidationErrors) error {
func (p *InmemSessionProvider) FlashErrors(ctx context.Context, errors ValidationErrors) error {
sessionID := getSessionIDFromContext(ctx)
p.errors[sessionID] = errors
return nil
}

func (p *InmemFlashProvider) GetErrors(ctx context.Context) (ValidationErrors, error) {
func (p *InmemSessionProvider) GetErrors(ctx context.Context) (ValidationErrors, error) {
sessionID := getSessionIDFromContext(ctx)
errors := p.errors[sessionID]
p.errors[sessionID] = nil
Expand Down
6 changes: 3 additions & 3 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ func tmpFile(t *testing.T, content string) *os.File {
return f
}

type flashProviderMock struct {
type sessionProviderMock struct {
errors ValidationErrors
}

func (p *flashProviderMock) FlashErrors(_ context.Context, errors ValidationErrors) error {
func (p *sessionProviderMock) FlashErrors(_ context.Context, errors ValidationErrors) error {
p.errors = errors
return nil
}

func (p *flashProviderMock) GetErrors(_ context.Context) (ValidationErrors, error) {
func (p *sessionProviderMock) GetErrors(_ context.Context) (ValidationErrors, error) {
return p.errors, nil
}
6 changes: 3 additions & 3 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Inertia struct {
sharedTemplateData TemplateData
sharedTemplateFuncs TemplateFuncs

flash FlashProvider
session SessionProvider

ssrURL string
ssrHTTPClient *http.Client
Expand Down Expand Up @@ -90,8 +90,8 @@ type Logger interface {
Println(v ...any)
}

// FlashProvider defines an interface for flash data provider.
type FlashProvider interface {
// SessionProvider defines an interface for session provider.
type SessionProvider interface {
FlashErrors(ctx context.Context, errors ValidationErrors) error
GetErrors(ctx context.Context) (ValidationErrors, error)
}
Expand Down
8 changes: 4 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (i *Inertia) Middleware(next http.Handler) http.Handler {
// https://github.com/inertiajs/inertia-laravel/pull/404
setInertiaVaryInResponse(w)

// Resolve validation errors from the flash data provider.
// Resolve validation errors from the session provider.
r = i.resolveValidationErrors(r)

if !IsInertiaRequest(r) {
Expand Down Expand Up @@ -70,13 +70,13 @@ func (i *Inertia) Middleware(next http.Handler) http.Handler {
}

func (i *Inertia) resolveValidationErrors(r *http.Request) *http.Request {
if i.flash == nil {
if i.session == nil {
return r
}

validationErrors, err := i.flash.GetErrors(r.Context())
validationErrors, err := i.session.GetErrors(r.Context())
if err != nil {
i.logger.Printf("get validation errors from flash data provider error: %s", err)
i.logger.Printf("get validation errors from the session provider error: %s", err)
return r
}

Expand Down
14 changes: 7 additions & 7 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInertia_Middleware(t *testing.T) {
assertResponseStatusCode(t, w, http.StatusOK)
})

t.Run("resolve validation errors from flash data provider", func(t *testing.T) {
t.Run("resolve validation errors from session data provider", func(t *testing.T) {
t.Parallel()

w, r := requestMock(http.MethodGet, "/")
Expand All @@ -34,12 +34,12 @@ func TestInertia_Middleware(t *testing.T) {
"baz": "quz",
}

flashProvider := &flashProviderMock{
sessionProvider := &sessionProviderMock{
errors: want,
}

i := I(func(i *Inertia) {
i.flash = flashProvider
i.session = sessionProvider
})

var got ValidationErrors
Expand Down Expand Up @@ -67,13 +67,13 @@ func TestInertia_Middleware(t *testing.T) {
"baz": "quz",
}

flashProvider := &flashProviderMock{
sessionProvider := &sessionProviderMock{
errors: errors,
}

i := I(func(i *Inertia) {
i.version = "foo"
i.flash = flashProvider
i.session = sessionProvider
})

w, r := requestMock(http.MethodGet, "https://example.com/home")
Expand All @@ -87,8 +87,8 @@ func TestInertia_Middleware(t *testing.T) {
assertResponseStatusCode(t, w, http.StatusConflict)
assertInertiaLocation(t, w, "/home")

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
if !reflect.DeepEqual(sessionProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", sessionProvider.errors, errors)
}
})

Expand Down
6 changes: 3 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func WithSSR(url ...string) Option {
}
}

// WithFlashProvider returns Option that will set Inertia's flash data provider.
func WithFlashProvider(flashData FlashProvider) Option {
// WithSessionProvider returns Option that will set Inertia's session provider.
func WithSessionProvider(session SessionProvider) Option {
return func(i *Inertia) error {
i.flash = flashData
i.session = session
return nil
}
}
Expand Down
10 changes: 5 additions & 5 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,21 @@ func TestWithSSR(t *testing.T) {
})
}

func TestWithFlashProvider(t *testing.T) {
func TestWithSessionProvider(t *testing.T) {
t.Parallel()

i := I()

want := &flashProviderMock{}
want := &sessionProviderMock{}

option := WithFlashProvider(want)
option := WithSessionProvider(want)

if err := option(i); err != nil {
t.Fatalf("unexpected error: %s", err)
}

if i.flash != want {
t.Fatalf("flash provider=%v, want=%s", i.flash, want)
if i.session != want {
t.Fatalf("session provider=%v, want=%s", i.session, want)
}
}

Expand Down
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (i *Inertia) Redirect(w http.ResponseWriter, r *http.Request, url string, s
}

func (i *Inertia) flashValidationErrorsFromContext(ctx context.Context) {
if i.flash == nil {
if i.session == nil {
return
}

Expand All @@ -193,7 +193,7 @@ func (i *Inertia) flashValidationErrorsFromContext(ctx context.Context) {
return
}

err := i.flash.FlashErrors(ctx, validationErrors)
err := i.session.FlashErrors(ctx, validationErrors)
if err != nil {
i.logger.Printf("cannot flash validation errors: %s", err)
}
Expand Down
32 changes: 16 additions & 16 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,10 @@ func TestInertia_Location(t *testing.T) {

w, r := requestMock(http.MethodGet, "/")

flashProvider := &flashProviderMock{}
sessionProvider := &sessionProviderMock{}

i := I(func(i *Inertia) {
i.flash = flashProvider
i.session = sessionProvider
})

errors := ValidationErrors{
Expand All @@ -728,8 +728,8 @@ func TestInertia_Location(t *testing.T) {
withValidationErrors(r, errors)
i.Location(w, r, "/foo")

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
if !reflect.DeepEqual(sessionProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", sessionProvider.errors, errors)
}
})

Expand All @@ -739,10 +739,10 @@ func TestInertia_Location(t *testing.T) {
w, r := requestMock(http.MethodGet, "/")
asInertiaRequest(r)

flashProvider := &flashProviderMock{}
sessionProvider := &sessionProviderMock{}

i := I(func(i *Inertia) {
i.flash = flashProvider
i.session = sessionProvider
})

errors := ValidationErrors{
Expand All @@ -753,8 +753,8 @@ func TestInertia_Location(t *testing.T) {
withValidationErrors(r, errors)
i.Location(w, r, "/foo", http.StatusMovedPermanently)

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
if !reflect.DeepEqual(sessionProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", sessionProvider.errors, errors)
}
})
})
Expand Down Expand Up @@ -814,10 +814,10 @@ func TestInertia_Redirect(t *testing.T) {

w, r := requestMock(http.MethodGet, "/")

flashProvider := &flashProviderMock{}
sessionProvider := &sessionProviderMock{}

i := I(func(i *Inertia) {
i.flash = flashProvider
i.session = sessionProvider
})

errors := ValidationErrors{
Expand All @@ -828,8 +828,8 @@ func TestInertia_Redirect(t *testing.T) {
withValidationErrors(r, errors)
i.Redirect(w, r, "https://example.com/foo")

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
if !reflect.DeepEqual(sessionProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", sessionProvider.errors, errors)
}
})
}
Expand Down Expand Up @@ -892,10 +892,10 @@ func TestInertia_Back(t *testing.T) {
w, r := requestMock(http.MethodGet, "/")
r.Header.Set("Referer", "https://example.com/foo")

flashProvider := &flashProviderMock{}
sessionProvider := &sessionProviderMock{}

i := I(func(i *Inertia) {
i.flash = flashProvider
i.session = sessionProvider
})

errors := ValidationErrors{
Expand All @@ -906,8 +906,8 @@ func TestInertia_Back(t *testing.T) {
withValidationErrors(r, errors)
i.Back(w, r)

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
if !reflect.DeepEqual(sessionProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", sessionProvider.errors, errors)
}
})
}
Expand Down

0 comments on commit 37c8d30

Please sign in to comment.