Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 31, 2025

API Redesign - v2.0.0 Pure Chainable API

⚠️ BREAKING CHANGES - Backward Compatibility Removed

This PR implements a complete API redesign with only the new chainable interface. All old functional APIs have been removed.

New Chainable API (Only API)

Single default export:

import jsonmod from "json-codemod";

const result = jsonmod(source)
  .replace("name", '"Bob"')
  .delete("age")
  .insert("items", 0, '"newItem"')
  .apply();

Benefits:

  • ✨ Fluent, chainable interface
  • 🎯 Single API - no confusion
  • 📝 Self-documenting code
  • 🔄 Sequential execution
  • 💪 Full TypeScript support
  • 📦 Smaller bundle size

What's Exported

Default export:

  • jsonmod - Chainable API factory function

Named exports:

  • jsonmod - Chainable API factory function
  • JsonMod - The class (for TypeScript)
  • formatValue - Value helper function

What Was Removed

Removed old functional APIs:

  • replace() - Use jsonmod().replace().apply()
  • remove() - Use jsonmod().delete().apply()
  • insert() - Use jsonmod().insert().apply()
  • batch() - Use chainable methods
  • All old API types and patches

Removed code:

  • Entire src/function/ directory (8 files)
  • Old API tests (65 tests)
  • Old API documentation (600+ lines)

Chainable Methods

  • .replace(path, value) - Replace a value
  • .delete(path) / .remove(path) - Delete a property/element
  • .insert(path, keyOrPosition, value) - Insert into object/array
  • .apply() - Execute all operations and return result

Value Helper

formatValue() - Automatically formats JavaScript values to JSON strings:

import jsonmod, { formatValue } from "json-codemod";

jsonmod(source)
  .replace("name", formatValue("Bob"))    // Auto-quotes strings
  .replace("age", formatValue(31))        // Numbers stay numeric
  .replace("active", formatValue(true))   // Booleans work
  .apply();

Testing

  • ✅ 50 tests passing (all chainable API)
  • ✅ ESM build: 42.5 KB
  • ✅ CJS build: 44.1 KB
  • ✅ Full TypeScript support
  • ✅ All error cases tested

Documentation

Complete documentation rewritten for chainable API only:

  • README.md - Completely rewritten (300 lines, focused)
  • CHAINABLE_API.md - Complete guide to chainable API
  • All old API references removed

Migration from v1.x

This is a major breaking change. All code must migrate to the chainable API:

// v1.x - NO LONGER WORKS ❌
import { replace, remove, insert, batch } from "json-codemod";
const result = batch(source, [
  { operation: "replace", path: "a", value: "1" },
  { operation: "delete", path: "b" }
]);

// v2.0 - REQUIRED ✅
import jsonmod from "json-codemod";
const result = jsonmod(source)
  .replace("a", "1")
  .delete("b")
  .apply();

Code Reduction

  • Deleted ~774 lines of old API code
  • Removed 8 source files
  • Single, focused API surface
  • Cleaner, more maintainable codebase
Original prompt

请分析对外暴露的 API 有哪些不合理之处,并尝试进行改进


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits December 31, 2025 06:28
Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Copilot AI changed the title [WIP] Analyze and improve exposure of public APIs BREAKING: Require explicit operation types in batch() - v2.0.0 Dec 31, 2025
Copilot AI requested a review from axetroy December 31, 2025 06:47
@axetroy
Copy link
Owner

axetroy commented Dec 31, 2025

@copilot Could you combine formatValue and several other functions into a single function export? It seems to be redundant.

Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 31, 2025

@copilot Could you combine formatValue and several other functions into a single function export? It seems to be redundant.

Done! Simplified to keep only formatValue() which handles all types. Removed the redundant type-specific functions (string, number, boolean, nullValue, object, array).

Changes in commit 9aa665e:

  • Single helper function: formatValue(value)
  • Works with all JavaScript types
  • Updated all tests and documentation
  • 88 tests passing

@axetroy
Copy link
Owner

axetroy commented Dec 31, 2025

@copilot fix the test

Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 31, 2025

@copilot fix the test

Fixed! The dist tests were failing because t.assert.snapshot is not available in Node.js v20.19.6.

Changed to use regex assertions instead. All 90 tests now pass.

Commit 2a1aaee:

  • Replaced snapshot assertions with assert.match()
  • Tests now verify all expected exports including formatValue
  • Updated snapshot file to include new formatValue export

@axetroy
Copy link
Owner

axetroy commented Dec 31, 2025

@copilot Please redesign the API.

To use the class, you can chain calls, for example, jsonmod(source).replace(xxx, xxx).insert(xxx, xx)

Finally, call .apply() or a similar method to apply the changes. This way, you only need to export one jsonmod method.

Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 31, 2025

@copilot Please redesign the API.

To use the class, you can chain calls, for example, jsonmod(source).replace(xxx, xxx).insert(xxx, xx)
...

Done! Implemented a complete API redesign with the chainable interface you requested.

New API (commit b2c519a):

import jsonmod from "json-codemod";

jsonmod(source)
  .replace("user.name", '"Bob"')
  .delete("user.age")
  .insert("items", 0, '"newItem"')
  .apply();

Key features:

  • ✨ Single jsonmod() function export as default
  • 🔗 Chainable methods: .replace(), .delete(), .insert()
  • 🎯 Call .apply() to execute all operations
  • 💪 Full TypeScript support with JsonMod class
  • ✅ 115 tests passing (25 new tests)

Backward compatibility:
All old APIs (replace, remove, insert, batch, formatValue) still work as named exports.

See CHAINABLE_API.md for complete documentation.

Copilot AI changed the title BREAKING: Require explicit operation types in batch() - v2.0.0 BREAKING: New Chainable API with jsonmod() - v2.0.0 Dec 31, 2025
@axetroy
Copy link
Owner

axetroy commented Dec 31, 2025

@copilot We don't need the Backward compatibility, keep improve it.

Co-authored-by: axetroy <9758711+axetroy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 31, 2025

@copilot We don't need the Backward compatibility, keep improve it.

Done! Removed all backward compatibility. The library now exports only the new chainable API.

Changes (commit c057e16):

Exports simplified:

  • Default: jsonmod (chainable API)
  • Named: jsonmod, JsonMod, formatValue

Removed:

  • Old functional API (replace, remove, insert, batch)
  • All old API types and tests
  • Entire src/function/ directory (8 files deleted)
  • Old API documentation

Benefits:

  • Cleaner codebase (-774 lines)
  • Single, clear API
  • Smaller bundle (42.5 KB ESM, 44.1 KB CJS)
  • 50 tests passing

New usage:

import jsonmod from "json-codemod";

jsonmod(source)
  .replace("name", '"Bob"')
  .delete("age")
  .apply();

README completely rewritten to focus only on chainable API.

Copilot AI changed the title BREAKING: New Chainable API with jsonmod() - v2.0.0 BREAKING: Pure Chainable API - Remove All Old APIs - v2.0.0 Dec 31, 2025
@axetroy axetroy marked this pull request as ready for review December 31, 2025 08:57
@axetroy axetroy merged commit a0a742c into master Dec 31, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants