@fonoster/sdk 0.6.0 → 0.6.1

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 (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +358 -2
  3. package/dist/node/Applications.d.ts +206 -0
  4. package/dist/node/Applications.js +267 -0
  5. package/dist/node/client/AbstractClient.d.ts +22 -0
  6. package/dist/node/client/AbstractClient.js +86 -0
  7. package/dist/node/client/Client.d.ts +15 -0
  8. package/dist/node/client/Client.js +61 -0
  9. package/dist/node/client/TokenRefresherNode.d.ts +8 -0
  10. package/dist/node/client/TokenRefresherNode.js +43 -0
  11. package/dist/node/client/TokenRefresherWeb.d.ts +13 -0
  12. package/dist/node/client/TokenRefresherWeb.js +44 -0
  13. package/dist/node/client/isJwtExpired.d.ts +2 -0
  14. package/dist/node/client/isJwtExpired.js +37 -0
  15. package/dist/node/client/jsonToObject.d.ts +8 -0
  16. package/dist/node/client/jsonToObject.js +31 -0
  17. package/dist/node/client/makeRpcRequest.d.ts +11 -0
  18. package/dist/node/client/makeRpcRequest.js +44 -0
  19. package/dist/node/client/objectToJson.d.ts +3 -0
  20. package/dist/node/client/objectToJson.js +32 -0
  21. package/dist/node/client/types.d.ts +26 -0
  22. package/dist/node/client/types.js +2 -0
  23. package/dist/node/client/utils.d.ts +5 -0
  24. package/dist/node/client/utils.js +17 -0
  25. package/dist/node/generated/node/acls.ts +1054 -0
  26. package/dist/node/generated/node/acls_grpc_pb.js +200 -0
  27. package/dist/node/generated/node/acls_pb.js +2063 -0
  28. package/dist/node/generated/node/agents.ts +1388 -0
  29. package/dist/node/generated/node/agents_grpc_pb.js +202 -0
  30. package/dist/node/generated/node/agents_pb.js +2403 -0
  31. package/dist/node/generated/node/applications.ts +1411 -0
  32. package/dist/node/generated/node/applications_grpc_pb.js +201 -0
  33. package/dist/node/generated/node/applications_pb.js +2651 -0
  34. package/dist/node/generated/node/calls.ts +1123 -0
  35. package/dist/node/generated/node/calls_grpc_pb.js +169 -0
  36. package/dist/node/generated/node/calls_pb.js +1916 -0
  37. package/dist/node/generated/node/credentials.ts +1054 -0
  38. package/dist/node/generated/node/credentials_grpc_pb.js +200 -0
  39. package/dist/node/generated/node/credentials_pb.js +1928 -0
  40. package/dist/node/generated/node/domains.ts +1144 -0
  41. package/dist/node/generated/node/domains_grpc_pb.js +200 -0
  42. package/dist/node/generated/node/domains_pb.js +2170 -0
  43. package/dist/node/generated/node/google/protobuf/empty.ts +48 -0
  44. package/dist/node/generated/node/google/protobuf/struct.ts +408 -0
  45. package/dist/node/generated/node/identity.ts +4267 -0
  46. package/dist/node/generated/node/identity_grpc_pb.js +761 -0
  47. package/dist/node/generated/node/identity_pb.js +8058 -0
  48. package/dist/node/generated/node/numbers.ts +1371 -0
  49. package/dist/node/generated/node/numbers_grpc_pb.js +201 -0
  50. package/dist/node/generated/node/numbers_pb.js +2470 -0
  51. package/dist/node/generated/node/secrets.ts +985 -0
  52. package/dist/node/generated/node/secrets_grpc_pb.js +200 -0
  53. package/dist/node/generated/node/secrets_pb.js +1838 -0
  54. package/dist/node/generated/node/trunks.ts +1546 -0
  55. package/dist/node/generated/node/trunks_grpc_pb.js +202 -0
  56. package/dist/node/generated/node/trunks_pb.js +2777 -0
  57. package/dist/node/generated/web/AclsServiceClientPb.ts +258 -0
  58. package/dist/node/generated/web/AgentsServiceClientPb.ts +258 -0
  59. package/dist/node/generated/web/ApplicationsServiceClientPb.ts +258 -0
  60. package/dist/node/generated/web/CallsServiceClientPb.ts +194 -0
  61. package/dist/node/generated/web/CredentialsServiceClientPb.ts +258 -0
  62. package/dist/node/generated/web/DomainsServiceClientPb.ts +258 -0
  63. package/dist/node/generated/web/IdentityServiceClientPb.ts +990 -0
  64. package/dist/node/generated/web/NumbersServiceClientPb.ts +258 -0
  65. package/dist/node/generated/web/SecretsServiceClientPb.ts +258 -0
  66. package/dist/node/generated/web/TrunksServiceClientPb.ts +258 -0
  67. package/dist/node/generated/web/acls.ts +1054 -0
  68. package/dist/node/generated/web/acls_pb.d.ts +246 -0
  69. package/dist/node/generated/web/acls_pb.js +2063 -0
  70. package/dist/node/generated/web/agents.ts +1388 -0
  71. package/dist/node/generated/web/agents_pb.d.ts +300 -0
  72. package/dist/node/generated/web/agents_pb.js +2403 -0
  73. package/dist/node/generated/web/applications.ts +1411 -0
  74. package/dist/node/generated/web/applications_pb.d.ts +322 -0
  75. package/dist/node/generated/web/applications_pb.js +2651 -0
  76. package/dist/node/generated/web/calls.ts +1123 -0
  77. package/dist/node/generated/web/calls_pb.d.ts +262 -0
  78. package/dist/node/generated/web/calls_pb.js +1916 -0
  79. package/dist/node/generated/web/credentials.ts +1054 -0
  80. package/dist/node/generated/web/credentials_pb.d.ts +234 -0
  81. package/dist/node/generated/web/credentials_pb.js +1928 -0
  82. package/dist/node/generated/web/domains.ts +1144 -0
  83. package/dist/node/generated/web/domains_pb.d.ts +260 -0
  84. package/dist/node/generated/web/domains_pb.js +2170 -0
  85. package/dist/node/generated/web/google/protobuf/empty.ts +48 -0
  86. package/dist/node/generated/web/google/protobuf/struct.ts +408 -0
  87. package/dist/node/generated/web/identity.ts +4267 -0
  88. package/dist/node/generated/web/identity_pb.d.ts +973 -0
  89. package/dist/node/generated/web/identity_pb.js +8058 -0
  90. package/dist/node/generated/web/numbers.ts +1371 -0
  91. package/dist/node/generated/web/numbers_pb.d.ts +305 -0
  92. package/dist/node/generated/web/numbers_pb.js +2470 -0
  93. package/dist/node/generated/web/secrets.ts +985 -0
  94. package/dist/node/generated/web/secrets_pb.d.ts +222 -0
  95. package/dist/node/generated/web/secrets_pb.js +1838 -0
  96. package/dist/node/generated/web/trunks.ts +1546 -0
  97. package/dist/node/generated/web/trunks_pb.d.ts +338 -0
  98. package/dist/node/generated/web/trunks_pb.js +2777 -0
  99. package/dist/node/node.d.ts +2 -0
  100. package/dist/node/node.js +36 -0
  101. package/dist/node/tsconfig.node.tsbuildinfo +1 -0
  102. package/dist/node/utils.d.ts +22 -0
  103. package/dist/node/utils.js +54 -0
  104. package/dist/web/fonoster.min.js +1 -0
  105. package/dist/web/index.esm.js +1 -0
  106. package/package.json +24 -12
  107. package/dist/fonoster.min.js +0 -1
  108. package/dist/index.d.ts +0 -6
  109. package/dist/index.esm.js +0 -1
  110. package/dist/index.js +0 -1
  111. package/dist/printHello.d.ts +0 -2
@@ -0,0 +1,267 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Applications = void 0;
4
+ const makeRpcRequest_1 = require("./client/makeRpcRequest");
5
+ const applications_pb_1 = require("./generated/node/applications_pb");
6
+ const utils_1 = require("./utils");
7
+ /**
8
+ * @classdesc Fonoster Applications, part of the Fonoster Voice Subsystem,
9
+ * allow you to create, update, retrieve, and delete Voice Applications.
10
+ * Note that an active Fonoster deployment is required.
11
+ *
12
+ * @example
13
+ *
14
+ * const SDK = require("@fonoster/sdk");
15
+ *
16
+ * const request = {
17
+ * name: "My application",
18
+ * type: "PROGRAMMABLE_VOICE",
19
+ * appEndpoint: "myapp.mydomain.com",
20
+ * textToSpeech: {
21
+ * productRef: "tts.google",
22
+ * config: {
23
+ * voice: "en-US-Casual-K"
24
+ * }
25
+ * },
26
+ * speechToText: {
27
+ * productRef: "stt.google",
28
+ * config: {
29
+ * languageCode: "en-US"
30
+ * }
31
+ * },
32
+ * intelligence: {
33
+ * productRef: "nlu.dialogflowcx",
34
+ * credentials: {
35
+ * apiKey: "your-api-key"
36
+ * },
37
+ * config: {
38
+ * agentId: "your-agent-id"
39
+ * }
40
+ * }
41
+ * };
42
+ *
43
+ * const username = "admin@fonoster.local";
44
+ * const password = "changeme";
45
+ * const accessKeyId = "WO00000000000000000000000000000000";
46
+ *
47
+ * const client = new SDK.Client({ accessKeyId });
48
+ *
49
+ * client.login(username, password)
50
+ * .then(async () => {
51
+ * const apps = new SDK.Applications(client);
52
+ * const result = await apps.createApplication(request);
53
+ * console.log(result); // successful response
54
+ * }).catch(console.error); // an error occurred
55
+ */
56
+ class Applications {
57
+ client;
58
+ /**
59
+ * Constructs a new Applications object.
60
+ *
61
+ * @param {FonosterClient} client - Client object with underlying implementations to make requests to Fonoster's API
62
+ * @see AbstractClient
63
+ * @see FonosterClient
64
+ */
65
+ constructor(client) {
66
+ this.client = client;
67
+ }
68
+ /**
69
+ * Creates a new Application in Fonoster. The only required fields are the name and type of the application.
70
+ *
71
+ * @param {CreateApplicationRequest} request - The request object that contains the necessary information to create a new application
72
+ * @param {string} request.name - The name of the application
73
+ * @param {ApplicationType} request.type - The type of application (e.g., PROGRAMMABLE_VOICE)
74
+ * @param {string} request.appEndpoint - The endpoint where the application is hosted
75
+ * @param {TextToSpeech} request.textToSpeech - The text-to-speech configuration
76
+ * @param {string} request.textToSpeech.productRef - The product reference of the text-to-speech engine (e.g., tts.google)
77
+ * @param {object} request.textToSpeech.config - The configuration object for the text-to-speech engine (e.g., { voice: "en-US-Casual-K" })
78
+ * @param {SpeechToText} request.speechToText - The speech-to-text configuration
79
+ * @param {string} request.speechToText.productRef - The product reference of the speech-to-text engine (e.g., stt.google)
80
+ * @param {object} request.speechToText.config - The configuration object for the speech-to-text engine (e.g., { languageCode: "en-US" })
81
+ * @param {Intelligence} request.intelligence - The intelligence configuration
82
+ * @param {string} request.intelligence.productRef - The product reference of the intelligence engine (e.g., nlu.dialogflowcx)
83
+ * @param {object} request.intelligence.credentials - The credentials object for the intelligence engine (e.g., { apiKey: "your-api-key" })
84
+ * @param {object} request.intelligence.config - The configuration object for the intelligence engine (e.g., { agentId: "your-agent-id" })
85
+ * @return {Promise<CreateAppResponse>} - The response object that contains the reference to the newly created application
86
+ * @example
87
+ *
88
+ * const request = {
89
+ * name: "My application",
90
+ * type: "PROGRAMMABLE_VOICE",
91
+ * appEndpoint: "myapp.mydomain.com",
92
+ * textToSpeech: {
93
+ * productRef: "tts.google",
94
+ * config: {
95
+ * voice: "en-US-Casual-K"
96
+ * }
97
+ * },
98
+ * speechToText: {
99
+ * productRef: "stt.google",
100
+ * config: {
101
+ * languageCode: "en-US"
102
+ * }
103
+ * },
104
+ * intelligence: {
105
+ * productRef: "nlu.dialogflowcx",
106
+ * credentials: {
107
+ * apiKey: "your-api-key"
108
+ * },
109
+ * config: {
110
+ * agentId: "your-agent-id"
111
+ * }
112
+ * }
113
+ * };
114
+ *
115
+ * const apps = new SDK.Applications(client); // Existing client object
116
+ *
117
+ * apps.createApplication(request)
118
+ * .then(console.log) // successful response
119
+ * .catch(console.error); // an error occurred
120
+ */
121
+ async createApplication(request) {
122
+ const reqWithStructOverride = (0, utils_1.buildStructOverride)(request);
123
+ const applicationsClient = this.client.getApplicationsClient();
124
+ return await (0, makeRpcRequest_1.makeRpcRequest)({
125
+ method: applicationsClient.createApplication.bind(applicationsClient),
126
+ requestPBObjectConstructor: applications_pb_1.CreateApplicationRequest,
127
+ metadata: this.client.getMetadata(),
128
+ request: reqWithStructOverride,
129
+ enumMapping: [["type", applications_pb_1.ApplicationType]],
130
+ objectMapping: [
131
+ ["textToSpeech", applications_pb_1.ProductContainer],
132
+ ["speechToText", applications_pb_1.ProductContainer],
133
+ ["intelligence", applications_pb_1.ProductContainer]
134
+ ]
135
+ });
136
+ }
137
+ /**
138
+ * Retrieves an existing application from Fonoster.
139
+ *
140
+ * @param {string} ref - The reference of the application to retrieve
141
+ * @return {Promise<Application>} - The response object that contains the application information
142
+ * @example
143
+ *
144
+ * const request = {
145
+ * ref: "00000000-0000-0000-0000-000000000000"
146
+ * };
147
+ *
148
+ * const apps = new SDK.Applications(client); // Existing client object
149
+ *
150
+ * apps.getApplication(request)
151
+ * .then(console.log) // successful response
152
+ * .catch(console.error); // an error occurred
153
+ */
154
+ async getApplication(ref) {
155
+ const applicationsClient = this.client.getApplicationsClient();
156
+ return await (0, makeRpcRequest_1.makeRpcRequest)({
157
+ method: applicationsClient.getApplication.bind(applicationsClient),
158
+ requestPBObjectConstructor: applications_pb_1.GetApplicationRequest,
159
+ metadata: this.client.getMetadata(),
160
+ request: { ref }
161
+ });
162
+ }
163
+ /**
164
+ * Updates an existing application in Fonoster.
165
+ *
166
+ * @param {UpdateApplicationRequest} request - The request object that contains the necessary information to update an application
167
+ * @param {string} request.ref - The reference of the application to update
168
+ * @param {string} request.name - The name of the application
169
+ * @param {string} request.appEndpoint - The endpoint where the application is hosted
170
+ * @param {TextToSpeech} request.textToSpeech - The text-to-speech configuration
171
+ * @param {string} request.textToSpeech.productRef - The product reference of the text-to-speech engine (e.g., tts.google)
172
+ * @param {object} request.textToSpeech.config - The configuration object for the text-to-speech engine (e.g., { voice: "en-US-Casual-K" })
173
+ * @param {SpeechToText} request.speechToText - The speech-to-text configuration
174
+ * @param {string} request.speechToText.productRef - The product reference of the speech-to-text engine (e.g., stt.google)
175
+ * @param {object} request.speechToText.config - The configuration object for the speech-to-text engine (e.g., { languageCode: "en-US" })
176
+ * @param {Intelligence} request.intelligence - The intelligence configuration
177
+ * @param {string} request.intelligence.productRef - The product reference of the intelligence engine (e.g., nlu.dialogflowcx)
178
+ * @param {object} request.intelligence.credentials - The credentials object for the intelligence engine (e.g., { apiKey: "your-api-key" })
179
+ * @param {object} request.intelligence.config - The configuration object for the intelligence engine (e.g., { agentId: "your-agent-id" })
180
+ * @return {Promise<BaseApiObject>} - The response object that contains the reference to the updated application
181
+ * @example
182
+ *
183
+ * const request = {
184
+ * ref: "00000000-0000-0000-0000-000000000000",
185
+ * name: "My application",
186
+ * appEndpoint: "myapp.mydomain.com"
187
+ * }
188
+ *
189
+ * const apps = new SDK.Applications(client); // Existing client object
190
+ *
191
+ * apps.updateApplication(request)
192
+ * .then(console.log) // successful response
193
+ * .catch(console.error); // an error occurred
194
+ */
195
+ async updateApplication(request) {
196
+ const reqWithStructOverride = (0, utils_1.buildStructOverride)(request);
197
+ const applicationsClient = this.client.getApplicationsClient();
198
+ return await (0, makeRpcRequest_1.makeRpcRequest)({
199
+ method: applicationsClient.updateApplication.bind(applicationsClient),
200
+ requestPBObjectConstructor: applications_pb_1.UpdateApplicationRequest,
201
+ metadata: this.client.getMetadata(),
202
+ request: reqWithStructOverride,
203
+ enumMapping: [["type", applications_pb_1.ApplicationType]],
204
+ objectMapping: [
205
+ ["textToSpeech", applications_pb_1.ProductContainer],
206
+ ["speechToText", applications_pb_1.ProductContainer],
207
+ ["intelligence", applications_pb_1.ProductContainer]
208
+ ]
209
+ });
210
+ }
211
+ /**
212
+ * Retrieves a list of applications from Fonoster.
213
+ *
214
+ * @param {ListApplicationsRequest} request - The request object that contains the necessary information to retrieve a list of applications
215
+ * @param {number} request.pageSize - The number of applications to retrieve
216
+ * @param {string} request.pageToken - The token to retrieve the next page of applications
217
+ * @return {Promise<ListApplicationsResponse>} - The response object that contains the list of applications
218
+ * @example
219
+ *
220
+ * const request = {
221
+ * pageSize: 10,
222
+ * pageToken: "00000000-0000-0000-0000-000000000000"
223
+ * };
224
+ *
225
+ * const apps = new SDK.Applications(client); // Existing client object
226
+ *
227
+ * apps.listApplications(request)
228
+ * .then(console.log) // successful response
229
+ * .catch(console.error); // an error occurred
230
+ */
231
+ async listApplications(request) {
232
+ const applicationsClient = this.client.getApplicationsClient();
233
+ return await (0, makeRpcRequest_1.makeRpcRequest)({
234
+ method: applicationsClient.listApplications.bind(applicationsClient),
235
+ requestPBObjectConstructor: applications_pb_1.ListApplicationsRequest,
236
+ metadata: this.client.getMetadata(),
237
+ request,
238
+ repeatableObjectMapping: [["itemsList", applications_pb_1.Application]]
239
+ });
240
+ }
241
+ /**
242
+ * Deletes an existing application from Fonoster.
243
+ * Note that this operation is irreversible.
244
+ *
245
+ * @param {string} ref - The reference of the application to delete
246
+ * @return {Promise<BaseApiObject>} - The response object that contains the reference to the deleted application
247
+ * @example
248
+ *
249
+ * const ref = "00000000-0000-0000-0000-000000000000"
250
+ *
251
+ * const apps = new SDK.Applications(client); // Existing client object
252
+ *
253
+ * apps.deleteApplication(ref)
254
+ * .then(console.log) // successful response
255
+ * .catch(console.error); // an error occurred
256
+ */
257
+ async deleteApplication(ref) {
258
+ const applicationsClient = this.client.getApplicationsClient();
259
+ return await (0, makeRpcRequest_1.makeRpcRequest)({
260
+ method: applicationsClient.deleteApplication.bind(applicationsClient),
261
+ requestPBObjectConstructor: applications_pb_1.DeleteApplicationRequest,
262
+ metadata: this.client.getMetadata(),
263
+ request: { ref }
264
+ });
265
+ }
266
+ }
267
+ exports.Applications = Applications;
@@ -0,0 +1,22 @@
1
+ import { ApplicationsClient, FonosterClient, IdentityClient } from "./types";
2
+ declare abstract class AbstractClient implements FonosterClient {
3
+ protected idToken: string;
4
+ protected accessKeyId: string;
5
+ protected _accessToken: string;
6
+ protected _refreshToken: string;
7
+ protected identityClient: IdentityClient;
8
+ constructor(config: {
9
+ accessKeyId: string;
10
+ identityClient: IdentityClient;
11
+ });
12
+ login(username: string, password: string): Promise<void>;
13
+ loginWithRefreshToken(refreshToken: string): Promise<void>;
14
+ loginWithApiKey(apiKey: string): Promise<void>;
15
+ refreshToken(): Promise<void>;
16
+ getAccessKeyId(): string;
17
+ getAccessToken(): string;
18
+ getRefreshToken(): string;
19
+ abstract getMetadata(): unknown;
20
+ abstract getApplicationsClient(): ApplicationsClient;
21
+ }
22
+ export { AbstractClient };
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractClient = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
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
+ const makeRpcRequest_1 = require("./makeRpcRequest");
23
+ const identity_pb_1 = require("../generated/node/identity_pb");
24
+ class AbstractClient {
25
+ idToken;
26
+ accessKeyId;
27
+ _accessToken;
28
+ _refreshToken;
29
+ identityClient;
30
+ constructor(config) {
31
+ this.accessKeyId = config.accessKeyId;
32
+ this.identityClient = config.identityClient;
33
+ this._accessToken = "";
34
+ this._refreshToken = "";
35
+ }
36
+ async login(username, password) {
37
+ const { refreshToken, accessToken } = await (0, makeRpcRequest_1.makeRpcRequest)({
38
+ method: this.identityClient.exchangeCredentials.bind(this.identityClient),
39
+ requestPBObjectConstructor: identity_pb_1.ExchangeCredentialsRequest,
40
+ metadata: {},
41
+ request: {
42
+ username,
43
+ password
44
+ }
45
+ });
46
+ this._refreshToken = refreshToken;
47
+ this._accessToken = accessToken;
48
+ }
49
+ async loginWithRefreshToken(refreshToken) {
50
+ const { accessToken, refreshToken: newRefreshToken } = await (0, makeRpcRequest_1.makeRpcRequest)({
51
+ method: this.identityClient.exchangeRefreshToken.bind(this.identityClient),
52
+ requestPBObjectConstructor: identity_pb_1.ExchangeRefreshTokenRequest,
53
+ metadata: {},
54
+ request: {
55
+ refreshToken
56
+ }
57
+ });
58
+ this._refreshToken = newRefreshToken;
59
+ this._accessToken = accessToken;
60
+ }
61
+ async loginWithApiKey(apiKey) {
62
+ const { refreshToken, accessToken } = await (0, makeRpcRequest_1.makeRpcRequest)({
63
+ method: this.identityClient.exchangeApiKey,
64
+ requestPBObjectConstructor: identity_pb_1.ExchangeApiKeyRequest,
65
+ metadata: {},
66
+ request: {
67
+ apiKey
68
+ }
69
+ });
70
+ this._refreshToken = refreshToken;
71
+ this._accessToken = accessToken;
72
+ }
73
+ async refreshToken() {
74
+ return await this.loginWithRefreshToken(this._refreshToken);
75
+ }
76
+ getAccessKeyId() {
77
+ return this.accessKeyId;
78
+ }
79
+ getAccessToken() {
80
+ return this._accessToken;
81
+ }
82
+ getRefreshToken() {
83
+ return this._refreshToken;
84
+ }
85
+ }
86
+ exports.AbstractClient = AbstractClient;
@@ -0,0 +1,15 @@
1
+ import { Metadata } from "@grpc/grpc-js";
2
+ import { AbstractClient } from "./AbstractClient";
3
+ export declare class Client extends AbstractClient {
4
+ private endpoint;
5
+ private tokenRefresherInterceptor;
6
+ private channelCredentials;
7
+ constructor(config: {
8
+ endpoint?: string;
9
+ accessKeyId: string;
10
+ allowInsecure?: boolean;
11
+ });
12
+ getMetadata(): Metadata;
13
+ getApplicationsClient(): any;
14
+ getIdentityClient(): any;
15
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
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
+ const grpc_js_1 = require("@grpc/grpc-js");
23
+ const AbstractClient_1 = require("./AbstractClient");
24
+ const TokenRefresherNode_1 = require("./TokenRefresherNode");
25
+ const applications_grpc_pb_1 = require("../generated/node/applications_grpc_pb");
26
+ const identity_grpc_pb_1 = require("../generated/node/identity_grpc_pb");
27
+ const DEFAULT_ENDPOINT = "api.fonoster.io:50051";
28
+ class Client extends AbstractClient_1.AbstractClient {
29
+ endpoint;
30
+ tokenRefresherInterceptor;
31
+ channelCredentials;
32
+ constructor(config) {
33
+ const channelCredentials = config.allowInsecure
34
+ ? grpc_js_1.credentials.createInsecure()
35
+ : grpc_js_1.credentials.createSsl();
36
+ super({
37
+ accessKeyId: config.accessKeyId,
38
+ identityClient: new identity_grpc_pb_1.IdentityClient(config.endpoint, channelCredentials)
39
+ });
40
+ this.channelCredentials = channelCredentials;
41
+ this.endpoint = config?.endpoint || DEFAULT_ENDPOINT;
42
+ this.tokenRefresherInterceptor = new TokenRefresherNode_1.TokenRefresherNode(this)
43
+ .createInterceptor()
44
+ .bind(this);
45
+ }
46
+ getMetadata() {
47
+ const metadata = new grpc_js_1.Metadata();
48
+ metadata.set("token", this.getAccessToken());
49
+ metadata.set("accessKeyId", this.getAccessKeyId());
50
+ return metadata;
51
+ }
52
+ getApplicationsClient() {
53
+ return new applications_grpc_pb_1.ApplicationsClient(this.endpoint, this.channelCredentials, {
54
+ interceptors: [this.tokenRefresherInterceptor]
55
+ });
56
+ }
57
+ getIdentityClient() {
58
+ return new identity_grpc_pb_1.IdentityClient(this.endpoint, this.channelCredentials);
59
+ }
60
+ }
61
+ exports.Client = Client;
@@ -0,0 +1,8 @@
1
+ import { Interceptor } from "@grpc/grpc-js";
2
+ import { AbstractClient } from "./AbstractClient";
3
+ declare class TokenRefresherNode {
4
+ private client;
5
+ constructor(client: AbstractClient);
6
+ createInterceptor(): Interceptor;
7
+ }
8
+ export { TokenRefresherNode };
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenRefresherNode = void 0;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
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
+ const grpc_js_1 = require("@grpc/grpc-js");
23
+ const isJwtExpired_1 = require("./isJwtExpired");
24
+ class TokenRefresherNode {
25
+ client;
26
+ constructor(client) {
27
+ this.client = client;
28
+ }
29
+ createInterceptor() {
30
+ return (options, nextCall) => {
31
+ return new grpc_js_1.InterceptingCall(nextCall(options), {
32
+ sendMessage: async (message, next) => {
33
+ const token = this.client.getRefreshToken();
34
+ if ((0, isJwtExpired_1.isJwtExpired)(token)) {
35
+ await this.client.refreshToken();
36
+ }
37
+ next(message);
38
+ }
39
+ });
40
+ };
41
+ }
42
+ }
43
+ exports.TokenRefresherNode = TokenRefresherNode;
@@ -0,0 +1,13 @@
1
+ import * as grpcWeb from "grpc-web";
2
+ import { AbstractClient } from "./AbstractClient";
3
+ type ClientReadableStream = grpcWeb.ClientReadableStream<unknown>;
4
+ declare class TokenRefresherWeb {
5
+ private client;
6
+ constructor(client: AbstractClient);
7
+ intercept(request: unknown, invoker: (request: unknown) => ClientReadableStream): {
8
+ refresher: TokenRefresherWeb;
9
+ stream: ClientReadableStream;
10
+ on(eventType: string, callback: () => void): any;
11
+ };
12
+ }
13
+ export { TokenRefresherWeb };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenRefresherWeb = void 0;
4
+ const isJwtExpired_1 = require("./isJwtExpired");
5
+ class TokenRefresherWeb {
6
+ client;
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ intercept(request, invoker) {
11
+ class InterceptedStream {
12
+ refresher;
13
+ stream;
14
+ constructor(stream, refresher) {
15
+ this.stream = stream;
16
+ this.refresher = refresher;
17
+ }
18
+ on(eventType, callback) {
19
+ const token = this.refresher.client.getRefreshToken();
20
+ if ((0, isJwtExpired_1.isJwtExpired)(token)) {
21
+ this.refresher.client.refreshToken().then(() => { });
22
+ }
23
+ if (eventType == "data") {
24
+ this.stream.on(eventType, callback);
25
+ }
26
+ else if (eventType == "error") {
27
+ this.stream.on("error", callback);
28
+ }
29
+ else if (eventType == "metadata") {
30
+ this.stream.on("metadata", callback);
31
+ }
32
+ else if (eventType == "status") {
33
+ this.stream.on("status", callback);
34
+ }
35
+ else if (eventType == "end") {
36
+ this.stream.on("end", callback);
37
+ }
38
+ return this;
39
+ }
40
+ }
41
+ return new InterceptedStream(invoker(request), this);
42
+ }
43
+ }
44
+ exports.TokenRefresherWeb = TokenRefresherWeb;
@@ -0,0 +1,2 @@
1
+ declare function isJwtExpired(token: string): boolean;
2
+ export { isJwtExpired };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isJwtExpired = isJwtExpired;
4
+ /*
5
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
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
+ function isJwtExpired(token) {
23
+ try {
24
+ const [, payloadBase64] = token.split(".");
25
+ const payloadJson = Buffer.from(payloadBase64, "base64").toString("utf8");
26
+ const payload = JSON.parse(payloadJson);
27
+ if (!payload.exp) {
28
+ return false;
29
+ }
30
+ const expirationTime = payload.exp * 1000;
31
+ const currentTime = Date.now();
32
+ return currentTime > expirationTime;
33
+ }
34
+ catch (error) {
35
+ return true;
36
+ }
37
+ }
@@ -0,0 +1,8 @@
1
+ import { MappingTuple } from "./types";
2
+ declare function jsonToObject<J extends Record<string, unknown>, T>(params: {
3
+ json: J;
4
+ objectConstructor: new () => T;
5
+ enumMapping?: MappingTuple<unknown>;
6
+ objectMapping?: MappingTuple<unknown>;
7
+ }): T;
8
+ export { jsonToObject };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonToObject = jsonToObject;
4
+ const utils_1 = require("./utils");
5
+ function jsonToObject(params) {
6
+ const { json, objectConstructor: ObjectConstructor, enumMapping, objectMapping } = params;
7
+ const instance = new ObjectConstructor();
8
+ Object.keys(json).forEach((key) => {
9
+ const setterName = `set${key.charAt(0).toUpperCase() + key.slice(1)}`;
10
+ if (!json[key]) {
11
+ return;
12
+ }
13
+ if ((0, utils_1.isMapping)(key, enumMapping)) {
14
+ const enumValue = (0, utils_1.getEnumValue)(key, json[key], enumMapping);
15
+ instance[setterName](enumValue);
16
+ }
17
+ else if ((0, utils_1.isMapping)(key, objectMapping)) {
18
+ const objectValue = jsonToObject({
19
+ json: json[key],
20
+ objectConstructor: objectMapping.find((tuple) => tuple[0] === key)[1],
21
+ enumMapping,
22
+ objectMapping
23
+ });
24
+ instance[setterName](objectValue);
25
+ }
26
+ else if (typeof instance[setterName] === "function") {
27
+ instance[setterName](json[key]);
28
+ }
29
+ });
30
+ return instance;
31
+ }
@@ -0,0 +1,11 @@
1
+ import { ClientFunction, MappingTuple } from "./types";
2
+ declare function makeRpcRequest<RequestPB, ResponsePB, Request extends Record<string, unknown>, Response extends Record<string, unknown>>(params: {
3
+ method: ClientFunction<RequestPB, ResponsePB>;
4
+ requestPBObjectConstructor: new () => RequestPB;
5
+ metadata: unknown;
6
+ request: Request;
7
+ enumMapping?: MappingTuple<unknown>;
8
+ objectMapping?: MappingTuple<unknown>;
9
+ repeatableObjectMapping?: MappingTuple<unknown>;
10
+ }): Promise<Response>;
11
+ export { makeRpcRequest };