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
21 changes: 13 additions & 8 deletions src/services/batch_service.js → src/services/batch_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import baseService from './base_service';

export const DEFAULT_LABEL_FORMAT = 'pdf';

type BatchParams = Record<string, unknown>;

export default (easypostClient) =>
/**
* The BatchService class provides methods for interacting with EasyPost {@link Batch} objects.
Expand All @@ -14,7 +16,7 @@ export default (easypostClient) =>
* @param {Object} params - Parameters for the batch to be created.
* @returns {Batch} - The created batch.
*/
static async create(params) {
static async create(params: BatchParams): Promise<unknown> {
const url = 'batches';

const wrappedParams = {
Expand All @@ -31,7 +33,7 @@ export default (easypostClient) =>
* @param {Array} shipmentIds - The ids of the shipments to add to the batch.
* @returns {Batch} - The updated batch.
*/
static async addShipments(id, shipmentIds) {
static async addShipments(id: string, shipmentIds: string[]): Promise<unknown> {
const url = `batches/${id}/add_shipments`;
const wrappedParams = {
shipments: shipmentIds.map((s) => ({ id: s })),
Expand All @@ -52,7 +54,7 @@ export default (easypostClient) =>
* @param {Array} shipmentIds - The ids of the shipments to remove from the batch.
* @returns {Batch} - The updated batch.
*/
static async removeShipments(id, shipmentIds) {
static async removeShipments(id: string, shipmentIds: string[]): Promise<unknown> {
const url = `batches/${id}/remove_shipments`;
const wrappedParams = {
shipments: shipmentIds.map((s) => ({ id: s })),
Expand All @@ -74,7 +76,10 @@ export default (easypostClient) =>
* @param {string} fileFormat - The format of the label to generate. Defaults to 'pdf'.
* @returns {Batch} - The updated batch.
*/
static async generateLabel(id, fileFormat = DEFAULT_LABEL_FORMAT) {
static async generateLabel(
id: string,
fileFormat: string = DEFAULT_LABEL_FORMAT,
): Promise<unknown> {
const url = `batches/${id}/label`;
const wrappedParams = { file_format: fileFormat };

Expand All @@ -93,7 +98,7 @@ export default (easypostClient) =>
* @param {string} id - The id of the batch to create a scan form for.
* @returns {Batch} - The updated batch.
*/
static async createScanForm(id) {
static async createScanForm(id: string): Promise<unknown> {
const url = `batches/${id}/scan_form`;

try {
Expand All @@ -111,7 +116,7 @@ export default (easypostClient) =>
* @param {string} id - The id of the batch to purchase.
* @returns {Batch} - The purchased batch.
*/
static async buy(id) {
static async buy(id: string): Promise<unknown> {
const url = `batches/${id}/buy`;

try {
Expand All @@ -129,7 +134,7 @@ export default (easypostClient) =>
* @param {Object} [params] - Parameters to filter the list of batches.
* @returns {Object} - An object containing a list of {@link Batch batches} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'batches';

return this._all(url, params);
Expand All @@ -141,7 +146,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the batch to retrieve.
* @returns {Batch} - The retrieved batch.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `batches/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import baseService from './base_service';

type OrderParams = Record<string, unknown>;

export default (easypostClient) =>
/**
* The OrderService class provides methods for interacting with EasyPost {@link Order} objects.
Expand All @@ -12,7 +14,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create an order with.
* @returns {Order} - The created order.
*/
static async create(params) {
static async create(params: OrderParams): Promise<unknown> {
const url = 'orders';

const wrappedParams = {
Expand All @@ -30,7 +32,7 @@ export default (easypostClient) =>
* @param {string} service - The service to use for the order purchase.
* @returns {Order} - The purchased order.
*/
static async buy(id, carrier, service) {
static async buy(id: string, carrier: string, service: string): Promise<unknown> {
const url = `orders/${id}/buy`;
const wrappedParams = { carrier, service };
try {
Expand All @@ -48,7 +50,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the order to get rates for.
* @returns {Order} - The order with rates.
*/
static async getRates(id) {
static async getRates(id: string): Promise<unknown> {
const url = `orders/${id}/rates`;

try {
Expand All @@ -66,7 +68,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the order to retrieve.
* @returns {Order} - The retrieved order.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `orders/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import baseService from './base_service';

type PickupParams = Record<string, unknown>;
type PickupCollection = Record<string, unknown>;

export default (easypostClient) =>
/**
* The PickupService class provides methods for interacting with EasyPost {@link Pickup} objects.
Expand All @@ -12,7 +15,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create a pickup with.
* @returns {Pickup} - The created pickup.
*/
static async create(params) {
static async create(params: PickupParams): Promise<unknown> {
const url = 'pickups';

const wrappedParams = {
Expand All @@ -30,7 +33,7 @@ export default (easypostClient) =>
* @param {string} service - The service to purchase the pickup with.
* @returns {Pickup} - The purchased pickup.
*/
static async buy(id, carrier, service) {
static async buy(id: string, carrier: string, service: string): Promise<unknown> {
const url = `pickups/${id}/buy`;
const wrappedParams = { carrier, service };
try {
Expand All @@ -48,7 +51,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the pickup to cancel.
* @returns {Pickup} - The cancelled pickup.
*/
static async cancel(id) {
static async cancel(id: string): Promise<unknown> {
const url = `pickups/${id}/cancel`;
try {
const response = await easypostClient._post(url);
Expand All @@ -65,7 +68,7 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to filter the pickups by.
* @returns {Object} - An object containing a list of {@link Pickup pickups} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'pickups';

return this._all(url, params);
Expand All @@ -77,7 +80,10 @@ export default (easypostClient) =>
* @param {Number} pageSize The number of records to return on each page
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
*/
static async getNextPage(pickups, pageSize = null) {
static async getNextPage(
pickups: PickupCollection,
pageSize: number | null = null,
): Promise<unknown> {
const url = 'pickups';
return this._getNextPage(url, 'pickups', pickups, pageSize);
}
Expand All @@ -88,7 +94,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the pickup to retrieve.
* @returns {Pickup} - The retrieved pickup.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `pickups/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the rate to retrieve.
* @returns {Rate} - The retrieved rate.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `rates/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import baseService from './base_service';

type RefundParams = Record<string, unknown>;
type RefundCollection = Record<string, unknown>;

export default (easypostClient) =>
/**
* The RefundService class provides methods for interacting with EasyPost {@link Refund} objects.
Expand All @@ -12,7 +15,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create a refund with.
* @returns {Refund} - The created refund.
*/
static async create(params) {
static async create(params: RefundParams): Promise<unknown> {
const url = 'refunds';

const wrappedParams = {
Expand All @@ -28,7 +31,7 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to filter the refunds by.
* @returns {Object} - An object containing the list of {@link Refund refunds} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'refunds';

return this._all(url, params);
Expand All @@ -40,7 +43,10 @@ export default (easypostClient) =>
* @param {Number} pageSize The number of records to return on each page
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
*/
static async getNextPage(refunds, pageSize = null) {
static async getNextPage(
refunds: RefundCollection,
pageSize: number | null = null,
): Promise<unknown> {
const url = 'refunds';
return this._getNextPage(url, 'refunds', refunds, pageSize);
}
Expand All @@ -51,7 +57,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the refund to retrieve.
* @returns {Refund} - The retrieved refund.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `refunds/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import baseService from './base_service';

type ScanFormParams = Record<string, unknown> & {
shipments?: Array<string | { id: string }>;
};
type ScanFormCollection = Record<string, unknown>;

export default (easypostClient) =>
/**
* The ScanFormService class provides methods for interacting with EasyPost {@link ScanForm} objects.
Expand All @@ -12,7 +17,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create a scan form with.
* @returns {ScanForm} - The created scan form.
*/
static async create(params) {
static async create(params: ScanFormParams): Promise<unknown> {
const url = 'scan_forms';

// wraps up params in `shipments` if the user didn't do it
Expand Down Expand Up @@ -40,7 +45,7 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to filter the scan forms by.
* @returns {Object} - An object containing the list of {@link ScanForm scan forms} and pagination information.
*/
static async all(params = {}) {
static async all(params: Record<string, unknown> = {}): Promise<unknown> {
const url = 'scan_forms';

return this._all(url, params);
Expand All @@ -52,7 +57,10 @@ export default (easypostClient) =>
* @param {Number} pageSize The number of records to return on each page
* @returns {EasyPostObject|Promise<never>} The retrieved {@link EasyPostObject}-based class instance, or a `Promise` that rejects with an error.
*/
static async getNextPage(scanforms, pageSize = null) {
static async getNextPage(
scanforms: ScanFormCollection,
pageSize: number | null = null,
): Promise<unknown> {
const url = 'scan_forms';
return this._getNextPage(url, 'scan_forms', scanforms, pageSize);
}
Expand All @@ -63,7 +71,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the scan form to retrieve.
* @returns {ScanForm} - The retrieved scan form.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<unknown> {
const url = `scan_forms/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import baseService from './base_service';

type SmartRateParams = Record<string, unknown>;

export default (easypostClient) =>
/**
* The SmartRateService class provides methods for interacting with EasyPost SmartRate APIs.
Expand All @@ -11,7 +13,7 @@ export default (easypostClient) =>
* @param params - The parameters to estimate the delivery date with.
* @returns {Object} - Estimates and related metadata.
*/
static async estimateDeliveryDate(params) {
static async estimateDeliveryDate(params: SmartRateParams): Promise<unknown> {
const url = 'smartrate/deliver_by';

try {
Expand All @@ -28,7 +30,7 @@ export default (easypostClient) =>
* @param params - The parameters to recommend the ship date with.
* @returns {Object} - Recommendation and related metadata.
*/
static async recommendShipDate(params) {
static async recommendShipDate(params: SmartRateParams): Promise<unknown> {
const url = 'smartrate/deliver_on';

try {
Expand Down