@globalfishingwatch/api-client
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -64,7 +64,11 @@ /*! ***************************************************************************** | ||
var GATEWAY_URL = process.env.API_AUTH_URL || | ||
process.env.REACT_APP_API_AUTH_URL || | ||
'https://gateway.api.dev.globalfishingwatch.org/auth'; | ||
var GATEWAY_URL = process.env.GATEWAY_URL || | ||
process.env.REACT_APP_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) { | ||
if (client === void 0) { client = 'gfw'; } | ||
return GATEWAY_URL + "/auth?client=" + client + "&callback=" + callback; | ||
} | ||
var processStatus = function (response) { | ||
@@ -127,3 +131,3 @@ return response.status >= 200 && response.status < 300 | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/token?access-token=" + accessToken) | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token?access-token=" + accessToken) | ||
.then(processStatus) | ||
@@ -137,3 +141,3 @@ .then(parseJSON)]; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/token/reload", { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token/reload", { | ||
headers: { | ||
@@ -157,3 +161,3 @@ Authorization: "Bearer " + refreshToken, | ||
_e.trys.push([0, 12, , 13]); | ||
if (!(this.logging && url !== this.baseUrl + "/me")) return [3 /*break*/, 2]; | ||
if (!(this.logging && url !== this.baseUrl + "/auth/me")) return [3 /*break*/, 2]; | ||
// Don't do any request until the login is completed | ||
@@ -228,3 +232,3 @@ // and don't wait for the login request itselft | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/me")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/me")]; | ||
case 1: | ||
@@ -361,3 +365,3 @@ user = _a.sent(); | ||
this.setRefreshToken(''); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/logout")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/logout")]; | ||
case 1: | ||
@@ -385,3 +389,3 @@ _a.sent(); | ||
export default apiClient; | ||
export { GFWAPI, USER_REFRESH_TOKEN_STORAGE_KEY, USER_TOKEN_STORAGE_KEY }; | ||
export { GFWAPI, USER_REFRESH_TOKEN_STORAGE_KEY, USER_TOKEN_STORAGE_KEY, getLoginUrl }; | ||
//# sourceMappingURL=api-client.es5.js.map |
@@ -70,7 +70,11 @@ (function (global, factory) { | ||
var GATEWAY_URL = process.env.API_AUTH_URL || | ||
process.env.REACT_APP_API_AUTH_URL || | ||
'https://gateway.api.dev.globalfishingwatch.org/auth'; | ||
var GATEWAY_URL = process.env.GATEWAY_URL || | ||
process.env.REACT_APP_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) { | ||
if (client === void 0) { client = 'gfw'; } | ||
return GATEWAY_URL + "/auth?client=" + client + "&callback=" + callback; | ||
} | ||
var processStatus = function (response) { | ||
@@ -133,3 +137,3 @@ return response.status >= 200 && response.status < 300 | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/token?access-token=" + accessToken) | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token?access-token=" + accessToken) | ||
.then(processStatus) | ||
@@ -143,3 +147,3 @@ .then(parseJSON)]; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, fetch(this.baseUrl + "/token/reload", { | ||
return [2 /*return*/, fetch(this.baseUrl + "/auth/token/reload", { | ||
headers: { | ||
@@ -163,3 +167,3 @@ Authorization: "Bearer " + refreshToken, | ||
_e.trys.push([0, 12, , 13]); | ||
if (!(this.logging && url !== this.baseUrl + "/me")) return [3 /*break*/, 2]; | ||
if (!(this.logging && url !== this.baseUrl + "/auth/me")) return [3 /*break*/, 2]; | ||
// Don't do any request until the login is completed | ||
@@ -234,3 +238,3 @@ // and don't wait for the login request itselft | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/me")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/me")]; | ||
case 1: | ||
@@ -367,3 +371,3 @@ user = _a.sent(); | ||
this.setRefreshToken(''); | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/logout")]; | ||
return [4 /*yield*/, this.fetch(this.baseUrl + "/auth/logout")]; | ||
case 1: | ||
@@ -394,2 +398,3 @@ _a.sent(); | ||
exports.default = apiClient; | ||
exports.getLoginUrl = getLoginUrl; | ||
@@ -396,0 +401,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
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; | ||
interface UserPermission { | ||
@@ -4,0 +5,0 @@ type: 'application' | 'dataset'; |
{ | ||
"name": "@globalfishingwatch/api-client", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "js library to help with the login/logout and resources fetch of the GFW API", | ||
@@ -29,3 +29,5 @@ "keywords": [], | ||
"build": "cross-env NODE_ENV=production tsc --module commonjs && rollup -c rollup.config.ts", | ||
"typedoc": "typedoc --out docs --target es6 --theme minimal --mode file src" | ||
"typedoc": "typedoc --out docs --target es6 --theme minimal --mode file src", | ||
"gh-pages": "gh-pages -d docs", | ||
"publishdoc": "npm run typedoc && npm run gh-pages" | ||
}, | ||
@@ -75,2 +77,3 @@ "husky": { | ||
"eslint-plugin-react-hooks": "^2.3.0", | ||
"gh-pages": "^2.2.0", | ||
"husky": "^4.0.7", | ||
@@ -77,0 +80,0 @@ "jest": "^24.9.0", |
@@ -7,6 +7,4 @@ # GFW API CLIENT | ||
## Usage | ||
## Install | ||
### Install | ||
yarn | ||
@@ -22,3 +20,3 @@ ```bash | ||
### Initialize | ||
## Initialize | ||
@@ -28,27 +26,34 @@ There are two options to initialize the library: | ||
1. The simplest way, just include the basic config in the `.env` when no extra configuration is needed. | ||
`API_AUTH_URL=https://gateway.api.dev.globalfishingwatch.org` | ||
`API_GATEWAY_URL | REACT_APP_API_GATEWAY_URL=https://gateway.api.dev.globalfishingwatch.org` | ||
```js | ||
import GFWAPI from 'gfw-api-client' | ||
// use where needed | ||
``` | ||
```js | ||
import GFW_API from '@globalfishingwatch/api-client' | ||
// use where needed | ||
``` | ||
The library will use by default the tokens stored in the localStorage to keep it as simple as possible, if you need to use your own config use the option 2. | ||
The library will use by default the tokens stored in the localStorage to keep it as simple as possible, if you need to use your own config use the option 2. | ||
2. Create your own class instance using the configuration params, when extra configuration is needed. | ||
```js | ||
Example: | ||
import { GFW_API } from 'gfw-api-client' | ||
const GFWAPI = new GFW_API({ | ||
baseUrl: 'https://gateway.api...', | ||
token: 'your_token', | ||
tokenStorageKey: 'MY_KEY...' | ||
debug: true|false | ||
}) | ||
``` | ||
```js | ||
Example: | ||
import { GFW_API } from '@globalfishingwatch/api-client' | ||
const GFWAPI = new GFW_API({ | ||
baseUrl: 'https://gateway.api...', | ||
token: 'your_token', | ||
tokenStorageKey: 'MY_KEY...' | ||
debug: true|false | ||
}) | ||
``` | ||
### Usage | ||
## Login | ||
#### Login | ||
The url to the SSO is exposed used the `getLoginUrl` helper, which needs the following params: | ||
- callback url | ||
- client (optional, using gfw by default) | ||
```js | ||
import { getLoginUrl } from '@globalfishingwatch/api-client' | ||
const url = getLoginUrl('your_callback_url_here', 'client_optional') | ||
``` | ||
1. On the very first usage time the library needs the `access-token` to generate the session `token` and the `refreshToken` but it won't be useful anymore. | ||
@@ -86,3 +91,3 @@ | ||
#### Fetch resources | ||
### Fetch resources | ||
@@ -100,5 +105,4 @@ 1. Once the initialization and the login were good to you will be able to consume Global Fishing Watch endpoints data using: | ||
### Logout | ||
#### Logout | ||
1. To remove invalidate the current session and removed stored `token` and `refreshToken` just use: | ||
@@ -115,3 +119,3 @@ | ||
### FAQ | ||
## FAQ | ||
@@ -146,1 +150,35 @@ <details> | ||
</details> | ||
## Development | ||
1. Install dependencies | ||
```bash | ||
yarn install | ||
``` | ||
2. Start the project with watcher to recompile | ||
```bash | ||
yarn start | ||
``` | ||
3. Run the tests | ||
```bash | ||
yarn test | ||
``` | ||
4. Build it! | ||
```bash | ||
yarn build | ||
``` | ||
5. Prepare the release! | ||
```bash | ||
npx release major|minor|patch | ||
``` | ||
6. Publish the new version and update the documentation | ||
```bash | ||
npm publish | ||
npm publishdoc | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
111600
1189
178
34