soft deprecated apis.RequestData(c) in favor of apis.RequestInfo(c) and updated jsvm bindings

This commit is contained in:
Gani Georgiev
2023-07-17 23:13:39 +03:00
parent 7d4017225c
commit 0110869c89
22 changed files with 3158 additions and 2990 deletions
+20 -1
View File
@@ -319,6 +319,25 @@ func baseBinds(vm *goja.Runtime) {
return structConstructor(vm, call, instance)
})
vm.Set("RequestInfo", func(call goja.ConstructorCall) *goja.Object {
instance := &models.RequestInfo{}
return structConstructor(vm, call, instance)
})
vm.Set("DateTime", func(call goja.ConstructorCall) *goja.Object {
instance := types.NowDateTime()
val, _ := call.Argument(0).Export().(string)
if val != "" {
instance, _ = types.ParseDateTime(val)
}
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return structConstructor(vm, call, instance)
})
vm.Set("ValidationError", func(call goja.ConstructorCall) *goja.Object {
code, _ := call.Argument(0).Export().(string)
message, _ := call.Argument(1).Export().(string)
@@ -462,7 +481,7 @@ func apisBinds(vm *goja.Runtime) {
obj.Set("activityLogger", apis.ActivityLogger)
// record helpers
obj.Set("requestData", apis.RequestData)
obj.Set("requestInfo", apis.RequestInfo)
obj.Set("recordAuthResponse", apis.RecordAuthResponse)
obj.Set("enrichRecord", apis.EnrichRecord)
obj.Set("enrichRecords", apis.EnrichRecords)
+45 -1
View File
@@ -45,7 +45,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)
testBindsCount(vm, "this", 14, t)
testBindsCount(vm, "this", 16, t)
}
func TestBaseBindsRecord(t *testing.T) {
@@ -248,6 +248,50 @@ func TestBaseBindsCommand(t *testing.T) {
}
}
func TestBaseBindsRequestInfo(t *testing.T) {
vm := goja.New()
baseBinds(vm)
_, err := vm.RunString(`
let info = new RequestInfo({
admin: new Admin({id: "test1"}),
data: {"name": "test2"}
});
if (info.admin?.id != "test1") {
throw new Error('Expected info.admin.id to be test1, got: ' + info.admin?.id);
}
if (info.data?.name != "test2") {
throw new Error('Expected info.data.name to be test2, got: ' + info.data?.name);
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsDateTime(t *testing.T) {
vm := goja.New()
baseBinds(vm)
_, err := vm.RunString(`
const v0 = new DateTime();
if (v0.isZero()) {
throw new Error('Expected to fallback to now, got zero value');
}
const v1 = new DateTime('2023-01-01 00:00:00.000Z');
const expected = "2023-01-01 00:00:00.000Z"
if (v1.string() != expected) {
throw new Error('Expected ' + expected + ', got ', v1.string());
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsValidationError(t *testing.T) {
vm := goja.New()
baseBinds(vm)
File diff suppressed because it is too large Load Diff
+48 -1
View File
@@ -308,6 +308,8 @@ interface Command extends cobra.Command{} // merge
/**
* Command defines a single console command.
*
* Example:
*
* ` + "```" + `js
* const command = new Command({
* use: "hello",
@@ -323,6 +325,51 @@ declare class Command implements cobra.Command {
constructor(cmd?: Partial<cobra.Command>)
}
interface RequestInfo extends models.RequestInfo{} // merge
/**
* RequestInfo defines a single models.RequestInfo instance, usually used
* as part of various filter checks.
*
* Example:
*
* ` + "```" + `js
* const authRecord = $app.dao().findAuthRecordByEmail("users", "test@example.com")
*
* const info = new RequestInfo({
* authRecord: authRecord,
* data: {"name": 123},
* headers: {"x-token": "..."},
* })
*
* const record = $app.dao().findFirstRecordByData("articles", "slug", "hello")
*
* const canAccess = $app.dao().canAccessRecord(record, info, "@request.auth.id != '' && @request.data.name = 123")
* ` + "```" + `
*
* @group PocketBase
*/
declare class RequestInfo implements models.RequestInfo {
constructor(date?: Partial<models.RequestInfo>)
}
interface DateTime extends types.DateTime{} // merge
/**
* DateTime defines a single DateTime type instance.
*
* Example:
*
* ` + "```" + `js
* const dt0 = new DateTime() // now
*
* const dt1 = new DateTime('2023-07-01 00:00:00.000Z')
* ` + "```" + `
*
* @group PocketBase
*/
declare class DateTime implements types.DateTime {
constructor(date?: string)
}
interface ValidationError extends ozzo_validation.Error{} // merge
/**
* ValidationError defines a single formatted data validation error,
@@ -690,7 +737,7 @@ declare namespace $apis {
let requireAdminOrRecordAuth: apis.requireAdminOrRecordAuth
let requireAdminOrOwnerAuth: apis.requireAdminOrOwnerAuth
let activityLogger: apis.activityLogger
let requestData: apis.requestData
let requestInfo: apis.requestInfo
let recordAuthResponse: apis.recordAuthResponse
let enrichRecord: apis.enrichRecord
let enrichRecords: apis.enrichRecords