@corti/sdk 0.0.0-rc → 0.0.0-rc.2

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.
@@ -64,8 +64,8 @@ class CortiClient {
64
64
  "Tenant-Name": _options === null || _options === void 0 ? void 0 : _options.tenantName,
65
65
  "X-Fern-Language": "JavaScript",
66
66
  "X-Fern-SDK-Name": "@corti/sdk",
67
- "X-Fern-SDK-Version": "0.0.0-rc",
68
- "User-Agent": "@corti/sdk/0.0.0-rc",
67
+ "X-Fern-SDK-Version": "0.0.0-rc.2",
68
+ "User-Agent": "@corti/sdk/0.0.0-rc.2",
69
69
  "X-Fern-Runtime": core.RUNTIME.type,
70
70
  "X-Fern-Runtime-Version": core.RUNTIME.version,
71
71
  }, _options === null || _options === void 0 ? void 0 : _options.headers) });
@@ -92,7 +92,7 @@ class CortiClient {
92
92
  /**
93
93
  * Patch: resolve tenantName and environment from options or token
94
94
  */
95
- const { tenantName, environment } = (0, resolveClientOptions_js_1.resolveClientOptions)(_options);
95
+ const { tenantName, environment, initialTokenResponse } = (0, resolveClientOptions_js_1.resolveClientOptions)(_options);
96
96
  this._options = Object.assign(Object.assign({}, _options), { headers: (0, headers_js_1.mergeHeaders)({
97
97
  "Tenant-Name": tenantName,
98
98
  "X-Fern-Language": "JavaScript",
@@ -118,7 +118,7 @@ class CortiClient {
118
118
  */
119
119
  authClient: new CortiAuth_js_1.Auth(this._options),
120
120
  })
121
- : new RefreshBearerProvider_js_1.RefreshBearerProvider(_options.auth);
121
+ : new RefreshBearerProvider_js_1.RefreshBearerProvider(Object.assign(Object.assign({}, _options.auth), { initialTokenResponse }));
122
122
  }
123
123
  get interactions() {
124
124
  var _a;
@@ -2,8 +2,9 @@
2
2
  * RefreshBearerProvider used as a replacement of OAuthTokenProvider, in case when accessToken from outside of library was used instead of Client credentials.
3
3
  */
4
4
  import * as api from "../api/index.js";
5
- type ExpectedTokenResponse = Omit<api.GetTokenResponse, 'tokenType'> & {
5
+ export type ExpectedTokenResponse = Omit<api.GetTokenResponse, 'tokenType' | 'expiresIn'> & {
6
6
  tokenType?: string;
7
+ expiresIn?: number;
7
8
  };
8
9
  type RefreshAccessTokenFunction = (refreshToken?: string) => Promise<ExpectedTokenResponse> | ExpectedTokenResponse;
9
10
  export type BearerOptions = Partial<Omit<api.GetTokenResponse, 'accessToken'>> & ({
@@ -12,7 +13,9 @@ export type BearerOptions = Partial<Omit<api.GetTokenResponse, 'accessToken'>> &
12
13
  } | {
13
14
  refreshAccessToken: RefreshAccessTokenFunction;
14
15
  accessToken?: string;
15
- });
16
+ }) & {
17
+ initialTokenResponse?: Promise<ExpectedTokenResponse>;
18
+ };
16
19
  export declare class RefreshBearerProvider {
17
20
  private readonly BUFFER_IN_MINUTES;
18
21
  private _accessToken;
@@ -20,7 +23,8 @@ export declare class RefreshBearerProvider {
20
23
  private _refreshAccessToken;
21
24
  private _expiresAt;
22
25
  private _refreshExpiresAt;
23
- constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, }: BearerOptions);
26
+ private _initialTokenResponse;
27
+ constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, initialTokenResponse, }: BearerOptions);
24
28
  getToken(): Promise<string>;
25
29
  private refresh;
26
30
  private getExpiresAt;
@@ -49,23 +49,29 @@ exports.RefreshBearerProvider = void 0;
49
49
  const core = __importStar(require("../core/index.js"));
50
50
  const decodeToken_js_1 = require("./utils/decodeToken.js");
51
51
  class RefreshBearerProvider {
52
- constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, }) {
52
+ constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, initialTokenResponse, }) {
53
53
  this.BUFFER_IN_MINUTES = 2;
54
54
  this._accessToken = accessToken || 'no_token';
55
55
  this._refreshToken = refreshToken;
56
- this._expiresAt = typeof expiresIn === "number"
57
- ? this.getExpiresAt(expiresIn, this.BUFFER_IN_MINUTES)
58
- : this.parseTokenExpiry(this._accessToken, this.BUFFER_IN_MINUTES) || this.getExpiresAt(0, this.BUFFER_IN_MINUTES);
59
- this._refreshExpiresAt = typeof refreshExpiresIn === "number"
60
- ? this.getExpiresAt(refreshExpiresIn, 0)
61
- : (this._refreshToken && this.parseTokenExpiry(this._refreshToken, 0)) || this.getExpiresAt(0, 0);
56
+ this._initialTokenResponse = initialTokenResponse;
57
+ this._expiresAt = this.getExpiresAt(expiresIn, this._accessToken, this.BUFFER_IN_MINUTES);
58
+ this._refreshExpiresAt = this.getExpiresAt(refreshExpiresIn, this._refreshToken, 0);
62
59
  this._refreshAccessToken = refreshAccessToken;
63
60
  }
