From bd9047d3eb18c776f628428da3ab228405e686c2 Mon Sep 17 00:00:00 2001 From: Roman Sarvarov Date: Thu, 10 Oct 2024 21:07:09 +0300 Subject: [PATCH] inertia v2: clearHistory and encryptHistory --- context.go | 41 ++++++++++++++++++++++++++++++++++------- inertia.go | 1 + option.go | 8 ++++++++ response.go | 28 ++++++++++++++++++++-------- 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/context.go b/context.go index a711e31..aa9284b 100644 --- a/context.go +++ b/context.go @@ -10,14 +10,16 @@ const ( templateDataContextKey = contextKey(iota + 1) propsContextKey validationErrorsContextKey + encryptHistoryContextKey + clearHistoryContextKey ) -// SetTemplateData sets template data to the passed context.Context. +// SetTemplateData sets template data to the passed context. func SetTemplateData(ctx context.Context, templateData TemplateData) context.Context { return context.WithValue(ctx, templateDataContextKey, templateData) } -// SetTemplateDatum sets single template data item to the passed context.Context. +// SetTemplateDatum sets single template data item to the passed context. func SetTemplateDatum(ctx context.Context, key string, val any) context.Context { templateData := TemplateDataFromContext(ctx) templateData[key] = val @@ -33,12 +35,12 @@ func TemplateDataFromContext(ctx context.Context) TemplateData { return TemplateData{} } -// SetProps sets props values to the passed context.Context. +// SetProps sets props values to the passed context. func SetProps(ctx context.Context, props Props) context.Context { return context.WithValue(ctx, propsContextKey, props) } -// SetProp sets prop value to the passed context.Context. +// SetProp sets prop value to the passed context. func SetProp(ctx context.Context, key string, val any) context.Context { props := PropsFromContext(ctx) props[key] = val @@ -54,12 +56,12 @@ func PropsFromContext(ctx context.Context) Props { return Props{} } -// SetValidationErrors sets validation errors to the passed context.Context. +// SetValidationErrors sets validation errors to the passed context. func SetValidationErrors(ctx context.Context, errors ValidationErrors) context.Context { return context.WithValue(ctx, validationErrorsContextKey, errors) } -// AddValidationErrors appends validation errors to the passed context.Context. +// AddValidationErrors appends validation errors to the passed context. func AddValidationErrors(ctx context.Context, errors ValidationErrors) context.Context { validationErrors := ValidationErrorsFromContext(ctx) for key, val := range errors { @@ -68,7 +70,7 @@ func AddValidationErrors(ctx context.Context, errors ValidationErrors) context.C return SetValidationErrors(ctx, validationErrors) } -// SetValidationError sets validation error to the passed context.Context. +// SetValidationError sets validation error to the passed context. func SetValidationError(ctx context.Context, key string, msg string) context.Context { validationErrors := ValidationErrorsFromContext(ctx) validationErrors[key] = msg @@ -83,3 +85,28 @@ func ValidationErrorsFromContext(ctx context.Context) ValidationErrors { } return ValidationErrors{} } + +// SetEncryptHistory enables or disables history encryption. +func SetEncryptHistory(ctx context.Context, encrypt ...bool) context.Context { + return context.WithValue(ctx, encryptHistoryContextKey, firstOr[bool](encrypt, true)) +} + +// EncryptHistoryFromContext returns history encryption value from the context. +func EncryptHistoryFromContext(ctx context.Context) (bool, bool) { + encrypt, ok := ctx.Value(encryptHistoryContextKey).(bool) + return encrypt, ok +} + +// SetClearHistory cleaning history state. +func SetClearHistory(ctx context.Context) context.Context { + return context.WithValue(ctx, clearHistoryContextKey, true) +} + +// ClearHistoryFromContext returns clear history value from the context. +func ClearHistoryFromContext(ctx context.Context) bool { + clear, ok := ctx.Value(clearHistoryContextKey).(bool) + if ok { + return clear + } + return false +} diff --git a/inertia.go b/inertia.go index 0fc4a35..2542c3f 100644 --- a/inertia.go +++ b/inertia.go @@ -26,6 +26,7 @@ type Inertia struct { containerID string version string + encryptHistory bool jsonMarshaller JSONMarshaller logger Logger } diff --git a/option.go b/option.go index 1a4dc52..c4ecab6 100644 --- a/option.go +++ b/option.go @@ -88,3 +88,11 @@ func WithFlashProvider(flashData FlashProvider) Option { return nil } } + +// WithEncryptHistory returns Option that will enable Inertia's global history encryption. +func WithEncryptHistory(encrypt ...bool) Option { + return func(i *Inertia) error { + i.encryptHistory = firstOr[bool](encrypt, true) + return nil + } +} diff --git a/response.go b/response.go index e99cbd1..e967415 100644 --- a/response.go +++ b/response.go @@ -132,10 +132,12 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin } type page struct { - Component string `json:"component"` - Props Props `json:"props"` - URL string `json:"url"` - Version string `json:"version"` + Component string `json:"component"` + Props Props `json:"props"` + URL string `json:"url"` + Version string `json:"version"` + EncryptHistory bool `json:"encryptHistory"` + ClearHistory bool `json:"clearHistory"` } func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*page, error) { @@ -145,10 +147,12 @@ func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*pa } return &page{ - Component: component, - Props: props, - URL: r.RequestURI, - Version: i.version, + Component: component, + Props: props, + URL: r.RequestURI, + Version: i.version, + EncryptHistory: i.resolveEncryptHistory(r.Context()), + ClearHistory: ClearHistoryFromContext(r.Context()), }, nil } @@ -253,6 +257,14 @@ func resolvePropVal(val any) (_ any, err error) { return val, nil } +func (i *Inertia) resolveEncryptHistory(ctx context.Context) bool { + encryptHistory, ok := EncryptHistoryFromContext(ctx) + if ok { + return encryptHistory + } + return i.encryptHistory +} + func (i *Inertia) doInertiaResponse(w http.ResponseWriter, page *page) error { pageJSON, err := i.jsonMarshaller.Marshal(page) if err != nil {