1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-18 21:57:50 +02:00

make use of the after hook finalizer

This commit is contained in:
Gani Georgiev 2023-07-18 15:31:36 +03:00
parent 624b443f98
commit d9e1a759a1
7 changed files with 82 additions and 127 deletions

View File

@ -69,11 +69,9 @@ func (api *adminApi) authRefresh(c echo.Context) error {
event.Admin = admin
return api.app.OnAdminBeforeAuthRefreshRequest().Trigger(event, func(e *core.AdminAuthRefreshEvent) error {
if err := api.app.OnAdminAfterAuthRefreshRequest().Trigger(event); err != nil {
return err
}
return api.authResponse(e.HttpContext, e.Admin)
return api.app.OnAdminAfterAuthRefreshRequest().Trigger(event, func(e *core.AdminAuthRefreshEvent) error {
return api.authResponse(e.HttpContext, e.Admin)
})
})
}
@ -97,11 +95,9 @@ func (api *adminApi) authWithPassword(c echo.Context) error {
return NewBadRequestError("Failed to authenticate.", err)
}
if err := api.app.OnAdminAfterAuthWithPasswordRequest().Trigger(event); err != nil {
return err
}
return api.authResponse(e.HttpContext, e.Admin)
return api.app.OnAdminAfterAuthWithPasswordRequest().Trigger(event, func(e *core.AdminAuthWithPasswordEvent) error {
return api.authResponse(e.HttpContext, e.Admin)
})
})
}
})
@ -135,11 +131,9 @@ func (api *adminApi) requestPasswordReset(c echo.Context) error {
}
})
if err := api.app.OnAdminAfterRequestPasswordResetRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnAdminAfterRequestPasswordResetRequest().Trigger(event, func(e *core.AdminRequestPasswordResetEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -171,11 +165,9 @@ func (api *adminApi) confirmPasswordReset(c echo.Context) error {
return NewBadRequestError("Failed to set new password.", err)
}
if err := api.app.OnAdminAfterConfirmPasswordResetRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnAdminAfterConfirmPasswordResetRequest().Trigger(event, func(e *core.AdminConfirmPasswordResetEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -252,11 +244,9 @@ func (api *adminApi) create(c echo.Context) error {
return NewBadRequestError("Failed to create admin.", err)
}
if err := api.app.OnAdminAfterCreateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Admin)
return api.app.OnAdminAfterCreateRequest().Trigger(event, func(e *core.AdminCreateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Admin)
})
})
}
})
@ -296,11 +286,9 @@ func (api *adminApi) update(c echo.Context) error {
return NewBadRequestError("Failed to update admin.", err)
}
if err := api.app.OnAdminAfterUpdateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Admin)
return api.app.OnAdminAfterUpdateRequest().Trigger(event, func(e *core.AdminUpdateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Admin)
})
})
}
})
@ -328,10 +316,8 @@ func (api *adminApi) delete(c echo.Context) error {
return NewBadRequestError("Failed to delete admin.", err)
}
if err := api.app.OnAdminAfterDeleteRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnAdminAfterDeleteRequest().Trigger(event, func(e *core.AdminDeleteEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}

View File

@ -91,11 +91,9 @@ func (api *collectionApi) create(c echo.Context) error {
return NewBadRequestError("Failed to create the collection.", err)
}
if err := api.app.OnCollectionAfterCreateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Collection)
return api.app.OnCollectionAfterCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
})
}
})
@ -128,11 +126,9 @@ func (api *collectionApi) update(c echo.Context) error {
return NewBadRequestError("Failed to update the collection.", err)
}
if err := api.app.OnCollectionAfterUpdateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Collection)
return api.app.OnCollectionAfterUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
})
}
})
@ -153,11 +149,9 @@ func (api *collectionApi) delete(c echo.Context) error {
return NewBadRequestError("Failed to delete collection due to existing dependency.", err)
}
if err := api.app.OnCollectionAfterDeleteRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnCollectionAfterDeleteRequest().Trigger(event, func(e *core.CollectionDeleteEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
@ -183,11 +177,9 @@ func (api *collectionApi) bulkImport(c echo.Context) error {
return NewBadRequestError("Failed to import the submitted collections.", err)
}
if err := api.app.OnCollectionsAfterImportRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnCollectionsAfterImportRequest().Trigger(event, func(e *core.CollectionsImportEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})

View File

@ -50,12 +50,10 @@ func (api *fileApi) fileToken(c echo.Context) error {
return NewBadRequestError("Failed to generate file token.", nil)
}
if err := api.app.OnFileAfterTokenRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, map[string]string{
"token": e.Token,
return api.app.OnFileAfterTokenRequest().Trigger(event, func(e *core.FileTokenEvent) error {
return e.HttpContext.JSON(http.StatusOK, map[string]string{
"token": e.Token,
})
})
})
}

View File

