diff --git a/README.md b/README.md index 253038a..ca9f411 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/helpers_test.go b/helpers_test.go index 8d9a4dc..5244161 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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 } diff --git a/inertia.go b/inertia.go index 2542c3f..6c5592f 100644 --- a/inertia.go +++ b/inertia.go @@ -19,7 +19,7 @@ type Inertia struct { sharedTemplateData TemplateData sharedTemplateFuncs TemplateFuncs - flash FlashProvider + session SessionProvider ssrURL string ssrHTTPClient *http.Client @@ -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) } diff --git a/middleware.go b/middleware.go index 2448ed9..e16f045 100644 --- a/middleware.go +++ b/middleware.go @@ -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) { @@ -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 } diff --git a/middleware_test.go b/middleware_test.go index 1ee56e1..b6f8f10 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -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, "/") @@ -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 @@ -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") @@ -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) } }) diff --git a/option.go b/option.go index 8dd4451..3b6b738 100644 --- a/option.go +++ b/option.go @@ -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 } } diff --git a/option_test.go b/option_test.go index 164a095..12d02d0 100644 --- a/option_test.go +++ b/option_test.go @@ -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) } } diff --git a/response.go b/response.go index 4fc276b..29a3dc5 100644 --- a/response.go +++ b/response.go @@ -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 } @@ -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) } diff --git a/response_test.go b/response_test.go index d72bcbf..6af2ef9 100644 --- a/response_test.go +++ b/response_test.go @@ -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{ @@ -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) } }) @@ -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{ @@ -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) } }) }) @@ -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{ @@ -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) } }) } @@ -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{ @@ -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) } }) }