64
61
  getToken() {
65
62
  return __awaiter(this, void 0, void 0, function* () {
66
- if (this._accessToken && this._expiresAt > new Date()) {
63
+ if (this._accessToken && this._accessToken !== 'no_token' && this._expiresAt > new Date()) {
67
64
  return core.Supplier.get(this._accessToken);
68
65
  }
66
+ if (this._initialTokenResponse) {
67
+ const tokenResponse = yield this._initialTokenResponse;
68
+ this._initialTokenResponse = undefined;
69
+ this._accessToken = tokenResponse.accessToken;
70
+ this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, tokenResponse.accessToken, this.BUFFER_IN_MINUTES);
71
+ this._refreshToken = tokenResponse.refreshToken;
72
+ this._refreshExpiresAt = this.getExpiresAt(tokenResponse.refreshExpiresIn, this._refreshToken, 0);
73
+ return this.getToken();
74
+ }
69
75
  return this.refresh();
70
76
  });
71
77
  }
@@ -76,19 +82,18 @@ class RefreshBearerProvider {
76
82
  }
77
83
  const tokenResponse = yield this._refreshAccessToken(this._refreshToken);
78
84
  this._accessToken = tokenResponse.accessToken;
79
- this._expiresAt = typeof tokenResponse.expiresIn === "number"
80
- ? this.getExpiresAt(tokenResponse.expiresIn, this.BUFFER_IN_MINUTES)
81
- : this.parseTokenExpiry(tokenResponse.accessToken, this.BUFFER_IN_MINUTES) || this.getExpiresAt(0, this.BUFFER_IN_MINUTES);
85
+ this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, tokenResponse.accessToken, this.BUFFER_IN_MINUTES);
82
86
  this._refreshToken = tokenResponse.refreshToken;
83
- this._refreshExpiresAt = typeof tokenResponse.refreshExpiresIn === "number"
84
- ? this.getExpiresAt(tokenResponse.refreshExpiresIn, 0)
85
- : this.parseTokenExpiry(this._refreshToken, 0) || this.getExpiresAt(0, 0);
87
+ this._refreshExpiresAt = this.getExpiresAt(tokenResponse.refreshExpiresIn, this._refreshToken, 0);
86
88
  return this._accessToken;
87
89
  });
88
90
  }
