Skip to content
Open
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
25 changes: 17 additions & 8 deletions src/services/batch_service.js → src/services/batch_service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import baseService from './base_service';
import Batch from '../models/batch';

export const DEFAULT_LABEL_FORMAT = 'pdf';

type BatchCreateParameters = Record<string, unknown> & {
shipments?: Array<string | Record<string, unknown>> | null;
};
type BatchListResponse = { batches: Batch[]; has_more: boolean };

export default (easypostClient) =>
/**
* The BatchService class provides methods for interacting with EasyPost {@link Batch} objects.
Expand All @@ -14,7 +20,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: BatchCreateParameters): Promise<Batch> {
const url = 'batches';

const wrappedParams = {
Expand All @@ -31,7 +37,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<Batch> {
const url = `batches/${id}/add_shipments`;
const wrappedParams = {
shipments: shipmentIds.map((s) => ({ id: s })),
Expand All @@ -52,7 +58,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<Batch> {
const url = `batches/${id}/remove_shipments`;
const wrappedParams = {
shipments: shipmentIds.map((s) => ({ id: s })),
Expand All @@ -74,7 +80,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<Batch> {
const url = `batches/${id}/label`;
const wrappedParams = { file_format: fileFormat };

Expand All @@ -93,7 +102,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<Batch> {
const url = `batches/${id}/scan_form`;

try {
Expand All @@ -111,7 +120,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<Batch> {
const url = `batches/${id}/buy`;

try {
Expand All @@ -129,7 +138,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<BatchListResponse> {
const url = 'batches';

return this._all(url, params);
Expand All @@ -141,7 +150,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<Batch> {
const url = `batches/${id}`;

return this._retrieve(url);
Expand Down
File renamed without changes.
19 changes: 15 additions & 4 deletions src/services/order_service.js → src/services/order_service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import baseService from './base_service';
import Order from '../models/order';
import Rate from '../models/rate';

type OrderCreateParameters = Record<string, unknown> & {
reference?: string | null;
to_address?: Record<string, unknown> | string | null;
from_address?: Record<string, unknown> | string | null;
shipments?: Array<Record<string, unknown> | string> | null;
carrier_accounts?: string[] | null;
};
type OrderRatesResponse = { rates: Rate[] };

export default (easypostClient) =>
/**
Expand All @@ -12,7 +23,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: OrderCreateParameters): Promise<Order> {
const url = 'orders';

const wrappedParams = {
Expand All @@ -30,7 +41,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<Order> {
const url = `orders/${id}/buy`;
const wrappedParams = { carrier, service };
try {
Expand All @@ -48,7 +59,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<OrderRatesResponse> {
const url = `orders/${id}/rates`;

try {
Expand All @@ -66,7 +77,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<Order> {
const url = `orders/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import baseService from './base_service';
import Pickup from '../models/pickup';

type PickupCreateParameters = Record<string, unknown> & {
address?: Record<string, unknown> | string | null;
carrier_accounts?: Array<Record<string, unknown> | string> | null;
confirmation?: string | null;
instructions?: string | null;
is_account_address?: boolean | null;
max_datetime?: string | null;
min_datetime?: string | null;
pickup_rates?: Record<string, unknown> | null;
reference?: string | null;
status?: string | null;
shipment?: Record<string, unknown> | string | null;
batch?: Record<string, unknown> | string | null;
};
type PickupCollection = Record<string, unknown>;
type PickupListResponse = { pickups: Pickup[]; has_more: boolean };

export default (easypostClient) =>
/**
Expand All @@ -12,7 +30,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: PickupCreateParameters): Promise<Pickup> {
const url = 'pickups';

const wrappedParams = {
Expand All @@ -30,7 +48,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<Pickup> {
const url = `pickups/${id}/buy`;
const wrappedParams = { carrier, service };
try {
Expand All @@ -48,7 +66,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<Pickup> {
const url = `pickups/${id}/cancel`;
try {
const response = await easypostClient._post(url);
Expand All @@ -65,7 +83,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<PickupListResponse> {
const url = 'pickups';

return this._all(url, params);
Expand All @@ -77,7 +95,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<PickupListResponse> {
const url = 'pickups';
return this._getNextPage(url, 'pickups', pickups, pageSize);
}
Expand All @@ -88,7 +109,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<Pickup> {
const url = `pickups/${id}`;

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

export default (easypostClient) =>
/**
Expand All @@ -12,7 +13,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<Rate> {
const url = `rates/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import baseService from './base_service';
import Refund from '../models/refund';

type RefundCreateParameters = Record<string, unknown> & {
carrier?: string | null;
tracking_codes?: string[] | null;
};
type RefundCollection = Record<string, unknown>;
type RefundListResponse = { refunds: Refund[]; has_more: boolean };

export default (easypostClient) =>
/**
Expand All @@ -12,7 +20,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: RefundCreateParameters): Promise<Refund[]> {
const url = 'refunds';

const wrappedParams = {
Expand All @@ -28,7 +36,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<RefundListResponse> {
const url = 'refunds';

return this._all(url, params);
Expand All @@ -40,7 +48,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<RefundListResponse> {
const url = 'refunds';
return this._getNextPage(url, 'refunds', refunds, pageSize);
}
Expand All @@ -51,7 +62,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<Refund> {
const url = `refunds/${id}`;

return this._retrieve(url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import baseService from './base_service';
import Report from '../models/report';

type ReportCreateParameters = Record<string, unknown> & {
type: string;
};

type ReportAllParameters = Record<string, unknown> & {
type?: string;
};

type ReportCollection = Record<string, unknown> & {
reports?: Array<Record<string, any>>;
};
type ReportListResponse = { reports: Report[]; has_more: boolean };

export default (easypostClient) =>
/**
Expand All @@ -12,7 +26,7 @@ export default (easypostClient) =>
* @param {Object} params - The parameters to create a report with.
* @returns {Report} - The created report.
*/
static async create(params) {
static async create(params: ReportCreateParameters): Promise<Report> {
const url = `reports/${params.type}`;
return this._create(url, params);
}
Expand All @@ -23,8 +37,8 @@ export default (easypostClient) =>
* @param {Object} [params] - The parameters to filter the reports by.
* @returns {Object} - An object containing the list of {@link Report reports} and pagination information.
*/
static async all(params = {}) {
const type = params.type;
static async all(params: ReportAllParameters): Promise<ReportListResponse> {
const type = params.type || '';
const url = `reports/${type}`;

// delete "type" from params, so it doesn't get sent to the API
Expand All @@ -49,8 +63,11 @@ 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(reports, pageSize = null) {
const url = `reports/${reports.reports[0]._params.type}`;
static async getNextPage(
reports: ReportCollection,
pageSize: number | null = null,
): Promise<ReportListResponse> {
const url = `reports/${reports.reports?.[0]?._params?.type || ''}`;
return this._getNextPage(url, 'reports', reports, pageSize);
}

Expand All @@ -60,7 +77,7 @@ export default (easypostClient) =>
* @param {string} id - The ID of the report to retrieve.
* @returns {Report} - The retrieved report.
*/
static async retrieve(id) {
static async retrieve(id: string): Promise<Report> {
const url = `reports/${id}`;

return this._retrieve(url);
Expand Down
Loading