{"version":3,"sources":["decorators.ts"],"sourcesContent":["import { endpoint_name, target_clause } from \"../datex_all.ts\";\nimport { Endpoint } from \"../types/addressing.ts\";\nimport { PermissionError } from \"../types/errors.ts\";\nimport { JSTransferableFunction } from \"../types/js-function.ts\";\nimport type { Type } from \"../types/type.ts\";\nimport type { Class, datex_meta } from \"../utils/global_types.ts\";\nimport { Decorators } from \"./js_class_adapter.ts\";\n\n\nexport function handleClassFieldDecoratorWithOptionalArgs<C extends ClassFieldDecoratorContext, const T extends unknown[], R>(args:T, context: C|undefined, callback: (arg: T, context: C)=>R): ((value: undefined, context: C) => R)|R {\n\tif (!isDecoratorContext(context)) return (_value: undefined, context: C) => callback(args, context)\n\telse return callback([] as unknown as T, context!)\n}\nexport function handleClassFieldDecoratorWithArgs<C extends ClassFieldDecoratorContext, const T extends unknown[], R>(args:T, callback: (arg: T, context: C)=>R): ((value: undefined, context: C) => R) {\n\treturn (_value: undefined, context: C) => callback(args, context)\n}\nexport function handleClassDecoratorWithOptionalArgs<_Class extends Class, C extends ClassDecoratorContext<_Class>, const T extends unknown[], R>(args:T, value: _Class, context: C|undefined, callback: (arg: T, value: _Class, context: C)=>R): ((value: _Class, context: C) => R)|R {\n\tif (!isDecoratorContext(context)) return (value: _Class, context: C) => callback(args, value, context)\n\telse return callback([] as unknown as T, value, context!)\n}\nexport function handleClassDecoratorWithArgs<_Class extends Class, C extends ClassDecoratorContext<_Class>, const T extends unknown[], R>(args:T, callback: (arg: T, value: _Class, context: C)=>R): ((value: _Class, context: C) => R) {\n\treturn (value: _Class, context: C) => callback(args, value, context)\n}\nexport function handleClassMethodDecoratorWithArgs<C extends ClassMethodDecoratorContext, const T extends unknown[], R>(args:T, callback: (arg: T, value:(...args:any[])=>any, context: C)=>R): ((value: (...args:any[])=>any, context: C) => R) {\n\treturn (value: (...args:any[])=>any, context: C) => callback(args, value, context)\n}\n\nfunction isDecoratorContext(context: unknown) {\n\treturn context && typeof context === \"object\" && \"kind\" in context\n}\n\n\n\ntype PropertyDecoratorContext<T=unknown> = ClassFieldDecoratorContext<unknown, T|undefined>|ClassGetterDecoratorContext<unknown, T|undefined>|ClassMethodDecoratorContext<unknown, T&((...args: any[])=>any)>\n\n/**\n * Marks a (static) class field as a property accessible by DATEX.\n * @param type optional type for the property, must match the declared TypeScript type\n */\nexport function property<T>(type: string|Type<T>|Class<T>): (value: ((...args: any[])=>any)|undefined, context: PropertyDecoratorContext<T>)=>void\nexport function property(value: ((...args: any[])=>any)|undefined, context: PropertyDecoratorContext): void\nexport function property(type: ((...args: any[])=>any)|undefined|string|Type|Class, context?: PropertyDecoratorContext) {\n    return handleClassFieldDecoratorWithOptionalArgs([type], context as ClassFieldDecoratorContext, ([type], context:PropertyDecoratorContext) => {\n\t\treturn Decorators.property(type as Type, context)\n\t})\n}\n\n// TODO: experimental alias for @property:\n/**\n * Binds a (static) class property to a DATEX ref\n * @param type optional type for the ref, must match the declared TypeScript property type\n */\nexport function ref<T>(type: string|Type<T>|Class<T>): (value: ((...args: any[])=>any)|undefined, context: PropertyDecoratorContext<T>)=>void\nexport function ref(value: ((...args: any[])=>any)|undefined, context: PropertyDecoratorContext): void\nexport function ref(type: ((...args: any[])=>any)|undefined|string|Type|Class, context?: PropertyDecoratorContext) {\n    return handleClassFieldDecoratorWithOptionalArgs([type], context as ClassFieldDecoratorContext, ([type], context:PropertyDecoratorContext) => {\n\t\treturn Decorators.property(type as Type, context)\n\t})\n}\n\n\n/**\n * Adds an assertion to a class field that is checked before the field is set\n * @returns \n */\nexport function assert<T>(assertion:(val:T)=>boolean|string|undefined): (value: undefined, context: ClassFieldDecoratorContext<unknown, T|undefined>)=>void {\n\treturn handleClassFieldDecoratorWithArgs([assertion], ([assertion], context) => {\n\t\treturn Decorators.assert(assertion, context)\n\t})\n}\n\nexport function allow<T extends (...args:any)=>any>(assertion:(meta: datex_meta) => boolean|Promise<boolean>): (value: T, context: ClassMethodDecoratorContext<unknown, T>) => T {\n\treturn handleClassMethodDecoratorWithArgs([assertion], ([assertion], fn, context) => {\n\t\t// async\n\t\tif (JSTransferableFunction.functionIsAsync(assertion as (...args:any)=>any)) {\n\t\t\treturn async function(this:any, ...args:any) {\n\t\t\t\tif (!await assertion(datex.meta)) throw new PermissionError(\"Endpoint has no permission to call this function\")\n\t\t\t\treturn fn.apply(this, args)\n\t\t\t} as any\n\t\t}\n\t\t// sync\n\t\telse {\n\t\t\treturn function(this:any, ...args:any) {\n\t\t\t\tif (!assertion(datex.meta)) throw new PermissionError(\"Endpoint has no permission to call this function\")\n\t\t\t\treturn fn.apply(this, args)\n\t\t\t} as any\n\t\t}\n\t})\n}\n\n/**\n * Make a class publicly accessible for an endpoint (only static methods and properties marked with @property are exposed)\n * Also enables calling static class methods on other endpoints\n * @param endpoint \n * @param scope_name \n */\nexport function endpoint(endpoint:target_clause|endpoint_name, scope_name?:string): (value: Class, context: ClassDecoratorContext)=>void\nexport function endpoint(value: Class, context: ClassDecoratorContext): void\nexport function endpoint(value: Class|target_clause|endpoint_name, context?: ClassDecoratorContext|string) {\n\treturn handleClassDecoratorWithOptionalArgs([value as target_clause|endpoint_name, context as string], value as Class, context as ClassDecoratorContext, ([endpoint, scope_name], value, context) => {\n\t\treturn Decorators.endpoint(endpoint, scope_name, value, context)\n\t})\n}\n\n\n/**\n * Sets a class as the entrypoint for the current endpoint\n */\nexport function entrypoint(value: Class, context: ClassDecoratorContext) {\n\treturn Decorators.entrypoint(value, context)\n}\n\n/**\n * Adds a class as a property of entrypoint for the current endpoint\n */\nexport function entrypointProperty(value: Class, context: ClassDecoratorContext) {\n\treturn Decorators.entrypointProperty(value, context)\n}\n\n/**\n * Sets the maximum allowed time (in ms) for a remote function execution \n * before a timeout error is thrown (default: 5s)\n * @param timeMs timeout in ms\n * @returns \n */\nexport function timeout(timeMs:number): (value: (...args:any[])=>any, context: ClassMethodDecoratorContext)=>void {\n\treturn handleClassMethodDecoratorWithArgs([timeMs], ([timeMs], _value, context) => {\n\t\treturn Decorators.timeout(timeMs, context)\n\t});\n}\n\n/**\n * Maps a class to a corresponding DATEX type\n */\nexport function sync(type: string): (value: Class, context: ClassDecoratorContext)=>void\nexport function sync(value: Class, context: ClassDecoratorContext): void\nexport function sync(value: Class|string, context?: ClassDecoratorContext) {\n\treturn handleClassDecoratorWithOptionalArgs([value as string], value as Class, context as ClassDecoratorContext, ([type], value, context) => {\n\t\treturn Decorators.sync(type, value, context)\n\t})\n}"],"names":["PermissionError","JSTransferableFunction","Decorators","handleClassFieldDecoratorWithOptionalArgs","args","context","callback","isDecoratorContext","_value","handleClassFieldDecoratorWithArgs","handleClassDecoratorWithOptionalArgs","value","handleClassDecoratorWithArgs","handleClassMethodDecoratorWithArgs","property","type","ref","assert","assertion","allow","fn","functionIsAsync","datex","meta","apply","endpoint","scope_name","entrypoint","entrypointProperty","timeout","timeMs","sync"],"mappings":"AAEA,SAASA,eAAe,QAAQ,qBAAqB;AACrD,SAASC,sBAAsB,QAAQ,0BAA0B;AAGjE,SAASC,UAAU,QAAQ,wBAAwB;AAGnD,OAAO,SAASC,0CAA8GC,IAAM,EAAEC,OAAoB,EAAEC,QAAiC;WAC5L,AAAKC,mBAAmBF,WACZC,SAAS,EAAE,EAAkBD,WADA,CAACG,QAAmBH,UAAeC,SAASF,MAAMC;AAE5F;AACA,OAAO,SAASI,kCAAsGL,IAAM,EAAEE,QAAiC;IAC9J,OAAO,CAACE,QAAmBH,UAAeC,SAASF,MAAMC;AAC1D;AACA,OAAO,SAASK,qCAAkIN,IAAM,EAAEO,KAAa,EAAEN,OAAoB,EAAEC,QAAgD;WAC9O,AAAKC,mBAAmBF,WACZC,SAAS,EAAE,EAAkBK,OAAON,WADP,CAACM,OAAeN,UAAeC,SAASF,MAAMO,OAAON;AAE/F;AACA,OAAO,SAASO,6BAA0HR,IAAM,EAAEE,QAAgD;IACjM,OAAO,CAACK,OAAeN,UAAeC,SAASF,MAAMO,OAAON;AAC7D;AACA,OAAO,SAASQ,mCAAwGT,IAAM,EAAEE,QAA6D;IAC5L,O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