@openapi-qraft/react 1.9.0 → 1.10.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +14 -57
- package/dist/Unstable_QraftSecureRequestFn.d.ts +81 -0
- package/dist/Unstable_QraftSecureRequestFn.d.ts.map +1 -0
- package/dist/Unstable_QraftSecureRequestFn.js +187 -0
- package/dist/Unstable_QraftSecureRequestFn.js.map +1 -0
- package/dist/lib/jwt-decode/index.d.ts +29 -0
- package/dist/lib/jwt-decode/index.d.ts.map +1 -0
- package/dist/lib/jwt-decode/index.js +61 -0
- package/dist/lib/jwt-decode/index.js.map +1 -0
- package/dist/lib/jwt-decode/index.test.d.ts +2 -0
- package/dist/lib/jwt-decode/index.test.d.ts.map +1 -0
- package/dist/lib/jwt-decode/index.test.js +99 -0
- package/dist/lib/jwt-decode/index.test.js.map +1 -0
- package/dist/lib/requestFn.d.ts +3 -1
- package/dist/lib/requestFn.d.ts.map +1 -1
- package/dist/lib/requestFn.js +1 -1
- package/dist/lib/requestFn.js.map +1 -1
- package/package.json +3 -3
- package/src/Unstable_QraftSecureRequestFn.ts +365 -0
- package/src/lib/jwt-decode/index.test.ts +131 -0
- package/src/lib/jwt-decode/index.ts +105 -0
- package/src/lib/requestFn.ts +4 -2
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export interface JwtDecodeOptions {
|
|
2
|
+
header?: boolean;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface JwtHeader {
|
|
6
|
+
typ?: string;
|
|
7
|
+
alg?: string;
|
|
8
|
+
kid?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface JwtPayload {
|
|
12
|
+
iss?: string;
|
|
13
|
+
sub?: string;
|
|
14
|
+
aud?: string[] | string;
|
|
15
|
+
exp?: number;
|
|
16
|
+
nbf?: number;
|
|
17
|
+
iat?: number;
|
|
18
|
+
jti?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class InvalidTokenError extends Error {}
|
|
22
|
+
|
|
23
|
+
InvalidTokenError.prototype.name = 'InvalidTokenError';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Decode a base64 string to a UTF-8 string.
|
|
27
|
+
* Could be used in Node.js or in the browser.
|
|
28
|
+
*/
|
|
29
|
+
export function b64DecodeUnicode(data: string): string {
|
|
30
|
+
if (typeof atob === 'function')
|
|
31
|
+
return decodeURIComponent(Array.from(atob(data), byteToPercent).join(''));
|
|
32
|
+
|
|
33
|
+
return Buffer.from(data, 'base64').toString('utf-8');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function base64UrlDecode(str: string) {
|
|
37
|
+
let output = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
38
|
+
switch (output.length % 4) {
|
|
39
|
+
case 0:
|
|
40
|
+
break;
|
|
41
|
+
case 2:
|
|
42
|
+
output += '==';
|
|
43
|
+
break;
|
|
44
|
+
case 3:
|
|
45
|
+
output += '=';
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
throw new Error('base64 string is not of the correct length');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
return b64DecodeUnicode(output);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
return atob(output);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function jwtDecode<T = JwtHeader>(
|
|
59
|
+
token: string,
|
|
60
|
+
options: JwtDecodeOptions & { header: true }
|
|
61
|
+
): T;
|
|
62
|
+
export function jwtDecode<T = JwtPayload>(
|
|
63
|
+
token: string,
|
|
64
|
+
options?: JwtDecodeOptions
|
|
65
|
+
): T;
|
|
66
|
+
export function jwtDecode<T = JwtHeader | JwtPayload>(
|
|
67
|
+
token: string,
|
|
68
|
+
options?: JwtDecodeOptions
|
|
69
|
+
): T {
|
|
70
|
+
if (typeof token !== 'string') {
|
|
71
|
+
throw new InvalidTokenError('Invalid token specified: must be a string');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
options ||= {};
|
|
75
|
+
|
|
76
|
+
const pos = options.header === true ? 0 : 1;
|
|
77
|
+
const part = token.split('.')[pos];
|
|
78
|
+
|
|
79
|
+
if (typeof part !== 'string') {
|
|
80
|
+
throw new InvalidTokenError(
|
|
81
|
+
`Invalid token specified: missing part #${pos + 1}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let decoded: string;
|
|
86
|
+
try {
|
|
87
|
+
decoded = base64UrlDecode(part);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
throw new InvalidTokenError(
|
|
90
|
+
`Invalid token specified: invalid base64 for part #${pos + 1} (${(e as Error).message})`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(decoded) as T;
|
|
96
|
+
} catch (e) {
|
|
97
|
+
throw new InvalidTokenError(
|
|
98
|
+
`Invalid token specified: invalid json for part #${pos + 1} (${(e as Error).message})`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function byteToPercent(b: string) {
|
|
104
|
+
return `%${`00${b.charCodeAt(0).toString(16)}`.slice(-2)}`;
|
|
105
|
+
}
|
package/src/lib/requestFn.ts
CHANGED
|
@@ -141,7 +141,7 @@ function getQueryString(params: Record<string, any>): string {
|
|
|
141
141
|
return '';
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]) {
|
|
144
|
+
export function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]) {
|
|
145
145
|
const headers = new Headers();
|
|
146
146
|
|
|
147
147
|
for (const headerSet of allHeaders) {
|
|
@@ -300,6 +300,8 @@ export interface OperationSchema {
|
|
|
300
300
|
* @example application/json
|
|
301
301
|
*/
|
|
302
302
|
readonly mediaType?: string;
|
|
303
|
+
|
|
304
|
+
readonly security?: string[];
|
|
303
305
|
}
|
|
304
306
|
|
|
305
307
|
export interface RequestFnPayload {
|
|
@@ -337,7 +339,7 @@ export interface RequestFnPayload {
|
|
|
337
339
|
signal?: AbortSignal | null;
|
|
338
340
|
}
|
|
339
341
|
|
|
340
|
-
interface RequestFnInfo
|
|
342
|
+
export interface RequestFnInfo
|
|
341
343
|
extends RequestFnPayload,
|
|
342
344
|
Omit<RequestInit, 'headers' | 'method' | 'body' | 'signal'> {
|
|
343
345
|
/**
|