@or-sdk/base
Advanced tools
Comparing version 0.19.3 to 0.20.0
@@ -59,3 +59,4 @@ "use strict"; | ||
function Base(_a) { | ||
var token = _a.token, discoveryUrl = _a.discoveryUrl, serviceKey = _a.serviceKey, requestAccountId = _a.requestAccountId, requestProvidersAccountId = _a.requestProvidersAccountId, feature = _a.feature, accountId = _a.accountId; | ||
var token = _a.token, discoveryUrl = _a.discoveryUrl, serviceKey = _a.serviceKey, requestAccountId = _a.requestAccountId, requestProvidersAccountId = _a.requestProvidersAccountId, feature = _a.feature, accountId = _a.accountId, serviceUrl = _a.serviceUrl; | ||
this.discoveryUrl = null; | ||
this.discoveryRoute = "api/" + constants_1.VERSION; | ||
@@ -65,12 +66,15 @@ this.requestAccountId = false; | ||
this._isCrossAccount = false; | ||
this.status = constants_1.SdkStatus.NEW; | ||
this._currentAccountId = null; | ||
this._targetAccountId = null; | ||
this._providersAccountId = null; | ||
this.status = constants_1.SdkStatus.NEW; | ||
this._serviceUrl = ''; | ||
this._serviceUrl = null; | ||
this.token = token; | ||
this.discoveryUrl = discoveryUrl; | ||
this.discoveryUrl = discoveryUrl || null; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios_1.default.create({ paramsSerializer: utils_1.paramsSerializer }); | ||
this._serviceUrl = serviceUrl || null; | ||
if (accountId) { | ||
this._currentAccountId = accountId; | ||
this._targetAccountId = accountId; | ||
this._isCrossAccount = true; | ||
@@ -91,2 +95,9 @@ } | ||
}); | ||
Object.defineProperty(Base.prototype, "targetAccountId", { | ||
get: function () { | ||
return this._targetAccountId; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Base.prototype, "providersAccountId", { | ||
@@ -112,3 +123,8 @@ get: function () { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
_a.trys.push([0, 4, , 5]); | ||
if (!this.serviceUrl) return [3, 1]; | ||
this._serviceUrl = this.makeApiUrl({ url: this.serviceUrl }); | ||
return [3, 3]; | ||
case 1: | ||
if (!this.discoveryUrl) return [3, 3]; | ||
return [4, this.axios({ | ||
@@ -119,3 +135,3 @@ url: (0, utils_1.normalizeUrl)(this.discoveryUrl) + "/" + (0, utils_1.normalizeRoute)(this.discoveryRoute), | ||
})]; | ||
case 1: | ||
case 2: | ||
data = (_a.sent()).data; | ||
@@ -129,9 +145,11 @@ if (this.requestAccountId) { | ||
this._serviceUrl = this.makeApiUrl(data); | ||
_a.label = 3; | ||
case 3: | ||
this.status = constants_1.SdkStatus.SUCCESS; | ||
return [3, 3]; | ||
case 2: | ||
return [3, 5]; | ||
case 4: | ||
e_1 = _a.sent(); | ||
this.status = constants_1.SdkStatus.ERROR; | ||
throw e_1; | ||
case 3: return [2]; | ||
case 5: return [2]; | ||
} | ||
@@ -213,4 +231,4 @@ }); | ||
query = params.params || {}; | ||
if (this._isCrossAccount && this._currentAccountId) { | ||
query.accountId = this._currentAccountId; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
} | ||
@@ -217,0 +235,0 @@ return [4, this.axios({ |
@@ -14,3 +14,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class Base { | ||
constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId }) { | ||
constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId, serviceUrl, }) { | ||
this.discoveryUrl = null; | ||
this.discoveryRoute = `api/${VERSION}`; | ||
@@ -20,12 +21,15 @@ this.requestAccountId = false; | ||
this._isCrossAccount = false; | ||
this.status = SdkStatus.NEW; | ||
this._currentAccountId = null; | ||
this._targetAccountId = null; | ||
this._providersAccountId = null; | ||
this.status = SdkStatus.NEW; | ||
this._serviceUrl = ''; | ||
this._serviceUrl = null; | ||
this.token = token; | ||
this.discoveryUrl = discoveryUrl; | ||
this.discoveryUrl = discoveryUrl || null; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios.create({ paramsSerializer }); | ||
this._serviceUrl = serviceUrl || null; | ||
if (accountId) { | ||
this._currentAccountId = accountId; | ||
this._targetAccountId = accountId; | ||
this._isCrossAccount = true; | ||
@@ -42,2 +46,5 @@ } | ||
} | ||
get targetAccountId() { | ||
return this._targetAccountId; | ||
} | ||
get providersAccountId() { | ||
@@ -52,14 +59,19 @@ return this._providersAccountId; | ||
try { | ||
const { data } = yield this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: Object.assign(Object.assign(Object.assign({ serviceName: this.serviceKey }, (this.requestAccountId ? { requestAccountId: this.requestAccountId } : {})), (this.requestProvidersAccountId ? { requestProvidersAccountId: this.requestProvidersAccountId } : {})), (this.feature ? { feature: this.feature } : {})), | ||
}); | ||
if (this.requestAccountId) { | ||
this._currentAccountId = data.accountId; | ||
if (this.serviceUrl) { | ||
this._serviceUrl = this.makeApiUrl({ url: this.serviceUrl }); | ||
} | ||
if (this.requestProvidersAccountId) { | ||
this._providersAccountId = data.providersAccountId; | ||
else if (this.discoveryUrl) { | ||
const { data } = yield this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: Object.assign(Object.assign(Object.assign({ serviceName: this.serviceKey }, (this.requestAccountId ? { requestAccountId: this.requestAccountId } : {})), (this.requestProvidersAccountId ? { requestProvidersAccountId: this.requestProvidersAccountId } : {})), (this.feature ? { feature: this.feature } : {})), | ||
}); | ||
if (this.requestAccountId) { | ||
this._currentAccountId = data.accountId; | ||
} | ||
if (this.requestProvidersAccountId) { | ||
this._providersAccountId = data.providersAccountId; | ||
} | ||
this._serviceUrl = this.makeApiUrl(data); | ||
} | ||
this._serviceUrl = this.makeApiUrl(data); | ||
this.status = SdkStatus.SUCCESS; | ||
@@ -122,4 +134,4 @@ } | ||
const query = params.params || {}; | ||
if (this._isCrossAccount && this._currentAccountId) { | ||
query.accountId = this._currentAccountId; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
} | ||
@@ -126,0 +138,0 @@ const { data } = yield this.axios({ |
@@ -13,12 +13,14 @@ import { AxiosRequestHeaders, AxiosError } from 'axios'; | ||
private readonly _isCrossAccount; | ||
private status; | ||
private _currentAccountId; | ||
private _targetAccountId; | ||
private _providersAccountId; | ||
private status; | ||
private _serviceUrl; | ||
protected constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId }: BaseConfig); | ||
protected constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId, serviceUrl, }: BaseConfig); | ||
get currentAccountId(): string | null; | ||
get targetAccountId(): string | null; | ||
get providersAccountId(): string | null; | ||
get isCrossAccount(): boolean; | ||
init(): Promise<void>; | ||
get serviceUrl(): string; | ||
get serviceUrl(): string | null; | ||
makeApiUrl(data: ServiceDiscoveryResponse): string; | ||
@@ -25,0 +27,0 @@ parseError(e: AxiosError): Error; |
@@ -5,6 +5,7 @@ import { Method } from 'axios'; | ||
token: Token; | ||
discoveryUrl: string; | ||
discoveryUrl?: string; | ||
serviceKey: string; | ||
feature?: string; | ||
accountId?: string; | ||
serviceUrl?: string; | ||
requestAccountId?: boolean; | ||
@@ -39,4 +40,4 @@ requestProvidersAccountId?: boolean; | ||
url: string; | ||
version: string; | ||
version?: string; | ||
accountId?: string; | ||
} |
{ | ||
"version": "0.19.3", | ||
"version": "0.20.0", | ||
"name": "@or-sdk/base", | ||
@@ -31,3 +31,3 @@ "main": "dist/cjs/index.js", | ||
}, | ||
"gitHead": "e36a291c30c423da5ef31083dbedb2c7461585d0" | ||
"gitHead": "ba5d11f46cbdbf5210cffa2552d3a93bc2f3601e" | ||
} |
@@ -9,3 +9,3 @@ import axios, { AxiosInstance, AxiosRequestHeaders, AxiosError } from 'axios'; | ||
private readonly axios: AxiosInstance; | ||
private readonly discoveryUrl: string; | ||
private readonly discoveryUrl: string | null = null; | ||
private readonly discoveryRoute = `api/${VERSION}`; | ||
@@ -18,15 +18,28 @@ private readonly serviceKey: string; | ||
private status: SdkStatus = SdkStatus.NEW; | ||
private _currentAccountId: string | null = null; | ||
private _targetAccountId: string | null = null; | ||
private _providersAccountId: string | null = null; | ||
private status: SdkStatus = SdkStatus.NEW; | ||
private _serviceUrl = ''; | ||
private _serviceUrl: string | null = null; | ||
protected constructor({ token, discoveryUrl, serviceKey, requestAccountId, requestProvidersAccountId, feature, accountId }: BaseConfig) { | ||
protected constructor({ | ||
token, | ||
discoveryUrl, | ||
serviceKey, | ||
requestAccountId, | ||
requestProvidersAccountId, | ||
feature, | ||
accountId, | ||
serviceUrl, | ||
}: BaseConfig) { | ||
this.token = token; | ||
this.discoveryUrl = discoveryUrl; | ||
this.discoveryUrl = discoveryUrl || null; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios.create({ paramsSerializer }); | ||
this._serviceUrl = serviceUrl || null; | ||
if (accountId) { | ||
this._currentAccountId = accountId; | ||
this._targetAccountId = accountId; | ||
this._isCrossAccount = true; | ||
@@ -47,2 +60,6 @@ } else { | ||
public get targetAccountId(): string | null { | ||
return this._targetAccountId; | ||
} | ||
/** | ||
@@ -61,22 +78,27 @@ * @deprecated used for migration stage only | ||
try { | ||
const { data } = await this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: { | ||
serviceName: this.serviceKey, | ||
...(this.requestAccountId ? { requestAccountId: this.requestAccountId } : {}), | ||
...(this.requestProvidersAccountId ? { requestProvidersAccountId: this.requestProvidersAccountId } : {}), | ||
...(this.feature ? { feature: this.feature } : {}), | ||
}, | ||
}); | ||
if (this.serviceUrl) { | ||
this._serviceUrl = this.makeApiUrl({ url: this.serviceUrl }); | ||
} else if (this.discoveryUrl) { | ||
const { data } = await this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: { | ||
serviceName: this.serviceKey, | ||
// todo: remove requestAccountId | ||
...(this.requestAccountId ? { requestAccountId: this.requestAccountId } : {}), | ||
// todo: remove requestProvidersAccountId | ||
...(this.requestProvidersAccountId ? { requestProvidersAccountId: this.requestProvidersAccountId } : {}), | ||
...(this.feature ? { feature: this.feature } : {}), | ||
}, | ||
}); | ||
if (this.requestAccountId) { | ||
this._currentAccountId = data.accountId; | ||
if (this.requestAccountId) { | ||
this._currentAccountId = data.accountId; | ||
} | ||
if (this.requestProvidersAccountId) { | ||
this._providersAccountId = data.providersAccountId; | ||
} | ||
this._serviceUrl = this.makeApiUrl(data); | ||
} | ||
if (this.requestProvidersAccountId) { | ||
this._providersAccountId = data.providersAccountId; | ||
} | ||
this._serviceUrl = this.makeApiUrl(data); | ||
this.status = SdkStatus.SUCCESS; | ||
@@ -89,3 +111,3 @@ } catch (e) { | ||
public get serviceUrl(): string { | ||
public get serviceUrl(): string | null { | ||
return this._serviceUrl; | ||
@@ -176,4 +198,4 @@ } | ||
const query: any = params.params || {}; | ||
if (this._isCrossAccount && this._currentAccountId) { | ||
query.accountId = this._currentAccountId; | ||
if (this.isCrossAccount) { | ||
query.accountId = this.targetAccountId; | ||
} | ||
@@ -180,0 +202,0 @@ const { data } = await this.axios({ |
import { Method } from 'axios'; | ||
export { AxiosError } from 'axios'; | ||
export interface BaseConfig { | ||
@@ -13,3 +14,3 @@ /** | ||
*/ | ||
discoveryUrl: string; | ||
discoveryUrl?: string; | ||
@@ -20,2 +21,3 @@ /** | ||
serviceKey: string; | ||
/** | ||
@@ -25,2 +27,3 @@ * Can be used to access to api deployed on feature sub domain | ||
feature?: string; | ||
/** | ||
@@ -32,2 +35,7 @@ * Account ID for cross-account requests (super admin only) | ||
/** | ||
* Direct service url, can be used to avoid discovery api call | ||
*/ | ||
serviceUrl?: string; | ||
/** | ||
* @deprecated used for migration stage only | ||
@@ -43,3 +51,3 @@ */ | ||
export type Token = string | (()=> string); | ||
export type Token = string | (() => string); | ||
@@ -76,4 +84,4 @@ export interface Headers { | ||
url: string; | ||
version: string; | ||
version?: string; | ||
accountId?: string; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
55911
1044