Skip to content

Commit

Permalink
Fix exports
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcig committed Apr 17, 2021
1 parent 587ec0a commit a7ef4c7
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 153 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quidjs",
"version": "0.4.1",
"version": "0.4.2",
"description": "Requests library for the Quid json web tokens server",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
155 changes: 3 additions & 152 deletions src/index.ts
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 }
154 changes: 154 additions & 0 deletions src/model.ts
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;
}
}

0 comments on commit a7ef4c7

Please sign in to comment.