1 |
|
2 |
|
3 | import { Database, Databases } from "./client/Database";
|
4 | import { Offer, Offers } from "./client/Offer";
|
5 | import { ClientContext } from "./ClientContext";
|
6 | import { parseConnectionString } from "./common";
|
7 | import { Constants } from "./common/constants";
|
8 | import { getUserAgent } from "./common/platform";
|
9 | import { defaultConnectionPolicy } from "./documents";
|
10 | import { GlobalEndpointManager } from "./globalEndpointManager";
|
11 | import { ResourceResponse } from "./request";
|
12 | import { checkURL } from "./utils/checkURL";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export class CosmosClient {
|
33 | constructor(optionsOrConnectionString) {
|
34 | var _a, _b;
|
35 | if (typeof optionsOrConnectionString === "string") {
|
36 | optionsOrConnectionString = parseConnectionString(optionsOrConnectionString);
|
37 | }
|
38 | const endpoint = checkURL(optionsOrConnectionString.endpoint);
|
39 | if (!endpoint) {
|
40 | throw new Error("Invalid endpoint specified");
|
41 | }
|
42 | optionsOrConnectionString.connectionPolicy = Object.assign({}, defaultConnectionPolicy, optionsOrConnectionString.connectionPolicy);
|
43 | optionsOrConnectionString.defaultHeaders = optionsOrConnectionString.defaultHeaders || {};
|
44 | optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.CacheControl] = "no-cache";
|
45 | optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.Version] =
|
46 | Constants.CurrentVersion;
|
47 | if (optionsOrConnectionString.consistencyLevel !== undefined) {
|
48 | optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.ConsistencyLevel] =
|
49 | optionsOrConnectionString.consistencyLevel;
|
50 | }
|
51 | optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.UserAgent] = getUserAgent(optionsOrConnectionString.userAgentSuffix);
|
52 | const globalEndpointManager = new GlobalEndpointManager(optionsOrConnectionString, async (opts) => this.getDatabaseAccount(opts));
|
53 | this.clientContext = new ClientContext(optionsOrConnectionString, globalEndpointManager);
|
54 | if (((_a = optionsOrConnectionString.connectionPolicy) === null || _a === void 0 ? void 0 : _a.enableEndpointDiscovery) &&
|
55 | ((_b = optionsOrConnectionString.connectionPolicy) === null || _b === void 0 ? void 0 : _b.enableBackgroundEndpointRefreshing)) {
|
56 | this.backgroundRefreshEndpointList(globalEndpointManager, optionsOrConnectionString.connectionPolicy.endpointRefreshRateInMs ||
|
57 | defaultConnectionPolicy.endpointRefreshRateInMs);
|
58 | }
|
59 | this.databases = new Databases(this, this.clientContext);
|
60 | this.offers = new Offers(this, this.clientContext);
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | async getDatabaseAccount(options) {
|
66 | const response = await this.clientContext.getDatabaseAccount(options);
|
67 | return new ResourceResponse(response.result, response.headers, response.code);
|
68 | }
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | getWriteEndpoint() {
|
75 | return this.clientContext.getWriteEndpoint();
|
76 | }
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | getReadEndpoint() {
|
83 | return this.clientContext.getReadEndpoint();
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | getWriteEndpoints() {
|
91 | return this.clientContext.getWriteEndpoints();
|
92 | }
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 | getReadEndpoints() {
|
99 | return this.clientContext.getReadEndpoints();
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | database(id) {
|
118 | return new Database(this, id, this.clientContext);
|
119 | }
|
120 | |
121 |
|
122 |
|
123 |
|
124 | offer(id) {
|
125 | return new Offer(this, id, this.clientContext);
|
126 | }
|
127 | |
128 |
|
129 |
|
130 | dispose() {
|
131 | clearTimeout(this.endpointRefresher);
|
132 | }
|
133 | async backgroundRefreshEndpointList(globalEndpointManager, refreshRate) {
|
134 | this.endpointRefresher = setInterval(() => {
|
135 | try {
|
136 | globalEndpointManager.refreshEndpointList();
|
137 | }
|
138 | catch (e) {
|
139 | console.warn("Failed to refresh endpoints", e);
|
140 | }
|
141 | }, refreshRate);
|
142 | if (this.endpointRefresher.unref && typeof this.endpointRefresher.unref === "function") {
|
143 | this.endpointRefresher.unref();
|
144 | }
|
145 | }
|
146 | }
|
147 |
|
\ | No newline at end of file |