@or-sdk/base
Advanced tools
Comparing version 0.0.26 to 0.1.0
@@ -45,29 +45,60 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
var constants_1 = require("./constants"); | ||
var Base = (function () { | ||
function Base(_a) { | ||
var userTokenFactory = _a.userTokenFactory, token = _a.token, baseUrl = _a.baseUrl; | ||
var token = _a.token, discoveryUrl = _a.discoveryUrl, serviceKey = _a.serviceKey; | ||
this.discoveryRoute = '/api/v1'; | ||
this.status = constants_1.SdkStatus.NEW; | ||
this.serviceUrl = ''; | ||
this.token = token; | ||
this.userTokenFactory = userTokenFactory; | ||
this.baseUrl = baseUrl; | ||
this.discoveryUrl = discoveryUrl; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios_1.default.create({ paramsSerializer: utils_1.paramsSerializer }); | ||
} | ||
Base.prototype.init = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4, this.axios({ | ||
url: (0, utils_1.normalizeUrl)(this.discoveryUrl) + "/" + (0, utils_1.normalizeRoute)(this.discoveryRoute), | ||
headers: this.getHeaders(), | ||
params: { | ||
serviceName: this.serviceKey, | ||
}, | ||
})]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
this.serviceUrl = this.makeApiUrl(data); | ||
this.status = constants_1.SdkStatus.SUCCESS; | ||
return [3, 3]; | ||
case 2: | ||
e_1 = _a.sent(); | ||
this.status = constants_1.SdkStatus.ERROR; | ||
throw e_1; | ||
case 3: return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Base.prototype.makeApiUrl = function (data) { | ||
return data.url; | ||
}; | ||
Base.prototype.getHeaders = function () { | ||
return { | ||
Authorization: this.userToken, | ||
Authorization: this.getToken(), | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
}; | ||
}; | ||
Object.defineProperty(Base.prototype, "userToken", { | ||
get: function () { | ||
if (this.token) { | ||
return this.token; | ||
} | ||
if (typeof this.userTokenFactory === 'function') { | ||
return this.userTokenFactory(); | ||
} | ||
throw new Error('token or getToken is not defined'); | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Base.prototype.getToken = function () { | ||
if (typeof this.token === 'string') { | ||
return this.token; | ||
} | ||
if (typeof this.token === 'function') { | ||
return this.token(); | ||
} | ||
throw new Error('token is not defined'); | ||
}; | ||
Base.prototype.callApi = function (params) { | ||
@@ -78,4 +109,10 @@ return __awaiter(this, void 0, void 0, function () { | ||
switch (_a.label) { | ||
case 0: return [4, this.axios({ | ||
url: "" + this.baseUrl + params.route, | ||
case 0: | ||
if (!(this.status === constants_1.SdkStatus.NEW)) return [3, 2]; | ||
return [4, this.init()]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: return [4, this.axios({ | ||
url: this.serviceUrl + "/" + (0, utils_1.normalizeRoute)(params.route), | ||
method: params.method || 'GET', | ||
@@ -87,3 +124,3 @@ data: params.data, | ||
})]; | ||
case 1: | ||
case 3: | ||
data = (_a.sent()).data; | ||
@@ -90,0 +127,0 @@ return [2, data]; |
@@ -6,5 +6,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.paramsSerializer = void 0; | ||
exports.normalizeUrl = exports.normalizeRoute = exports.paramsSerializer = void 0; | ||
var paramsSerializer_1 = require("./paramsSerializer"); | ||
Object.defineProperty(exports, "paramsSerializer", { enumerable: true, get: function () { return __importDefault(paramsSerializer_1).default; } }); | ||
var normalizeRoute_1 = require("./normalizeRoute"); | ||
Object.defineProperty(exports, "normalizeRoute", { enumerable: true, get: function () { return __importDefault(normalizeRoute_1).default; } }); | ||
var normalizeUrl_1 = require("./normalizeUrl"); | ||
Object.defineProperty(exports, "normalizeUrl", { enumerable: true, get: function () { return __importDefault(normalizeUrl_1).default; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -11,29 +11,58 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import axios from 'axios'; | ||
import { paramsSerializer } from './utils'; | ||
import { normalizeRoute, normalizeUrl, paramsSerializer } from './utils'; | ||
import { SdkStatus } from './constants'; | ||
export class Base { | ||
constructor({ userTokenFactory, token, baseUrl }) { | ||
constructor({ token, discoveryUrl, serviceKey }) { | ||
this.discoveryRoute = '/api/v1'; | ||
this.status = SdkStatus.NEW; | ||
this.serviceUrl = ''; | ||
this.token = token; | ||
this.userTokenFactory = userTokenFactory; | ||
this.baseUrl = baseUrl; | ||
this.discoveryUrl = discoveryUrl; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios.create({ paramsSerializer }); | ||
} | ||
init() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { data } = yield this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: { | ||
serviceName: this.serviceKey, | ||
}, | ||
}); | ||
this.serviceUrl = this.makeApiUrl(data); | ||
this.status = SdkStatus.SUCCESS; | ||
} | ||
catch (e) { | ||
this.status = SdkStatus.ERROR; | ||
throw e; | ||
} | ||
}); | ||
} | ||
makeApiUrl(data) { | ||
return data.url; | ||
} | ||
getHeaders() { | ||
return { | ||
Authorization: this.userToken, | ||
Authorization: this.getToken(), | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
}; | ||
} | ||
get userToken() { | ||
if (this.token) { | ||
getToken() { | ||
if (typeof this.token === 'string') { | ||
return this.token; | ||
} | ||
if (typeof this.userTokenFactory === 'function') { | ||
return this.userTokenFactory(); | ||
if (typeof this.token === 'function') { | ||
return this.token(); | ||
} | ||
throw new Error('token or getToken is not defined'); | ||
throw new Error('token is not defined'); | ||
} | ||
callApi(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.status === SdkStatus.NEW) { | ||
yield this.init(); | ||
} | ||
const { data } = yield this.axios({ | ||
url: `${this.baseUrl}${params.route}`, | ||
url: `${this.serviceUrl}/${normalizeRoute(params.route)}`, | ||
method: params.method || 'GET', | ||
@@ -40,0 +69,0 @@ data: params.data, |
export { default as paramsSerializer } from './paramsSerializer'; | ||
export { default as normalizeRoute } from './normalizeRoute'; | ||
export { default as normalizeUrl } from './normalizeUrl'; | ||
//# sourceMappingURL=index.js.map |
import { AxiosRequestHeaders } from 'axios'; | ||
import { BaseConfig, CalApiParams } from './types'; | ||
export declare class Base { | ||
private readonly userTokenFactory; | ||
import { BaseConfig, CalApiParams, ServiceDiscoveryResponse } from './types'; | ||
export declare abstract class Base { | ||
private readonly token; | ||
private readonly axios; | ||
private readonly baseUrl; | ||
protected constructor({ userTokenFactory, token, baseUrl }: BaseConfig); | ||
private readonly discoveryUrl; | ||
private readonly discoveryRoute; | ||
private readonly serviceKey; | ||
private status; | ||
private serviceUrl; | ||
protected constructor({ token, discoveryUrl, serviceKey }: BaseConfig); | ||
init(): Promise<void>; | ||
makeApiUrl(data: ServiceDiscoveryResponse): string; | ||
protected getHeaders(): AxiosRequestHeaders; | ||
get userToken(): string; | ||
private getToken; | ||
protected callApi<T>(params: CalApiParams): Promise<T>; | ||
} | ||
//# sourceMappingURL=Base.d.ts.map |
export { Base } from './Base'; | ||
export * from './types'; | ||
export { PaginationOptions } from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
import { Method } from 'axios'; | ||
export interface BaseConfig { | ||
token?: string; | ||
userTokenFactory?: () => string; | ||
baseUrl: string; | ||
token: Token; | ||
discoveryUrl: string; | ||
serviceKey: string; | ||
} | ||
export declare type Token = string | (() => string); | ||
export interface Headers { | ||
@@ -30,2 +31,6 @@ Authorization: string; | ||
} | ||
//# sourceMappingURL=types.d.ts.map | ||
export interface ServiceDiscoveryResponse { | ||
url: string; | ||
version: string; | ||
accountId: string; | ||
} |
export { default as paramsSerializer } from './paramsSerializer'; | ||
//# sourceMappingURL=index.d.ts.map | ||
export { default as normalizeRoute } from './normalizeRoute'; | ||
export { default as normalizeUrl } from './normalizeUrl'; |
declare function paramsSerializer(params: any): string; | ||
export default paramsSerializer; | ||
//# sourceMappingURL=paramsSerializer.d.ts.map |
{ | ||
"version": "0.0.26", | ||
"version": "0.1.0", | ||
"name": "@or-sdk/base", | ||
@@ -8,7 +8,10 @@ "main": "dist/cjs/index.js", | ||
"scripts": { | ||
"build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:types", | ||
"build:watch": "tsc --project tsconfig.json -w", | ||
"build": "npm run clean && concurrently \"npm run build:cjs\" \"npm run build:esm\" \"npm run build:types\"", | ||
"build:watch": "concurrently \"npm run build:watch:cjs\" \"npm run build:watch:esm\" \"npm run build:watch:types\"", | ||
"build:cjs": "tsc --project tsconfig.json", | ||
"build:watch:cjs": "tsc --project tsconfig.json -w", | ||
"build:esm": "tsc --project tsconfig.esm.json", | ||
"build:watch:esm": "tsc --project tsconfig.esm.json -w", | ||
"build:types": "tsc --project tsconfig.types.json", | ||
"build:watch:types": "tsc --project tsconfig.types.json -w", | ||
"clean": "rm -rf ./dist" | ||
@@ -28,3 +31,3 @@ }, | ||
}, | ||
"gitHead": "7281d4f8e05e4d5981b63ea8591fb4328ea5e931" | ||
"gitHead": "58bdb9726f8ef54c8166d6245b450c78f47a8383" | ||
} |
import axios, { AxiosInstance, AxiosRequestHeaders } from 'axios'; | ||
import { BaseConfig, CalApiParams } from './types'; | ||
import { paramsSerializer } from './utils'; | ||
import { BaseConfig, CalApiParams, ServiceDiscoveryResponse } from './types'; | ||
import { normalizeRoute, normalizeUrl, paramsSerializer } from './utils'; | ||
import { SdkStatus } from './constants'; | ||
export class Base { | ||
private readonly userTokenFactory: (()=> string) | undefined; | ||
private readonly token: string | undefined; | ||
export abstract class Base { | ||
private readonly token: string | (()=> string); | ||
private readonly axios: AxiosInstance; | ||
private readonly baseUrl: string; | ||
private readonly discoveryUrl: string; | ||
private readonly discoveryRoute = '/api/v1'; | ||
private readonly serviceKey: string; | ||
protected constructor({ userTokenFactory, token, baseUrl }: BaseConfig) { | ||
private status: SdkStatus = SdkStatus.NEW; | ||
private serviceUrl = ''; | ||
protected constructor({ token, discoveryUrl, serviceKey }: BaseConfig) { | ||
this.token = token; | ||
this.userTokenFactory = userTokenFactory; | ||
this.baseUrl = baseUrl; | ||
this.discoveryUrl = discoveryUrl; | ||
this.serviceKey = serviceKey; | ||
this.axios = axios.create({ paramsSerializer }); | ||
} | ||
async init(): Promise<void> { | ||
try { | ||
const { data } = await this.axios({ | ||
url: `${normalizeUrl(this.discoveryUrl)}/${normalizeRoute(this.discoveryRoute)}`, | ||
headers: this.getHeaders(), | ||
params: { | ||
serviceName: this.serviceKey, | ||
}, | ||
}); | ||
this.serviceUrl = this.makeApiUrl(data); | ||
this.status = SdkStatus.SUCCESS; | ||
} catch (e) { | ||
this.status = SdkStatus.ERROR; | ||
throw e; | ||
} | ||
} | ||
/** | ||
* | ||
* Override this method in case if api url have specific url arguments | ||
* | ||
*/ | ||
makeApiUrl(data: ServiceDiscoveryResponse): string { | ||
return data.url; | ||
} | ||
protected getHeaders(): AxiosRequestHeaders { | ||
return { | ||
Authorization: this.userToken, | ||
Authorization: this.getToken(), | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
@@ -25,15 +58,18 @@ }; | ||
get userToken(): string { | ||
if (this.token) { | ||
private getToken(): string { | ||
if (typeof this.token === 'string') { | ||
return this.token; | ||
} | ||
if (typeof this.userTokenFactory === 'function') { | ||
return this.userTokenFactory(); | ||
if (typeof this.token === 'function') { | ||
return this.token(); | ||
} | ||
throw new Error('token or getToken is not defined'); | ||
throw new Error('token is not defined'); | ||
} | ||
protected async callApi<T>(params: CalApiParams): Promise<T> { | ||
if (this.status === SdkStatus.NEW) { | ||
await this.init(); | ||
} | ||
const { data } = await this.axios({ | ||
url: `${this.baseUrl}${params.route}`, | ||
url: `${this.serviceUrl}/${normalizeRoute(params.route)}`, | ||
method: params.method || 'GET', | ||
@@ -40,0 +76,0 @@ data: params.data, |
@@ -5,12 +5,19 @@ import { Method } from 'axios'; | ||
/** | ||
* token | ||
* token or token getter | ||
*/ | ||
token?: string; | ||
token: Token; | ||
/** | ||
* function which return token | ||
* Url of OneReach service discovery api | ||
*/ | ||
userTokenFactory?: ()=> string; | ||
baseUrl: string; | ||
discoveryUrl: string; | ||
/** | ||
* Service key | ||
*/ | ||
serviceKey: string; | ||
} | ||
export type Token = string | (()=> string); | ||
export interface Headers { | ||
@@ -43,1 +50,7 @@ Authorization: string; | ||
} | ||
export interface ServiceDiscoveryResponse { | ||
url: string; | ||
version: string; | ||
accountId: string; | ||
} |
export { default as paramsSerializer } from './paramsSerializer'; | ||
export { default as normalizeRoute } from './normalizeRoute'; | ||
export { default as normalizeUrl } from './normalizeUrl'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28218
542
52