BindApis registers $apis.* namespaced object with reusable Web API handlers, middlewares and other related helpers. See https://pocketbase.io/jsvm/modules/_apis.html.
(vm *goja.Runtime)
| 843 | // |
| 844 | // See https://pocketbase.io/jsvm/modules/_apis.html. |
| 845 | func BindApis(vm *goja.Runtime) { |
| 846 | obj := vm.NewObject() |
| 847 | vm.Set("$apis", obj) |
| 848 | |
| 849 | obj.Set("static", func(dirOrFS any, indexFallback bool) func(*core.RequestEvent) error { |
| 850 | switch v := dirOrFS.(type) { |
| 851 | case fs.FS: |
| 852 | return apis.Static(v, indexFallback) |
| 853 | case string: |
| 854 | return apis.Static(os.DirFS(v), indexFallback) |
| 855 | default: |
| 856 | panic("$apis.static expects the first argument to be either a plain string path or fs.FS value") |
| 857 | } |
| 858 | }) |
| 859 | |
| 860 | // middlewares |
| 861 | obj.Set("requireGuestOnly", apis.RequireGuestOnly) |
| 862 | obj.Set("requireAuth", apis.RequireAuth) |
| 863 | obj.Set("requireSuperuserAuth", apis.RequireSuperuserAuth) |
| 864 | obj.Set("requireSuperuserOrOwnerAuth", apis.RequireSuperuserOrOwnerAuth) |
| 865 | obj.Set("skipSuccessActivityLog", apis.SkipSuccessActivityLog) |
| 866 | obj.Set("gzip", apis.Gzip) |
| 867 | obj.Set("bodyLimit", apis.BodyLimit) |
| 868 | |
| 869 | // record helpers |
| 870 | obj.Set("recordAuthResponse", apis.RecordAuthResponse) |
| 871 | obj.Set("enrichRecord", apis.EnrichRecord) |
| 872 | obj.Set("enrichRecords", apis.EnrichRecords) |
| 873 | |
| 874 | // api errors |
| 875 | registerFactoryAsConstructor(vm, "ApiError", router.NewApiError) |
| 876 | registerFactoryAsConstructor(vm, "NotFoundError", router.NewNotFoundError) |
| 877 | registerFactoryAsConstructor(vm, "BadRequestError", router.NewBadRequestError) |
| 878 | registerFactoryAsConstructor(vm, "ForbiddenError", router.NewForbiddenError) |
| 879 | registerFactoryAsConstructor(vm, "UnauthorizedError", router.NewUnauthorizedError) |
| 880 | registerFactoryAsConstructor(vm, "TooManyRequestsError", router.NewTooManyRequestsError) |
| 881 | registerFactoryAsConstructor(vm, "InternalServerError", router.NewInternalServerError) |
| 882 | } |
| 883 | |
| 884 | // BindHTTP registers $http.* namespaced object with common utils |
| 885 | // for sending HTTP requests. |
searching dependent graphs…