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
27 changes: 0 additions & 27 deletions src/models/easypost_object.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/models/easypost_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* An EasyPostObject is the base class for all EasyPost API resources.
* @internal
* @abstract
*/
export default class EasyPostObject {
static id: string;
static object: string;
static mode: string;
static created_at: string;
static updated_at: string;
static _params: Record<string, unknown>;

static [Symbol.hasInstance](instance: unknown): boolean {
if (instance == null || typeof instance !== 'object') {
return false;
}

const modelConstructor = (instance as Record<symbol, unknown>)[
Symbol.for('easypost.modelConstructor')
];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion:
You can use a type predicate function here, so that way you don't need to keep casting.

const isSymbolRecord = (value: unknown) value is Record<symbol, unknown> =>
  instance != null && typeof instance === 'object';
// ...
static [Symbol.hasInstance](instance: unknown): boolean {
  if (!isSymbolRecord(instance)) {
    return false;
  }

  const modelConstructor = instance[Symbol.for('easypost.modelConstructor')];

if (typeof modelConstructor === 'function') {
return modelConstructor === this;
}

// Fallback for plain API payloads that include object names like "Address" or "Shipment".
return (
typeof (instance as Record<string, unknown>).object === 'string' &&
(instance as Record<string, unknown>).object === this.name
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default (easypostClient) =>
* @param {*} params The parameters passed when fetching the response.
* @returns {*} A plain object or array suitable for JSON serialization.
*/
static _convertToEasyPostObject(response, params) {
static _convertToEasyPostObject(response, params = {}) {
const modelResponse = this._buildEasyPostObject(response, params);

return this._toPlainEasyPostObject(modelResponse);
Expand Down