-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const API_GATEWAY = | ||
process.env.API_GATEWAY || | ||
process.env.REACT_APP_API_GATEWAY || | ||
process.env.NEXT_PUBLIC_API_GATEWAY || | ||
'https://gateway.api.dev.globalfishingwatch.org' | ||
|
||
export const USER_TOKEN_STORAGE_KEY = 'GFW_API_USER_TOKEN' | ||
export const USER_REFRESH_TOKEN_STORAGE_KEY = 'GFW_API_USER_REFRESH_TOKEN' | ||
export const API_VERSION = process.env.NEXT_PUBLIC_API_VERSION || 'v3' | ||
|
||
export const DEBUG_API_REQUESTS: boolean = process.env.NEXT_PUBLIC_DEBUG_API_REQUESTS === 'true' | ||
export const AUTH_PATH = 'auth' | ||
export const REGISTER_PATH = 'registration' | ||
export const GUEST_USER_TYPE = 'guest' | ||
export const CONCURRENT_ERROR_STATUS = 429 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ResourceResponseType } from '@globalfishingwatch/api-types' | ||
|
||
export const processStatus = ( | ||
response: Response, | ||
requestStatus?: ResourceResponseType | ||
): Promise<Response> => { | ||
return new Promise(async (resolve, reject) => { | ||
const { status, statusText } = response | ||
try { | ||
if (response.status >= 200 && response.status < 400) { | ||
return resolve(response) | ||
} | ||
|
||
if (requestStatus === 'default') { | ||
return reject(response) | ||
} | ||
// Compatibility with v1 and v2 errors format | ||
const errors = { | ||
message: '', | ||
messages: [], | ||
} | ||
if (response.status >= 400 && response.status < 500) { | ||
await response.text().then((text) => { | ||
try { | ||
const res = JSON.parse(text) | ||
errors.message = res.message | ||
errors.messages = res.messages | ||
} catch (e: any) { | ||
errors.message = statusText | ||
} | ||
}) | ||
} | ||
return reject({ | ||
status, | ||
message: errors?.message || statusText, | ||
messages: errors.messages, | ||
}) | ||
} catch (e: any) { | ||
return reject({ status, message: statusText }) | ||
} | ||
}) | ||
} | ||
|
||
export const parseJSON = (response: Response) => response.json() |