89
- getExpiresAt(expiresInSeconds = 0, bufferInMinutes = this.BUFFER_IN_MINUTES) {
90
- const now = new Date();
91
- return new Date(now.getTime() + expiresInSeconds * 1000 - bufferInMinutes * 60 * 1000);
91
+ getExpiresAt(expiresIn, token, bufferInMinutes = this.BUFFER_IN_MINUTES) {
92
+ if (typeof expiresIn === "number") {
93
+ const now = new Date();
94
+ return new Date(now.getTime() + expiresIn * 1000 - bufferInMinutes * 60 * 1000);
95
+ }
96
+ return this.parseTokenExpiry(token, bufferInMinutes) || this.getExpiresAt(0, token, bufferInMinutes);
92
97
  }
93
98
  parseTokenExpiry(token, bufferInMinutes) {
94
99
  if (!token || token === 'no_token') {
@@ -1,9 +1,11 @@
1
1
  import * as core from "../../core/index.js";
2
2
  import { CortiClient } from "../CortiClient.js";
3
3
  import { Environment } from "./getEnvironmentFromString.js";
4
+ import { ExpectedTokenResponse } from "../RefreshBearerProvider.js";
4
5
  type ResolvedClientOptions = {
5
6
  environment: Environment;
6
7
  tenantName: core.Supplier<string>;
8
+ initialTokenResponse?: Promise<ExpectedTokenResponse>;
7
9
  };
8
10
  export declare function resolveClientOptions(options: CortiClient.Options): ResolvedClientOptions;
9
11
  export {};
@@ -94,6 +94,7 @@ function resolveClientOptions(options) {
94
94
  ]);
95
95
  }
96
96
  return {
97
+ tokenResponse,
97
98
  tenantName: decoded.tenantName,
98
99
  environment: decoded.environment,
99
100
  };
@@ -111,5 +112,6 @@ function resolveClientOptions(options) {
111
112
  return core.Supplier.get(environment);
112
113
  });
113
114
  },
115
+ initialTokenResponse: tokenResponsePromise.then(result => result.tokenResponse),
114
116
  };
115
117
  }
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.0.0-rc";
1
+ export declare const SDK_VERSION = "0.0.0-rc.2";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.0.0-rc";
4
+ exports.SDK_VERSION = "0.0.0-rc.2";
@@ -28,8 +28,8 @@ export class CortiClient {
28
28
  "Tenant-Name": _options === null || _options === void 0 ? void 0 : _options.tenantName,
29
29
  "X-Fern-Language": "JavaScript",
30
30
  "X-Fern-SDK-Name": "@corti/sdk",
31
- "X-Fern-SDK-Version": "0.0.0-rc",
32
- "User-Agent": "@corti/sdk/0.0.0-rc",
31
+ "X-Fern-SDK-Version": "0.0.0-rc.2",
32
+ "User-Agent": "@corti/sdk/0.0.0-rc.2",
33
33
  "X-Fern-Runtime": core.RUNTIME.type,
34
34
  "X-Fern-Runtime-Version": core.RUNTIME.version,
35
35
  }, _options === null || _options === void 0 ? void 0 : _options.headers) });
