@dintero/node-sdk 1.0.2 → 1.1.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/README.md +49 -47
- package/dist/package.json +53 -0
- package/dist/src/client.d.ts +6 -0
- package/dist/{client.js → src/client.js} +5 -4
- package/dist/src/middleware.d.ts +59 -0
- package/dist/src/middleware.js +73 -0
- package/dist/{client.d.ts → src/types.d.ts} +10 -5
- package/package.json +8 -8
- package/dist/middleware.d.ts +0 -9
- package/dist/middleware.js +0 -74
- package/dist/types.d.ts +0 -11
- /package/dist/{dintero.d.ts → src/dintero.d.ts} +0 -0
- /package/dist/{dintero.js → src/dintero.js} +0 -0
- /package/dist/{types.js → src/types.js} +0 -0
package/README.md
CHANGED
|
@@ -27,9 +27,9 @@ You first need to create a client with the required credentials:
|
|
|
27
27
|
import { createClient } from "@dintero/node-sdk";
|
|
28
28
|
|
|
29
29
|
const client = createClient({
|
|
30
|
-
clientId:
|
|
31
|
-
clientSecret:
|
|
32
|
-
audience:
|
|
30
|
+
clientId: "your_client_id", // Replace with your actual client ID
|
|
31
|
+
clientSecret: "your_client_secret", // Replace with your actual client secret
|
|
32
|
+
audience: "https://api.dintero.com/v1/accounts/your_account_id",
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
export default client;
|
|
@@ -39,69 +39,71 @@ export default client;
|
|
|
39
39
|
### Example: Session Profile
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
|
-
import { createClient } from
|
|
42
|
+
import { createClient } from "@dintero/node-sdk";
|
|
43
43
|
|
|
44
44
|
const client = createClient({
|
|
45
|
-
clientId:
|
|
46
|
-
clientSecret:
|
|
47
|
-
audience:
|
|
45
|
+
clientId: "your_client_id", // Replace with your actual client ID
|
|
46
|
+
clientSecret: "your_client_secret", // Replace with your actual client secret
|
|
47
|
+
audience: "https://api.dintero.com/v1/accounts/your_account_id",
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
const sessionProfileResponse = await client.checkout.POST(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
50
|
+
const sessionProfileResponse = await client.checkout.POST(
|
|
51
|
+
"/sessions-profile",
|
|
52
|
+
{
|
|
53
|
+
body: {
|
|
54
|
+
url: {
|
|
55
|
+
return_url: "https://example.com", // Replace with actual return URL
|
|
56
|
+
},
|
|
57
|
+
order: {
|
|
58
|
+
amount: 1000,
|
|
59
|
+
currency: "NOK",
|
|
60
|
+
items: [
|
|
61
|
+
{
|
|
62
|
+
id: "item1",
|
|
63
|
+
line_id: "line1",
|
|
64
|
+
description: "Item 1",
|
|
65
|
+
amount: 1000,
|
|
66
|
+
quantity: 1,
|
|
67
|
+
vat_amount: 0,
|
|
68
|
+
vat: 0,
|
|
69
|
+
eligible_for_discount: false,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
partial_payment: false,
|
|
73
|
+
merchant_reference: "ref123", // Replace with actual merchant reference
|
|
74
|
+
},
|
|
75
|
+
profile_id: "default"
|
|
73
76
|
},
|
|
74
|
-
profile_id: 'profile123', // Replace with actual profile ID
|
|
75
77
|
},
|
|
76
|
-
|
|
77
|
-
'Content-Type': 'application/json',
|
|
78
|
-
'Accept': 'application/json',
|
|
79
|
-
},
|
|
80
|
-
});
|
|
78
|
+
);
|
|
81
79
|
|
|
82
|
-
console.log(
|
|
80
|
+
console.log("Session Profile Response:", sessionProfileResponse.data);
|
|
83
81
|
```
|
|
84
82
|
|
|
85
83
|
### Example: Fetching Settlement
|
|
86
84
|
|
|
87
85
|
|
|
88
86
|
```ts
|
|
89
|
-
import { createClient } from
|
|
87
|
+
import { createClient } from "@dintero/node-sdk";
|
|
90
88
|
|
|
91
89
|
const client = createClient({
|
|
92
|
-
clientId:
|
|
93
|
-
clientSecret:
|
|
94
|
-
audience:
|
|
90
|
+
clientId: "your_client_id", // Replace with your actual client ID
|
|
91
|
+
clientSecret: "your_client_secret", // Replace with your actual client secret
|
|
92
|
+
audience: "https://api.dintero.com/v1/accounts/your_account_id",
|
|
95
93
|
});
|
|
96
94
|
|
|
97
|
-
const settlementsResponse = await client.core.GET(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
const settlementsResponse = await client.core.GET(
|
|
96
|
+
"/accounts/{aid}/settlements",
|
|
97
|
+
{
|
|
98
|
+
params: {
|
|
99
|
+
path: {
|
|
100
|
+
aid: "T12345678",
|
|
101
|
+
},
|
|
101
102
|
},
|
|
102
|
-
}
|
|
103
|
+
},
|
|
103
104
|
);
|
|
104
|
-
|
|
105
|
+
|
|
106
|
+
console.log("Settlements Response:", settlementsResponse.data);
|
|
105
107
|
```
|
|
106
108
|
|
|
107
109
|
## Bugs
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dintero/node-sdk",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Node.js library for the Dintero API",
|
|
5
|
+
"main": "./dist/dintero.js",
|
|
6
|
+
"typings": "./dist/dintero.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"private": false,
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"author": "Dintero <support@dintero.com> (https://dintero.com/)",
|
|
16
|
+
"jest": {
|
|
17
|
+
"preset": "ts-jest",
|
|
18
|
+
"testMatch": [
|
|
19
|
+
"**/test/**/*.test.ts"
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "yarn -s build-ts && yarn -s lint",
|
|
24
|
+
"build-ts": "tsc -p ./tsconfig.build.json",
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"lint": "biome check",
|
|
27
|
+
"convert-spec": "ts-node -T scripts/convert-spec.ts",
|
|
28
|
+
"semantic-release": "semantic-release",
|
|
29
|
+
"prepublishOnly": "yarn run build-ts"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@biomejs/biome": "1.9.3",
|
|
33
|
+
"@types/jest": "29.5.13",
|
|
34
|
+
"@types/js-yaml": "4.0.9",
|
|
35
|
+
"@types/node": "20.16.11",
|
|
36
|
+
"@types/swagger2openapi": "7.0.4",
|
|
37
|
+
"jest": "29.7.0",
|
|
38
|
+
"js-yaml": "4.1.0",
|
|
39
|
+
"msw": "2.4.11",
|
|
40
|
+
"openapi-typescript": "7.4.1",
|
|
41
|
+
"semantic-release": "24.1.2",
|
|
42
|
+
"swagger2openapi": "7.0.8",
|
|
43
|
+
"ts-jest": "29.2.5",
|
|
44
|
+
"ts-node": "10.9.2",
|
|
45
|
+
"typescript": "5.6.3"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=20.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"openapi-fetch": "^0.12.0"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CheckoutPaths, ClientOptions, CorePaths } from "./types";
|
|
2
|
+
export declare const createClient: (options: ClientOptions) => {
|
|
3
|
+
checkout: import("openapi-fetch").Client<CheckoutPaths, `${string}/${string}`>;
|
|
4
|
+
core: import("openapi-fetch").Client<CorePaths, `${string}/${string}`>;
|
|
5
|
+
accountId: string;
|
|
6
|
+
};
|
|
@@ -12,13 +12,14 @@ const createClient = (options) => {
|
|
|
12
12
|
core: { baseUrl: "https://api.dintero.com" },
|
|
13
13
|
...options,
|
|
14
14
|
};
|
|
15
|
-
const authMiddleware = (0, middleware_1.createAuthMiddleware)(config);
|
|
16
|
-
const versionPrefixMiddleware = (0, middleware_1.createVersionPrefixMiddleware)();
|
|
17
15
|
const checkout = (0, openapi_fetch_1.default)(config.checkout);
|
|
18
16
|
const core = (0, openapi_fetch_1.default)(config.core);
|
|
19
17
|
const accountId = (0, middleware_1.extractAccountId)(config.audience);
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const authMiddleware = (0, middleware_1.createAuthMiddleware)(config, core);
|
|
19
|
+
const headersMiddleware = (0, middleware_1.createDefaultHeadersMiddleware)();
|
|
20
|
+
const versionPrefixMiddleware = (0, middleware_1.createVersionPrefixMiddleware)();
|
|
21
|
+
core.use(versionPrefixMiddleware, authMiddleware, headersMiddleware);
|
|
22
|
+
checkout.use(versionPrefixMiddleware, authMiddleware, headersMiddleware);
|
|
22
23
|
return { checkout, core, accountId };
|
|
23
24
|
};
|
|
24
25
|
exports.createClient = createClient;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Client, Middleware } from "openapi-fetch";
|
|
2
|
+
import type { CorePaths } from "./types";
|
|
3
|
+
import type { ClientOptions } from "./types";
|
|
4
|
+
export declare const extractAccountId: (audience: string) => string;
|
|
5
|
+
export declare const accessToken: (config: Required<ClientOptions>, client: Client<CorePaths>) => Promise<import("openapi-fetch").FetchResponse<{
|
|
6
|
+
parameters: {
|
|
7
|
+
query?: never;
|
|
8
|
+
header?: never;
|
|
9
|
+
path: {
|
|
10
|
+
oid: import("./generated/payments").components["parameters"]["owner"];
|
|
11
|
+
};
|
|
12
|
+
cookie?: never;
|
|
13
|
+
};
|
|
14
|
+
requestBody: {
|
|
15
|
+
content: {
|
|
16
|
+
"application/json": import("./generated/payments").components["schemas"]["AuthToken"];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
responses: {
|
|
20
|
+
200: {
|
|
21
|
+
headers: {
|
|
22
|
+
[name: string]: unknown;
|
|
23
|
+
};
|
|
24
|
+
content: {
|
|
25
|
+
"application/json": import("./generated/payments").components["schemas"]["AccessToken"];
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
400: import("./generated/payments").components["responses"]["BadRequest"];
|
|
29
|
+
401: import("./generated/payments").components["responses"]["AccessForbidden"];
|
|
30
|
+
403: {
|
|
31
|
+
headers: {
|
|
32
|
+
[name: string]: unknown;
|
|
33
|
+
};
|
|
34
|
+
content: {
|
|
35
|
+
"application/json": import("./generated/payments").components["schemas"]["Error"] & {
|
|
36
|
+
mfa_token?: string;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
429: import("./generated/payments").components["responses"]["TooManyRequests"];
|
|
41
|
+
500: import("./generated/payments").components["responses"]["ServerError"];
|
|
42
|
+
};
|
|
43
|
+
}, {
|
|
44
|
+
params: {
|
|
45
|
+
path: {
|
|
46
|
+
oid: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
headers: {
|
|
50
|
+
Authorization: string;
|
|
51
|
+
};
|
|
52
|
+
body: {
|
|
53
|
+
grant_type: string;
|
|
54
|
+
audience: string;
|
|
55
|
+
};
|
|
56
|
+
}, `${string}/${string}`>>;
|
|
57
|
+
export declare const createAuthMiddleware: (config: Required<ClientOptions>, client: Client<CorePaths>) => Middleware;
|
|
58
|
+
export declare const createDefaultHeadersMiddleware: () => Middleware;
|
|
59
|
+
export declare const createVersionPrefixMiddleware: () => Middleware;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createVersionPrefixMiddleware = exports.createDefaultHeadersMiddleware = exports.createAuthMiddleware = exports.accessToken = exports.extractAccountId = void 0;
|
|
4
|
+
const package_json_1 = require("../package.json");
|
|
5
|
+
const extractAccountId = (audience) => {
|
|
6
|
+
if (!audience || !audience.includes("://")) {
|
|
7
|
+
throw new Error("Account ID could not be extracted from the audience URL.");
|
|
8
|
+
}
|
|
9
|
+
const audienceUrl = new URL(audience);
|
|
10
|
+
const pathParts = audienceUrl.pathname.split("/").filter(Boolean);
|
|
11
|
+
const accountId = audienceUrl.username || (pathParts.length >= 3 ? pathParts[2] : null);
|
|
12
|
+
if (!accountId) {
|
|
13
|
+
throw new Error("Account ID could not be extracted from the audience URL.");
|
|
14
|
+
}
|
|
15
|
+
return accountId;
|
|
16
|
+
};
|
|
17
|
+
exports.extractAccountId = extractAccountId;
|
|
18
|
+
const accessToken = async (config, client) => {
|
|
19
|
+
const credentials = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
|
|
20
|
+
return await client.POST("/accounts/{oid}/auth/token", {
|
|
21
|
+
params: { path: { oid: (0, exports.extractAccountId)(config.audience) } },
|
|
22
|
+
headers: {
|
|
23
|
+
Authorization: `Basic ${credentials}`,
|
|
24
|
+
},
|
|
25
|
+
body: {
|
|
26
|
+
grant_type: "client_credentials",
|
|
27
|
+
audience: config.audience,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
exports.accessToken = accessToken;
|
|
32
|
+
const createAuthMiddleware = (config, client) => {
|
|
33
|
+
let auth = undefined;
|
|
34
|
+
let authExpires = 0;
|
|
35
|
+
return {
|
|
36
|
+
async onRequest({ request }) {
|
|
37
|
+
if (request.headers.get("Authorization")) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const now = Math.floor(Date.now() / 1000);
|
|
41
|
+
if (!auth?.access_token || authExpires < now) {
|
|
42
|
+
const result = await (0, exports.accessToken)(config, client);
|
|
43
|
+
if (result.error || !result.data) {
|
|
44
|
+
throw new Error(`Failed to fetch access token: ${result.response.status}`);
|
|
45
|
+
}
|
|
46
|
+
auth = result.data;
|
|
47
|
+
authExpires = now + Math.floor(auth.expires_in / 2);
|
|
48
|
+
}
|
|
49
|
+
request.headers.set("Authorization", `Bearer ${auth.access_token}`);
|
|
50
|
+
return request;
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
exports.createAuthMiddleware = createAuthMiddleware;
|
|
55
|
+
const createDefaultHeadersMiddleware = () => ({
|
|
56
|
+
onRequest({ request }) {
|
|
57
|
+
if (!request.headers.get("User-Agent")) {
|
|
58
|
+
request.headers.set("User-Agent", `Dintero.Node.SDK/${package_json_1.version} (+https://github.com/Dintero/Dintero.Node.SDK)`);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
exports.createDefaultHeadersMiddleware = createDefaultHeadersMiddleware;
|
|
63
|
+
const createVersionPrefixMiddleware = () => ({
|
|
64
|
+
async onRequest({ request, schemaPath }) {
|
|
65
|
+
if (schemaPath.startsWith("/v")) {
|
|
66
|
+
return request;
|
|
67
|
+
}
|
|
68
|
+
const url = new URL(request.url);
|
|
69
|
+
url.pathname = `/v1${url.pathname ?? "/"}`;
|
|
70
|
+
return new Request(url.toString(), request);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
exports.createVersionPrefixMiddleware = createVersionPrefixMiddleware;
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import type { paths } from "./generated/payments";
|
|
2
|
-
import type { ClientOptions } from "./types";
|
|
3
2
|
export type CheckoutPaths = Pick<paths, "/sessions-profile" | "/accounts/{oid}/auth/token" | "/sessions/{session_id}" | "/sessions/{session_id}/cancel" | "/transactions/{id}/capture" | "/transactions/{id}/authorization" | "/transactions/{id}/refund" | "/transactions/{id}/void" | "/transactions/{id}" | "/transactions">;
|
|
4
3
|
export type CorePaths = Pick<paths, "/accounts/{aid}/settlements" | "/accounts/{oid}/auth/token" | "/accounts/{aid}/reports/metadata" | "/accounts/{aid}/settlements/reports/configuration" | "/accounts/{aid}/settlements/reports/configuration/{id}" | "/accounts/{aid}/management/settings/approvals/payout-destinations" | "/accounts/{aid}/settlements/{settlementid}/attachments/{attachmentid}" | "/v2/accounts/{aid}/payout/fund-transfer" | "/v2/accounts/{aid}/payout/payout-destinations/{payout_destination_id}/balances" | "/v2/accounts/{aid}/payout/payout-destinations/{payout_destination_id}/transfers">;
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export type ClientOptions = {
|
|
5
|
+
clientId: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
audience: string;
|
|
8
|
+
core?: {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
};
|
|
11
|
+
checkout?: {
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
};
|
|
9
14
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dintero/node-sdk",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Node.js library for the Dintero API",
|
|
5
5
|
"main": "./dist/dintero.js",
|
|
6
6
|
"typings": "./dist/dintero.d.ts",
|
|
@@ -29,20 +29,20 @@
|
|
|
29
29
|
"prepublishOnly": "yarn run build-ts"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@biomejs/biome": "1.
|
|
33
|
-
"@types/jest": "29.5.
|
|
32
|
+
"@biomejs/biome": "1.9.3",
|
|
33
|
+
"@types/jest": "29.5.13",
|
|
34
34
|
"@types/js-yaml": "4.0.9",
|
|
35
|
-
"@types/node": "20.
|
|
35
|
+
"@types/node": "20.16.11",
|
|
36
36
|
"@types/swagger2openapi": "7.0.4",
|
|
37
37
|
"jest": "29.7.0",
|
|
38
38
|
"js-yaml": "4.1.0",
|
|
39
|
-
"msw": "2.4.
|
|
40
|
-
"openapi-typescript": "7.4.
|
|
41
|
-
"semantic-release": "24.1.
|
|
39
|
+
"msw": "2.4.11",
|
|
40
|
+
"openapi-typescript": "7.4.1",
|
|
41
|
+
"semantic-release": "24.1.2",
|
|
42
42
|
"swagger2openapi": "7.0.8",
|
|
43
43
|
"ts-jest": "29.2.5",
|
|
44
44
|
"ts-node": "10.9.2",
|
|
45
|
-
"typescript": "5.6.
|
|
45
|
+
"typescript": "5.6.3"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
48
|
"node": ">=20.0.0"
|
package/dist/middleware.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Middleware } from "openapi-fetch";
|
|
2
|
-
import type { ClientOptions } from "./types";
|
|
3
|
-
export declare const extractAccountId: (audience: string) => string;
|
|
4
|
-
export declare const fetchAccessToken: (config: Required<ClientOptions>) => Promise<{
|
|
5
|
-
accessToken: any;
|
|
6
|
-
expiresIn: any;
|
|
7
|
-
}>;
|
|
8
|
-
export declare const createAuthMiddleware: (config: Required<ClientOptions>) => Middleware;
|
|
9
|
-
export declare const createVersionPrefixMiddleware: () => Middleware;
|
package/dist/middleware.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createVersionPrefixMiddleware = exports.createAuthMiddleware = exports.fetchAccessToken = exports.extractAccountId = void 0;
|
|
4
|
-
const extractAccountId = (audience) => {
|
|
5
|
-
if (!audience || !audience.includes("://")) {
|
|
6
|
-
throw new Error("Account ID could not be extracted from the audience URL.");
|
|
7
|
-
}
|
|
8
|
-
const audienceUrl = new URL(audience);
|
|
9
|
-
const pathParts = audienceUrl.pathname.split("/").filter(Boolean);
|
|
10
|
-
const accountId = audienceUrl.username || (pathParts.length >= 3 ? pathParts[2] : null);
|
|
11
|
-
if (!accountId) {
|
|
12
|
-
throw new Error("Account ID could not be extracted from the audience URL.");
|
|
13
|
-
}
|
|
14
|
-
return accountId;
|
|
15
|
-
};
|
|
16
|
-
exports.extractAccountId = extractAccountId;
|
|
17
|
-
const fetchAccessToken = async (config) => {
|
|
18
|
-
const accountId = (0, exports.extractAccountId)(config.audience);
|
|
19
|
-
const url = `${config.core.baseUrl}/v1/accounts/${accountId}/auth/token`;
|
|
20
|
-
const authToken = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
|
|
21
|
-
const response = await fetch(url, {
|
|
22
|
-
method: "POST",
|
|
23
|
-
headers: {
|
|
24
|
-
"Content-Type": "application/json",
|
|
25
|
-
Accept: "application/json",
|
|
26
|
-
Authorization: `Basic ${authToken}`,
|
|
27
|
-
},
|
|
28
|
-
body: JSON.stringify({
|
|
29
|
-
grant_type: "client_credentials",
|
|
30
|
-
audience: config.audience,
|
|
31
|
-
}),
|
|
32
|
-
});
|
|
33
|
-
const responseText = await response.text();
|
|
34
|
-
if (response.status !== 200) {
|
|
35
|
-
throw new Error(`Failed to fetch access token: ${response.statusText}. Details: ${responseText}`);
|
|
36
|
-
}
|
|
37
|
-
const json = JSON.parse(responseText);
|
|
38
|
-
return {
|
|
39
|
-
accessToken: json.access_token,
|
|
40
|
-
expiresIn: json.expires_in,
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
exports.fetchAccessToken = fetchAccessToken;
|
|
44
|
-
const createAuthMiddleware = (config) => {
|
|
45
|
-
let accessToken = undefined;
|
|
46
|
-
let tokenExpiry = 0;
|
|
47
|
-
return {
|
|
48
|
-
async onRequest({ request }) {
|
|
49
|
-
if (request.headers.get("Authorization")) {
|
|
50
|
-
return request;
|
|
51
|
-
}
|
|
52
|
-
const currentTime = Math.floor(Date.now() / 1000);
|
|
53
|
-
if (!accessToken || tokenExpiry < currentTime) {
|
|
54
|
-
const tokenData = await (0, exports.fetchAccessToken)(config);
|
|
55
|
-
accessToken = tokenData.accessToken;
|
|
56
|
-
tokenExpiry = currentTime + Math.floor(tokenData.expiresIn / 2);
|
|
57
|
-
}
|
|
58
|
-
request.headers.set("Authorization", `Bearer ${accessToken}`);
|
|
59
|
-
return request;
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
exports.createAuthMiddleware = createAuthMiddleware;
|
|
64
|
-
const createVersionPrefixMiddleware = () => ({
|
|
65
|
-
async onRequest({ request, schemaPath }) {
|
|
66
|
-
if (schemaPath.startsWith("/v")) {
|
|
67
|
-
return request;
|
|
68
|
-
}
|
|
69
|
-
const url = new URL(request.url);
|
|
70
|
-
url.pathname = `/v1${url.pathname ?? "/"}`;
|
|
71
|
-
return new Request(url.toString(), request);
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
exports.createVersionPrefixMiddleware = createVersionPrefixMiddleware;
|
package/dist/types.d.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|