Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/easypost.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ export default class EasyPostClient {
this.responseHooks = [];
}

/**
* Make an API call to the EasyPost API.
*
* 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.
* @param {string} method - The HTTP method to use (e.g. 'get', 'post', 'put', 'patch', 'del').
* @param {string} endpoint - The API endpoint to call (e.g. '/addresses').
* @param {Object} [params] - The parameters to send with the request.
* @returns {Promise<Object>} The response from the API call.
*/
async makeApiCall(method, endpoint, params = {}) {
const response = await this._request(endpoint, method, params);

return response.body;
}

/**
* Create a copy of an {@link EasyPostClient} with overridden options.
* @param {EasyPostClient} client The `EasyPostClient` instance to clone.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/services/easypost.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ describe('EasyPost', function () {
expect(responseConfig1).to.be.null;
expect(responseConfig2).to.be.null;
});

it('makes an API call using the generic makeApiCall method', async function () {
const response = await client.makeApiCall('get', '/addresses', {
page_size: 1,
});

expect(response.addresses.length).to.equal(1);
expect(response.addresses[0].object).to.equal('Address');
});
});
18 changes: 18 additions & 0 deletions types/EasyPost.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,42 @@ export default class EasyPost {
* Adds a request hook to the EasyPost client. Useful for logging or debugging.
*/
public addRequestHook(fn: (config: IEasyPostRequest) => void): void;

/**
* Removes a request hook from the EasyPost client.
*/
public removeRequestHook(fn: (config: IEasyPostRequest) => void): void;

/**
* Clears all request hooks from the EasyPost client.
*/
public clearRequestHooks(): void;

/**
* Adds a response hook to the EasyPost client. Useful for logging or debugging.
*/
public addResponseHook(fn: (config: IEasyPostResponse) => void): void;

/**
* Removes a response hook from the EasyPost client.
*/
public removeResponseHook(fn: (config: IEasyPostResponse) => void): void;

/**
* Clears all response hooks from the EasyPost client.
*/
public clearResponseHooks(): void;

/**
* Make an API call to the EasyPost API.
*
* 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.
*/
public makeApiCall(
method: 'get' | 'post' | 'put' | 'patch' | 'del',
endpoint: string,
params?: Record<string, any>,
): Promise<Record<string, any>>;
}