Skip to content

Commit

Permalink
chore: deprecate the ctMock.add() call
Browse files Browse the repository at this point in the history
The `ctMock.add()` function directly insert objects in the in-memory
store of commercetools mock, which could result in invalid objects being
processed
  • Loading branch information
mvantellingen committed Jan 18, 2025
1 parent 388859f commit 2774582
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-scissors-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Add deprecation warning for `ctMock.add()`
12 changes: 12 additions & 0 deletions src/projectAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ export class ProjectAPI {
add<T extends keyof RepositoryMap & keyof ResourceMap>(
typeId: T,
resource: ResourceMap[T],
) {
process.emitWarning(
"ctMock.add() is deprecated, create resources via regular create endpoints " +
"or if you are really sure, use unsafeAdd() (but be aware of potential state issues)",
"DeprecationWarning",
);
this.unsafeAdd(typeId, resource);
}

unsafeAdd<T extends keyof RepositoryMap & keyof ResourceMap>(
typeId: T,
resource: ResourceMap[T],
) {
const repository = this._repositories[typeId];
if (repository) {
Expand Down

0 comments on commit 2774582

Please sign in to comment.