|
| 1 | +import 'dart:js_interop'; |
| 2 | +import 'dart:js_util'; |
| 3 | + |
| 4 | +import 'package:deno_postgres_interop/src/partial/partial_connection_options.dart'; |
| 5 | +import 'package:deno_postgres_interop/src/partial/partial_tls_options.dart'; |
| 6 | +import 'package:deno_postgres_interop/src/transport.dart'; |
| 7 | + |
| 8 | +/// [[email protected]/ClientOptions](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions). |
| 9 | +@JS() |
| 10 | +class ClientOptions { |
| 11 | + /// [[email protected]/ClientOptions/applicationName](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_applicationName). |
| 12 | + external String? get applicationName; |
| 13 | + |
| 14 | + /// [[email protected]/ClientOptions/database](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_database). |
| 15 | + external String? get database; |
| 16 | + |
| 17 | + /// [[email protected]/ClientOptions/hostname](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_hostname). |
| 18 | + external String? get hostname; |
| 19 | + |
| 20 | + /// [[email protected]/ClientOptions/password](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_password). |
| 21 | + external String? get password; |
| 22 | + |
| 23 | + /// [[email protected]/ClientOptions/user](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_applicationName/user). |
| 24 | + external String? get user; |
| 25 | + |
| 26 | + /// [[email protected]/ClientOptions](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions). |
| 27 | + factory ClientOptions({ |
| 28 | + String? applicationName, |
| 29 | + PartialConnectionOptions? connection, |
| 30 | + String? database, |
| 31 | + String? hostname, |
| 32 | + Transport? hostType, |
| 33 | + String? optionsString, |
| 34 | + Map<String, String>? optionsMap, |
| 35 | + String? password, |
| 36 | + String? portString, |
| 37 | + int? port, |
| 38 | + PartialTLSOptions? tls, |
| 39 | + String? user, |
| 40 | + }) { |
| 41 | + assert(optionsString == null || optionsMap == null); |
| 42 | + assert(portString == null || port == null); |
| 43 | + |
| 44 | + return jsify( |
| 45 | + { |
| 46 | + if (applicationName != null) 'applicationName': applicationName, |
| 47 | + if (connection != null) 'connection': jsify(connection.asMap()), |
| 48 | + if (database != null) 'database': database, |
| 49 | + if (hostname != null) 'hostname': hostname, |
| 50 | + if (hostType != null) 'host_type': hostType.name, |
| 51 | + if (optionsString != null) |
| 52 | + 'options': optionsString |
| 53 | + else if (optionsMap != null) |
| 54 | + 'options': jsify(optionsMap), |
| 55 | + if (password != null) 'password': password, |
| 56 | + if (portString != null) |
| 57 | + 'port': portString |
| 58 | + else if (port != null) |
| 59 | + 'port': port, |
| 60 | + if (tls != null) 'tls': jsify(tls.asMap()), |
| 61 | + if (user != null) 'user': user, |
| 62 | + }, |
| 63 | + ) as ClientOptions; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// [[email protected]/ClientOptions](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions). |
| 68 | +extension ClientOptionsProps on ClientOptions { |
| 69 | + /// [[email protected]/ClientOptions/host_type](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_host_type). |
| 70 | + Transport get hostType => Transport.parse(getProperty(this, 'host_type')); |
| 71 | + |
| 72 | + /// [[email protected]/ClientOptions/options](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_options). |
| 73 | + /// |
| 74 | + /// Either this or [optionsMap] is null. |
| 75 | + String? get optionsString { |
| 76 | + final prop = getProperty(this, 'options'); |
| 77 | + |
| 78 | + return prop is String ? prop : null; |
| 79 | + } |
| 80 | + |
| 81 | + /// [[email protected]/ClientOptions/options](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_options). |
| 82 | + /// |
| 83 | + /// Either this or [optionsString] is null. |
| 84 | + Map<String, String>? get optionsMap { |
| 85 | + final prop = getProperty(this, 'options'); |
| 86 | + |
| 87 | + return prop is String ? null : prop as Map<String, String>; |
| 88 | + } |
| 89 | + |
| 90 | + /// [[email protected]/ClientOptions/port](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_port). |
| 91 | + /// |
| 92 | + /// Either this or [port] is null. |
| 93 | + String? get portString { |
| 94 | + final prop = getProperty(this, 'port'); |
| 95 | + |
| 96 | + return prop is String ? prop : null; |
| 97 | + } |
| 98 | + |
| 99 | + /// [[email protected]/ClientOptions/port](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_port). |
| 100 | + /// |
| 101 | + /// Either this or [portString] is null. |
| 102 | + int? get port { |
| 103 | + final prop = getProperty(this, 'port'); |
| 104 | + |
| 105 | + return prop is int ? prop : null; |
| 106 | + } |
| 107 | + |
| 108 | + /// [[email protected]/ClientOptions/connection](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_connection). |
| 109 | + PartialConnectionOptions? get connection { |
| 110 | + final map = |
| 111 | + dartify(getProperty(this, 'connection')) as Map<dynamic, dynamic>?; |
| 112 | + |
| 113 | + return map == null ? null : PartialConnectionOptions.fromMap(map); |
| 114 | + } |
| 115 | + |
| 116 | + /// [[email protected]/ClientOptions/tls](https://deno.land/x/[email protected]/mod.ts?s=ClientOptions#prop_tls). |
| 117 | + PartialTLSOptions? get tls { |
| 118 | + final map = |
| 119 | + dartify(getProperty(this, 'connection')) as Map<dynamic, dynamic>?; |
| 120 | + |
| 121 | + if (map == null) return null; |
| 122 | + |
| 123 | + return PartialTLSOptions( |
| 124 | + caCertificates: map['caCertificates'] as List<String>?, |
| 125 | + isEnabled: map['enabled'] as bool?, |
| 126 | + isEnforced: map['enforced'] as bool?, |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments