@workos-inc/node 6.0.0 → 6.0.1
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/lib/common/utils/fetch-client.js +18 -9
- package/lib/common/utils/test-utils.d.ts +3 -1
- package/lib/common/utils/test-utils.js +4 -1
- package/lib/webhooks/webhooks.d.ts +1 -0
- package/lib/webhooks/webhooks.js +7 -4
- package/lib/workos.js +1 -1
- package/lib/workos.spec.js +8 -0
- package/package.json +1 -1
|
@@ -28,15 +28,10 @@ class FetchClient {
|
|
|
28
28
|
post(path, entity, options) {
|
|
29
29
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
30
|
const resourceURL = this.getResourceURL(path, options.params);
|
|
31
|
-
const bodyIsSearchParams = entity instanceof URLSearchParams;
|
|
32
|
-
const contentTypeHeader = bodyIsSearchParams
|
|
33
|
-
? { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
|
|
34
|
-
: undefined;
|
|
35
|
-
const body = bodyIsSearchParams ? entity : JSON.stringify(entity);
|
|
36
31
|
const response = yield this.fetch(resourceURL, {
|
|
37
32
|
method: 'POST',
|
|
38
|
-
headers: Object.assign(Object.assign({},
|
|
39
|
-
body,
|
|
33
|
+
headers: Object.assign(Object.assign({}, getContentTypeHeader(entity)), options.headers),
|
|
34
|
+
body: getBody(entity),
|
|
40
35
|
});
|
|
41
36
|
return { data: yield response.json() };
|
|
42
37
|
});
|
|
@@ -46,8 +41,8 @@ class FetchClient {
|
|
|
46
41
|
const resourceURL = this.getResourceURL(path, options.params);
|
|
47
42
|
const response = yield this.fetch(resourceURL, {
|
|
48
43
|
method: 'PUT',
|
|
49
|
-
headers: options.headers,
|
|
50
|
-
body:
|
|
44
|
+
headers: Object.assign(Object.assign({}, getContentTypeHeader(entity)), options.headers),
|
|
45
|
+
body: getBody(entity),
|
|
51
46
|
});
|
|
52
47
|
return { data: yield response.json() };
|
|
53
48
|
});
|
|
@@ -95,3 +90,17 @@ function getQueryString(queryObj) {
|
|
|
95
90
|
});
|
|
96
91
|
return new URLSearchParams(sanitizedQueryObj).toString();
|
|
97
92
|
}
|
|
93
|
+
function getContentTypeHeader(entity) {
|
|
94
|
+
if (entity instanceof URLSearchParams) {
|
|
95
|
+
return {
|
|
96
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
function getBody(entity) {
|
|
102
|
+
if (entity === null || entity instanceof URLSearchParams) {
|
|
103
|
+
return entity;
|
|
104
|
+
}
|
|
105
|
+
return JSON.stringify(entity);
|
|
106
|
+
}
|
|
@@ -5,4 +5,6 @@ export declare function fetchSearchParams(): {
|
|
|
5
5
|
[k: string]: string;
|
|
6
6
|
};
|
|
7
7
|
export declare function fetchHeaders(): HeadersInit | undefined;
|
|
8
|
-
export declare function fetchBody(
|
|
8
|
+
export declare function fetchBody({ raw }?: {
|
|
9
|
+
raw?: boolean | undefined;
|
|
10
|
+
}): any;
|
|
@@ -34,12 +34,15 @@ function fetchHeaders() {
|
|
|
34
34
|
return (_a = jest_fetch_mock_1.default.mock.calls[0][1]) === null || _a === void 0 ? void 0 : _a.headers;
|
|
35
35
|
}
|
|
36
36
|
exports.fetchHeaders = fetchHeaders;
|
|
37
|
-
function fetchBody() {
|
|
37
|
+
function fetchBody({ raw = false } = {}) {
|
|
38
38
|
var _a;
|
|
39
39
|
const body = (_a = jest_fetch_mock_1.default.mock.calls[0][1]) === null || _a === void 0 ? void 0 : _a.body;
|
|
40
40
|
if (body instanceof URLSearchParams) {
|
|
41
41
|
return body.toString();
|
|
42
42
|
}
|
|
43
|
+
if (raw) {
|
|
44
|
+
return body;
|
|
45
|
+
}
|
|
43
46
|
return JSON.parse(String(body));
|
|
44
47
|
}
|
|
45
48
|
exports.fetchBody = fetchBody;
|
package/lib/webhooks/webhooks.js
CHANGED
|
@@ -13,6 +13,9 @@ exports.Webhooks = void 0;
|
|
|
13
13
|
const exceptions_1 = require("../common/exceptions");
|
|
14
14
|
const serializers_1 = require("../common/serializers");
|
|
15
15
|
class Webhooks {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.encoder = new TextEncoder();
|
|
18
|
+
}
|
|
16
19
|
constructEvent({ payload, sigHeader, secret, tolerance = 180000, }) {
|
|
17
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
21
|
const options = { payload, sigHeader, secret, tolerance };
|
|
@@ -51,8 +54,8 @@ class Webhooks {
|
|
|
51
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
55
|
payload = JSON.stringify(payload);
|
|
53
56
|
const signedPayload = `${timestamp}.${payload}`;
|
|
54
|
-
const key = yield crypto.subtle.importKey('raw',
|
|
55
|
-
const signatureBuffer = yield crypto.subtle.sign('HMAC', key,
|
|
57
|
+
const key = yield crypto.subtle.importKey('raw', this.encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
58
|
+
const signatureBuffer = yield crypto.subtle.sign('HMAC', key, this.encoder.encode(signedPayload));
|
|
56
59
|
// crypto.subtle returns the signature in base64 format. This must be
|
|
57
60
|
// encoded in hex to match the CryptoProvider contract. We map each byte in
|
|
58
61
|
// the buffer to its corresponding hex octet and then combine into a string.
|
|
@@ -66,8 +69,8 @@ class Webhooks {
|
|
|
66
69
|
}
|
|
67
70
|
secureCompare(stringA, stringB) {
|
|
68
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
-
const bufferA =
|
|
70
|
-
const bufferB =
|
|
72
|
+
const bufferA = this.encoder.encode(stringA);
|
|
73
|
+
const bufferB = this.encoder.encode(stringB);
|
|
71
74
|
if (bufferA.length !== bufferB.length) {
|
|
72
75
|
return false;
|
|
73
76
|
}
|
package/lib/workos.js
CHANGED
|
@@ -24,7 +24,7 @@ const audit_logs_1 = require("./audit-logs/audit-logs");
|
|
|
24
24
|
const user_management_1 = require("./user-management/user-management");
|
|
25
25
|
const bad_request_exception_1 = require("./common/exceptions/bad-request.exception");
|
|
26
26
|
const fetch_client_1 = require("./common/utils/fetch-client");
|
|
27
|
-
const VERSION = '6.0.
|
|
27
|
+
const VERSION = '6.0.1';
|
|
28
28
|
const DEFAULT_HOSTNAME = 'api.workos.com';
|
|
29
29
|
class WorkOS {
|
|
30
30
|
constructor(key, options = {}) {
|
package/lib/workos.spec.js
CHANGED
|
@@ -147,5 +147,13 @@ describe('WorkOS', () => {
|
|
|
147
147
|
yield expect(workos.post('/path', {})).rejects.toStrictEqual(new exceptions_1.OauthException(400, 'a-request-id', 'error', 'error description', { error: 'error', error_description: 'error description' }));
|
|
148
148
|
}));
|
|
149
149
|
});
|
|
150
|
+
describe('when the entity is null', () => {
|
|
151
|
+
it('sends a null body', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
152
|
+
(0, test_utils_1.fetchOnce)();
|
|
153
|
+
const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
|
|
154
|
+
yield workos.post('/somewhere', null);
|
|
155
|
+
expect((0, test_utils_1.fetchBody)({ raw: true })).toBeNull();
|
|
156
|
+
}));
|
|
157
|
+
});
|
|
150
158
|
});
|
|
151
159
|
});
|
package/package.json
CHANGED