@@ -56,7 +56,7 @@ export class CortiClient {
56
56
  /**
57
57
  * Patch: resolve tenantName and environment from options or token
58
58
  */
59
- const { tenantName, environment } = resolveClientOptions(_options);
59
+ const { tenantName, environment, initialTokenResponse } = resolveClientOptions(_options);
60
60
  this._options = Object.assign(Object.assign({}, _options), { headers: mergeHeaders({
61
61
  "Tenant-Name": tenantName,
62
62
  "X-Fern-Language": "JavaScript",
@@ -82,7 +82,7 @@ export class CortiClient {
82
82
  */
83
83
  authClient: new Auth(this._options),
84
84
  })
85
- : new RefreshBearerProvider(_options.auth);
85
+ : new RefreshBearerProvider(Object.assign(Object.assign({}, _options.auth), { initialTokenResponse }));
86
86
  }
87
87
  get interactions() {
88
88
  var _a;
@@ -2,8 +2,9 @@
2
2
  * RefreshBearerProvider used as a replacement of OAuthTokenProvider, in case when accessToken from outside of library was used instead of Client credentials.
3
3
  */
4
4
  import * as api from "../api/index.mjs";
5
- type ExpectedTokenResponse = Omit<api.GetTokenResponse, 'tokenType'> & {
5
+ export type ExpectedTokenResponse = Omit<api.GetTokenResponse, 'tokenType' | 'expiresIn'> & {
6
6
  tokenType?: string;
7
+ expiresIn?: number;
7
8
  };
8
9
  type RefreshAccessTokenFunction = (refreshToken?: string) => Promise<ExpectedTokenResponse> | ExpectedTokenResponse;
9
10
  export type BearerOptions = Partial<Omit<api.GetTokenResponse, 'accessToken'>> & ({
@@ -12,7 +13,9 @@ export type BearerOptions = Partial<Omit<api.GetTokenResponse, 'accessToken'>> &
12
13
  } | {
13
14
  refreshAccessToken: RefreshAccessTokenFunction;
14
15
  accessToken?: string;
15
- });
16
+ }) & {
17
+ initialTokenResponse?: Promise<ExpectedTokenResponse>;
18
+ };
16
19
  export declare class RefreshBearerProvider {
17
20
  private readonly BUFFER_IN_MINUTES;
18
21
  private _accessToken;
@@ -20,7 +23,8 @@ export declare class RefreshBearerProvider {
20
23
  private _refreshAccessToken;
21
24
  private _expiresAt;
22
25
  private _refreshExpiresAt;
23
- constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, }: BearerOptions);
26
+ private _initialTokenResponse;
27
+ constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, initialTokenResponse, }: BearerOptions);
24
28
  getToken(): Promise<string>;
25
29
  private refresh;
26
30
  private getExpiresAt;
@@ -13,23 +13,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  import * as core from "../core/index.mjs";
14
14
  import { decodeToken } from "./utils/decodeToken.mjs";
15
15
  export class RefreshBearerProvider {
16
- constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, }) {
16
+ constructor({ accessToken, refreshAccessToken, refreshToken, refreshExpiresIn, expiresIn, initialTokenResponse, }) {
17
17
  this.BUFFER_IN_MINUTES = 2;
18
18
  this._accessToken = accessToken || 'no_token';
19
19
  this._refreshToken = refreshToken;
20
- this._expiresAt = typeof expiresIn === "number"
21
- ? this.getExpiresAt(expiresIn, this.BUFFER_IN_MINUTES)
22
- : this.parseTokenExpiry(this._accessToken, this.BUFFER_IN_MINUTES) || this.getExpiresAt(0, this.BUFFER_IN_MINUTES);
23
- this._refreshExpiresAt = typeof refreshExpiresIn === "number"
24
- ? this.getExpiresAt(refreshExpiresIn, 0)
25
- : (this._refreshToken && this.parseTokenExpiry(this._refreshToken, 0)) || this.getExpiresAt(0, 0);
20
+ this._initialTokenResponse = initialTokenResponse;
21
+ this._expiresAt = this.getExpiresAt(expiresIn, this._accessToken, this.BUFFER_IN_MINUTES);
22
+ this._refreshExpiresAt = this.getExpiresAt(refreshExpiresIn, this._refreshToken, 0);
26
23
  this._refreshAccessToken = refreshAccessToken;
27
24
  }
28
25
  getToken() {
29
26
  return __awaiter(this, void 0, void 0, function* () {
30
- if (this._accessToken && this._expiresAt > new Date()) {
27
+ if (this._accessToken && this._accessToken !== 'no_token' && this._expiresAt > new Date()) {
31
28
  return core.Supplier.get(this._accessToken);
32
29
  }
30
+ if (this._initialTokenResponse) {
31
+ const tokenResponse = yield this._initialTokenResponse;
32
+ this._initialTokenResponse = undefined;
33
+ this._accessToken = tokenResponse.accessToken;
34
+ this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, tokenResponse.accessToken, this.BUFFER_IN_MINUTES);
35
+ this._refreshToken = tokenResponse.refreshToken;
36
+ this._refreshExpiresAt = this.getExpiresAt(tokenResponse.refreshExpiresIn, this._refreshToken, 0);
37
+ return this.getToken();
38
+ }
33
39
  return this.refresh();
34
40
  });
35
41
  }
@@ -40,19 +46,18 @@ export class RefreshBearerProvider {
40
46
  }
41
47
  const tokenResponse = yield this._refreshAccessToken(this._refreshToken);
42
48
  this._accessToken = tokenResponse.accessToken;
43
- this._expiresAt = typeof tokenResponse.expiresIn === "number"
44
- ? this.getExpiresAt(tokenResponse.expiresIn, this.BUFFER_IN_MINUTES)
45
- : this.parseTokenExpiry(tokenResponse.accessToken, this.BUFFER_IN_MINUTES) || this.getExpiresAt(0, this.BUFFER_IN_MINUTES);
49
+ this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, tokenResponse.accessToken, this.BUFFER_IN_MINUTES);
46
50
  this._refreshToken = tokenResponse.refreshToken;
