@routr/common 2.0.8-alpha.13 → 2.0.8-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/connect/api/client.d.ts +3 -0
  2. package/dist/connect/api/client.js +154 -0
  3. package/dist/connect/api/index.d.ts +2 -0
  4. package/dist/connect/api/index.js +36 -0
  5. package/dist/connect/api/types.d.ts +55 -0
  6. package/dist/connect/api/types.js +2 -0
  7. package/dist/connect/config.d.ts +109 -0
  8. package/dist/connect/config.js +2 -0
  9. package/dist/connect/converters.d.ts +10 -0
  10. package/dist/connect/converters.js +176 -0
  11. package/dist/connect/grpc_client.d.ts +8 -1
  12. package/dist/connect/grpc_client.js +44 -6
  13. package/dist/connect/index.d.ts +3 -1
  14. package/dist/connect/index.js +3 -1
  15. package/dist/connect/protos/acl.proto +117 -0
  16. package/dist/connect/protos/agents.proto +154 -0
  17. package/dist/connect/protos/credentials.proto +116 -0
  18. package/dist/connect/protos/domains.proto +150 -0
  19. package/dist/connect/protos/numbers.proto +184 -0
  20. package/dist/connect/protos/peers.proto +166 -0
  21. package/dist/connect/protos/trunks.proto +186 -0
  22. package/dist/connect/schemas/acl.d.ts +61 -0
  23. package/dist/connect/schemas/acl.js +80 -0
  24. package/dist/connect/schemas/agent.d.ts +60 -0
  25. package/dist/connect/schemas/agent.js +79 -0
  26. package/dist/connect/schemas/crendentials.d.ts +55 -0
  27. package/dist/connect/schemas/crendentials.js +74 -0
  28. package/dist/connect/schemas/domain.d.ts +73 -0
  29. package/dist/connect/schemas/domain.js +92 -0
  30. package/dist/connect/schemas/index.d.ts +1 -0
  31. package/dist/connect/schemas/index.js +35 -0
  32. package/dist/connect/schemas/number.d.ts +78 -0
  33. package/dist/connect/schemas/number.js +97 -0
  34. package/dist/connect/schemas/peer.d.ts +68 -0
  35. package/dist/connect/schemas/peer.js +87 -0
  36. package/dist/connect/schemas/trunk.d.ts +116 -0
  37. package/dist/connect/schemas/trunk.js +135 -0
  38. package/dist/connect/schemas/validators.d.ts +2 -0
  39. package/dist/connect/schemas/validators.js +43 -0
  40. package/dist/connect/types.d.ts +74 -25
  41. package/dist/connect/types.js +7 -30
  42. package/dist/errors.d.ts +35 -1
  43. package/dist/errors.js +51 -5
  44. package/dist/helper.d.ts +1 -0
  45. package/dist/helper.js +11 -3
  46. package/dist/index.d.ts +5 -4
  47. package/dist/index.js +5 -5
  48. package/dist/protos/common.proto +0 -2
  49. package/dist/protos/location.proto +3 -2
  50. package/dist/service.d.ts +2 -2
  51. package/dist/service.js +1 -1
  52. package/dist/types.d.ts +2 -0
  53. package/package.json +3 -3
  54. package/dist/connect/data_api.d.ts +0 -11
  55. package/dist/connect/data_api.js +0 -91
  56. package/dist/protos/resources.proto +0 -108
