Skip to content

Commit cab3b52

Browse files
committed
Release 0.0.35
1 parent 95343d3 commit cab3b52

File tree

181 files changed

+2327
-1025
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+2327
-1025
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
steps:
1010
- name: Checkout repo
11-
uses: actions/checkout@v3
11+
uses: actions/checkout@v4
1212

1313
- name: Set up node
1414
uses: actions/setup-node@v3
@@ -21,7 +21,7 @@ jobs:
2121

2222
steps:
2323
- name: Checkout repo
24-
uses: actions/checkout@v3
24+
uses: actions/checkout@v4
2525

2626
- name: Set up node
2727
uses: actions/setup-node@v3
@@ -35,7 +35,7 @@ jobs:
3535
runs-on: ubuntu-latest
3636
steps:
3737
- name: Checkout repo
38-
uses: actions/checkout@v3
38+
uses: actions/checkout@v4
3939
- name: Set up node
4040
uses: actions/setup-node@v3
4141
- name: Install dependencies

README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,36 @@ try {
5757
console.log(err.statusCode);
5858
console.log(err.message);
5959
console.log(err.body);
60+
console.log(err.rawResponse);
6061
}
6162
}
6263
```
6364

6465
## Advanced
6566

67+
### Additional Headers
68+
69+
If you would like to send additional headers as part of the request, use the `headers` request option.
70+
71+
```typescript
72+
const response = await client.authentication.generateToken(..., {
73+
headers: {
74+
'X-Custom-Header': 'custom value'
75+
}
76+
});
77+
```
78+
6679
### Retries
6780

6881
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
69-
as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
82+
as the request is deemed retryable and the number of retry attempts has not grown larger than the configured
7083
retry limit (default: 2).
7184

72-
A request is deemed retriable when any of the following HTTP status codes is returned:
85+
A request is deemed retryable when any of the following HTTP status codes is returned:
7386

74-
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
75-
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
76-
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
87+
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
88+
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
89+
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
7790

7891
Use the `maxRetries` request option to configure this behavior.
7992

@@ -105,21 +118,33 @@ const response = await client.authentication.generateToken(..., {
105118
controller.abort(); // aborts the request
106119
```
107120

121+
### Access Raw Response Data
122+
123+
The SDK provides access to raw response data, including headers, through the `.withRawResponse()` method.
124+
The `.withRawResponse()` method returns a promise that results to an object with a `data` and a `rawResponse` property.
125+
126+
```typescript
127+
const { data, rawResponse } = await client.authentication.generateToken(...).withRawResponse();
128+
129+
console.log(data);
130+
console.log(rawResponse.headers['X-My-Header']);
131+
```
132+
108133
### Runtime Compatibility
109134

110135
The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK works in the following
111136
runtimes:
112137

113-
- Node.js 18+
114-
- Vercel
115-
- Cloudflare Workers
116-
- Deno v1.25+
117-
- Bun 1.0+
118-
- React Native
138+
- Node.js 18+
139+
- Vercel
140+
- Cloudflare Workers
141+
- Deno v1.25+
142+
- Bun 1.0+
143+
- React Native
119144

120145
### Customizing Fetch Client
121146

122-
The SDK provides a way for your to customize the underlying HTTP client / Fetch function. If you're running in an
147+
The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're running in an
123148
unsupported environment, this provides a way for you to break glass and ensure the SDK works.
124149

