Skip to content

Commit bdeb160

Browse files
committed
feat(js-api-client): bring more subscription contracts stuff
1 parent 71d1b89 commit bdeb160

File tree

9 files changed

+435
-14
lines changed

9 files changed

+435
-14
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ So far, the available helpers are:
1010
- Mass Call Client that relies on the Client for mass operations
1111
- Catalogue Fetcher
1212
- Searcher
13-
- Order Payment Updater
14-
- Order Pusher
13+
- Order
14+
- Payment Updater
15+
- Pusher
16+
- Pipeline Stage Setter
1517
- Product Hydrater
1618
- Paths
1719
- Skus
@@ -399,6 +401,15 @@ const result = await caller('xXxYyYZzZ', {
399401
});
400402
```
401403

404+
## Order Pipeline Stage Setter
405+
406+
You can use the *CrystallizeCreateOrderPipelineStageSetter* to put an order into a specific pipeline stage.
407+
408+
```javascript
409+
const caller = CrystallizeCreateOrderPipelineStageSetter;
410+
const result = await caller(orderId, pipelineId, stageId);
411+
```
412+
402413
## Searcher
403414

404415
You can use the *CrystallizeSearcher* to search through the Search API in a more sophisticated way.
@@ -525,6 +536,13 @@ An Update method exists as well:
525536
await CrystallizeSubscriptionContractManager.update(contractId, cleanUpdateContract);
526537
```
527538

539+
There is also 3 other helpers that you will most likely use and that work the same as Order Fetcher:
540+
541+
- `CrystallizeSubscriptionContractManager.fetchById`,
542+
- `CrystallizeSubscriptionContractManager.fetchByCustomerIdentifier`,
543+
- `CrystallizeSubscriptionContractManager.getCurrentPhase`,
544+
- `CrystallizeSubscriptionContractManager.getUsageForPeriod`
545+
528546
## Signature Verification
529547

530548
The full documentation is here https://crystallize.com/learn/developer-guides/api-overview/signature-verification

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/js-api-client",
33
"license": "MIT",
4-
"version": "3.3.0",
4+
"version": "4.0.0",
55
"type": "module",
66
"author": "Crystallize <[email protected]> (https://crystallize.com)",
77
"contributors": [
@@ -29,17 +29,17 @@
2929
"module": "./dist/index.mjs",
3030
"devDependencies": {
3131
"@tsconfig/node20": "^20.1.4",
32-
"@types/node": "^20.17.6",
32+
"@types/node": "^22.10.5",
3333
"tsup": "^8.3.5",
34-
"typescript": "^5.6.3",
35-
"vitest": "^1.6.0"
34+
"typescript": "^5.7.2",
35+
"vitest": "^2.1.8"
3636
},
3737
"dependencies": {
38-
"dotenv": "^16.4.5",
38+
"dotenv": "^16.4.7",
3939
"json-to-graphql-query": "^2.3.0",
4040
"mime-lite": "^1.0.3",
4141
"tiny-invariant": "^1.3.3",
42-
"zod": "^3.23.8"
42+
"zod": "^3.24.1"
4343
},
4444
"browser": {
4545
"fs": false,

src/core/client.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type ApiCaller<T> = (query: string, variables?: VariablesType) => Promise
5353

5454
export type ClientInterface = {
5555
catalogueApi: ApiCaller<any>;
56+
discoveryApi: ApiCaller<any>;
5657
searchApi: ApiCaller<any>;
5758
orderApi: ApiCaller<any>;
5859
subscriptionApi: ApiCaller<any>;
@@ -360,6 +361,12 @@ export function createClient(configuration: ClientConfiguration, options?: Creat
360361
accessTokenSecret: configuration.accessTokenSecret,
361362
};
362363

364+
// nothing expect static auth token
365+
const discoveryConfig: ClientConfiguration = {
366+
...commonConfig,
367+
staticAuthToken: configuration.staticAuthToken,
368+
};
369+
363370
// sessionId and static auth token are excluded
364371
const tokenOnlyConfig: ClientConfiguration = {
365372
...commonConfig,
@@ -369,6 +376,12 @@ export function createClient(configuration: ClientConfiguration, options?: Creat
369376

370377
return {
371378
catalogueApi: createApiCaller(grab, apiHost(configuration)([identifier, 'catalogue']), catalogConfig, options),
379+
discoveryApi: createApiCaller(
380+
grab,
381+
apiHost(configuration)([identifier, 'discovery']),
382+
discoveryConfig,
383+
options,
384+
),
372385
searchApi: createApiCaller(grab, apiHost(configuration)([identifier, 'search']), catalogConfig, options),
373386
orderApi: createApiCaller(grab, apiHost(configuration)([identifier, 'orders']), tokenOnlyConfig, options),
374387
subscriptionApi: createApiCaller(

src/core/massCallClient.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type MassClientInterface = ClientInterface & {
2727
nextPimApi: ApiCaller<any>;
2828
enqueue: {
2929
catalogueApi: QueuedApiCaller;
30+
discoveryApi: QueuedApiCaller;
3031
searchApi: QueuedApiCaller;
3132
orderApi: QueuedApiCaller;
3233
subscriptionApi: QueuedApiCaller;
@@ -187,6 +188,7 @@ export function createMassCallClient(
187188
return await execute();
188189
},
189190
catalogueApi: client.catalogueApi,
191+
discoveryApi: client.discoveryApi,
190192
searchApi: client.searchApi,
191193
orderApi: client.orderApi,
192194
subscriptionApi: client.subscriptionApi,
@@ -201,6 +203,11 @@ export function createMassCallClient(
201203
promises.push({ key, caller: client.catalogueApi, query, variables });
202204
return key;
203205
},
206+
discoveryApi: (query: string, variables?: VariablesType): string => {
207+
const key = `discoveryApi-${counter++}`;
208+
promises.push({ key, caller: client.discoveryApi, query, variables });
209+
return key;
210+
},
204211
searchApi: (query: string, variables?: VariablesType): string => {
205212
const key = `searchApi-${counter++}`;
206213
promises.push({ key, caller: client.searchApi, query, variables });

src/core/order.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ function convertDates(intent: CreateOrderInputRequest | UpdateOrderInputRequest)
180180
}),
181181
};
182182
}
183+
export function createOrderPipelineStageSetter(apiClient: ClientInterface) {
184+
return async function putInPipelineStage(id: string, pipelineId: string, stageId: string) {
185+
const mutation = {
186+
order: {
187+
setPipelineStage: {
188+
__args: {
189+
orderId: id,
190+
pipelineId: pipelineId,
191+
stageId: stageId,
192+
},
193+
id: true,
194+
},
195+
},
196+
};
197+
await apiClient.pimApi(jsonToGraphQLQuery({ mutation }));
198+
};
199+
}
183200

184201
export function createOrderPusher(apiClient: ClientInterface) {
185202
return async function pushOrder(intentOrder: CreateOrderInputRequest): Promise<OrderCreatedConfirmation> {

0 commit comments

Comments
 (0)