@outseta/node-sdk 0.2.0 → 0.2.2
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/index.js +32 -18
- package/package.json +9 -9
- package/src/generate-token.ts +10 -25
- package/src/index.ts +6 -1
- package/src/verify-jwt.ts +5 -15
package/dist/index.js
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// ../api-client/src/client.ts
|
|
2
|
+
function getAuthorization(creds) {
|
|
3
|
+
if ("accessToken" in creds) {
|
|
4
|
+
return `Bearer ${creds.accessToken}`;
|
|
5
|
+
}
|
|
6
|
+
return `Outseta ${creds.apiKey}:${creds.apiSecret}`;
|
|
7
|
+
}
|
|
8
|
+
function getRequestUrl(subdomain, path) {
|
|
9
|
+
if (/^https?:\/\//.test(path)) {
|
|
10
|
+
return path;
|
|
11
|
+
}
|
|
12
|
+
const normalizedPath = path.startsWith("/api/v1/") ? path : `/api/v1/${path.replace(/^\/+/, "")}`;
|
|
13
|
+
return new URL(normalizedPath, `https://${subdomain}.outseta.com`).toString();
|
|
14
|
+
}
|
|
15
|
+
function createClient(creds) {
|
|
16
|
+
return (path, init = {}) => {
|
|
17
|
+
const headers = new Headers(init.headers);
|
|
18
|
+
headers.set("Authorization", getAuthorization(creds));
|
|
19
|
+
return fetch(getRequestUrl(creds.subdomain, path), {
|
|
20
|
+
...init,
|
|
21
|
+
headers
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
}
|
|
4
25
|
// src/verify-webhook.ts
|
|
5
26
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
6
27
|
function verifyWebhookSignature(signature, body, signingKey) {
|
|
@@ -18,33 +39,26 @@ async function verifyJwt(token, options) {
|
|
|
18
39
|
const { payload } = await jwtVerify(token, jwks);
|
|
19
40
|
return payload;
|
|
20
41
|
}
|
|
21
|
-
async function verifyJwtWithProfile(
|
|
22
|
-
const response = await
|
|
23
|
-
headers: { Authorization: `Bearer ${token}` }
|
|
24
|
-
});
|
|
42
|
+
async function verifyJwtWithProfile(client, token) {
|
|
43
|
+
const response = await client(`profile?${new URLSearchParams({ fields: "*" }).toString()}`);
|
|
25
44
|
if (!response.ok) {
|
|
26
|
-
|
|
27
|
-
throw new Error(`Profile verification failed: [${response.status}] ${error.ErrorMessage || error.Message || response.statusText}`);
|
|
45
|
+
throw new Error(`Failed to fetch profile: ${response.status} ${response.statusText}`);
|
|
28
46
|
}
|
|
29
47
|
const profile = await response.json();
|
|
30
48
|
const payload = decodeJwt(token);
|
|
31
49
|
return { payload, profile };
|
|
32
50
|
}
|
|
33
51
|
// src/generate-token.ts
|
|
34
|
-
async function generateAccessToken(
|
|
35
|
-
const response = await
|
|
52
|
+
async function generateAccessToken(client, email) {
|
|
53
|
+
const response = await client("tokens", {
|
|
36
54
|
method: "POST",
|
|
37
|
-
headers: {
|
|
38
|
-
Authorization: `Outseta ${credentials.apiKey}:${credentials.apiSecret}`,
|
|
39
|
-
"Content-Type": "application/json"
|
|
40
|
-
},
|
|
55
|
+
headers: { "Content-Type": "application/json" },
|
|
41
56
|
body: JSON.stringify({ username: email })
|
|
42
57
|
});
|
|
43
|
-
const data = await response.json();
|
|
44
58
|
if (!response.ok) {
|
|
45
|
-
throw new Error(`Failed to generate token:
|
|
59
|
+
throw new Error(`Failed to generate access token: ${response.status} ${response.statusText}`);
|
|
46
60
|
}
|
|
47
|
-
return
|
|
61
|
+
return await response.json();
|
|
48
62
|
}
|
|
49
63
|
export {
|
|
50
64
|
verifyWebhookSignature,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outseta/node-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Node.js SDK for the Outseta API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,14 +17,9 @@
|
|
|
17
17
|
"import": "./dist/index.js"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "bun build src/index.ts --outdir dist --target node --format esm --packages external",
|
|
22
|
-
"typecheck": "tsc --noEmit",
|
|
23
|
-
"lint": "eslint src"
|
|
24
|
-
},
|
|
25
20
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
21
|
+
"jose": "^6.2.1",
|
|
22
|
+
"@outseta/api-client": "0.2.4"
|
|
28
23
|
},
|
|
29
24
|
"devDependencies": {
|
|
30
25
|
"typescript": "^5.7.0"
|
|
@@ -36,5 +31,10 @@
|
|
|
36
31
|
"publishConfig": {
|
|
37
32
|
"access": "public",
|
|
38
33
|
"provenance": true
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rimraf dist && bun build src/index.ts --outdir dist --target node --format esm --packages external",
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"lint": "eslint src"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|
package/src/generate-token.ts
CHANGED
|
@@ -1,39 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OutsetaClient } from "@outseta/api-client";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Generates a JWT access token for a user by email.
|
|
5
5
|
*
|
|
6
|
-
* Requires server-side API Key + Secret credentials.
|
|
6
|
+
* Requires a client created with server-side API Key + Secret credentials.
|
|
7
7
|
* Useful for logging in users programmatically, testing auth flows,
|
|
8
8
|
* or acting on behalf of a user from a backend service.
|
|
9
9
|
*/
|
|
10
|
-
export async function generateAccessToken(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
Authorization: `Outseta ${credentials.apiKey}:${credentials.apiSecret}`,
|
|
20
|
-
"Content-Type": "application/json",
|
|
21
|
-
},
|
|
22
|
-
body: JSON.stringify({ username: email }),
|
|
23
|
-
},
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
const data = (await response.json()) as Record<string, unknown>;
|
|
10
|
+
export async function generateAccessToken(client: OutsetaClient, email: string) {
|
|
11
|
+
const response = await client("tokens", {
|
|
12
|
+
method: "POST",
|
|
13
|
+
headers: { "Content-Type": "application/json" },
|
|
14
|
+
body: JSON.stringify({ username: email }),
|
|
15
|
+
});
|
|
27
16
|
|
|
28
17
|
if (!response.ok) {
|
|
29
18
|
throw new Error(
|
|
30
|
-
`Failed to generate token:
|
|
31
|
-
(data.ErrorMessage as string) ||
|
|
32
|
-
(data.Message as string) ||
|
|
33
|
-
response.statusText
|
|
34
|
-
}`,
|
|
19
|
+
`Failed to generate access token: ${response.status} ${response.statusText}`,
|
|
35
20
|
);
|
|
36
21
|
}
|
|
37
22
|
|
|
38
|
-
return
|
|
23
|
+
return await response.json() as { access_token: string } & Record<string, unknown>;
|
|
39
24
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export { createClient } from "@outseta/api-client";
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
OutsetaCredentials,
|
|
4
|
+
OutsetaApiKeyCredentials,
|
|
5
|
+
OutsetaBearerCredentials,
|
|
6
|
+
OutsetaClient,
|
|
7
|
+
} from "@outseta/api-client";
|
|
3
8
|
|
|
4
9
|
export { verifyWebhookSignature } from "./verify-webhook.js";
|
|
5
10
|
export { verifyJwt, verifyJwtWithProfile } from "./verify-jwt.js";
|
package/src/verify-jwt.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { OutsetaClient } from "@outseta/api-client";
|
|
1
2
|
import { jwtVerify, createRemoteJWKSet, decodeJwt } from "jose";
|
|
2
3
|
export type { JWTPayload } from "jose";
|
|
3
4
|
|
|
@@ -32,29 +33,18 @@ export async function verifyJwt(
|
|
|
32
33
|
* to Outseta on every call.
|
|
33
34
|
*/
|
|
34
35
|
export async function verifyJwtWithProfile(
|
|
36
|
+
client: OutsetaClient,
|
|
35
37
|
token: string,
|
|
36
|
-
options: { subdomain: string },
|
|
37
38
|
) {
|
|
38
|
-
const response = await
|
|
39
|
-
`https://${options.subdomain}.outseta.com/api/v1/profile?fields=*`,
|
|
40
|
-
{
|
|
41
|
-
headers: { Authorization: `Bearer ${token}` },
|
|
42
|
-
},
|
|
43
|
-
);
|
|
39
|
+
const response = await client(`profile?${new URLSearchParams({ fields: "*" }).toString()}`);
|
|
44
40
|
|
|
45
41
|
if (!response.ok) {
|
|
46
|
-
const error = (await response.json().catch(() => ({}))) as Record<
|
|
47
|
-
string,
|
|
48
|
-
string
|
|
49
|
-
>;
|
|
50
42
|
throw new Error(
|
|
51
|
-
`
|
|
52
|
-
error.ErrorMessage || error.Message || response.statusText
|
|
53
|
-
}`,
|
|
43
|
+
`Failed to fetch profile: ${response.status} ${response.statusText}`,
|
|
54
44
|
);
|
|
55
45
|
}
|
|
56
46
|
|
|
57
|
-
const profile =
|
|
47
|
+
const profile = await response.json() as Record<string, unknown>;
|
|
58
48
|
const payload = decodeJwt(token);
|
|
59
49
|
|
|
60
50
|
return { payload, profile };
|