@globalfishingwatch/api-client
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -64,10 +64,11 @@ /*! ***************************************************************************** | ||
var GATEWAY_URL = process.env.GATEWAY_URL || | ||
process.env.REACT_APP_GATEWAY_URL || | ||
var API_GATEWAY_URL = process.env.API_GATEWAY_URL || | ||
process.env.REACT_APP_API_GATEWAY_URL || | ||
'https://gateway.api.dev.globalfishingwatch.org'; | ||
var USER_TOKEN_STORAGE_KEY = 'GFW_API_USER_TOKEN'; | ||
var USER_REFRESH_TOKEN_STORAGE_KEY = 'GFW_API_USER_REFRESH_TOKEN'; | ||
function getLoginUrl(callback, client) { | ||
var AUTH_PATH = 'auth'; | ||
function getLoginUrl(callbackUrl, client) { | ||
if (client === void 0) { client = 'gfw'; } | ||
return GATEWAY_URL + "/auth?client=" + client + "&callback=" + callback; | ||
return API_GATEWAY_URL + "/" + AUTH_PATH + "?client=" + client + "&callback=" + callbackUrl; | ||
} | ||
@@ -85,11 +86,11 @@ var processStatus = function (response) { | ||
function GFWAPI(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.debug, debug = _c === void 0 ? false : _c, _d = _b.token, token = _d === void 0 ? null : _d, _e = _b.refreshToken, refreshToken = _e === void 0 ? null : _e, _f = _b.baseUrl, baseUrl = _f === void 0 ? GATEWAY_URL : _f, _g = _b.tokenStorageKey, tokenStorageKey = _g === void 0 ? USER_TOKEN_STORAGE_KEY : _g, _h = _b.refreshTokenStorageKey, refreshTokenStorageKey = _h === void 0 ? USER_REFRESH_TOKEN_STORAGE_KEY : _h; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.debug, debug = _c === void 0 ? false : _c, _d = _b.baseUrl, baseUrl = _d === void 0 ? API_GATEWAY_URL : _d, _e = _b.tokenStorageKey, tokenStorageKey = _e === void 0 ? USER_TOKEN_STORAGE_KEY : _e, _f = _b.refreshTokenStorageKey, refreshTokenStorageKey = _f === void 0 ? USER_REFRESH_TOKEN_STORAGE_KEY : _f; | ||
this.token = ''; | ||
this.refreshToken = ''; | ||
this.maxRefreshRetries = 1; | ||
this.debug = debug; | ||
this.baseUrl = baseUrl; | ||
this.storageKeys = { token: tokenStorageKey, refreshToken: refreshTokenStorageKey }; | ||
this.setToken(token || localStorage.getItem(tokenStorageKey) || ''); | ||
this.setRefreshToken(refreshToken || localStorage.getItem(refreshTokenStorageKey) || ''); | ||
this.refreshRetries = 0; | ||
this.setToken(localStorage.getItem(tokenStorageKey) || ''); | ||
this.setRefreshToken(localStorage.getItem(refreshTokenStorageKey) || ''); | ||
this.logging = null; | ||
@@ -109,2 +110,7 @@ if (debug) { | ||
}; | ||
GFWAPI.prototype.setConfig = function (_a) { | ||
var _b = _a.debug, debug = _b === void 0 ? this.debug : _b, _c = _a.baseUrl, baseUrl = _c === void 0 ? this.baseUrl : _c; | ||
this.debug = debug; | ||
this.baseUrl = baseUrl; | ||
}; | ||
GFWAPI.prototype.getToken = function () { | ||
@@ -133,3 +139,3 @@ return this.token; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token?access-token=" + accessToken) | ||
return [2 /*return*/, fetch(this.baseUrl + "/" + AUTH_PATH + "/token?access-token=" + accessToken) | ||
.then(processStatus) | ||
@@ -143,3 +149,3 @@ .then(parseJSON)]; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token/reload", { | ||
return [2 /*return*/, fetch(this.baseUrl + "/" + AUTH_PATH + "/token/reload", { | ||
headers: { | ||
@@ -154,7 +160,12 @@ Authorization: "Bearer " + refreshToken, | ||
}; | ||
GFWAPI.prototype.fetch = function (url, options, refreshRetries) { | ||
GFWAPI.prototype.fetch = function (url, options) { | ||
if (options === void 0) { options = {}; } | ||
return this._internalFetch(url, options); | ||
}; | ||
GFWAPI.prototype._internalFetch = function (url, options, refreshRetries, waitLogin) { | ||
if (options === void 0) { options = {}; } | ||
if (refreshRetries === void 0) { refreshRetries = 0; } | ||
if (waitLogin === void 0) { waitLogin = true; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, method, _b, body, _c, headers, _d, json_1, data, e_1, token, e_2, e_3; | ||
var _a, method, _b, body, _c, headers, _d, json_1, signal, data, e_1, token, e_2, e_3; | ||
return __generator(this, function (_e) { | ||
@@ -164,3 +175,3 @@ switch (_e.label) { | ||
_e.trys.push([0, 12, , 13]); | ||
if (!(this.logging && url !== this.baseUrl + "/auth/me")) return [3 /*break*/, 2]; | ||
if (!(this.logging && waitLogin)) return [3 /*break*/, 2]; | ||
// Don't do any request until the login is completed | ||
@@ -176,7 +187,7 @@ // and don't wait for the login request itselft | ||
_e.trys.push([2, 4, , 11]); | ||
_a = options.method, method = _a === void 0 ? 'GET' : _a, _b = options.body, body = _b === void 0 ? null : _b, _c = options.headers, headers = _c === void 0 ? {} : _c, _d = options.json, json_1 = _d === void 0 ? true : _d; | ||
_a = options.method, method = _a === void 0 ? 'GET' : _a, _b = options.body, body = _b === void 0 ? null : _b, _c = options.headers, headers = _c === void 0 ? {} : _c, _d = options.json, json_1 = _d === void 0 ? true : _d, signal = options.signal; | ||
if (this.debug) { | ||
console.log("GFWAPI: Fetching url: " + url); | ||
} | ||
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (body && body)), { headers: __assign(__assign({}, headers), { Authorization: "Bearer " + this.getToken() }) })) | ||
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (body && body)), { signal: signal, headers: __assign(__assign({}, headers), { Authorization: "Bearer " + this.getToken() }) })) | ||
.then(processStatus) | ||
@@ -190,3 +201,3 @@ .then(function (res) { return (json_1 ? parseJSON(res) : res); })]; | ||
if (this.debug) { | ||
if (refreshRetries >= 2) { | ||
if (refreshRetries >= this.maxRefreshRetries) { | ||
console.log("GFWAPI: Attemps to refresh the token excedeed"); | ||
@@ -197,3 +208,3 @@ } | ||
} | ||
if (!(e_1.status === 401 && refreshRetries < 2)) return [3 /*break*/, 9]; | ||
if (!(e_1.status === 401 && refreshRetries < this.maxRefreshRetries)) return [3 /*break*/, 9]; | ||
if (this.debug) { | ||
@@ -212,3 +223,3 @@ console.log("GFWAPI: Trying to refresh the token attempt: " + refreshRetries); | ||
} | ||
return [2 /*return*/, this.fetch(url, options, ++refreshRetries)]; | ||
return [2 /*return*/, this._internalFetch(url, options, ++refreshRetries)]; | ||
case 7: | ||
@@ -239,3 +250,3 @@ e_2 = _e.sent(); | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/me")]; | ||
return [4 /*yield*/, this._internalFetch(this.baseUrl + "/" + AUTH_PATH + "/me", {}, 0, false)]; | ||
case 1: | ||
@@ -372,3 +383,3 @@ user = _a.sent(); | ||
this.setRefreshToken(''); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/logout")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/" + AUTH_PATH + "/logout")]; | ||
case 1: | ||
@@ -396,3 +407,3 @@ _a.sent(); | ||
export default apiClient; | ||
export { GFWAPI, USER_REFRESH_TOKEN_STORAGE_KEY, USER_TOKEN_STORAGE_KEY, getLoginUrl }; | ||
export { USER_REFRESH_TOKEN_STORAGE_KEY, USER_TOKEN_STORAGE_KEY, getLoginUrl }; | ||
//# sourceMappingURL=api-client.es5.js.map |
@@ -70,10 +70,11 @@ (function (global, factory) { | ||
var GATEWAY_URL = process.env.GATEWAY_URL || | ||
process.env.REACT_APP_GATEWAY_URL || | ||
var API_GATEWAY_URL = process.env.API_GATEWAY_URL || | ||
process.env.REACT_APP_API_GATEWAY_URL || | ||
'https://gateway.api.dev.globalfishingwatch.org'; | ||
var USER_TOKEN_STORAGE_KEY = 'GFW_API_USER_TOKEN'; | ||
var USER_REFRESH_TOKEN_STORAGE_KEY = 'GFW_API_USER_REFRESH_TOKEN'; | ||
function getLoginUrl(callback, client) { | ||
var AUTH_PATH = 'auth'; | ||
function getLoginUrl(callbackUrl, client) { | ||
if (client === void 0) { client = 'gfw'; } | ||
return GATEWAY_URL + "/auth?client=" + client + "&callback=" + callback; | ||
return API_GATEWAY_URL + "/" + AUTH_PATH + "?client=" + client + "&callback=" + callbackUrl; | ||
} | ||
@@ -91,11 +92,11 @@ var processStatus = function (response) { | ||
function GFWAPI(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.debug, debug = _c === void 0 ? false : _c, _d = _b.token, token = _d === void 0 ? null : _d, _e = _b.refreshToken, refreshToken = _e === void 0 ? null : _e, _f = _b.baseUrl, baseUrl = _f === void 0 ? GATEWAY_URL : _f, _g = _b.tokenStorageKey, tokenStorageKey = _g === void 0 ? USER_TOKEN_STORAGE_KEY : _g, _h = _b.refreshTokenStorageKey, refreshTokenStorageKey = _h === void 0 ? USER_REFRESH_TOKEN_STORAGE_KEY : _h; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.debug, debug = _c === void 0 ? false : _c, _d = _b.baseUrl, baseUrl = _d === void 0 ? API_GATEWAY_URL : _d, _e = _b.tokenStorageKey, tokenStorageKey = _e === void 0 ? USER_TOKEN_STORAGE_KEY : _e, _f = _b.refreshTokenStorageKey, refreshTokenStorageKey = _f === void 0 ? USER_REFRESH_TOKEN_STORAGE_KEY : _f; | ||
this.token = ''; | ||
this.refreshToken = ''; | ||
this.maxRefreshRetries = 1; | ||
this.debug = debug; | ||
this.baseUrl = baseUrl; | ||
this.storageKeys = { token: tokenStorageKey, refreshToken: refreshTokenStorageKey }; | ||
this.setToken(token || localStorage.getItem(tokenStorageKey) || ''); | ||
this.setRefreshToken(refreshToken || localStorage.getItem(refreshTokenStorageKey) || ''); | ||
this.refreshRetries = 0; | ||
this.setToken(localStorage.getItem(tokenStorageKey) || ''); | ||
this.setRefreshToken(localStorage.getItem(refreshTokenStorageKey) || ''); | ||
this.logging = null; | ||
@@ -115,2 +116,7 @@ if (debug) { | ||
}; | ||
GFWAPI.prototype.setConfig = function (_a) { | ||
var _b = _a.debug, debug = _b === void 0 ? this.debug : _b, _c = _a.baseUrl, baseUrl = _c === void 0 ? this.baseUrl : _c; | ||
this.debug = debug; | ||
this.baseUrl = baseUrl; | ||
}; | ||
GFWAPI.prototype.getToken = function () { | ||
@@ -139,3 +145,3 @@ return this.token; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token?access-token=" + accessToken) | ||
return [2 /*return*/, fetch(this.baseUrl + "/" + AUTH_PATH + "/token?access-token=" + accessToken) | ||
.then(processStatus) | ||
@@ -149,3 +155,3 @@ .then(parseJSON)]; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token/reload", { | ||
return [2 /*return*/, fetch(this.baseUrl + "/" + AUTH_PATH + "/token/reload", { | ||
headers: { | ||
@@ -160,7 +166,12 @@ Authorization: "Bearer " + refreshToken, | ||
}; | ||
GFWAPI.prototype.fetch = function (url, options, refreshRetries) { | ||
GFWAPI.prototype.fetch = function (url, options) { | ||
if (options === void 0) { options = {}; } | ||
return this._internalFetch(url, options); | ||
}; | ||
GFWAPI.prototype._internalFetch = function (url, options, refreshRetries, waitLogin) { | ||
if (options === void 0) { options = {}; } | ||
if (refreshRetries === void 0) { refreshRetries = 0; } | ||
if (waitLogin === void 0) { waitLogin = true; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, method, _b, body, _c, headers, _d, json_1, data, e_1, token, e_2, e_3; | ||
var _a, method, _b, body, _c, headers, _d, json_1, signal, data, e_1, token, e_2, e_3; | ||
return __generator(this, function (_e) { | ||
@@ -170,3 +181,3 @@ switch (_e.label) { | ||
_e.trys.push([0, 12, , 13]); | ||
if (!(this.logging && url !== this.baseUrl + "/auth/me")) return [3 /*break*/, 2]; | ||
if (!(this.logging && waitLogin)) return [3 /*break*/, 2]; | ||
// Don't do any request until the login is completed | ||
@@ -182,7 +193,7 @@ // and don't wait for the login request itselft | ||
_e.trys.push([2, 4, , 11]); | ||
_a = options.method, method = _a === void 0 ? 'GET' : _a, _b = options.body, body = _b === void 0 ? null : _b, _c = options.headers, headers = _c === void 0 ? {} : _c, _d = options.json, json_1 = _d === void 0 ? true : _d; | ||
_a = options.method, method = _a === void 0 ? 'GET' : _a, _b = options.body, body = _b === void 0 ? null : _b, _c = options.headers, headers = _c === void 0 ? {} : _c, _d = options.json, json_1 = _d === void 0 ? true : _d, signal = options.signal; | ||
if (this.debug) { | ||
console.log("GFWAPI: Fetching url: " + url); | ||
} | ||
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (body && body)), { headers: __assign(__assign({}, headers), { Authorization: "Bearer " + this.getToken() }) })) | ||
return [4 /*yield*/, fetch(url, __assign(__assign({ method: method }, (body && body)), { signal: signal, headers: __assign(__assign({}, headers), { Authorization: "Bearer " + this.getToken() }) })) | ||
.then(processStatus) | ||
@@ -196,3 +207,3 @@ .then(function (res) { return (json_1 ? parseJSON(res) : res); })]; | ||
if (this.debug) { | ||
if (refreshRetries >= 2) { | ||
if (refreshRetries >= this.maxRefreshRetries) { | ||
console.log("GFWAPI: Attemps to refresh the token excedeed"); | ||
@@ -203,3 +214,3 @@ } | ||
} | ||
if (!(e_1.status === 401 && refreshRetries < 2)) return [3 /*break*/, 9]; | ||
if (!(e_1.status === 401 && refreshRetries < this.maxRefreshRetries)) return [3 /*break*/, 9]; | ||
if (this.debug) { | ||
@@ -218,3 +229,3 @@ console.log("GFWAPI: Trying to refresh the token attempt: " + refreshRetries); | ||
} | ||
return [2 /*return*/, this.fetch(url, options, ++refreshRetries)]; | ||
return [2 /*return*/, this._internalFetch(url, options, ++refreshRetries)]; | ||
case 7: | ||
@@ -245,3 +256,3 @@ e_2 = _e.sent(); | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/me")]; | ||
return [4 /*yield*/, this._internalFetch(this.baseUrl + "/" + AUTH_PATH + "/me", {}, 0, false)]; | ||
case 1: | ||
@@ -378,3 +389,3 @@ user = _a.sent(); | ||
this.setRefreshToken(''); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/logout")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/" + AUTH_PATH + "/logout")]; | ||
case 1: | ||
@@ -401,3 +412,2 @@ _a.sent(); | ||
exports.GFWAPI = GFWAPI; | ||
exports.USER_REFRESH_TOKEN_STORAGE_KEY = USER_REFRESH_TOKEN_STORAGE_KEY; | ||
@@ -404,0 +414,0 @@ exports.USER_TOKEN_STORAGE_KEY = USER_TOKEN_STORAGE_KEY; |
export declare const USER_TOKEN_STORAGE_KEY = "GFW_API_USER_TOKEN"; | ||
export declare const USER_REFRESH_TOKEN_STORAGE_KEY = "GFW_API_USER_REFRESH_TOKEN"; | ||
export declare function getLoginUrl(callback: string, client?: string): string; | ||
export declare function getLoginUrl(callbackUrl: string, client?: string): string; | ||
interface UserPermission { | ||
@@ -18,2 +18,6 @@ type: 'application' | 'dataset'; | ||
} | ||
interface LibOptions { | ||
debug: boolean; | ||
baseUrl: string; | ||
} | ||
interface FetchOptions { | ||
@@ -23,5 +27,6 @@ method?: 'GET' | 'POST' | 'PUT' | 'PATCH'; | ||
headers?: any; | ||
signal?: any; | ||
json?: boolean; | ||
} | ||
export declare class GFWAPI { | ||
declare class GFWAPI { | ||
debug: boolean; | ||
@@ -35,8 +40,6 @@ token: string; | ||
}; | ||
refreshRetries: number; | ||
maxRefreshRetries: number; | ||
logging: Promise<any> | null; | ||
constructor({ debug, token, refreshToken, baseUrl, tokenStorageKey, refreshTokenStorageKey, }?: { | ||
constructor({ debug, baseUrl, tokenStorageKey, refreshTokenStorageKey, }?: { | ||
debug?: boolean | undefined; | ||
token?: null | undefined; | ||
refreshToken?: null | undefined; | ||
baseUrl?: string | undefined; | ||
@@ -56,2 +59,3 @@ tokenStorageKey?: string | undefined; | ||
}; | ||
setConfig({ debug, baseUrl }: LibOptions): void; | ||
getToken(): string; | ||
@@ -63,3 +67,4 @@ private setToken; | ||
private getTokenWithRefreshToken; | ||
fetch<T>(url: string, options?: FetchOptions, refreshRetries?: number): Promise<T>; | ||
fetch<T>(url: string, options?: FetchOptions): Promise<T>; | ||
private _internalFetch; | ||
fetchUser(): Promise<UserData>; | ||
@@ -66,0 +71,0 @@ login({ accessToken, refreshToken }: { |
{ | ||
"name": "@globalfishingwatch/api-client", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "js library to help with the login/logout and resources fetch of the GFW API", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
114567
1215