You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Refactoring a tool Function in @commercetools/agent-essentials
3
-
globs:
2
+
description: Refactoring a tool Function in @commercetools/commerce-agent
3
+
globs:
4
4
alwaysApply: false
5
5
---
6
-
# Refactoring a tool Function in @commercetools/agent-essentials
7
6
8
-
This document outlines the steps taken to refactor a function in @commercetools/agent-essentials to connect to commercetools API.
7
+
# Refactoring a tool Function in @commercetools/commerce-agent
8
+
9
+
This document outlines the steps taken to refactor a function in @commercetools/commerce-agent to connect to commercetools API.
9
10
10
11
The following steps are defining implementation of an example `cart.read`. So the `(namespace)` here refers to `cart`.
11
12
12
-
# NOTES:
13
+
# NOTES:
14
+
13
15
1. CRUD functions are read, update and create. do not add delete functions.
14
16
2. DO NOT MODIFY `parameters.ts` or `prompts.ts` or `tools.ts`
15
17
3. when creating update base functions, use get (by id or key) base functions to first fetch the entity, and use the version from the fetched entity in the update payload
16
18
17
19
# Refactor steps
18
20
19
21
1. Create a `base.functions.ts` file in the `namespace` directory. This file will export a couple of basic CRUD functions that will be used inside other files in this namespace. you can extract this base functions from current `functions.ts` file in the namespace.
20
-
- IMPORTANT: base functions should be generic as possible, e.g. instead of having a separate function for query cart for a specific user and another one for user in a store, we create one that accepts a "where" cluase.
21
-
- Note: sometimes the commercetools SDK has different endpoint when a parameter is present, like query in store (look to the code sample below). in that case, we check that parameter inside the base function but keep the function as simple as possible and low complexity.
22
-
- GOAL: the goal of this step is maximize reusability.
23
-
- example: base functions created for 'cart.read' are: readCartById, readCartByKey, queryCart, queryCarts.
- IMPORTANT: base functions should be generic as possible, e.g. instead of having a separate function for query cart for a specific user and another one for user in a store, we create one that accepts a "where" cluase.
23
+
- Note: sometimes the commercetools SDK has different endpoint when a parameter is present, like query in store (look to the code sample below). in that case, we check that parameter inside the base function but keep the function as simple as possible and low complexity.
24
+
- GOAL: the goal of this step is maximize reusability.
25
+
- example: base functions created for 'cart.read' are: readCartById, readCartByKey, queryCart, queryCarts.
2. Create a `customer.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.customerId` is present.
53
-
- GOAL: these functions are to limit the operations to the specific customerId.
54
-
- example: when querying carts, it should always inject `context.customerId` to the query. If not possible, it should check the entity after it's fetched.
- GOAL: these functions are to limit the operations to the specific customerId.
56
+
- example: when querying carts, it should always inject `context.customerId` to the query. If not possible, it should check the entity after it's fetched.
3. Create a `store.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.storeKey` is present.
58
-
- GOAL: these functions are to limit the operations to the specific store.
59
-
- example: Limit carts fetched to a store.
60
+
- GOAL: these functions are to limit the operations to the specific store.
61
+
- example: Limit carts fetched to a store.
60
62
61
63
4. Create a `admin.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.isAdmin` is present.
62
-
- GOAL: these functions doesn't have any limitations.
5. Modify `functions.ts` to import all exported methods from customer, admin and store. create a method called `contextTo<namespace>FunctionMapping` which accepts the context and returns an object of name to method mapping.
66
-
- IMPORTANT: if no context is there, empty object should return
67
-
- IMPORTANT: use type `Context` from `typescript/src/types/configuration.ts`
6. create a test file to check if correct context is loading right functions from `contextTo<namespace>FunctionMapping` and if none is provided, it should be empty
102
+
6. create a test file to check if correct context is loading right functions from `contextTo<namespace>FunctionMapping` and if none is provided, it should be empty
99
103
7. update current tests to match the function calls
100
-
8. refactor `typescript/src/shared/functions.ts` and import `import {contextToCartFunctionMapping} from './cart/functions';` then update this method return
101
-
9. confirm that this method call `apiRoot.withProjectKey()` is only being called from base functions not in `customer.functions.ts` and not in `customer` and `admin`. if usage of `apiRoot.withProjectKey` found, move the method to base and reused from base functions.
104
+
8. refactor `typescript/src/shared/functions.ts` and import `import {contextToCartFunctionMapping} from './cart/functions';` then update this method return
105
+
9. confirm that this method call `apiRoot.withProjectKey()` is only being called from base functions not in `customer.functions.ts` and not in `customer` and `admin`. if usage of `apiRoot.withProjectKey` found, move the method to base and reused from base functions.
0 commit comments