Skip to content

Commit a7ef4c7

Browse files
committed
Fix exports
1 parent 587ec0a commit a7ef4c7

File tree

3 files changed

+158
-153
lines changed

3 files changed

+158
-153
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quidjs",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "Requests library for the Quid json web tokens server",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/index.ts

Lines changed: 3 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,5 @@
1-
import { QuidParams, QuidLoginParams } from "./types";
1+
import QuidRequests from "./model";
22
import QuidError from "./errors";
3+
import { QuidParams, QuidLoginParams } from "./types";
34

4-
export default class QuidRequests {
5-
public refreshToken: string | null = null;
6-
private accessToken: string | null = null;
7-
private quidUri: string;
8-
private serverUri: string;
9-
private namespace: string;
10-
private timeouts: Record<string, string>;
11-
private verbose: boolean;
12-
private headers: HeadersInit;
13-
private credentials: string | null;
14-
15-
public constructor({ quidUri, serverUri, namespace, timeouts = {
16-
accessToken: "20m",
17-
refreshToken: "24h"
18-
}, credentials = "include", verbose = false }: QuidParams) {
19-
this.quidUri = quidUri;
20-
this.serverUri = serverUri;
21-
this.namespace = namespace;
22-
this.timeouts = timeouts;
23-
this.credentials = credentials;
24-
this.verbose = verbose;
25-
this.headers = {
26-
'Content-Type': 'application/json',
27-
} as HeadersInit;
28-
if (verbose) {
29-
console.log("Initializing QuidRequests", this.quidUri);
30-
}
31-
console.log("INIT CREDS", this.credentials)
32-
}
33-
34-
async get<T>(url: string): Promise<T> {
35-
return this._request<T>(url, "get");
36-
}
37-
38-
async post<T>(url: string): Promise<T> {
39-
return this._request<T>(url, "post");
40-
}
41-
42-
async getRefreshToken({ username, password, refreshTokenTtl = "24h" }: QuidLoginParams) {
43-
const uri = this.quidUri + "/token/refresh/" + refreshTokenTtl;
44-
const payload = {
45-
namespace: this.namespace,
46-
username: username,
47-
password: password,
48-
}
49-
try {
50-
const opts: RequestInit = {
51-
method: 'POST',
52-
headers: this.headers,
53-
body: JSON.stringify(payload),
54-
};
55-
const response = await fetch(uri, opts);
56-
if (!response.ok) {
57-
console.log("RESP NOT OK", response);
58-
throw new Error(response.statusText)
59-
}
60-
const t = await response.json();
61-
this.refreshToken = t.token;
62-
} catch (e) {
63-
throw new Error(e);
64-
}
65-
}
66-
67-
async checkTokens(): Promise<void> {
68-
if (this.refreshToken === null) {
69-
if (this.verbose) {
70-
console.log("Tokens check: no refresh token")
71-
}
72-
throw new QuidError('No refresh token found', true);
73-
}
74-
if (this.accessToken === null) {
75-
if (this.verbose) {
76-
console.log("Tokens check: no access token")
77-
}
78-
const status = await this._getAccessToken();
79-
if (status === 401) {
80-
if (this.verbose) {
81-
console.log("The refresh token has expired")
82-
}
83-
throw new QuidError('The refresh token has expired');
84-
}
85-
this.headers = {
86-
'Content-Type': 'application/json',
87-
'Accept': 'application/json',
88-
'Authorization': "Bearer " + this.accessToken
89-
} as HeadersInit;
90-
}
91-
}
92-
93-
private async _request<T>(url: string, method: string, payload: Record<string, any> | Array<any> | null = null, retry = 0): Promise<T> { // eslint-disable-line
94-
if (this.verbose) {
95-
console.log(method + " request to " + url)
96-
}
97-
const status = await this.checkTokens();
98-
console.log("STATUS", status, this.accessToken);
99-
let opts: RequestInit;
100-
if (method === "post") {
101-
//console.log("GET", this.#accessToken, uri);
102-
opts = {
103-
method: 'POST',
104-
headers: this.headers,
105-
body: JSON.stringify(payload),
106-
};
107-
if (this.credentials !== null) {
108-
opts.credentials = this.credentials as RequestCredentials;
109-
}
110-
} else {
111-
opts = {
112-
method: 'GET',
113-
headers: this.headers,
114-
};
115-
if (this.credentials !== null) {
116-
opts.credentials = this.credentials as RequestCredentials;
117-
}
118-
}
119-
//console.log("FETCH", this.serverUri + url);
120-
//console.log(JSON.stringify(opts, null, " "))
121-
const response = await fetch(this.serverUri + url, opts);
122-
if (!response.ok) {
123-
console.log("RESP NOT OK", response);
124-
throw new Error(response.statusText)
125-
}
126-
return await response.json() as T;
127-
}
128-
129-
private async _getAccessToken(): Promise<number> {
130-
const payload = {
131-
namespace: this.namespace,
132-
refresh_token: this.refreshToken, // eslint-disable-line
133-
}
134-
const url = this.quidUri + "/token/access/" + this.timeouts.accessToken;
135-
if (this.verbose) {
136-
console.log("Getting an access token from", url, payload)
137-
}
138-
const headers: HeadersInit = {
139-
'Content-Type': 'application/json',
140-
}
141-
const opts: RequestInit = {
142-
method: 'POST',
143-
headers,
144-
body: JSON.stringify(payload),
145-
};
146-
const response = await fetch(url, opts);
147-
if (!response.ok) {
148-
return response.status;
149-
}
150-
const data = await response.json();
151-
this.accessToken = data.token;
152-
return response.status;
153-
}
154-
}
5+
export { QuidError, QuidRequests, QuidParams, QuidLoginParams }

