Skip to content

Commit 2566f05

Browse files
Justintime50claude
andcommitted
feat: add a generic API request interface
This public, generic interface is useful for making arbitrary API calls to the EasyPost API that are not yet supported by the client library's services. When possible, the service for your use case should be used instead as it provides a more convenient and higher-level interface depending on the endpoint. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a33eeb5 commit 2566f05

4 files changed

Lines changed: 198 additions & 0 deletions

File tree

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,14 @@ 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(EasyPostClient.METHODS.GET, '/addresses', {
153+
page_size: 1,
154+
});
155+
156+
expect(response.addresses).to.be.an('array');
157+
expect(response.addresses.length).to.equal(1);
158+
expect(response.addresses[0].object).to.equal('Address');
159+
});
150160
});

types/EasyPost.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,16 @@ export default class EasyPost {
136136
* Clears all response hooks from the EasyPost client.
137137
*/
138138
public clearResponseHooks(): void;
139+
/**
140+
* Make an API call to the EasyPost API.
141+
*
142+
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
143+
* are not yet supported by the client library's services. When possible, the service for your use case
144+
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
145+
*/
146+
public makeApiCall(
147+
method: 'get' | 'post' | 'put' | 'patch' | 'del',
148+
endpoint: string,
149+
params?: Record<string, any>,
150+
): Promise<Record<string, any>>;
139151
}

0 commit comments

Comments
 (0)