@@ -0,0 +1,3 @@
1
+ import { APIClient, DataAPIOptions, ServiceAPI, ServiceAPIOptions } from "./types";
2
+ export declare function apiClient(options: DataAPIOptions): APIClient;
3
+ export declare function serviceAPI(options: ServiceAPIOptions): ServiceAPI;
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.serviceAPI = exports.apiClient = void 0;
27
+ /*
28
+ * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
29
+ * http://github.com/fonoster/routr
30
+ *
31
+ * This file is part of Routr
32
+ *
33
+ * Licensed under the MIT License (the "License");
34
+ * you may not use this file except in compliance with
35
+ * the License. You may obtain a copy of the License at
36
+ *
37
+ * https://opensource.org/licenses/MIT
38
+ *
39
+ * Unless required by applicable law or agreed to in writing, software
40
+ * distributed under the License is distributed on an "AS IS" BASIS,
41
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42
+ * See the License for the specific language governing permissions and
43
+ * limitations under the License.
44
+ */
45
+ /* eslint-disable require-jsdoc */
46
+ const grpc = __importStar(require("@grpc/grpc-js"));
47
+ const protobufUtil = __importStar(require("pb-util"));
48
+ const errors_1 = require("../../errors");
49
+ const grpc_client_1 = require("../grpc_client");
50
+ const types_1 = require("../types");
51
+ const jsonToStruct = protobufUtil.struct.encode;
52
+ const structToJson = protobufUtil.struct.decode;
53
+ function fire(err, apiAddr) {
54
+ if (err.code === grpc.status.UNAVAILABLE) {
55
+ return new errors_1.ServiceUnavailableError(`api server at ${apiAddr} is unavailable`);
56
+ }
57
+ return err;
58
+ }
59
+ function apiClient(options) {
60
+ const { apiAddr, credentials } = options;
61
+ return {
62
+ agents: serviceAPI({ kind: types_1.Kind.AGENT, apiAddr, credentials }),
63
+ domains: serviceAPI({ kind: types_1.Kind.DOMAIN, apiAddr, credentials }),
64
+ trunks: serviceAPI({ kind: types_1.Kind.TRUNK, apiAddr, credentials }),
65
+ credentials: serviceAPI({ kind: types_1.Kind.CREDENTIALS, apiAddr, credentials }),
66
+ acl: serviceAPI({ kind: types_1.Kind.AGENT, apiAddr, credentials }),
67
+ peers: serviceAPI({ kind: types_1.Kind.PEER, apiAddr, credentials }),
68
+ numbers: serviceAPI({ kind: types_1.Kind.NUMBER, apiAddr, credentials })
69
+ };
70
+ }
71
+ exports.apiClient = apiClient;
72
+ function serviceAPI(options) {
73
+ const { kind, apiAddr, credentials } = options;
74
+ const client = (0, grpc_client_1.createClient)({
75
+ kind,
76
+ apiAddr,
77
+ credentials: credentials !== null && credentials !== void 0 ? credentials : grpc.credentials.createInsecure()
78
+ });
79
+ return {
80
+ create: (request) => new Promise((resolve, reject) => {
81
+ if (request.extended) {
82
+ request.extended = jsonToStruct(request.extended);
83
+ }
84
+ client.create(request, (err, response) => {
85
+ if (err) {
86
+ return reject(fire(err, apiAddr));
87
+ }
88
+ if (response.extended) {
89
+ response.extended = structToJson(response.extended);
90
+ }
91
+ resolve(response);
92
+ });
93
+ }),
94
+ update: (request) => new Promise((resolve, reject) => {
95
+ if (request.extended) {
96
+ request.extended = jsonToStruct(request.extended);
97
+ }
98
+ client.update(request, (err, response) => {
99
+ if (err) {
100
+ return reject(fire(err, apiAddr));
101
+ }
102
+ if (response.extended) {
103
+ response.extended = structToJson(response.extended);
104
+ }
105
+ resolve(response);
106
+ });
107
+ }),
108
+ get: (ref) => new Promise((resolve, reject) => {
109
+ client.get({ ref }, (err, response) => {
110
+ if (err) {
111
+ return reject(fire(err, apiAddr));
112
+ }
113
+ if (response.extended) {
114
+ response.extended = structToJson(response.extended);
115
+ }
116
+ resolve(response);
117
+ });
118
+ }),
119
+ list: (request) => new Promise((resolve, reject) => {
120
+ client.list(request, (err, response) => {
121
+ var _a;
122
+ if (err) {
123
+ return reject(fire(err, apiAddr));
124
+ }
125
+ response.items.forEach((item) => {
126
+ if (item.extended) {
127
+ item.extended = structToJson(item.extended);
128
+ }
129
+ });
130
+ resolve({
131
+ items: (_a = response === null || response === void 0 ? void 0 : response.items) !== null && _a !== void 0 ? _a : [],
132
+ nextPageToken: response.nextPageToken
133
+ });
134
+ });
135
+ }),
136
+ findBy: (request) => new Promise((resolve, reject) => {
137
+ client.findBy(request, (err, response) => {
138
+ if (err) {
139
+ return reject(fire(err, apiAddr));
140
+ }
141
+ response.items.forEach((item) => {
142
+ if (item.extended) {
143
+ item.extended = structToJson(item.extended);
144
+ }
145
+ });
146
+ resolve(response);
147
+ });
148
+ }),
149
+ del: (ref) => new Promise((resolve, reject) => {
150
+ client.get({ ref }, (err) => err ? reject(fire(err, apiAddr)) : resolve());
151
+ })
152
+ };
153
+ }
154
+ exports.serviceAPI = serviceAPI;
@@ -0,0 +1,2 @@
1
+ export * from "./client";
2
+ export * from "./types";
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /*
18
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster
20
+ *
21
+ * This file is part of Routr.
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ __exportStar(require("./client"), exports);
36
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,55 @@
1
+ import * as grpc from "@grpc/grpc-js";
2
+ import { JsonData } from "../../types";
3
+ import { Kind } from "../types";
4
+ export interface DataAPIOptions {
5
+ apiAddr: string;
6
+ credentials?: grpc.ChannelCredentials;
7
+ }
8
+ export interface ServiceAPIOptions {
9
+ apiAddr: string;
10
+ kind: Kind;
11
+ credentials?: grpc.ChannelCredentials;
12
+ }
13
+ export interface FindByRequest {
14
+ fieldName: string;
15
+ fieldValue: string | boolean | number;
16
+ }
17
+ export interface ListRequest {
18
+ pageSize: number;
19
+ pageToken: string;
20
+ }
21
+ export interface ListResponse<R> {
22
+ items: R[];
23
+ nextPageToken: string;
24
+ }
25
+ export interface FindByResponse<R> {
26
+ items: R[];
27
+ }
28
+ export type APIClient = {
29
+ agents: ServiceAPI;
30
+ domains: ServiceAPI;
31
+ trunks: ServiceAPI;
32
+ credentials: ServiceAPI;
33
+ acl: ServiceAPI;
34
+ peers: ServiceAPI;
35
+ numbers: ServiceAPI;
36
+ };
37
+ export type ServiceAPI = {
38
+ create: <R extends {
39
+ extended?: JsonData;
40
+ }>(request: JsonData) => Promise<R>;
41
+ update: <R extends {
42
+ extended?: JsonData;
43
+ }>(request: JsonData) => Promise<R>;
44
+ get: <R extends {
45
+ extended?: JsonData;
46
+ }>(ref: string) => Promise<R>;
47
+ del: (ref: string) => Promise<void>;
48
+ list: <R extends {
49
+ extended?: JsonData;
50
+ }>(request: {
51
+ pageSize: number;
52
+ pageToken: string;
53
+ }) => Promise<ListResponse<R>>;
54
+ findBy: <R>(request: FindByRequest) => Promise<FindByResponse<R>>;
55
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,109 @@
1
+ import { Transport } from "../types";
2
+ import { Kind } from "./types";
3
+ export interface ConfigBase {
4
+ apiVersion: string;
5
+ ref: string;
6
+ metadata: {
7
+ name: string;
8
+ };
9
+ }
10
+ export interface AccessControlListConfig extends ConfigBase {
11
+ kind: Kind.ACL;
12
+ spec: {
13
+ accessControlList: {
14
+ allow: string[];
15
+ deny: string[];
16
+ };
17
+ };
18
+ }
19
+ export interface AgentConfig extends ConfigBase {
20
+ kind: Kind.AGENT;
21
+ spec: {
22
+ username: string;
23
+ domainRef: string;
24
+ credentialsRef: string;
25
+ privacy: string;
26
+ enabled: boolean;
27
+ };
28
+ }
29
+ export interface PeerConfig extends ConfigBase {
30
+ kind: Kind.PEER;
31
+ spec: {
32
+ aor: string;
33
+ username: string;
34
+ credentialsRef: string;
35
+ contactAddr: string;
36
+ enabled: boolean;
37
+ };
38
+ }
39
+ export interface CredentialsConfig extends ConfigBase {
40
+ kind: Kind.CREDENTIALS;
41
+ spec: {
42
+ credentials: {
43
+ username: string;
44
+ password: string;
45
+ };
46
+ };
47
+ }
48
+ export interface DomainConfig extends ConfigBase {
49
+ kind: Kind.DOMAIN;
50
+ spec: {
51
+ accessControlListRef: string;
52
+ context: {
53
+ domainUri: string;
54
+ egressPolicies: {
55
+ rule: string;
56
+ numberRef: string;
57
+ }[];
58
+ };
59
+ };
60
+ }
61
+ export interface NumberConfig extends Omit<ConfigBase, "metadata"> {
62
+ kind: Kind.NUMBER;
63
+ metadata: {
64
+ name: string;
65
+ geoInfo: {
66
+ city: string;
67
+ country: string;
68
+ countryISOCode: string;
69
+ };
70
+ };
71
+ spec: {
72
+ trunkRef: string;
73
+ location: {
74
+ telUrl: string;
75
+ aorLink: string;
76
+ sessionAffinityHeader: string;
77
+ extraHeaders: {
78
+ name: string;
79
+ value: string;
80
+ }[];
81
+ };
82
+ };
83
+ }
84
+ export interface TrunkConfig extends ConfigBase {
85
+ kind: Kind.TRUNK;
86
+ spec: {
87
+ inbound: {
88
+ uri: string;
89
+ accessControlListRef: string;
90
+ credentialsRef: string;
91
+ };
92
+ outbound: {
93
+ sendRegister: boolean;
94
+ credentialsRef: string;
95
+ uris: {
96
+ uri: {
97
+ user: string;
98
+ host: string;
99
+ port: number;
100
+ transport: Transport;
101
+ };
102
+ priority: number;
103
+ weight: number;
104
+ enabled: boolean;
105
+ }[];
106
+ };
107
+ };
108
+ }
109
+ export type UserConfig = AccessControlListConfig | AgentConfig | PeerConfig | CredentialsConfig | DomainConfig | NumberConfig | TrunkConfig;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { UserConfig } from "./config";
2
+ import { AccessControlList, Agent, Credentials, Domain, INumber, Peer, Trunk } from "./types";
3
+ export declare const getConverter: (kind: string) => typeof convertAgentConfigToAgent | typeof convertDomainConfigToDomain | typeof convertPeerConfigToPeer | typeof convertNumberConfigToNumber | typeof convertTrunkConfigToTrunk | typeof convertCredentialsConfigToCredentials | typeof convertACLConfigToACL;
4
+ export declare function convertAgentConfigToAgent(config: UserConfig, list: UserConfig[]): Agent;
5
+ export declare function convertDomainConfigToDomain(config: UserConfig, list: UserConfig[]): Domain;
6
+ export declare function convertPeerConfigToPeer(config: UserConfig, list: UserConfig[]): Peer;
7
+ export declare function convertNumberConfigToNumber(config: UserConfig, list: UserConfig[]): INumber;
8
+ export declare function convertTrunkConfigToTrunk(config: UserConfig, list: UserConfig[]): Trunk;
9
+ export declare function convertCredentialsConfigToCredentials(config: UserConfig): Credentials;
10
+ export declare function convertACLConfigToACL(config: UserConfig): AccessControlList;
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertACLConfigToACL = exports.convertCredentialsConfigToCredentials = exports.convertTrunkConfigToTrunk = exports.convertNumberConfigToNumber = exports.convertPeerConfigToPeer = exports.convertDomainConfigToDomain = exports.convertAgentConfigToAgent = exports.getConverter = void 0;
4
+ /*
5
+ * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster
7
+ *
8
+ * This file is part of Routr.
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ /* eslint-disable require-jsdoc */
23
+ const logger_1 = require("@fonoster/logger");
24
+ const types_1 = require("./types");
25
+ const logger = (0, logger_1.getLogger)({ service: "simpledata", filePath: __filename });
26
+ const findByRef = (ref, list) => {
27
+ const resource = list.find((r) => r.ref === ref);
28
+ if (ref != null && resource == null) {
29
+ logger.error(`the resource with ref ${ref} does not exist in the configuration file; exiting`);
30
+ process.exit(1);
31
+ }
32
+ return resource ? (0, exports.getConverter)(resource.kind)(resource, list) : null;
33
+ };
34
+ const getConverter = (kind) => {
35
+ switch (kind) {
36
+ case types_1.Kind.AGENT:
37
+ return convertAgentConfigToAgent;
38
+ case types_1.Kind.DOMAIN:
39
+ return convertDomainConfigToDomain;
40
+ case types_1.Kind.PEER:
41
+ return convertPeerConfigToPeer;
42
+ case types_1.Kind.NUMBER:
43
+ return convertNumberConfigToNumber;
44
+ case types_1.Kind.TRUNK:
45
+ return convertTrunkConfigToTrunk;
46
+ case types_1.Kind.CREDENTIALS:
47
+ return convertCredentialsConfigToCredentials;
48
+ case types_1.Kind.ACL:
49
+ return convertACLConfigToACL;
50
+ }
51
+ };
52
+ exports.getConverter = getConverter;
53
+ function convertAgentConfigToAgent(config, list) {
54
+ if (config.kind !== types_1.Kind.AGENT)
55
+ throw new Error("invalid resource type `Agent`");
56
+ return {
57
+ apiVersion: config.apiVersion,
58
+ ref: config.ref,
59
+ name: config.metadata.name,
60
+ username: config.spec.username,
61
+ privacy: config.spec.privacy,
62
+ enabled: config.spec.enabled,
63
+ domain: findByRef(config.spec.domainRef, list),
64
+ credentials: findByRef(config.spec.credentialsRef, list)
65
+ };
66
+ }
67
+ exports.convertAgentConfigToAgent = convertAgentConfigToAgent;
68
+ function convertDomainConfigToDomain(config, list) {
69
+ var _a;
70
+ if (config.kind !== types_1.Kind.DOMAIN)
71
+ throw new Error("invalid resource type `Domain`");
72
+ const egressPolicies = (_a = config.spec.context.egressPolicies) === null || _a === void 0 ? void 0 : _a.map((policy) => {
73
+ return {
74
+ rule: policy.rule,
75
+ number: findByRef(policy.numberRef, list)
76
+ };
77
+ });
78
+ return {
79
+ apiVersion: config.apiVersion,
80
+ ref: config.ref,
81
+ name: config.metadata.name,
82
+ domainUri: config.spec.context.domainUri,
83
+ accessControlList: findByRef(config.spec.accessControlListRef, list),
84
+ egressPolicies
85
+ };
86
+ }
87
+ exports.convertDomainConfigToDomain = convertDomainConfigToDomain;
88
+ function convertPeerConfigToPeer(config, list) {
89
+ if (config.kind !== types_1.Kind.PEER)
90
+ throw new Error("invalid resource type `Peer`");
91
+ return {
92
+ apiVersion: config.apiVersion,
93
+ ref: config.ref,
94
+ name: config.metadata.name,
95
+ username: config.spec.username,
96
+ aor: config.spec.aor,
97
+ contactAddr: config.spec.contactAddr,
98
+ enabled: config.spec.enabled,
99
+ credentials: findByRef(config.spec.credentialsRef, list)
100
+ };
101
+ }
102
+ exports.convertPeerConfigToPeer = convertPeerConfigToPeer;
103
+ function convertNumberConfigToNumber(config, list) {
104
+ if (config.kind !== types_1.Kind.NUMBER)
105
+ throw new Error("invalid resource type `Number`");
106
+ const metadata = config.metadata;
107
+ const location = config.spec.location;
108
+ return {
109
+ apiVersion: config.apiVersion,
110
+ ref: config.ref,
111
+ name: metadata.name,
112
+ city: metadata.geoInfo.city,
113
+ country: metadata.geoInfo.country,
114
+ countryIsoCode: metadata.geoInfo.countryISOCode,
115
+ telUrl: location.telUrl,
116
+ aorLink: location.aorLink,
117
+ sessionAffinityHeader: location.sessionAffinityHeader,
118
+ extraHeaders: location.extraHeaders,
119
+ trunk: findByRef(config.spec.trunkRef, list)
120
+ };
121
+ }
122
+ exports.convertNumberConfigToNumber = convertNumberConfigToNumber;
123
+ function convertTrunkConfigToTrunk(config, list) {
124
+ var _a;
125
+ if (config.kind !== types_1.Kind.TRUNK)
126
+ throw new Error("invalid resource type `Trunk`");
127
+ const inbound = config.spec.inbound;
128
+ const outbound = config.spec.outbound;
129
+ return {
130
+ apiVersion: config.apiVersion,
131
+ ref: config.ref,
132
+ name: config.metadata.name,
133
+ sendRegister: outbound.sendRegister,
134
+ inboundUri: inbound.uri,
135
+ inboundCredentials: findByRef(inbound.credentialsRef, list),
136
+ outboundCredentials: findByRef(outbound.credentialsRef, list),
137
+ accessControlList: findByRef(inbound.accessControlListRef, list),
138
+ uris: (_a = config.spec.outbound.uris) === null || _a === void 0 ? void 0 : _a.map((entry) => {
139
+ return {
140
+ user: entry.uri.user,
141
+ host: entry.uri.host,
142
+ port: entry.uri.port,
143
+ transport: entry.uri.transport,
144
+ enabled: entry.enabled,
145
+ weight: entry.weight,
146
+ priority: entry.priority
147
+ };
148
+ })
149
+ };
150
+ }
151
+ exports.convertTrunkConfigToTrunk = convertTrunkConfigToTrunk;
152
+ function convertCredentialsConfigToCredentials(config) {
153
+ if (config.kind !== types_1.Kind.CREDENTIALS)
154
+ throw new Error("invalid resource type `Credentials`");
155
+ return {
156
+ apiVersion: config.apiVersion,
157
+ ref: config.ref,
158
+ name: config.metadata.name,
159
+ username: config.spec.credentials.username,
160
+ password: config.spec.credentials.password
161
+ };
162
+ }
163
+ exports.convertCredentialsConfigToCredentials = convertCredentialsConfigToCredentials;
164
+ function convertACLConfigToACL(config) {
165
+ var _a, _b, _c, _d;
166
+ if (config.kind !== types_1.Kind.ACL)
167
+ throw new Error("invalid resource type `AccessControlList`");
168
+ return {
169
+ apiVersion: config.apiVersion,
170
+ ref: config.ref,
171
+ name: config.metadata.name,
172
+ allow: (_b = (_a = config.spec.accessControlList) === null || _a === void 0 ? void 0 : _a.allow) !== null && _b !== void 0 ? _b : [],
173
+ deny: (_d = (_c = config.spec.accessControlList) === null || _c === void 0 ? void 0 : _c.deny) !== null && _d !== void 0 ? _d : []
174
+ };
175
+ }
176
+ exports.convertACLConfigToACL = convertACLConfigToACL;
@@ -1 +1,8 @@
1
- export declare const RESOURCES_PROTO: any;
1
+ import * as grpc from "@grpc/grpc-js";
2
+ import { Kind } from "./types";
3
+ export declare function createClient(options: {
4
+ kind: Kind;
5
+ credentials: grpc.ChannelCredentials;
6
+ apiAddr: string;
7
+ }): any;
8
+ export declare function createService(kind: Kind): any;
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.RESOURCES_PROTO = void 0;
26
+ exports.createService = exports.createClient = void 0;
27
27
  /*
28
28
  * Copyright (C) 2022 by Fonoster Inc (https://fonoster.com)
29
29
  * http://github.com/fonoster/routr
@@ -42,15 +42,53 @@ exports.RESOURCES_PROTO = void 0;
42
42
  * See the License for the specific language governing permissions and
43
43
  * limitations under the License.
44
44
  */
