@blocksdiy/blocks-client-sdk 1.0.0
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.
- package/README.md +1 -0
- package/dist/Action.d.ts +142 -0
- package/dist/Action.d.ts.map +1 -0
- package/dist/Action.js +143 -0
- package/dist/Action.js.map +1 -0
- package/dist/AgentChat.d.ts +42 -0
- package/dist/AgentChat.d.ts.map +1 -0
- package/dist/AgentChat.js +34 -0
- package/dist/AgentChat.js.map +1 -0
- package/dist/ClientSdk.d.ts +309 -0
- package/dist/ClientSdk.d.ts.map +1 -0
- package/dist/ClientSdk.js +396 -0
- package/dist/ClientSdk.js.map +1 -0
- package/dist/Entity.d.ts +299 -0
- package/dist/Entity.d.ts.map +1 -0
- package/dist/Entity.js +329 -0
- package/dist/Entity.js.map +1 -0
- package/dist/Page.d.ts +56 -0
- package/dist/Page.d.ts.map +1 -0
- package/dist/Page.js +52 -0
- package/dist/Page.js.map +1 -0
- package/dist/ReactClientSdk.d.ts +886 -0
- package/dist/ReactClientSdk.d.ts.map +1 -0
- package/dist/ReactClientSdk.jsx +1238 -0
- package/dist/ReactClientSdk.jsx.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientSdk.d.ts","sourceRoot":"","sources":["../src/ClientSdk.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC;AAE/C;;;;;;;;;;;;GAYG;AACH,MA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AAE,EAAE,GAQ7B,MAAM,CAAC,EAAE,CAAC;IAG5C;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,IAAI,CAAC,EAAE,SAAS,UAAU,GAAG,UAAU,EAAE,UAAU,EAAE,EAAE,GAKJ,IAAI,CAAC,EAAE,CAAC;IAG3D;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,SAAS,CAAC,GAAG,SAAS,eAAe,GAAG,eAAe,EAAE,eAAe,EAAE,GAAG,GAQhB,SAAS,CAAC,GAAG,CAAC;YAG7D,YAAY;IAgCpB,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,UAAU,EAAE,EAAE;QAAE,UAAU,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAwCvG;;;;;;;;;;;;;OAaG;IACH,OAAO;IAIP;;;;;;;;;;OAUG;IACG,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB;IAoBlF;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACG,aAAa,CAAC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE;IAmBhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,iBAAiB;CAGlB"}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { AppVersionService } from "@blockscom/blocks-client-api/appVersionService";
|
|
2
|
+
import { getGoogleBaseUrl, getHost } from "@blockscom/environment";
|
|
3
|
+
import { Action } from "./Action.js";
|
|
4
|
+
import { AgentChat } from "./AgentChat.js";
|
|
5
|
+
import { Entity } from "./Entity.js";
|
|
6
|
+
import { Page } from "./Page.js";
|
|
7
|
+
/**
|
|
8
|
+
* Main client SDK for interacting with entities, actions, and pages
|
|
9
|
+
*
|
|
10
|
+
* This SDK provides a unified interface for working with:
|
|
11
|
+
* - Entities: Data objects stored in tables (CRUD operations)
|
|
12
|
+
* - Actions: Executable workflows that perform operations (any server-side logic)
|
|
13
|
+
* - Pages: Application pages with URL and parameter handling
|
|
14
|
+
* - AgentChat: Interact with the agent chat
|
|
15
|
+
* - User: The user's data
|
|
16
|
+
*
|
|
17
|
+
* @class ClientSdk
|
|
18
|
+
*/
|
|
19
|
+
export class ClientSdk {
|
|
20
|
+
appId;
|
|
21
|
+
user = { isAuthenticated: false };
|
|
22
|
+
token;
|
|
23
|
+
entities;
|
|
24
|
+
actions;
|
|
25
|
+
pages;
|
|
26
|
+
agentChats;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new ClientSdk instance
|
|
29
|
+
* @param {ClientConfig} config - Configuration options
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const client = new ClientSdk({ appId: 'my-application-id' });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(config) {
|
|
36
|
+
this.appId = config.appId;
|
|
37
|
+
this.token = config.token;
|
|
38
|
+
this.entities = new Map();
|
|
39
|
+
this.actions = new Map();
|
|
40
|
+
this.pages = new Map();
|
|
41
|
+
this.agentChats = new Map();
|
|
42
|
+
}
|
|
43
|
+
async authenticate() {
|
|
44
|
+
const headers = {
|
|
45
|
+
"x-app-id": this.appId,
|
|
46
|
+
};
|
|
47
|
+
if (this.token) {
|
|
48
|
+
headers["Authorization"] = `Bearer ${this.token}`;
|
|
49
|
+
}
|
|
50
|
+
const response = await fetch(`${getHost()}/safe-users/current`, {
|
|
51
|
+
headers,
|
|
52
|
+
});
|
|
53
|
+
const { user } = await response.json();
|
|
54
|
+
this.user = { ...(user || {}), isAuthenticated: !!user };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets or creates an Entity instance for the specified entity configuration
|
|
58
|
+
*
|
|
59
|
+
* An Entity represents a data object stored in a table with CRUD operations.
|
|
60
|
+
* EntityConfig requires:
|
|
61
|
+
* - tableBlockId: Unique identifier for the table
|
|
62
|
+
* - instanceType: TypeScript type defining the entity's structure
|
|
63
|
+
*
|
|
64
|
+
* @template EC - Entity configuration type
|
|
65
|
+
* @param {EC} entityConfig - Configuration for the entity
|
|
66
|
+
* @returns {Entity<EC>} An Entity instance for the given configuration
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* // Define a User entity configuration
|
|
70
|
+
* const userEntityConfig = {
|
|
71
|
+
* tableBlockId: 'users-table-id',
|
|
72
|
+
* instanceType: {} as {
|
|
73
|
+
* name: string;
|
|
74
|
+
* email: string;
|
|
75
|
+
* role: 'admin' | 'user';
|
|
76
|
+
* }
|
|
77
|
+
* };
|
|
78
|
+
*
|
|
79
|
+
* // Get the entity instance
|
|
80
|
+
* const userEntity = client.entity(userEntityConfig);
|
|
81
|
+
*
|
|
82
|
+
* // Use it for CRUD operations
|
|
83
|
+
* const users = await userEntity.findMany();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
entity(entityConfig) {
|
|
87
|
+
if (!this.entities.has(entityConfig.tableBlockId)) {
|
|
88
|
+
this.entities.set(entityConfig.tableBlockId, new Entity(entityConfig, { token: this.token }));
|
|
89
|
+
}
|
|
90
|
+
return this.entities.get(entityConfig.tableBlockId);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Gets or creates an Action instance for the specified action configuration
|
|
94
|
+
*
|
|
95
|
+
* An Action represents an executable workflow that performs server-side operations
|
|
96
|
+
* of any kind. Actions can handle data processing, business logic, integrations
|
|
97
|
+
* with external services, AI operations, and any other server-side functionality.
|
|
98
|
+
* Actions are particularly powerful for implementing AI capabilities.
|
|
99
|
+
*
|
|
100
|
+
* ActionConfig requires:
|
|
101
|
+
* - actionBlockId: Unique identifier for the action workflow
|
|
102
|
+
* - inputInstanceType: TypeScript type defining the action's input parameters
|
|
103
|
+
* - outputInstanceType: TypeScript type defining the action's output structure
|
|
104
|
+
*
|
|
105
|
+
* @template AC - Action configuration type
|
|
106
|
+
* @param {AC} actionConfig - Configuration for the action
|
|
107
|
+
* @returns {Action<AC>} An Action instance for the given configuration
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* // Example 1: Data processing action
|
|
111
|
+
* const processOrderConfig = {
|
|
112
|
+
* actionBlockId: 'process-order-id',
|
|
113
|
+
* inputInstanceType: {} as {
|
|
114
|
+
* orderId: string;
|
|
115
|
+
* userId: string;
|
|
116
|
+
* items: Array<{ productId: string; quantity: number }>;
|
|
117
|
+
* },
|
|
118
|
+
* outputInstanceType: {} as {
|
|
119
|
+
* success: boolean;
|
|
120
|
+
* orderNumber: string;
|
|
121
|
+
* estimatedDelivery: string;
|
|
122
|
+
* }
|
|
123
|
+
* };
|
|
124
|
+
*
|
|
125
|
+
* const processOrderAction = client.action(processOrderConfig);
|
|
126
|
+
* const result = await processOrderAction.execute({
|
|
127
|
+
* orderId: 'order-123',
|
|
128
|
+
* userId: 'user-456',
|
|
129
|
+
* items: [{ productId: 'prod-789', quantity: 2 }]
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* // Example 2: Authentication action
|
|
133
|
+
* const loginUserConfig = {
|
|
134
|
+
* actionBlockId: 'login-user-id',
|
|
135
|
+
* inputInstanceType: {} as {
|
|
136
|
+
* email: string;
|
|
137
|
+
* password: string;
|
|
138
|
+
* },
|
|
139
|
+
* outputInstanceType: {} as {
|
|
140
|
+
* success: boolean;
|
|
141
|
+
* token?: string;
|
|
142
|
+
* error?: string;
|
|
143
|
+
* }
|
|
144
|
+
* };
|
|
145
|
+
*
|
|
146
|
+
* const loginAction = client.action(loginUserConfig);
|
|
147
|
+
* const loginResult = await loginAction.execute({
|
|
148
|
+
* email: 'user@example.com',
|
|
149
|
+
* password: 'password123'
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
action(actionConfig) {
|
|
154
|
+
const key = actionConfig.toolId
|
|
155
|
+
? `${actionConfig.toolId}-${actionConfig.actionBlockId}`
|
|
156
|
+
: actionConfig.actionBlockId;
|
|
157
|
+
if (!this.actions.has(key)) {
|
|
158
|
+
this.actions.set(key, new Action(actionConfig, { appId: this.appId, token: this.token }));
|
|
159
|
+
}
|
|
160
|
+
return this.actions.get(key);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets or creates a Page instance for the specified page configuration
|
|
164
|
+
*
|
|
165
|
+
* A Page represents an application page with URL and parameter handling.
|
|
166
|
+
* PageConfig requires:
|
|
167
|
+
* - pageBlockId: Unique identifier for the page
|
|
168
|
+
* - pageName: Name of the page
|
|
169
|
+
*
|
|
170
|
+
* @template PC - Page configuration type
|
|
171
|
+
* @param {PC} pageConfig - Configuration for the page
|
|
172
|
+
* @returns {Page<PC>} A Page instance for the given configuration
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* // Define a product page configuration
|
|
176
|
+
* const productPageConfig = {
|
|
177
|
+
* pageBlockId: 'product-page-id'
|
|
178
|
+
* pageName: 'product-page'
|
|
179
|
+
* };
|
|
180
|
+
*
|
|
181
|
+
* // Get the page instance
|
|
182
|
+
* const productPage = client.page(productPageConfig);
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
page(pageConfig) {
|
|
186
|
+
if (!this.pages.has(pageConfig.pageBlockId)) {
|
|
187
|
+
this.pages.set(pageConfig.pageBlockId, new Page(pageConfig, this.appId));
|
|
188
|
+
}
|
|
189
|
+
return this.pages.get(pageConfig.pageBlockId);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Gets or creates an AgentChat instance for the specified agent chat configuration
|
|
193
|
+
*
|
|
194
|
+
* An AgentChat represents an agent chat with the user.
|
|
195
|
+
* AgentChatConfig requires:
|
|
196
|
+
* - agentChatId: Unique identifier for the agent chat
|
|
197
|
+
*
|
|
198
|
+
* @template ACC - AgentChat configuration type
|
|
199
|
+
* @param {ACC} agentChatConfig - Configuration for the agent chat
|
|
200
|
+
* @returns {AgentChat<ACC>} An AgentChat instance for the given configuration
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* // Define a agent chat configuration
|
|
204
|
+
* const agentChatConfig = {
|
|
205
|
+
* agentChatId: 'agent-chat-id'
|
|
206
|
+
* };
|
|
207
|
+
*
|
|
208
|
+
* // Get the agent chat instance
|
|
209
|
+
* const agentChat = client.agentChat(agentChatConfig);
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
agentChat(agentChatConfig) {
|
|
213
|
+
if (!this.agentChats.has(agentChatConfig.agentChatId)) {
|
|
214
|
+
this.agentChats.set(agentChatConfig.agentChatId, new AgentChat(agentChatConfig, { appId: this.appId, token: this.token }));
|
|
215
|
+
}
|
|
216
|
+
return this.agentChats.get(agentChatConfig.agentChatId);
|
|
217
|
+
}
|
|
218
|
+
async getUploadUrl(data) {
|
|
219
|
+
const headers = {};
|
|
220
|
+
// headers["Content-Type"] = "application/json";
|
|
221
|
+
if (this.token) {
|
|
222
|
+
headers.Authorization = this.token;
|
|
223
|
+
const appVersion = AppVersionService.getCurrentVersionNumber();
|
|
224
|
+
if (appVersion !== null) {
|
|
225
|
+
headers["x-app-version"] = appVersion.toString();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const response = await fetch(`${getHost()}/product/builder/data-assets/get-upload-url`, {
|
|
229
|
+
method: "post",
|
|
230
|
+
body: JSON.stringify(data),
|
|
231
|
+
headers,
|
|
232
|
+
});
|
|
233
|
+
const { result } = await response.json();
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
async upload(file, { onProgress }) {
|
|
237
|
+
const formData = new FormData();
|
|
238
|
+
const { uploadUrl, fields, protectedUrl } = await this.getUploadUrl({
|
|
239
|
+
productId: this.appId,
|
|
240
|
+
fileName: file.name,
|
|
241
|
+
fileType: file.type,
|
|
242
|
+
});
|
|
243
|
+
const request = new XMLHttpRequest();
|
|
244
|
+
if (uploadUrl && fields) {
|
|
245
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
246
|
+
formData.append(key, value);
|
|
247
|
+
}
|
|
248
|
+
formData.append("file", file);
|
|
249
|
+
request.open("POST", uploadUrl);
|
|
250
|
+
}
|
|
251
|
+
request.upload.onprogress = (e) => {
|
|
252
|
+
if (e.total) {
|
|
253
|
+
const progress = Math.round((e.loaded * 100) / e.total);
|
|
254
|
+
onProgress(progress);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
return new Promise((resolve, reject) => {
|
|
258
|
+
request.onload = () => {
|
|
259
|
+
onProgress(100);
|
|
260
|
+
if (request.status >= 200 && request.status < 300) {
|
|
261
|
+
resolve(protectedUrl);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
reject(new Error("Error uploading file"));
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
request.send(formData);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Returns the current user information
|
|
272
|
+
*
|
|
273
|
+
* This method provides access to the user's data
|
|
274
|
+
* that was provided during SDK initialization.
|
|
275
|
+
*
|
|
276
|
+
* @returns {User} The current user object
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* // Get the current user information
|
|
280
|
+
* const user = client.getUser();
|
|
281
|
+
* console.log(`Logged in as: ${user.username}`);
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
getUser() {
|
|
285
|
+
return this.user;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Changes a user's role
|
|
289
|
+
* @param userId - The user ID to change the role for
|
|
290
|
+
* @param role - The new role to set for the user
|
|
291
|
+
* @param options - Options for the change
|
|
292
|
+
* @returns {Promise<void>} A promise that resolves when the role change is complete
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* await client.changeUserRole('123', 'admin');
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
async changeUserRole(userId, role, options = {}) {
|
|
299
|
+
if (!options.clientOnly) {
|
|
300
|
+
const headers = {};
|
|
301
|
+
// headers["Content-Type"] = "application/json";
|
|
302
|
+
if (this.token) {
|
|
303
|
+
headers.Authorization = this.token;
|
|
304
|
+
}
|
|
305
|
+
await fetch(`${getHost()}/safe-users/${userId}`, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
headers,
|
|
308
|
+
body: JSON.stringify({ role }),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (this.user.id === userId) {
|
|
312
|
+
this.user = { ...this.user, role };
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Sends a passwordless login link (magic link) to the user's email
|
|
317
|
+
*
|
|
318
|
+
* This method sends an email containing a one-time login link that allows users
|
|
319
|
+
* to authenticate without entering a password. When the user clicks the link in
|
|
320
|
+
* their email, they will be automatically logged into the application.
|
|
321
|
+
*
|
|
322
|
+
* @param {Object} params - Parameters for sending the login link
|
|
323
|
+
* @param {string} params.email - The email address to send the login link to
|
|
324
|
+
* @returns {Promise<void>} A promise that resolves when the login link is sent successfully
|
|
325
|
+
* @example
|
|
326
|
+
* ```ts
|
|
327
|
+
* // Send a login link to a user
|
|
328
|
+
* const result = await client.sendLoginLink({
|
|
329
|
+
* email: 'user@example.com'
|
|
330
|
+
* });
|
|
331
|
+
*
|
|
332
|
+
* // Typically used in a login form
|
|
333
|
+
* async function handlePasswordlessLogin(email: string) {
|
|
334
|
+
* try {
|
|
335
|
+
* await client.sendLoginLink({ email });
|
|
336
|
+
* alert('Check your email for a login link!');
|
|
337
|
+
* } catch (error) {
|
|
338
|
+
* console.error('Failed to send login link:', error);
|
|
339
|
+
* }
|
|
340
|
+
* }
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
async sendLoginLink({ email }) {
|
|
344
|
+
const trimmedEmail = email?.trim();
|
|
345
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
346
|
+
if (!trimmedEmail || !emailRegex.test(trimmedEmail)) {
|
|
347
|
+
throw new Error("Invalid email address");
|
|
348
|
+
}
|
|
349
|
+
const response = await fetch(`${getHost()}/auth/send-login-link`, {
|
|
350
|
+
method: "POST",
|
|
351
|
+
body: JSON.stringify({ email: trimmedEmail, appUrl: window.location.origin }),
|
|
352
|
+
});
|
|
353
|
+
const { success, errors } = await response.json();
|
|
354
|
+
if (!success) {
|
|
355
|
+
throw new Error(errors?.[0]?.message || "Failed to send login link");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Gets the Google OAuth login URL for authentication
|
|
360
|
+
*
|
|
361
|
+
* This method generates a URL that initiates the Google OAuth login flow.
|
|
362
|
+
* When users navigate to this URL, they will be redirected to Google's
|
|
363
|
+
* authentication page. After successful authentication with Google, they
|
|
364
|
+
* will be redirected back to the application and automatically logged in.
|
|
365
|
+
*
|
|
366
|
+
* @returns {string} The Google OAuth login URL
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* // Get the Google login URL
|
|
370
|
+
* const googleUrl = client.getGoogleLoginUrl();
|
|
371
|
+
*
|
|
372
|
+
* // Redirect user to Google login
|
|
373
|
+
* window.location.href = googleUrl;
|
|
374
|
+
*
|
|
375
|
+
* // Use in a button with onClick
|
|
376
|
+
* function GoogleLoginButton() {
|
|
377
|
+
* const handleGoogleLogin = () => {
|
|
378
|
+
* const url = client.getGoogleLoginUrl();
|
|
379
|
+
* window.location.href = url;
|
|
380
|
+
* };
|
|
381
|
+
*
|
|
382
|
+
* return <Button onClick={handleGoogleLogin}>Sign in with Google</Button>;
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* // Use in a Link component
|
|
386
|
+
* function GoogleLoginLink() {
|
|
387
|
+
* const googleUrl = client.getGoogleLoginUrl();
|
|
388
|
+
* return <Link to={googleUrl}>Sign in with Google</Link>;
|
|
389
|
+
* }
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
392
|
+
getGoogleLoginUrl() {
|
|
393
|
+
return `${getGoogleBaseUrl()}/authentication/oauth?flow=app-login&appUrl=${window.location.origin}`;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
//# sourceMappingURL=ClientSdk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientSdk.js","sourceRoot":"","sources":["../src/ClientSdk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gDAAgD,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEnE,OAAO,EAAE,MAAM,EAAqB,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,MAAM,EAAqB,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,IAAI,EAAmB,MAAM,QAAQ,CAAC;AA0C/C;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,SAAS;IACX,KAAK,CAAS;IACf,IAAI,GAAS,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC;IACvC,KAAK,CAAU;IACP,QAAQ,CAAsB;IAC9B,OAAO,CAAsB;IAC7B,KAAK,CAAoB;IACzB,UAAU,CAAyB;IAEpD;;;;;;;OAOG;IACH,YAAY,MAAoB;QAC9B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,YAAY;QAChB,MAAM,OAAO,GAA2B;YACtC,UAAU,EAAE,IAAI,CAAC,KAAK;SACvB,CAAC;QAEF,IAAI,IAAI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