47
- this._refreshExpiresAt = typeof tokenResponse.refreshExpiresIn === "number"
48
- ? this.getExpiresAt(tokenResponse.refreshExpiresIn, 0)
49
- : this.parseTokenExpiry(this._refreshToken, 0) || this.getExpiresAt(0, 0);
51
+ this._refreshExpiresAt = this.getExpiresAt(tokenResponse.refreshExpiresIn, this._refreshToken, 0);
50
52
  return this._accessToken;
51
53
  });
52
54
  }
53
- getExpiresAt(expiresInSeconds = 0, bufferInMinutes = this.BUFFER_IN_MINUTES) {
54
- const now = new Date();
55
- return new Date(now.getTime() + expiresInSeconds * 1000 - bufferInMinutes * 60 * 1000);
55
+ getExpiresAt(expiresIn, token, bufferInMinutes = this.BUFFER_IN_MINUTES) {
56
+ if (typeof expiresIn === "number") {
57
+ const now = new Date();
58
+ return new Date(now.getTime() + expiresIn * 1000 - bufferInMinutes * 60 * 1000);
59
+ }
60
+ return this.parseTokenExpiry(token, bufferInMinutes) || this.getExpiresAt(0, token, bufferInMinutes);
56
61
  }
57
62
  parseTokenExpiry(token, bufferInMinutes) {
58
63
  if (!token || token === 'no_token') {
@@ -1,9 +1,11 @@
1
1
  import * as core from "../../core/index.mjs";
2
2
  import { CortiClient } from "../CortiClient.mjs";
3
3
  import { Environment } from "./getEnvironmentFromString.mjs";
4
+ import { ExpectedTokenResponse } from "../RefreshBearerProvider.mjs";
4
5
  type ResolvedClientOptions = {
5
6
  environment: Environment;
6
7
  tenantName: core.Supplier<string>;
8
+ initialTokenResponse?: Promise<ExpectedTokenResponse>;
7
9
  };
8
10
  export declare function resolveClientOptions(options: CortiClient.Options): ResolvedClientOptions;
9
11
  export {};
@@ -58,6 +58,7 @@ export function resolveClientOptions(options) {
58
58
  ]);
59
59
  }
60
60
  return {
61
+ tokenResponse,
61
62
  tenantName: decoded.tenantName,
62
63
  environment: decoded.environment,
63
64
  };
@@ -75,5 +76,6 @@ export function resolveClientOptions(options) {
75
76
  return core.Supplier.get(environment);
76
77
  });
77
78
  },
79
+ initialTokenResponse: tokenResponsePromise.then(result => result.tokenResponse),
78
80
  };
79
81
  }
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.0.0-rc";
1
+ export declare const SDK_VERSION = "0.0.0-rc.2";
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.0.0-rc";
1
+ export const SDK_VERSION = "0.0.0-rc.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corti/sdk",
3
- "version": "0.0.0-rc",
3
+ "version": "0.0.0-rc.2",
4
4
  "private": false,
5
5
  "repository": "github:corticph/corti-sdk-javascript",
6
6
  "license": "MIT",