@ -142,8 +142,11 @@ func (api *realtimeApi) connect(c echo.Context) error {
return nil
}
if err := api.app.OnRealtimeAfterMessageSend().Trigger(msgEvent); err != nil && api.app.IsDebug() {
log.Println("OnRealtimeAfterMessageSend error:", err)
if err := api.app.OnRealtimeAfterMessageSend().Trigger(msgEvent); err != nil {
if api.app.IsDebug() {
log.Println("Realtime connection closed (OnRealtimeAfterMessageSend error):", client.Id(), err)
}
return nil
}
idleTimer.Stop()
@ -202,11 +205,9 @@ func (api *realtimeApi) setSubscriptions(c echo.Context) error {
// subscribe to the new subscriptions
e.Client.Subscribe(e.Subscriptions...)
if err := api.app.OnRealtimeAfterSubscribeRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRealtimeAfterSubscribeRequest().Trigger(event, func(e *core.RealtimeSubscribeEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}

View File

@ -332,11 +332,9 @@ func (api *recordAuthApi) requestPasswordReset(c echo.Context) error {
}
})
if err := api.app.OnRecordAfterRequestPasswordResetRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterRequestPasswordResetRequest().Trigger(event, func(e *core.RecordRequestPasswordResetEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -374,11 +372,9 @@ func (api *recordAuthApi) confirmPasswordReset(c echo.Context) error {
return NewBadRequestError("Failed to set new password.", err)
}
if err := api.app.OnRecordAfterConfirmPasswordResetRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterConfirmPasswordResetRequest().Trigger(event, func(e *core.RecordConfirmPasswordResetEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -417,11 +413,9 @@ func (api *recordAuthApi) requestVerification(c echo.Context) error {
}
})
if err := api.app.OnRecordAfterRequestVerificationRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterRequestVerificationRequest().Trigger(event, func(e *core.RecordRequestVerificationEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -459,11 +453,9 @@ func (api *recordAuthApi) confirmVerification(c echo.Context) error {
return NewBadRequestError("An error occurred while submitting the form.", err)
}
if err := api.app.OnRecordAfterConfirmVerificationRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterConfirmVerificationRequest().Trigger(event, func(e *core.RecordConfirmVerificationEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -499,11 +491,9 @@ func (api *recordAuthApi) requestEmailChange(c echo.Context) error {
return NewBadRequestError("Failed to request email change.", err)
}
if err := api.app.OnRecordAfterRequestEmailChangeRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterRequestEmailChangeRequest().Trigger(event, func(e *core.RecordRequestEmailChangeEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -533,11 +523,9 @@ func (api *recordAuthApi) confirmEmailChange(c echo.Context) error {
return NewBadRequestError("Failed to confirm email change.", err)
}
if err := api.app.OnRecordAfterConfirmEmailChangeRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterConfirmEmailChangeRequest().Trigger(event, func(e *core.RecordConfirmEmailChangeEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}
})
@ -610,11 +598,9 @@ func (api *recordAuthApi) unlinkExternalAuth(c echo.Context) error {
return NewBadRequestError("Cannot unlink the external auth provider.", err)
}
if err := api.app.OnRecordAfterUnlinkExternalAuthRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterUnlinkExternalAuthRequest().Trigger(event, func(e *core.RecordUnlinkExternalAuthEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}

View File

@ -238,11 +238,9 @@ func (api *recordApi) create(c echo.Context) error {
log.Println(err)
}
if err := api.app.OnRecordAfterCreateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Record)
return api.app.OnRecordAfterCreateRequest().Trigger(event, func(e *core.RecordCreateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Record)
})
})
}
})
@ -323,11 +321,9 @@ func (api *recordApi) update(c echo.Context) error {
log.Println(err)
}
if err := api.app.OnRecordAfterUpdateRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.JSON(http.StatusOK, e.Record)
return api.app.OnRecordAfterUpdateRequest().Trigger(event, func(e *core.RecordUpdateEvent) error {
return e.HttpContext.JSON(http.StatusOK, e.Record)
})
})
}
})
@ -380,11 +376,9 @@ func (api *recordApi) delete(c echo.Context) error {
return NewBadRequestError("Failed to delete record. Make sure that the record is not part of a required relation reference.", err)
}
if err := api.app.OnRecordAfterDeleteRequest().Trigger(event); err != nil {
return err
}
return e.HttpContext.NoContent(http.StatusNoContent)
return api.app.OnRecordAfterDeleteRequest().Trigger(event, func(e *core.RecordDeleteEvent) error {
return e.HttpContext.NoContent(http.StatusNoContent)
})
})
}

View File

@ -63,16 +63,14 @@ func (api *settingsApi) set(c echo.Context) error {
return NewBadRequestError("An error occurred while submitting the form.", err)
}
if err := api.app.OnSettingsAfterUpdateRequest().Trigger(event); err != nil {
return err
}
return api.app.OnSettingsAfterUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
redactedSettings, err := api.app.Settings().RedactClone()
if err != nil {
return NewBadRequestError("", err)
}
redactedSettings, err := api.app.Settings().RedactClone()
if err != nil {
return NewBadRequestError("", err)
}
return e.HttpContext.JSON(http.StatusOK, redactedSettings)
return e.HttpContext.JSON(http.StatusOK, redactedSettings)
})
})
}
})