@typeb-digital/nucleus-sdk 0.0.5 → 0.0.6
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/dist/es/index.js +14 -18
- package/package.json +2 -1
package/dist/es/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var ofetch = require('ofetch');
|
|
4
|
-
var jose = require('jose');
|
|
1
|
+
import { $fetch, FetchError } from 'ofetch';
|
|
2
|
+
import { createRemoteJWKSet, jwtVerify, errors } from 'jose';
|
|
5
3
|
|
|
6
4
|
const DEFAULT_BASE_URL$1 = 'https://nucleus.typeb-lab.online';
|
|
7
5
|
function statusToCode(status, apiCode) {
|
|
@@ -29,7 +27,7 @@ class NucleusTransport {
|
|
|
29
27
|
async get(path, query) {
|
|
30
28
|
try {
|
|
31
29
|
const params = query ? Object.fromEntries(Object.entries(query).filter(([, v])=>v !== undefined)) : undefined;
|
|
32
|
-
const envelope = await
|
|
30
|
+
const envelope = await $fetch(`${this.baseUrl}${path}`, {
|
|
33
31
|
method: 'GET',
|
|
34
32
|
headers: this.headers,
|
|
35
33
|
params
|
|
@@ -41,7 +39,7 @@ class NucleusTransport {
|
|
|
41
39
|
data: envelope.data
|
|
42
40
|
};
|
|
43
41
|
} catch (err) {
|
|
44
|
-
if (err instanceof
|
|
42
|
+
if (err instanceof FetchError) {
|
|
45
43
|
const body = err.data;
|
|
46
44
|
const code = statusToCode(err.status ?? 0, body?.error);
|
|
47
45
|
return makeErrorResult(code, err.message);
|
|
@@ -51,7 +49,7 @@ class NucleusTransport {
|
|
|
51
49
|
}
|
|
52
50
|
async post(path, body) {
|
|
53
51
|
try {
|
|
54
|
-
const envelope = await
|
|
52
|
+
const envelope = await $fetch(`${this.baseUrl}${path}`, {
|
|
55
53
|
method: 'POST',
|
|
56
54
|
headers: this.headers,
|
|
57
55
|
body: JSON.stringify(body)
|
|
@@ -61,7 +59,7 @@ class NucleusTransport {
|
|
|
61
59
|
data: envelope.data
|
|
62
60
|
};
|
|
63
61
|
} catch (err) {
|
|
64
|
-
if (err instanceof
|
|
62
|
+
if (err instanceof FetchError) {
|
|
65
63
|
const errBody = err.data;
|
|
66
64
|
return makeErrorResult(statusToCode(err.status ?? 0, errBody?.error), err.message);
|
|
67
65
|
}
|
|
@@ -71,7 +69,7 @@ class NucleusTransport {
|
|
|
71
69
|
async del(path, query) {
|
|
72
70
|
try {
|
|
73
71
|
const params = query ? Object.fromEntries(Object.entries(query).filter(([, v])=>v !== undefined)) : undefined;
|
|
74
|
-
const envelope = await
|
|
72
|
+
const envelope = await $fetch(`${this.baseUrl}${path}`, {
|
|
75
73
|
method: 'DELETE',
|
|
76
74
|
headers: this.headers,
|
|
77
75
|
params
|
|
@@ -81,7 +79,7 @@ class NucleusTransport {
|
|
|
81
79
|
data: envelope.data
|
|
82
80
|
};
|
|
83
81
|
} catch (err) {
|
|
84
|
-
if (err instanceof
|
|
82
|
+
if (err instanceof FetchError) {
|
|
85
83
|
const errBody = err.data;
|
|
86
84
|
return makeErrorResult(statusToCode(err.status ?? 0, errBody?.error), err.message);
|
|
87
85
|
}
|
|
@@ -91,7 +89,7 @@ class NucleusTransport {
|
|
|
91
89
|
async getList(path, query) {
|
|
92
90
|
try {
|
|
93
91
|
const params = query ? Object.fromEntries(Object.entries(query).filter(([, v])=>v !== undefined)) : undefined;
|
|
94
|
-
const envelope = await
|
|
92
|
+
const envelope = await $fetch(`${this.baseUrl}${path}`, {
|
|
95
93
|
method: 'GET',
|
|
96
94
|
headers: this.headers,
|
|
97
95
|
params
|
|
@@ -111,7 +109,7 @@ class NucleusTransport {
|
|
|
111
109
|
meta: listMeta
|
|
112
110
|
};
|
|
113
111
|
} catch (err) {
|
|
114
|
-
if (err instanceof
|
|
112
|
+
if (err instanceof FetchError) {
|
|
115
113
|
const body = err.data;
|
|
116
114
|
const code = statusToCode(err.status ?? 0, body?.error);
|
|
117
115
|
return makeErrorResult(code, err.message);
|
|
@@ -635,7 +633,7 @@ const DEFAULT_BASE_URL = 'https://nucleus.typeb-lab.online';
|
|
|
635
633
|
class AuthAccessor {
|
|
636
634
|
constructor(baseUrl){
|
|
637
635
|
const url = (baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, '');
|
|
638
|
-
this.jwks =
|
|
636
|
+
this.jwks = createRemoteJWKSet(new URL(`${url}/api/v1/platform/auth/jwks.json`));
|
|
639
637
|
}
|
|
640
638
|
/**
|
|
641
639
|
* Verify a Nucleus user access token locally using the platform's public key.
|
|
@@ -653,7 +651,7 @@ class AuthAccessor {
|
|
|
653
651
|
* ```
|
|
654
652
|
*/ async verifyToken(token) {
|
|
655
653
|
try {
|
|
656
|
-
const { payload } = await
|
|
654
|
+
const { payload } = await jwtVerify(token, this.jwks, {
|
|
657
655
|
issuer: 'nucleus',
|
|
658
656
|
algorithms: [
|
|
659
657
|
'RS256'
|
|
@@ -667,7 +665,7 @@ class AuthAccessor {
|
|
|
667
665
|
}
|
|
668
666
|
};
|
|
669
667
|
} catch (err) {
|
|
670
|
-
if (err instanceof
|
|
668
|
+
if (err instanceof errors.JOSEError) {
|
|
671
669
|
return {
|
|
672
670
|
error: {
|
|
673
671
|
code: 'FORBIDDEN',
|
|
@@ -734,6 +732,4 @@ function isError(result) {
|
|
|
734
732
|
return 'error' in result;
|
|
735
733
|
}
|
|
736
734
|
|
|
737
|
-
|
|
738
|
-
exports.NucleusClient = NucleusClient;
|
|
739
|
-
exports.isError = isError;
|
|
735
|
+
export { FilesAccessor, NucleusClient, isError };
|