diff --git a/src/core/generate/rs/src/bindings/typescript_native/compile_interface.rs b/src/core/generate/rs/src/bindings/typescript_native/compile_interface.rs index 813bb46..f8f9548 100644 --- a/src/core/generate/rs/src/bindings/typescript_native/compile_interface.rs +++ b/src/core/generate/rs/src/bindings/typescript_native/compile_interface.rs @@ -184,14 +184,6 @@ pub fn interface_actor_var(module: &mut Module, type_id: &str, service_name: &st } fn add_create_actor_interface_exports(module: &mut Module, service_name: &str) { - let type_process_error_fn = super::preamble::actor::process_error_fn_type(); - module - .body - .push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { - span: DUMMY_SP, - decl: Decl::TsTypeAlias(Box::new(type_process_error_fn)), - }))); - // CreateActorOptions interface let create_actor_options_interface = super::preamble::actor::create_actor_options_interface(); module @@ -245,26 +237,6 @@ fn add_create_actor_interface_exports(module: &mut Module, service_name: &str) { })), }), }, - Param { - span: DUMMY_SP, - decorators: vec![], - pat: Pat::Ident(BindingIdent { - id: Ident { - span: DUMMY_SP, - sym: "processError".into(), - optional: true, - ctxt: swc_core::common::SyntaxContext::empty(), - }, - type_ann: Some(Box::new(TsTypeAnn { - span: DUMMY_SP, - type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { - span: DUMMY_SP, - type_name: TsEntityName::Ident(get_ident_guarded("ProcessErrorFn")), - type_params: None, - })), - })), - }), - }, ], decorators: vec![], span: DUMMY_SP, diff --git a/src/core/generate/rs/src/bindings/typescript_native/compile_wrapper.rs b/src/core/generate/rs/src/bindings/typescript_native/compile_wrapper.rs index 46569ae..d4f8c00 100644 --- a/src/core/generate/rs/src/bindings/typescript_native/compile_wrapper.rs +++ b/src/core/generate/rs/src/bindings/typescript_native/compile_wrapper.rs @@ -50,7 +50,6 @@ pub fn compile_wrapper( }; if let Some(actor_type) = actor { - type_process_error_fn(&mut actor_module); let syntax_actor = prog.resolve_actor().ok().flatten(); let span = syntax_actor .as_ref() @@ -217,121 +216,50 @@ fn create_actor_class( serv: &[(String, Type)], converter: &mut TypeConverter, ) -> ClassDecl { - // Create private actor field - let actor_field = ClassMember::ClassProp(ClassProp { + // Create constructor + let constructor = ClassMember::Constructor(Constructor { span: DUMMY_SP, - key: PropName::Ident(IdentName { - span: DUMMY_SP, - sym: "actor".into(), - }), - value: None, - type_ann: Some(Box::new(TsTypeAnn { + key: PropName::Ident( + Ident::new("constructor".into(), DUMMY_SP, SyntaxContext::empty()).into(), + ), + params: vec![ParamOrTsParamProp::TsParamProp(TsParamProp { span: DUMMY_SP, - type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { - span: DUMMY_SP, - type_name: TsEntityName::Ident(Ident::new( - "ActorSubclass".into(), - DUMMY_SP, - SyntaxContext::empty(), - )), - type_params: Some(Box::new(TsTypeParamInstantiation { + decorators: vec![], + accessibility: Some(Accessibility::Private), + is_override: false, + readonly: false, + param: TsParamPropParam::Ident(BindingIdent { + id: Ident { + span: DUMMY_SP, + sym: "actor".into(), + optional: false, + ctxt: SyntaxContext::empty(), + }, + type_ann: Some(Box::new(TsTypeAnn { span: DUMMY_SP, - params: vec![Box::new(TsType::TsTypeRef(TsTypeRef { + type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { span: DUMMY_SP, type_name: TsEntityName::Ident(Ident::new( - "_SERVICE".into(), + "ActorSubclass".into(), DUMMY_SP, SyntaxContext::empty(), )), - type_params: None, - }))], - })), - })), - })), - accessibility: Some(Accessibility::Private), - is_static: false, - decorators: vec![], - is_abstract: false, - declare: false, - is_optional: false, - is_override: false, - readonly: false, - definite: false, - }); - - // Create constructor - let constructor = ClassMember::Constructor(Constructor { - span: DUMMY_SP, - key: PropName::Ident( - Ident::new("constructor".into(), DUMMY_SP, SyntaxContext::empty()).into(), - ), - params: vec![ - ParamOrTsParamProp::TsParamProp(TsParamProp { - span: DUMMY_SP, - decorators: vec![], - accessibility: Some(Accessibility::Private), - is_override: false, - readonly: false, - param: TsParamPropParam::Ident(BindingIdent { - id: Ident { - span: DUMMY_SP, - sym: "actor".into(), - optional: false, - ctxt: SyntaxContext::empty(), - }, - type_ann: Some(Box::new(TsTypeAnn { - span: DUMMY_SP, - type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { + type_params: Some(Box::new(TsTypeParamInstantiation { span: DUMMY_SP, - type_name: TsEntityName::Ident(Ident::new( - "ActorSubclass".into(), - DUMMY_SP, - SyntaxContext::empty(), - )), - type_params: Some(Box::new(TsTypeParamInstantiation { + params: vec![Box::new(TsType::TsTypeRef(TsTypeRef { span: DUMMY_SP, - params: vec![Box::new(TsType::TsTypeRef(TsTypeRef { - span: DUMMY_SP, - type_name: TsEntityName::Ident(Ident::new( - "_SERVICE".into(), - DUMMY_SP, - SyntaxContext::empty(), - )), - type_params: None, - }))], - })), - })), - })), - }), - }), - ParamOrTsParamProp::TsParamProp(TsParamProp { - span: DUMMY_SP, - decorators: vec![], - accessibility: Some(Accessibility::Private), - is_override: false, - readonly: false, - param: TsParamPropParam::Ident(BindingIdent { - id: Ident { - span: DUMMY_SP, - sym: "processError".into(), - optional: true, - ctxt: SyntaxContext::empty(), - }, - type_ann: Some(Box::new(TsTypeAnn { - span: DUMMY_SP, - type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { - span: DUMMY_SP, - type_name: TsEntityName::Ident(Ident::new( - "ProcessErrorFn".into(), - DUMMY_SP, - SyntaxContext::empty(), - )), - type_params: None, + type_name: TsEntityName::Ident(Ident::new( + "_SERVICE".into(), + DUMMY_SP, + SyntaxContext::empty(), + )), + type_params: None, + }))], })), })), - }), + })), }), - ], + })], body: Some(BlockStmt { span: DUMMY_SP, stmts: vec![], @@ -363,15 +291,15 @@ fn create_actor_class( .collect(); // Combine all members - let mut members = vec![actor_field, constructor]; - members.extend(methods); + let mut class_body_members = vec![constructor]; + class_body_members.extend(methods); ClassDecl { ident: get_ident_guarded(capitalized_service_name), declare: false, class: Box::new(Class { span: DUMMY_SP, - body: members, + body: class_body_members, super_class: None, type_params: None, super_type_params: None, @@ -568,110 +496,6 @@ fn create_actor_method( }), ]; - // Method body with conditional error handling - let body_stmts = vec![ - // if (this.processError) { - Stmt::If(IfStmt { - span: DUMMY_SP, - test: Box::new(Expr::Member(MemberExpr { - span: DUMMY_SP, - obj: Box::new(Expr::This(ThisExpr { span: DUMMY_SP })), - prop: MemberProp::Ident( - Ident::new("processError".into(), DUMMY_SP, SyntaxContext::empty()).into(), - ), - })), - cons: Box::new(Stmt::Block(BlockStmt { - span: DUMMY_SP, - stmts: vec![ - // try { - Stmt::Try(Box::new(TryStmt { - span: DUMMY_SP, - block: BlockStmt { - span: DUMMY_SP, - stmts: call_and_convert_stmts.clone(), - ctxt: SyntaxContext::empty(), - }, - handler: Some(CatchClause { - span: DUMMY_SP, - param: Some(Pat::Ident(BindingIdent { - id: Ident::new("e".into(), DUMMY_SP, SyntaxContext::empty()), - type_ann: None, - })), - body: BlockStmt { - span: DUMMY_SP, - stmts: vec![ - // this.processError(e); - Stmt::Expr(ExprStmt { - span: DUMMY_SP, - expr: Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: Callee::Expr(Box::new(Expr::Member( - MemberExpr { - span: DUMMY_SP, - obj: Box::new(Expr::This(ThisExpr { - span: DUMMY_SP, - })), - prop: MemberProp::Ident( - Ident::new( - "processError".into(), - DUMMY_SP, - SyntaxContext::empty(), - ) - .into(), - ), - }, - ))), - args: vec![ExprOrSpread { - spread: None, - expr: Box::new(Expr::Ident(Ident::new( - "e".into(), - DUMMY_SP, - SyntaxContext::empty(), - ))), - }], - type_args: None, - ctxt: SyntaxContext::empty(), - })), - }), - // throw new Error("unreachable"); - Stmt::Throw(ThrowStmt { - span: DUMMY_SP, - arg: Box::new(Expr::New(NewExpr { - span: DUMMY_SP, - callee: Box::new(Expr::Ident(Ident::new( - "Error".into(), - DUMMY_SP, - SyntaxContext::empty(), - ))), - args: Some(vec![ExprOrSpread { - spread: None, - expr: Box::new(Expr::Lit(Lit::Str(Str { - span: DUMMY_SP, - value: "unreachable".into(), - raw: None, - }))), - }]), - type_args: None, - ctxt: SyntaxContext::empty(), - })), - }), - ], - ctxt: SyntaxContext::empty(), - }, - }), - finalizer: None, - })), - ], - ctxt: SyntaxContext::empty(), - })), - alt: Some(Box::new(Stmt::Block(BlockStmt { - span: DUMMY_SP, - stmts: call_and_convert_stmts, - ctxt: SyntaxContext::empty(), - }))), - }), - ]; - ClassMember::Method(ClassMethod { span: DUMMY_SP, key: PropName::Ident(get_ident_guarded(method_id).into()), @@ -681,7 +505,7 @@ fn create_actor_method( span: DUMMY_SP, body: Some(BlockStmt { span: DUMMY_SP, - stmts: body_stmts, + stmts: call_and_convert_stmts, ctxt: SyntaxContext::empty(), }), is_generator: false, @@ -702,17 +526,6 @@ fn create_actor_method( }) } -fn type_process_error_fn(module: &mut Module) { - let type_declaration = super::preamble::actor::process_error_fn_type(); - - module - .body - .push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { - span: DUMMY_SP, - decl: Decl::TsTypeAlias(Box::new(type_declaration)), - }))); -} - fn add_create_actor_exports(module: &mut Module, service_name: &str) { // CreateActorOptions interface let create_actor_options_interface = super::preamble::actor::create_actor_options_interface(); @@ -791,31 +604,6 @@ fn create_actor_function(service_name: &str) -> FnDecl { })), }), }, - // processError?: ProcessErrorFn - Param { - span, - decorators: vec![], - pat: Pat::Ident(BindingIdent { - id: Ident { - span, - sym: "processError".into(), - optional: true, - ctxt: SyntaxContext::empty(), - }, - type_ann: Some(Box::new(TsTypeAnn { - span, - type_ann: Box::new(TsType::TsTypeRef(TsTypeRef { - span, - type_name: TsEntityName::Ident(Ident::new( - "ProcessErrorFn".into(), - span, - SyntaxContext::empty(), - )), - type_params: None, - })), - })), - }), - }, ], decorators: vec![], span, @@ -1074,7 +862,7 @@ fn create_actor_function_body(capitalized_service_name: &str) -> Vec { }], ctxt: SyntaxContext::empty(), }))), - // return new Service(actor, processError); + // return new Service(actor); Stmt::Return(ReturnStmt { span, arg: Some(Box::new(Expr::New(NewExpr { @@ -1093,14 +881,6 @@ fn create_actor_function_body(capitalized_service_name: &str) -> Vec { SyntaxContext::empty(), ))), }, - ExprOrSpread { - spread: None, - expr: Box::new(Expr::Ident(Ident::new( - "processError".into(), - span, - SyntaxContext::empty(), - ))), - }, ]), type_args: None, ctxt: SyntaxContext::empty(), diff --git a/src/core/generate/rs/src/bindings/typescript_native/preamble/actor.rs b/src/core/generate/rs/src/bindings/typescript_native/preamble/actor.rs index 67cd11e..a61ab47 100644 --- a/src/core/generate/rs/src/bindings/typescript_native/preamble/actor.rs +++ b/src/core/generate/rs/src/bindings/typescript_native/preamble/actor.rs @@ -1,4 +1,4 @@ -use swc_core::common::{DUMMY_SP, SyntaxContext}; +use swc_core::common::DUMMY_SP; use swc_core::ecma::ast::*; use crate::bindings::typescript_native::utils::get_ident_guarded; @@ -63,35 +63,3 @@ pub fn create_actor_options_interface() -> TsInterfaceDecl { }, } } - -pub fn process_error_fn_type() -> TsTypeAliasDecl { - TsTypeAliasDecl { - span: DUMMY_SP, - declare: false, - id: Ident::new("ProcessErrorFn".into(), DUMMY_SP, SyntaxContext::empty()), - type_params: None, - type_ann: Box::new(TsType::TsFnOrConstructorType( - TsFnOrConstructorType::TsFnType(TsFnType { - span: DUMMY_SP, - params: vec![TsFnParam::Ident(BindingIdent { - id: Ident::new("error".into(), DUMMY_SP, SyntaxContext::empty()), - type_ann: Some(Box::new(TsTypeAnn { - span: DUMMY_SP, - type_ann: Box::new(TsType::TsKeywordType(TsKeywordType { - span: DUMMY_SP, - kind: TsKeywordTypeKind::TsUnknownKeyword, - })), - })), - })], - type_params: None, - type_ann: Box::new(TsTypeAnn { - span: DUMMY_SP, - type_ann: Box::new(TsType::TsKeywordType(TsKeywordType { - span: DUMMY_SP, - kind: TsKeywordTypeKind::TsNeverKeyword, - })), - }), - }), - )), - } -} diff --git a/tests/snapshots/generate/example/example.d.ts.snapshot b/tests/snapshots/generate/example/example.d.ts.snapshot index f8a2b1f..9f5ecc3 100644 --- a/tests/snapshots/generate/example/example.d.ts.snapshot +++ b/tests/snapshots/generate/example/example.d.ts.snapshot @@ -209,10 +209,9 @@ export interface exampleInterface { */ bbbbb(arg0: b): Promise; } -export type ProcessErrorFn = (error: unknown) => never; export interface CreateActorOptions { agent?: Agent; agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): exampleInterface; +export declare function createActor(canisterId: string, options: CreateActorOptions = {}): exampleInterface; diff --git a/tests/snapshots/generate/example/example.ts.snapshot b/tests/snapshots/generate/example/example.ts.snapshot index c7e3164..fda0821 100644 --- a/tests/snapshots/generate/example/example.ts.snapshot +++ b/tests/snapshots/generate/example/example.ts.snapshot @@ -202,7 +202,6 @@ export enum Variant_a_b { a = "a", b = "b" } -export type ProcessErrorFn = (error: unknown) => never; /** * Doc comment for service */ @@ -246,43 +245,18 @@ export interface exampleInterface { } import type { A as _A, B as _B, List as _List, a as _a, b as _b, list as _list, my_variant as _my_variant, nested as _nested, nested_records as _nested_records, nested_res as _nested_res, node as _node, res as _res, stream as _stream, tree as _tree } from "./declarations/example.did.d.ts"; export class Example implements exampleInterface { - private actor: ActorSubclass<_SERVICE>; - constructor(private actor: ActorSubclass<_SERVICE>, private processError?: ProcessErrorFn){} + constructor(private actor: ActorSubclass<_SERVICE>){} async f1(arg0: list, arg1: Uint8Array | number[], arg2: boolean | null): Promise { - if (this.processError) { - try { - const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); - return result; - } + const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); + return result; } async g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]> { - if (this.processError) { - try { - const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); - return [ - result[0], - result[1], - from_candid_nested_res_n13(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); - return [ - result[0], - result[1], - from_candid_nested_res_n13(result[2]) - ]; - } + const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); + return [ + result[0], + result[1], + from_candid_nested_res_n13(result[2]) + ]; } async h(arg0: Array, arg1: { __kind__: "A"; @@ -295,38 +269,15 @@ export class Example implements exampleInterface { }; id: bigint; }> { - if (this.processError) { - try { - const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); - return result; - } + const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); + return result; } async i(arg0: List, arg1: [Principal, string]): Promise<[List | null, res]> { - if (this.processError) { - try { - const result = await this.actor.i(to_candid_List_n6(arg0), arg1); - return [ - from_candid_opt_n20(result[0]), - from_candid_res_n24(result[1]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.i(to_candid_List_n6(arg0), arg1); - return [ - from_candid_opt_n20(result[0]), - from_candid_res_n24(result[1]) - ]; - } + const result = await this.actor.i(to_candid_List_n6(arg0), arg1); + return [ + from_candid_opt_n20(result[0]), + from_candid_res_n24(result[1]) + ]; } async x(arg0: a, arg1: b): Promise<[a | null, b | null, { __kind__: "Ok"; @@ -337,90 +288,32 @@ export class Example implements exampleInterface { __kind__: "Err"; Err: Variant_a_b; }]> { - if (this.processError) { - try { - const result = await this.actor.x(to_candid_a_n26(arg0), arg1); - return [ - from_candid_opt_n28(result[0]), - from_candid_opt_n31(result[1]), - from_candid_variant_n32(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.x(to_candid_a_n26(arg0), arg1); - return [ - from_candid_opt_n28(result[0]), - from_candid_opt_n31(result[1]), - from_candid_variant_n32(result[2]) - ]; - } + const result = await this.actor.x(to_candid_a_n26(arg0), arg1); + return [ + from_candid_opt_n28(result[0]), + from_candid_opt_n31(result[1]), + from_candid_variant_n32(result[2]) + ]; } async y(arg0: nested_records): Promise<[nested_records, my_variant]> { - if (this.processError) { - try { - const result = await this.actor.y(to_candid_nested_records_n34(arg0)); - return from_candid_tuple_n36(result); - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.y(to_candid_nested_records_n34(arg0)); - return from_candid_tuple_n36(result); - } + const result = await this.actor.y(to_candid_nested_records_n34(arg0)); + return from_candid_tuple_n36(result); } async f(arg0: Principal): Promise { - if (this.processError) { - try { - const result = await this.actor.f(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.f(arg0); - return result; - } + const result = await this.actor.f(arg0); + return result; } async g(arg0: list): Promise<[B, tree, stream]> { - if (this.processError) { - try { - const result = await this.actor.g(to_candid_list_n1(arg0)); - return [ - from_candid_B_n43(result[0]), - from_candid_tree_n46(result[1]), - from_candid_stream_n49(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.g(to_candid_list_n1(arg0)); - return [ - from_candid_B_n43(result[0]), - from_candid_tree_n46(result[1]), - from_candid_stream_n49(result[2]) - ]; - } + const result = await this.actor.g(to_candid_list_n1(arg0)); + return [ + from_candid_B_n43(result[0]), + from_candid_tree_n46(result[1]), + from_candid_stream_n49(result[2]) + ]; } async bbbbb(arg0: b): Promise { - if (this.processError) { - try { - const result = await this.actor.bbbbb(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.bbbbb(arg0); - return result; - } + const result = await this.actor.bbbbb(arg0); + return result; } } function from_candid_A_n45(value: _A): A { @@ -932,7 +825,7 @@ export interface CreateActorOptions { agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Example { +export function createActor(canisterId: string, options: CreateActorOptions = {}): Example { const agent = options.agent || HttpAgent.createSync({ ...options.agentOptions }); @@ -944,5 +837,5 @@ export function createActor(canisterId: string, options: CreateActorOptions = {} canisterId: canisterId, ...options.actorOptions }); - return new Example(actor, processError); + return new Example(actor); } diff --git a/tests/snapshots/generate/hello_world/hello_world.d.ts.snapshot b/tests/snapshots/generate/hello_world/hello_world.d.ts.snapshot index e86a926..a98e0b9 100644 --- a/tests/snapshots/generate/hello_world/hello_world.d.ts.snapshot +++ b/tests/snapshots/generate/hello_world/hello_world.d.ts.snapshot @@ -20,10 +20,9 @@ export type Option = Some | None; export interface hello_worldInterface { greet(name: string): Promise; } -export type ProcessErrorFn = (error: unknown) => never; export interface CreateActorOptions { agent?: Agent; agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): hello_worldInterface; +export declare function createActor(canisterId: string, options: CreateActorOptions = {}): hello_worldInterface; diff --git a/tests/snapshots/generate/hello_world/hello_world.ts.snapshot b/tests/snapshots/generate/hello_world/hello_world.ts.snapshot index 2401b9d..de71ac1 100644 --- a/tests/snapshots/generate/hello_world/hello_world.ts.snapshot +++ b/tests/snapshots/generate/hello_world/hello_world.ts.snapshot @@ -51,26 +51,14 @@ function candid_none(): [] { function record_opt_to_undefined(arg: T | null): T | undefined { return arg == null ? undefined : arg; } -export type ProcessErrorFn = (error: unknown) => never; export interface hello_worldInterface { greet(name: string): Promise; } export class Hello_world implements hello_worldInterface { - private actor: ActorSubclass<_SERVICE>; - constructor(private actor: ActorSubclass<_SERVICE>, private processError?: ProcessErrorFn){} + constructor(private actor: ActorSubclass<_SERVICE>){} async greet(arg0: string): Promise { - if (this.processError) { - try { - const result = await this.actor.greet(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.greet(arg0); - return result; - } + const result = await this.actor.greet(arg0); + return result; } } export interface CreateActorOptions { @@ -78,7 +66,7 @@ export interface CreateActorOptions { agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Hello_world { +export function createActor(canisterId: string, options: CreateActorOptions = {}): Hello_world { const agent = options.agent || HttpAgent.createSync({ ...options.agentOptions }); @@ -90,5 +78,5 @@ export function createActor(canisterId: string, options: CreateActorOptions = {} canisterId: canisterId, ...options.actorOptions }); - return new Hello_world(actor, processError); + return new Hello_world(actor); } diff --git a/tests/snapshots/wasm-generate/example/example.d.ts.snapshot b/tests/snapshots/wasm-generate/example/example.d.ts.snapshot index 2ba3715..4cf5c70 100644 --- a/tests/snapshots/wasm-generate/example/example.d.ts.snapshot +++ b/tests/snapshots/wasm-generate/example/example.d.ts.snapshot @@ -201,10 +201,9 @@ export interface exampleInterface { */ bbbbb(arg0: b): Promise; } -export type ProcessErrorFn = (error: unknown) => never; export interface CreateActorOptions { agent?: Agent; agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): exampleInterface; +export declare function createActor(canisterId: string, options: CreateActorOptions = {}): exampleInterface; diff --git a/tests/snapshots/wasm-generate/example/example.ts.snapshot b/tests/snapshots/wasm-generate/example/example.ts.snapshot index 40bb8d6..667f456 100644 --- a/tests/snapshots/wasm-generate/example/example.ts.snapshot +++ b/tests/snapshots/wasm-generate/example/example.ts.snapshot @@ -194,7 +194,6 @@ export enum Variant_a_b { a = "a", b = "b" } -export type ProcessErrorFn = (error: unknown) => never; /** * Doc comment for service */ @@ -238,43 +237,18 @@ export interface exampleInterface { } import type { A as _A, B as _B, List as _List, a as _a, b as _b, list as _list, my_variant as _my_variant, nested as _nested, nested_records as _nested_records, nested_res as _nested_res, node as _node, res as _res, stream as _stream, tree as _tree } from "./declarations/example.did.d.ts"; export class Example implements exampleInterface { - private actor: ActorSubclass<_SERVICE>; - constructor(private actor: ActorSubclass<_SERVICE>, private processError?: ProcessErrorFn){} + constructor(private actor: ActorSubclass<_SERVICE>){} async f1(arg0: list, arg1: Uint8Array | number[], arg2: boolean | null): Promise { - if (this.processError) { - try { - const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); - return result; - } + const result = await this.actor.f1(to_candid_list_n1(arg0), arg1, to_candid_opt_n5(arg2)); + return result; } async g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]> { - if (this.processError) { - try { - const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); - return [ - result[0], - result[1], - from_candid_nested_res_n13(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); - return [ - result[0], - result[1], - from_candid_nested_res_n13(result[2]) - ]; - } + const result = await this.actor.g1(arg0, to_candid_List_n6(arg1), to_candid_opt_n9(arg2), to_candid_nested_n10(arg3)); + return [ + result[0], + result[1], + from_candid_nested_res_n13(result[2]) + ]; } async h(arg0: Array, arg1: { __kind__: "A"; @@ -287,38 +261,15 @@ export class Example implements exampleInterface { }; id: bigint; }> { - if (this.processError) { - try { - const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); - return result; - } + const result = await this.actor.h(to_candid_vec_n17(arg0), to_candid_variant_n19(arg1), to_candid_opt_n9(arg2)); + return result; } async i(arg0: List, arg1: [Principal, string]): Promise<[List | null, res]> { - if (this.processError) { - try { - const result = await this.actor.i(to_candid_List_n6(arg0), arg1); - return [ - from_candid_opt_n20(result[0]), - from_candid_res_n24(result[1]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.i(to_candid_List_n6(arg0), arg1); - return [ - from_candid_opt_n20(result[0]), - from_candid_res_n24(result[1]) - ]; - } + const result = await this.actor.i(to_candid_List_n6(arg0), arg1); + return [ + from_candid_opt_n20(result[0]), + from_candid_res_n24(result[1]) + ]; } async x(arg0: a, arg1: b): Promise<[a | null, b | null, { __kind__: "Ok"; @@ -329,90 +280,32 @@ export class Example implements exampleInterface { __kind__: "Err"; Err: Variant_a_b; }]> { - if (this.processError) { - try { - const result = await this.actor.x(to_candid_a_n26(arg0), arg1); - return [ - from_candid_opt_n28(result[0]), - from_candid_opt_n31(result[1]), - from_candid_variant_n32(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.x(to_candid_a_n26(arg0), arg1); - return [ - from_candid_opt_n28(result[0]), - from_candid_opt_n31(result[1]), - from_candid_variant_n32(result[2]) - ]; - } + const result = await this.actor.x(to_candid_a_n26(arg0), arg1); + return [ + from_candid_opt_n28(result[0]), + from_candid_opt_n31(result[1]), + from_candid_variant_n32(result[2]) + ]; } async y(arg0: nested_records): Promise<[nested_records, my_variant]> { - if (this.processError) { - try { - const result = await this.actor.y(to_candid_nested_records_n34(arg0)); - return from_candid_tuple_n36(result); - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.y(to_candid_nested_records_n34(arg0)); - return from_candid_tuple_n36(result); - } + const result = await this.actor.y(to_candid_nested_records_n34(arg0)); + return from_candid_tuple_n36(result); } async f(arg0: Principal): Promise { - if (this.processError) { - try { - const result = await this.actor.f(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.f(arg0); - return result; - } + const result = await this.actor.f(arg0); + return result; } async g(arg0: list): Promise<[B, tree, stream]> { - if (this.processError) { - try { - const result = await this.actor.g(to_candid_list_n1(arg0)); - return [ - from_candid_B_n43(result[0]), - from_candid_tree_n46(result[1]), - from_candid_stream_n49(result[2]) - ]; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.g(to_candid_list_n1(arg0)); - return [ - from_candid_B_n43(result[0]), - from_candid_tree_n46(result[1]), - from_candid_stream_n49(result[2]) - ]; - } + const result = await this.actor.g(to_candid_list_n1(arg0)); + return [ + from_candid_B_n43(result[0]), + from_candid_tree_n46(result[1]), + from_candid_stream_n49(result[2]) + ]; } async bbbbb(arg0: b): Promise { - if (this.processError) { - try { - const result = await this.actor.bbbbb(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.bbbbb(arg0); - return result; - } + const result = await this.actor.bbbbb(arg0); + return result; } } function from_candid_A_n45(value: _A): A { @@ -924,7 +817,7 @@ export interface CreateActorOptions { agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Example { +export function createActor(canisterId: string, options: CreateActorOptions = {}): Example { const agent = options.agent || HttpAgent.createSync({ ...options.agentOptions }); @@ -936,5 +829,5 @@ export function createActor(canisterId: string, options: CreateActorOptions = {} canisterId: canisterId, ...options.actorOptions }); - return new Example(actor, processError); + return new Example(actor); } diff --git a/tests/snapshots/wasm-generate/hello_world/hello_world.d.ts.snapshot b/tests/snapshots/wasm-generate/hello_world/hello_world.d.ts.snapshot index a912330..9296f12 100644 --- a/tests/snapshots/wasm-generate/hello_world/hello_world.d.ts.snapshot +++ b/tests/snapshots/wasm-generate/hello_world/hello_world.d.ts.snapshot @@ -12,10 +12,9 @@ export type Option = Some | None; export interface hello_worldInterface { greet(name: string): Promise; } -export type ProcessErrorFn = (error: unknown) => never; export interface CreateActorOptions { agent?: Agent; agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): hello_worldInterface; +export declare function createActor(canisterId: string, options: CreateActorOptions = {}): hello_worldInterface; diff --git a/tests/snapshots/wasm-generate/hello_world/hello_world.ts.snapshot b/tests/snapshots/wasm-generate/hello_world/hello_world.ts.snapshot index 1476d8a..092b587 100644 --- a/tests/snapshots/wasm-generate/hello_world/hello_world.ts.snapshot +++ b/tests/snapshots/wasm-generate/hello_world/hello_world.ts.snapshot @@ -43,26 +43,14 @@ function candid_none(): [] { function record_opt_to_undefined(arg: T | null): T | undefined { return arg == null ? undefined : arg; } -export type ProcessErrorFn = (error: unknown) => never; export interface hello_worldInterface { greet(name: string): Promise; } export class Hello_world implements hello_worldInterface { - private actor: ActorSubclass<_SERVICE>; - constructor(private actor: ActorSubclass<_SERVICE>, private processError?: ProcessErrorFn){} + constructor(private actor: ActorSubclass<_SERVICE>){} async greet(arg0: string): Promise { - if (this.processError) { - try { - const result = await this.actor.greet(arg0); - return result; - } catch (e) { - this.processError(e); - throw new Error("unreachable"); - } - } else { - const result = await this.actor.greet(arg0); - return result; - } + const result = await this.actor.greet(arg0); + return result; } } export interface CreateActorOptions { @@ -70,7 +58,7 @@ export interface CreateActorOptions { agentOptions?: HttpAgentOptions; actorOptions?: ActorConfig; } -export function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Hello_world { +export function createActor(canisterId: string, options: CreateActorOptions = {}): Hello_world { const agent = options.agent || HttpAgent.createSync({ ...options.agentOptions }); @@ -82,5 +70,5 @@ export function createActor(canisterId: string, options: CreateActorOptions = {} canisterId: canisterId, ...options.actorOptions }); - return new Hello_world(actor, processError); + return new Hello_world(actor); }