src/model.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { QuidParams, QuidLoginParams } from "./types";
2+
import QuidError from "./errors";
3+
4+
export default class QuidRequests {
5+
public refreshToken: string | null = null;
6+
private accessToken: string | null = null;
7+
private quidUri: string;
8+
private serverUri: string;
9+
private namespace: string;
10+
private timeouts: Record<string, string>;
11+
private verbose: boolean;
12+
private headers: HeadersInit;
13+
private credentials: string | null;
14+
15+
public constructor({ quidUri, serverUri, namespace, timeouts = {
16+
accessToken: "20m",
17+
refreshToken: "24h"
18+
}, credentials = "include", verbose = false }: QuidParams) {
19+
this.quidUri = quidUri;
20+
this.serverUri = serverUri;
21+
this.namespace = namespace;
22+
this.timeouts = timeouts;
23+
this.credentials = credentials;
24+
this.verbose = verbose;
25+
this.headers = {
26+
'Content-Type': 'application/json',
27+
} as HeadersInit;
28+
if (verbose) {
29+
console.log("Initializing QuidRequests", this.quidUri);
30+
}
31+
console.log("INIT CREDS", this.credentials)
32+
}
33+
34+
async get<T>(url: string): Promise<T> {
35+
return this._request<T>(url, "get");
36+
}
37+
38+
async post<T>(url: string): Promise<T> {
39+
return this._request<T>(url, "post");
40+
}
41+
42+
async getRefreshToken({ username, password, refreshTokenTtl = "24h" }: QuidLoginParams) {
43+
const uri = this.quidUri + "/token/refresh/" + refreshTokenTtl;
44+
const payload = {
45+
namespace: this.namespace,
46+
username: username,
47+
password: password,
48+
}
49+
try {
50+
const opts: RequestInit = {
51+
method: 'POST',
52+
headers: this.headers,
53+
body: JSON.stringify(payload),
54+
};
55+
const response = await fetch(uri, opts);
56+
if (!response.ok) {
57+
console.log("RESP NOT OK", response);
58+
throw new Error(response.statusText)
59+
}
60+
const t = await response.json();
61+
this.refreshToken = t.token;
62+
} catch (e) {
63+
throw new Error(e);
64+
}
65+
}
66+
67+
async checkTokens(): Promise<void> {
68+
if (this.refreshToken === null) {
69+
if (this.verbose) {
70+
console.log("Tokens check: no refresh token")
71+
}
72+
throw new QuidError('No refresh token found', true);
73+
}
74+
if (this.accessToken === null) {
75+
if (this.verbose) {
76+
console.log("Tokens check: no access token")
77+
}
78+
const status = await this._getAccessToken();
79+
if (status === 401) {
80+
if (this.verbose) {
81+
console.log("The refresh token has expired")
82+
}
83+
throw new QuidError('The refresh token has expired');
84+
}
85+
this.headers = {
86+
'Content-Type': 'application/json',
87+
'Accept': 'application/json',
88+
'Authorization': "Bearer " + this.accessToken
89+
} as HeadersInit;
90+
}
91+
}
92+
93+
private async _request<T>(url: string, method: string, payload: Record<string, any> | Array<any> | null = null, retry = 0): Promise<T> { // eslint-disable-line
94+
if (this.verbose) {
95+
console.log(method + " request to " + url)
96+
}
97+
const status = await this.checkTokens();
98+
console.log("STATUS", status, this.accessToken);
99+
let opts: RequestInit;
100+
if (method === "post") {
101+
//console.log("GET", this.#accessToken, uri);
102+
opts = {
103+
method: 'POST',
104+
headers: this.headers,
105+
body: JSON.stringify(payload),
106+
};
107+
if (this.credentials !== null) {
108+
opts.credentials = this.credentials as RequestCredentials;
109+
}
110+
} else {
111+
opts = {
112+
method: 'GET',
113+
headers: this.headers,
114+
};
115+
if (this.credentials !== null) {
116+
opts.credentials = this.credentials as RequestCredentials;
117+
}
118+
}
119+
//console.log("FETCH", this.serverUri + url);
120+
//console.log(JSON.stringify(opts, null, " "))
121+
const response = await fetch(this.serverUri + url, opts);
122+
if (!response.ok) {
123+
console.log("RESP NOT OK", response);
124+
throw new Error(response.statusText)
125+
}
126+
return await response.json() as T;
127+
}
128+
129+
private async _getAccessToken(): Promise<number> {
130+
const payload = {
131+
namespace: this.namespace,
132+
refresh_token: this.refreshToken, // eslint-disable-line
133+
}
134+
const url = this.quidUri + "/token/access/" + this.timeouts.accessToken;
135+
if (this.verbose) {
136+
console.log("Getting an access token from", url, payload)
137+
}
138+
const headers: HeadersInit = {
139+
'Content-Type': 'application/json',
140+
}
141+
const opts: RequestInit = {
142+
method: 'POST',
143+
headers,
144+
body: JSON.stringify(payload),
145+
};
146+
const response = await fetch(url, opts);
147+
if (!response.ok) {
148+
return response.status;
149+
}
150+
const data = await response.json();
151+
this.accessToken = data.token;
152+
return response.status;
153+
}
154+
}

0 commit comments

Comments
 (0)