Js adapter pattern

From Intense-Visions/harness-engineering · View interactive version

Js adapter pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Convert the interface of a class into another interface that clients expect

How to use this skill

Skill file

JS Adapter Pattern

Convert the interface of a class into another interface that clients expect

When to Use

Instructions

  1. Identify the incompatible interface — e.g., a legacy API returning XML when your code expects JSON.
  2. Create an adapter class or function that wraps the incompatible object.
  3. The adapter translates calls from the expected interface to the wrapped object's interface.
  4. The client code interacts only with the adapter, never with the adapted object directly.
// Legacy API returns { firstName, lastName }
class LegacyUser {
  constructor(data) {
    this.firstName = data.firstName;
    this.lastName = data.lastName;
  }
}

// Modern code expects { name, email }
class UserAdapter {
  constructor(legacyUser) {
    this.legacyUser = legacyUser;
  }
  get name() {
    return `${this.legacyUser.firstName} ${this.legacyUser.lastName}`;
  }
  get email() {
    return '';
  } // field not available in legacy
}
  1. For functional adapters, write a transform function: const adapt = (legacy) => ({ name: legacy.firstName + ' ' + legacy.lastName }).

Details

The Adapter pattern (also called Wrapper) lets classes with incompatible interfaces work together. In JavaScript, this often takes the form of a thin translation layer between an external API and your internal types.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/adapter-pattern

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.