125150
```typescript
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/** @type {import('jest').Config} */
2-
module.exports = {
2+
export default {
33
preset: "ts-jest",
44
testEnvironment: "node",
5+
moduleNameMapper: {
6+
"(.+)\.js$": "$1",
7+
},
58
};

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openledger/typescript-sdk",
3-
"version": "0.40.8",
3+
"version": "0.0.35",
44
"private": false,
55
"repository": "https://github.com/openledger/openledger-typescript-sdk",
66
"main": "./index.js",
@@ -15,30 +15,30 @@
1515
"url-join": "4.0.1",
1616
"form-data": "^4.0.0",
1717
"formdata-node": "^6.0.3",
18-
"node-fetch": "2.7.0",
19-
"qs": "6.11.2",
18+
"node-fetch": "^2.7.0",
19+
"qs": "^6.13.1",
2020
"readable-stream": "^4.5.2",
21-
"js-base64": "3.7.2"
21+
"js-base64": "3.7.7"
2222
},
2323
"devDependencies": {
2424
"@types/url-join": "4.0.1",
25-
"@types/qs": "6.9.8",
26-
"@types/node-fetch": "2.6.9",
27-
"@types/readable-stream": "^4.0.15",
28-
"fetch-mock-jest": "^1.5.1",
29-
"webpack": "^5.94.0",
30-
"ts-loader": "^9.3.1",
31-
"jest": "29.7.0",
32-
"@types/jest": "29.5.5",
33-
"ts-jest": "29.1.1",
34-
"jest-environment-jsdom": "29.7.0",
35-
"@types/node": "17.0.33",
36-
"prettier": "2.7.1",
37-
"typescript": "4.6.4"
25+
"@types/qs": "^6.9.17",
26+
"@types/node-fetch": "^2.6.12",
27+
"@types/readable-stream": "^4.0.18",
28+
"webpack": "^5.97.1",
29+
"ts-loader": "^9.5.1",
30+
"jest": "^29.7.0",
31+
"@types/jest": "^29.5.14",
32+
"ts-jest": "^29.1.1",
33+
"jest-environment-jsdom": "^29.7.0",
34+
"@types/node": "^18.19.70",
35+
"prettier": "^3.4.2",
36+
"typescript": "~5.7.2"
3837
},
3938
"browser": {
4039
"fs": false,
4140
"os": false,
4241
"path": false
43-
}
42+
},
43+
"packageManager": "yarn@1.22.22"
4444
}

scripts/rename-to-esm-files.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs").promises;
4+
const path = require("path");
5+
6+
const extensionMap = {
7+
".js": ".mjs",
8+
".d.ts": ".d.mts",
9+
};
10+
const oldExtensions = Object.keys(extensionMap);
11+
12+
async function findFiles(rootPath) {
13+
const files = [];
14+
15+
async function scan(directory) {
16+
const entries = await fs.readdir(directory, { withFileTypes: true });
17+
18+
for (const entry of entries) {
19+
const fullPath = path.join(directory, entry.name);
20+
21+
if (entry.isDirectory()) {
22+
if (entry.name !== "node_modules" && !entry.name.startsWith(".")) {
23+
await scan(fullPath);
24+
}
25+
} else if (entry.isFile()) {
26+
if (oldExtensions.some((ext) => entry.name.endsWith(ext))) {
27+
files.push(fullPath);
28+
}
29+
}
30+
}
31+
}
32+
33+
await scan(rootPath);
34+
return files;
35+
}
36+
37+
async function updateFiles(files) {
38+
const updatedFiles = [];
39+
for (const file of files) {
40+
const updated = await updateFileContents(file);
41+
updatedFiles.push(updated);
42+
}
43+
44+
console.log(`Updated imports in ${updatedFiles.length} files.`);
45+
}
46+
47+
async function updateFileContents(file) {
48+
const content = await fs.readFile(file, "utf8");
49+
50+
let newContent = content;
51+
// Update each extension type defined in the map
52+
for (const [oldExt, newExt] of Object.entries(extensionMap)) {
53+
const regex = new RegExp(`(import|export)(.+from\\s+['"])(\\.\\.?\\/[^'"]+)(\\${oldExt})(['"])`, "g");
54+
newContent = newContent.replace(regex, `$1$2$3${newExt}$5`);
55+
}
56+
57+
if (content !== newContent) {
58+
await fs.writeFile(file, newContent, "utf8");
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
async function renameFiles(files) {
65+
let counter = 0;
66+
for (const file of files) {
67+
const ext = oldExtensions.find((ext) => file.endsWith(ext));
68+
const newExt = extensionMap[ext];
69+
70+
if (newExt) {
71+
const newPath = file.slice(0, -ext.length) + newExt;
72+
await fs.rename(file, newPath);
73+
counter++;
74+
}
75+
}
76+
77+
console.log(`Renamed ${counter} files.`);
78+
}
79+
80+
async function main() {
81+
try {
82+
const targetDir = process.argv[2];
83+
if (!targetDir) {
84+
console.error("Please provide a target directory");
85+
process.exit(1);
86+
}
87+
88+
const targetPath = path.resolve(targetDir);
89+
const targetStats = await fs.stat(targetPath);
90+
91+
if (!targetStats.isDirectory()) {
92+
console.error("The provided path is not a directory");
93+
process.exit(1);
94+
}
95+
96+
console.log(`Scanning directory: ${targetDir}`);
97+
98+
const files = await findFiles(targetDir);
99+
100+
if (files.length === 0) {
101+
console.log("No matching files found.");
102+
process.exit(0);
103+
}
104+
105+
console.log(`Found ${files.length} files.`);
106+
await updateFiles(files);
107+
await renameFiles(files);
108+
console.log("\nDone!");
109+
} catch (error) {
110+
console.error("An error occurred:", error.message);
111+
process.exit(1);
112+
}
113+
}
114+
115+
main();

src/Client.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,66 @@ import { Ai } from "./api/resources/ai/client/Client";
1414
import { Sandbox } from "./api/resources/sandbox/client/Client";
1515

1616
export declare namespace OpenLedgerClientClient {
17-
interface Options {
17+
export interface Options {
1818
environment?: core.Supplier<environments.OpenLedgerClientEnvironment | string>;
19+
/** Specify a custom URL to connect the client to. */
20+
baseUrl?: core.Supplier<string>;
1921
token?: core.Supplier<core.BearerToken | undefined>;
2022
fetcher?: core.FetchFunction;
2123
}
2224

23-
interface RequestOptions {
25+
export interface RequestOptions {
2426
/** The maximum time to wait for a response in seconds. */
2527
timeoutInSeconds?: number;
2628
/** The number of times to retry the request. Defaults to 2. */
2729
maxRetries?: number;
2830
/** A hook to abort the request. */
2931
abortSignal?: AbortSignal;
32+
/** Additional headers to include in the request. */
33+
headers?: Record<string, string>;
3034
}
3135
}
3236

3337
export class OpenLedgerClientClient {
34-
constructor(protected readonly _options: OpenLedgerClientClient.Options = {}) {}
35-
3638
protected _authentication: Authentication | undefined;
39+
protected _transactions: Transactions | undefined;
40+
protected _reports: Reports | undefined;
41+
protected _banks: Banks | undefined;
42+
protected _integrations: Integrations | undefined;
43+
protected _entities: Entities | undefined;
44+
protected _ai: Ai | undefined;
45+
protected _sandbox: Sandbox | undefined;
46+
47+
constructor(protected readonly _options: OpenLedgerClientClient.Options = {}) {}
3748

3849
public get authentication(): Authentication {
3950
return (this._authentication ??= new Authentication(this._options));
4051
}
4152

42-
protected _transactions: Transactions | undefined;
43-
4453
public get transactions(): Transactions {
4554
return (this._transactions ??= new Transactions(this._options));
4655
}
4756

48-
protected _reports: Reports | undefined;
49-
5057
public get reports(): Reports {
5158
return (this._reports ??= new Reports(this._options));
5259
}
5360

54-
protected _banks: Banks | undefined;
55-
5661
public get banks(): Banks {
5762
return (this._banks ??= new Banks(this._options));
5863
}
5964

60-
protected _integrations: Integrations | undefined;
61-
6265
public get integrations(): Integrations {
6366
return (this._integrations ??= new Integrations(this._options));
6467
}
6568

66-
protected _entities: Entities | undefined;
67-
6869
public get entities(): Entities {
6970
return (this._entities ??= new Entities(this._options));
7071
}
7172

72-
protected _ai: Ai | undefined;
73-
7473
public get ai(): Ai {
7574
return (this._ai ??= new Ai(this._options));
7675
}
7776

78-
protected _sandbox: Sandbox | undefined;
79-
8077
public get sandbox(): Sandbox {
8178
return (this._sandbox ??= new Sandbox(this._options));
8279
}

src/api/errors/BadRequestError.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*/
44

55
import * as errors from "../../errors/index";
6+
import * as core from "../../core";
67

78
export class BadRequestError extends errors.OpenLedgerClientError {
8-
constructor(body?: unknown) {
9+
constructor(body?: unknown, rawResponse?: core.RawResponse) {
910
super({
1011
message: "BadRequestError",
1112
statusCode: 400,
1213
body: body,
14+
rawResponse: rawResponse,
1315
});
1416
Object.setPrototypeOf(this, BadRequestError.prototype);
1517
}

0 commit comments

Comments
 (0)