Skip to content

Commit a2c5d09

Browse files
Justintime50claude
andauthored
feat: add a generic API request interface (#540)
# Description Adds a generic API request interface to make arbitrary API requests. # Testing - All tests pass - New unit test added for the `makeApiCall` method # Pull Request Type Please select the option(s) that are relevant to this PR. - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Improvement (fixing a typo, updating readme, renaming a variable name, etc) --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent be96ca7 commit a2c5d09

File tree

7 files changed

+210
-3
lines changed

7 files changed

+210
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v8.7.0 (2026-02-25)
4+
5+
- Adds generic `makeApiCall` function
6+
37
## v8.6.0 (2026-02-20)
48

59
- Adds the following functions:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@easypost/api",
33
"description": "EasyPost Node Client Library",
4-
"version": "8.6.0",
4+
"version": "8.7.0",
55
"author": "Easypost Engineering <oss@easypost.com>",
66
"homepage": "https://easypost.com",
77
"exports": {

src/easypost.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ export default class EasyPostClient {
115115
this.responseHooks = [];
116116
}
117117

118+
/**
119+
* Make an API call to the EasyPost API.
120+
*
121+
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
122+
* are not yet supported by the client library's services. When possible, the service for your use case
123+
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
124+
* @param {string} method - The HTTP method to use (e.g. 'get', 'post', 'put', 'patch', 'del').
125+
* @param {string} endpoint - The API endpoint to call (e.g. '/addresses').
126+
* @param {Object} [params] - The parameters to send with the request.
127+
* @returns {Promise<Object>} The response from the API call.
128+
*/
129+
async makeApiCall(method, endpoint, params = {}) {
130+
const response = await this._request(endpoint, method, params);
131+
132+
return response.body;
133+
}
134+
118135
/**
119136
* Create a copy of an {@link EasyPostClient} with overridden options.
120137
* @param {EasyPostClient} client The `EasyPostClient` instance to clone.

test/cassettes/EasyPost_3093958733/makes-an-API-call-using-the-generic-makeApiCall-method_2342601902/recording.har

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/services/easypost.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,13 @@ describe('EasyPost', function () {
147147
expect(responseConfig1).to.be.null;
148148
expect(responseConfig2).to.be.null;
149149
});
150+
151+
it('makes an API call using the generic makeApiCall method', async function () {
152+
const response = await client.makeApiCall('get', '/addresses', {
153+
page_size: 1,
154+
});
155+
156+
expect(response.addresses.length).to.equal(1);
157+
expect(response.addresses[0].object).to.equal('Address');
158+
});
150159
});

types/EasyPost.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,42 @@ export default class EasyPost {
116116
* Adds a request hook to the EasyPost client. Useful for logging or debugging.
117117
*/
118118
public addRequestHook(fn: (config: IEasyPostRequest) => void): void;
119+
119120
/**
120121
* Removes a request hook from the EasyPost client.
121122
*/
122123
public removeRequestHook(fn: (config: IEasyPostRequest) => void): void;
124+
123125
/**
124126
* Clears all request hooks from the EasyPost client.
125127
*/
126128
public clearRequestHooks(): void;
129+
127130
/**
128131
* Adds a response hook to the EasyPost client. Useful for logging or debugging.
129132
*/
130133
public addResponseHook(fn: (config: IEasyPostResponse) => void): void;
134+
131135
/**
132136
* Removes a response hook from the EasyPost client.
133137
*/
134138
public removeResponseHook(fn: (config: IEasyPostResponse) => void): void;
139+
135140
/**
136141
* Clears all response hooks from the EasyPost client.
137142
*/
138143
public clearResponseHooks(): void;
144+
145+
/**
146+
* Make an API call to the EasyPost API.
147+
*
148+
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
149+
* are not yet supported by the client library's services. When possible, the service for your use case
150+
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
151+
*/
152+
public makeApiCall(
153+
method: 'get' | 'post' | 'put' | 'patch' | 'del',
154+
endpoint: string,
155+
params?: Record<string, any>,
156+
): Promise<Record<string, any>>;
139157
}

0 commit comments

Comments
 (0)