|
1
|
# TSDoc: Service Interfaces, JS SDK, Providers, and Workflow SDK |
|
2
|
|
|
3
|
This reference covers four file types that all use the same **full JSDoc** pattern: |
|
4
|
- Service interfaces in `packages/core/types/src/` |
|
5
|
- JS SDK methods in `packages/core/js-sdk/src/` |
|
6
|
- Abstract providers in `packages/core/utils/src/` |
|
7
|
- Workflow SDK composer functions in `packages/core/workflows-sdk/src/utils/composer/` |
|
8
|
|
|
9
|
## Rules |
|
10
|
|
|
11
|
- Document the interface/class itself with a 1-sentence description |
|
12
|
- Document every public method with: description, `@param`, `@returns`, `@example` |
|
13
|
- For JS SDK methods, also add `@tags <category>` matching the SDK module (e.g. `@tags products`) |
|
14
|
- Method description starts with "This method [verb phrase]." |
|
15
|
- `@param` format: `@param {Type} name - Description.` |
|
16
|
- `@returns` format: `@returns {Promise<Type>} Description of resolved value.` |
|
17
|
- `@example` shows realistic usage code |
|
18
|
|
|
19
|
## Interface/Class Description |
|
20
|
|
|
21
|
```typescript |
|
22
|
/** |
|
23
|
* The main service interface for the Product Module. |
|
24
|
*/ |
|
25
|
export interface IProductModuleService extends IModuleService { |
|
26
|
``` |
|
27
|
|
|
28
|
```typescript |
|
29
|
/** |
|
30
|
* An abstract class for payment providers. Extend this class to create a payment provider. |
|
31
|
*/ |
|
32
|
export abstract class AbstractPaymentProvider { |
|
33
|
``` |
|
34
|
|
|
35
|
## Full Method Pattern |
|
36
|
|
|
37
|
```typescript |
|
38
|
/** |
|
39
|
* This method retrieves a product by its ID. |
|
40
|
* |
|
41
|
* @param {string} productId - The ID of the product to retrieve. |
|
42
|
* @param {FindConfig<ProductDTO>} config - The configurations determining how the product is retrieved. Its properties, such as `select` or `relations`, accept the attributes or relations associated with a product. |
|
43
|
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. |
|
44
|
* @returns {Promise<ProductDTO>} The retrieved product. |
|
45
|
* |
|
46
|
* @example |
|
47
|
* const product = await productModuleService.retrieveProduct("prod_123") |
|
48
|
*/ |
|
49
|
retrieveProduct( |
|
50
|
productId: string, |
|
51
|
config?: FindConfig<ProductDTO>, |
|
52
|
sharedContext?: Context |
|
53
|
): Promise<ProductDTO> |
|
54
|
``` |
|
55
|
|
|
56
|
## List Methods |
|
57
|
|
|
58
|
```typescript |
|
59
|
/** |
|
60
|
* This method retrieves a paginated list of products based on optional filters and configuration. |
|
61
|
* |
|
62
|
* @param {FilterableProductProps} filters - The filters to apply on the retrieved products. |
|
63
|
* @param {FindConfig<ProductDTO>} config - The configurations determining how the products are retrieved. Its properties, such as `select` or `relations`, accept the attributes or relations associated with a product. |
|
64
|
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. |
|
65
|
* @returns {Promise<ProductDTO[]>} The list of products. |
|
66
|
* |
|
67
|
* @example |
|
68
|
* To retrieve a list of products using their IDs: |
|
69
|
* |
|
70
|
* ```ts |
|
71
|
* const products = await productModuleService.listProducts({ |
|
72
|
* id: ["prod_123", "prod_456"], |
|
73
|
* }) |
|
74
|
* ``` |
|
75
|
* |
|
76
|
* To specify relations that should be retrieved: |
|
77
|
* |
|
78
|
* ```ts |
|
79
|
* const products = await productModuleService.listProducts( |
|
80
|
* {}, |
|
81
|
* { relations: ["variants"] } |
|
82
|
* ) |
|
83
|
* ``` |
|
84
|
*/ |
|
85
|
listProducts( |
|
86
|
filters?: FilterableProductProps, |
|
87
|
config?: FindConfig<ProductDTO>, |
|
88
|
sharedContext?: Context |
|
89
|
): Promise<ProductDTO[]> |
|
90
|
``` |
|
91
|
|
|
92
|
## Create/Update Methods |
|
93
|
|
|
94
|
```typescript |
|
95
|
/** |
|
96
|
* This method creates products. |
|
97
|
* |
|
98
|
* @param {CreateProductDTO[]} data - The products to create. |
|
99
|
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. |
|
100
|
* @returns {Promise<ProductDTO[]>} The created products. |
|
101
|
* |
|
102
|
* @example |
|
103
|
* const products = await productModuleService.createProducts([ |
|
104
|
* { |
|
105
|
* title: "Shirt", |
|
106
|
* options: [{ title: "Color", values: ["Blue", "Green"] }], |
|
107
|
* }, |
|
108
|
* ]) |
|
109
|
*/ |
|
110
|
createProducts( |
|
111
|
data: CreateProductDTO[], |
|
112
|
sharedContext?: Context |
|
113
|
): Promise<ProductDTO[]> |
|
114
|
``` |
|
115
|
|
|
116
|
## JS SDK Methods — add @tags |
|
117
|
|
|
118
|
JS SDK methods should include `@tags` matching the parent SDK property's tag pattern: |
|
119
|
|
|
120
|
```typescript |
|
121
|
/** |
|
122
|
* This method retrieves a product by its ID. |
|
123
|
* |
|
124
|
* @param {string} id - The ID of the product to retrieve. |
|
125
|
* @param {SelectParams} query - Configure the fields to retrieve in the product. |
|
126
|
* @param {RequestInit} headers - Headers to pass in the request. |
|
127
|
* @returns {Promise<HttpTypes.AdminProductResponse>} The product's details. |
|
128
|
* |
|
129
|
* @example |
|
130
|
* const product = await sdk.admin.product.retrieve("prod_123") |
|
131
|
* |
|
132
|
* @tags products |
|
133
|
*/ |
|
134
|
async retrieve( |
|
135
|
id: string, |
|
136
|
query?: SelectParams, |
|
137
|
headers?: RequestInit["headers"] |
|
138
|
): Promise<HttpTypes.AdminProductResponse> { |
|
139
|
``` |
|
140
|
|
|
141
|
## Abstract Provider Methods |
|
142
|
|
|
143
|
```typescript |
|
144
|
/** |
|
145
|
* This method initiates a payment session. |
|
146
|
* |
|
147
|
* @param {InitiatePaymentInput} input - The details of the payment session to initiate. |
|
148
|
* @returns {Promise<PaymentProviderSessionResponse>} The created payment session. |
|
149
|
* |
|
150
|
* @example |
|
151
|
* class MyPaymentProvider extends AbstractPaymentProvider { |
|
152
|
* async initiatePayment(input) { |
|
153
|
* const externalSession = await this.client.createSession(input.amount) |
|
154
|
* return { |
|
155
|
* id: externalSession.id, |
|
156
|
* data: externalSession, |
|
157
|
* } |
|
158
|
* } |
|
159
|
* } |
|
160
|
*/ |
|
161
|
abstract initiatePayment( |
|
162
|
input: InitiatePaymentInput |
|
163
|
): Promise<PaymentProviderSessionResponse> |
|
164
|
``` |
|
165
|
|
|
166
|
## Workflow SDK Composer Functions |
|
167
|
|
|
168
|
```typescript |
|
169
|
/** |
|
170
|
* Creates a new workflow step with the specified ID and handler. |
|
171
|
* |
|
172
|
* @param {string} stepId - The unique identifier for the step. |
|
173
|
* @param {Function} invokeFn - The function to run when the step executes. |
|
174
|
* @param {Function} [compensateFn] - The function to run when compensating (rolling back) the step. |
|
175
|
* @returns {StepFunction} The created step function. |
|
176
|
* |
|
177
|
* @example |
|
178
|
* const myStep = createStep( |
|
179
|
* "my-step", |
|
180
|
* async (input: { id: string }, { container }) => { |
|
181
|
* const service = container.resolve("myService") |
|
182
|
* await service.doSomething(input.id) |
|
183
|
* return new StepResponse(void 0, input.id) |
|
184
|
* }, |
|
185
|
* async (id, { container }) => { |
|
186
|
* const service = container.resolve("myService") |
|
187
|
* await service.undoSomething(id) |
|
188
|
* } |
|
189
|
* ) |
|
190
|
*/ |
|
191
|
export function createStep<TInput, TOutput, TCompensateInput>( |
|
192
|
stepId: string, |
|
193
|
invokeFn: StepFunction<TInput, TOutput>, |
|
194
|
compensateFn?: StepFunction<TCompensateInput, void> |
|
195
|
): ReturnType<typeof createStep> |
|
196
|
``` |
|
197
|
|
|
198
|
## @example with Multiple Scenarios |
|
199
|
|
|
200
|
When a method has multiple common use cases, show them with labels: |
|
201
|
|
|
202
|
```typescript |
|
203
|
* @example |
|
204
|
* A simple example that retrieves a product by its ID: |
|
205
|
* |
|
206
|
* ```ts |
|
207
|
* const product = await productModuleService.retrieveProduct("prod_123") |
|
208
|
* ``` |
|
209
|
* |
|
210
|
* To specify relations that should be retrieved: |
|
211
|
* |
|
212
|
* ```ts |
|
213
|
* const product = await productModuleService.retrieveProduct( |
|
214
|
* "prod_123", |
|
215
|
* { relations: ["variants"] } |
|
216
|
* ) |
|
217
|
* ``` |
|
218
|
``` |
|
219
|
|
|
220
|
## sharedContext Parameter |
|
221
|
|
|
222
|
The `sharedContext` parameter appears on virtually all module service methods. Use this standard description: |
|
223
|
|
|
224
|
``` |
|
225
|
@param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. |
|
226
|
``` |