-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
158 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,5 @@ | ||
import { QuidParams, QuidLoginParams } from "./types"; | ||
import QuidRequests from "./model"; | ||
import QuidError from "./errors"; | ||
import { QuidParams, QuidLoginParams } from "./types"; | ||
|
||
export default class QuidRequests { | ||
public refreshToken: string | null = null; | ||
private accessToken: string | null = null; | ||
private quidUri: string; | ||
private serverUri: string; | ||
private namespace: string; | ||
private timeouts: Record<string, string>; | ||
private verbose: boolean; | ||
private headers: HeadersInit; | ||
private credentials: string | null; | ||
|
||
public constructor({ quidUri, serverUri, namespace, timeouts = { | ||
accessToken: "20m", | ||
refreshToken: "24h" | ||
}, credentials = "include", verbose = false }: QuidParams) { | ||
this.quidUri = quidUri; | ||
this.serverUri = serverUri; | ||
this.namespace = namespace; | ||
this.timeouts = timeouts; | ||
this.credentials = credentials; | ||
this.verbose = verbose; | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
} as HeadersInit; | ||
if (verbose) { | ||
console.log("Initializing QuidRequests", this.quidUri); | ||
} | ||
console.log("INIT CREDS", this.credentials) | ||
} | ||
|
||
async get<T>(url: string): Promise<T> { | ||
return this._request<T>(url, "get"); | ||
} | ||
|
||
async post<T>(url: string): Promise<T> { | ||
return this._request<T>(url, "post"); | ||
} | ||
|
||
async getRefreshToken({ username, password, refreshTokenTtl = "24h" }: QuidLoginParams) { | ||
const uri = this.quidUri + "/token/refresh/" + refreshTokenTtl; | ||
const payload = { | ||
namespace: this.namespace, | ||
username: username, | ||
password: password, | ||
} | ||
try { | ||
const opts: RequestInit = { | ||
method: 'POST', | ||
headers: this.headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
const response = await fetch(uri, opts); | ||
if (!response.ok) { | ||
console.log("RESP NOT OK", response); | ||
throw new Error(response.statusText) | ||
} | ||
const t = await response.json(); | ||
this.refreshToken = t.token; | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
async checkTokens(): Promise<void> { | ||
if (this.refreshToken === null) { | ||
if (this.verbose) { | ||
console.log("Tokens check: no refresh token") | ||
} | ||
throw new QuidError('No refresh token found', true); | ||
} | ||
if (this.accessToken === null) { | ||
if (this.verbose) { | ||
console.log("Tokens check: no access token") | ||
} | ||
const status = await this._getAccessToken(); | ||
if (status === 401) { | ||
if (this.verbose) { | ||
console.log("The refresh token has expired") | ||
} | ||
throw new QuidError('The refresh token has expired'); | ||
} | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': "Bearer " + this.accessToken | ||
} as HeadersInit; | ||
} | ||
} | ||
|
||
private async _request<T>(url: string, method: string, payload: Record<string, any> | Array<any> | null = null, retry = 0): Promise<T> { // eslint-disable-line | ||
if (this.verbose) { | ||
console.log(method + " request to " + url) | ||
} | ||
const status = await this.checkTokens(); | ||
console.log("STATUS", status, this.accessToken); | ||
let opts: RequestInit; | ||
if (method === "post") { | ||
//console.log("GET", this.#accessToken, uri); | ||
opts = { | ||
method: 'POST', | ||
headers: this.headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
if (this.credentials !== null) { | ||
opts.credentials = this.credentials as RequestCredentials; | ||
} | ||
} else { | ||
opts = { | ||
method: 'GET', | ||
headers: this.headers, | ||
}; | ||
if (this.credentials !== null) { | ||
opts.credentials = this.credentials as RequestCredentials; | ||
} | ||
} | ||
//console.log("FETCH", this.serverUri + url); | ||
//console.log(JSON.stringify(opts, null, " ")) | ||
const response = await fetch(this.serverUri + url, opts); | ||
if (!response.ok) { | ||
console.log("RESP NOT OK", response); | ||
throw new Error(response.statusText) | ||
} | ||
return await response.json() as T; | ||
} | ||
|
||
private async _getAccessToken(): Promise<number> { | ||
const payload = { | ||
namespace: this.namespace, | ||
refresh_token: this.refreshToken, // eslint-disable-line | ||
} | ||
const url = this.quidUri + "/token/access/" + this.timeouts.accessToken; | ||
if (this.verbose) { | ||
console.log("Getting an access token from", url, payload) | ||
} | ||
const headers: HeadersInit = { | ||
'Content-Type': 'application/json', | ||
} | ||
const opts: RequestInit = { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
const response = await fetch(url, opts); | ||
if (!response.ok) { | ||
return response.status; | ||
} | ||
const data = await response.json(); | ||
this.accessToken = data.token; | ||
return response.status; | ||
} | ||
} | ||
export { QuidError, QuidRequests, QuidParams, QuidLoginParams } |
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,154 @@ | ||
import { QuidParams, QuidLoginParams } from "./types"; | ||
import QuidError from "./errors"; | ||
|
||
export default class QuidRequests { | ||
public refreshToken: string | null = null; | ||
private accessToken: string | null = null; | ||
private quidUri: string; | ||
private serverUri: string; | ||
private namespace: string; | ||
private timeouts: Record<string, string>; | ||
private verbose: boolean; | ||
private headers: HeadersInit; | ||
private credentials: string | null; | ||
|
||
public constructor({ quidUri, serverUri, namespace, timeouts = { | ||
accessToken: "20m", | ||
refreshToken: "24h" | ||
}, credentials = "include", verbose = false }: QuidParams) { | ||
this.quidUri = quidUri; | ||
this.serverUri = serverUri; | ||
this.namespace = namespace; | ||
this.timeouts = timeouts; | ||
this.credentials = credentials; | ||
this.verbose = verbose; | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
} as HeadersInit; | ||
if (verbose) { | ||
console.log("Initializing QuidRequests", this.quidUri); | ||
} | ||
console.log("INIT CREDS", this.credentials) | ||
} | ||
|
||
async get<T>(url: string): Promise<T> { | ||
return this._request<T>(url, "get"); | ||
} | ||
|
||
async post<T>(url: string): Promise<T> { | ||
return this._request<T>(url, "post"); | ||
} | ||
|
||
async getRefreshToken({ username, password, refreshTokenTtl = "24h" }: QuidLoginParams) { | ||
const uri = this.quidUri + "/token/refresh/" + refreshTokenTtl; | ||
const payload = { | ||
namespace: this.namespace, | ||
username: username, | ||
password: password, | ||
} | ||
try { | ||
const opts: RequestInit = { | ||
method: 'POST', | ||
headers: this.headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
const response = await fetch(uri, opts); | ||
if (!response.ok) { | ||
console.log("RESP NOT OK", response); | ||
throw new Error(response.statusText) | ||
} | ||
const t = await response.json(); | ||
this.refreshToken = t.token; | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
async checkTokens(): Promise<void> { | ||
if (this.refreshToken === null) { | ||
if (this.verbose) { | ||
console.log("Tokens check: no refresh token") | ||
} | ||
throw new QuidError('No refresh token found', true); | ||
} | ||
if (this.accessToken === null) { | ||
if (this.verbose) { | ||
console.log("Tokens check: no access token") | ||
} | ||
const status = await this._getAccessToken(); | ||
if (status === 401) { | ||
if (this.verbose) { | ||
console.log("The refresh token has expired") | ||
} | ||
throw new QuidError('The refresh token has expired'); | ||
} | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': "Bearer " + this.accessToken | ||
} as HeadersInit; | ||
} | ||
} | ||
|
||
private async _request<T>(url: string, method: string, payload: Record<string, any> | Array<any> | null = null, retry = 0): Promise<T> { // eslint-disable-line | ||
if (this.verbose) { | ||
console.log(method + " request to " + url) | ||
} | ||
const status = await this.checkTokens(); | ||
console.log("STATUS", status, this.accessToken); | ||
let opts: RequestInit; | ||
if (method === "post") { | ||
//console.log("GET", this.#accessToken, uri); | ||
opts = { | ||
method: 'POST', | ||
headers: this.headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
if (this.credentials !== null) { | ||
opts.credentials = this.credentials as RequestCredentials; | ||
} | ||
} else { | ||
opts = { | ||
method: 'GET', | ||
headers: this.headers, | ||
}; | ||
if (this.credentials !== null) { | ||
opts.credentials = this.credentials as RequestCredentials; | ||
} | ||
} | ||
//console.log("FETCH", this.serverUri + url); | ||
//console.log(JSON.stringify(opts, null, " ")) | ||
const response = await fetch(this.serverUri + url, opts); | ||
if (!response.ok) { | ||
console.log("RESP NOT OK", response); | ||
throw new Error(response.statusText) | ||
} | ||
return await response.json() as T; | ||
} | ||
|
||
private async _getAccessToken(): Promise<number> { | ||
const payload = { | ||
namespace: this.namespace, | ||
refresh_token: this.refreshToken, // eslint-disable-line | ||
} | ||
const url = this.quidUri + "/token/access/" + this.timeouts.accessToken; | ||
if (this.verbose) { | ||
console.log("Getting an access token from", url, payload) | ||
} | ||
const headers: HeadersInit = { | ||
'Content-Type': 'application/json', | ||
} | ||
const opts: RequestInit = { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(payload), | ||
}; | ||
const response = await fetch(url, opts); | ||
if (!response.ok) { | ||
return response.status; | ||
} | ||
const data = await response.json(); | ||
this.accessToken = data.token; | ||
return response.status; | ||
} | ||
} |