45
+ /* eslint-disable require-jsdoc */
45
46
  const grpc = __importStar(require("@grpc/grpc-js"));
46
47
  const protoLoader = require("@grpc/proto-loader");
47
- const packageDefinition = protoLoader.loadSync(__dirname + "/../protos/resources.proto", {
48
+ const helper_1 = require("../helper");
49
+ const types_1 = require("./types");
50
+ const protoOptions = {
48
51
  keepCase: false,
49
52
  longs: String,
50
53
  enums: String,
51
54
  defaults: true,
52
55
  oneofs: true
53
- });
54
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
56
- exports.RESOURCES_PROTO = protoDescriptor.fonoster.routr.resources;
56
+ };
57
+ function getProtoPath(kind) {
58
+ switch (kind) {
59
+ case types_1.Kind.ACL:
60
+ return __dirname + "/protos/acl.proto";
61
+ case types_1.Kind.CREDENTIALS:
62
+ return __dirname + "/protos/credentials.proto";
63
+ default:
64
+ return __dirname + `/protos/${kind}s.proto`;
65
+ }
66
+ }
67
+ function createClient(options) {
68
+ const def = protoLoader.loadSync(getProtoPath(options.kind), protoOptions);
69
+ const descriptor = grpc.loadPackageDefinition(def);
70
+ const base = descriptor.fonoster.routr.connect;
71
+ switch (options.kind) {
72
+ case types_1.Kind.ACL:
73
+ return new base.acl.v2draft1.ACLService(options.apiAddr, options.credentials);
74
+ case types_1.Kind.CREDENTIALS:
75
+ return new base.credentials.v2draft1.CredentialsService(options.apiAddr, options.credentials);
76
+ default:
77
+ return new base[options.kind + "s"].v2draft1[(0, helper_1.toPascaleCase)(options.kind) + "s"](options.apiAddr, options.credentials);
78
+ }
79
+ }
80
+ exports.createClient = createClient;
81
+ function createService(kind) {
82
+ const def = protoLoader.loadSync(getProtoPath(kind), protoOptions);
83
+ const descriptor = grpc.loadPackageDefinition(def);
84
+ const base = descriptor.fonoster.routr.connect;
85
+ switch (kind) {
86
+ case types_1.Kind.ACL:
87
+ return base.acl.v2draft1.ACLService.service;
88
+ case types_1.Kind.CREDENTIALS:
89
+ return base.credentials.v2draft1.CredentialsService.service;
90
+ default:
91
+ return base[kind + "s"].v2draft1[(0, helper_1.toPascaleCase)(kind) + "s"].service;
92
+ }
93
+ }
94
+ exports.createService = createService;
@@ -1,3 +1,5 @@
1
- export * from "./data_api";
1
+ export * from "./api";
2
2
  export * from "./grpc_client";
3
3
  export * from "./types";
4
+ export * from "./config";
5
+ export * from "./converters";
@@ -32,6 +32,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
32
32
  * See the License for the specific language governing permissions and
33
33
  * limitations under the License.
34
34
  */
35
- __exportStar(require("./data_api"), exports);
35
+ __exportStar(require("./api"), exports);
36
36
  __exportStar(require("./grpc_client"), exports);
37
37
  __exportStar(require("./types"), exports);
38
+ __exportStar(require("./config"), exports);
39
+ __exportStar(require("./converters"), exports);