Skip to content

Commit

Permalink
inertia v2: clearHistory and encryptHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
romsar committed Oct 10, 2024
1 parent 8b97ee8 commit bd9047d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
41 changes: 34 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)

Check failure on line 107 in context.go

View workflow job for this annotation

GitHub Actions / audit

variable clear has same name as predeclared identifier (predeclared)

Check failure on line 107 in context.go

View workflow job for this annotation

GitHub Actions / audit

variable clear has same name as predeclared identifier (predeclared)
if ok {
return clear
}
return false
}
1 change: 1 addition & 0 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Inertia struct {

containerID string
version string
encryptHistory bool
jsonMarshaller JSONMarshaller
logger Logger
}
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
28 changes: 20 additions & 8 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bd9047d

Please sign in to comment.