Skip to content

Commit

Permalink
Merge pull request #236 from CrystallizeAPI/feature/shop-api-functions
Browse files Browse the repository at this point in the history
Feature/shop api functions
  • Loading branch information
Plopix authored Jul 1, 2024
2 parents d5c9e05 + d61c1ee commit a123c34
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/js-api-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@crystallize/js-api-client",
"license": "MIT",
"version": "2.5.0",
"version": "2.6.0",
"author": "Crystallize <hello@crystallize.com> (https://crystallize.com)",
"contributors": [
"Sébastien Morel <sebastien@crystallize.com>",
Expand Down
116 changes: 116 additions & 0 deletions components/js-api-client/src/core/editCart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
import { ClientInterface } from './client';
import { Customer } from '../types/customer';

type Deps = {
apiClient: ClientInterface;
};

export const placeCart = async (cartId: string, { apiClient }: Deps, extraQuery?: any): Promise<void> => {
const mutation = {
place: {
__args: {
id: cartId,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.place;
};

export const addSkuItem = async (
cartId: string,
sku: string,
quantity: number,
{ apiClient }: Deps,
extraQuery?: any,
) => {
const mutation = {
addSkuItem: {
__args: {
id: cartId,
input: {
sku,
quantity,
},
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.addSkuItem;
};

export const removeCartItem = async (
cartId: string,
sku: string,
quantity: number,
{ apiClient }: Deps,
extraQuery?: any,
) => {
const mutation = {
removeCartItem: {
__args: {
id: cartId,
sku,
quantity,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.removeCartItem;
};

export const setCartMeta = async (
cartId: string,
meta: Array<{
key: string;
value: string;
}>,
merge: Boolean,
{ apiClient }: Deps,
extraQuery?: any,
): Promise<void> => {
const mutation = {
setMeta: {
__args: {
id: cartId,
merge,
meta,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.setMeta;
};

export const setCartCustomer = async (
cartId: string,
customer: Customer,
isGuest: boolean,
{ apiClient }: Deps,
extraQuery?: any,
): Promise<void> => {
const mutation = {
setCustomer: {
__args: {
id: cartId,
input: {
isGuest,
...customer,
},
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.setCustomer;
};
1 change: 1 addition & 0 deletions components/js-api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './types/customer';
export * from './types/signature';
export * from './types/pricing';
export * from './core/uploadImage';
export * from './core/editCart';

import { createClient } from './core/client';
import { createNavigationFetcher } from './core/navigation';
Expand Down
3 changes: 2 additions & 1 deletion components/js-api-client/src/types/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export const updateCustomerInputRequest = createCustomerInputRequest.omit({ iden
export type UpdateCustomerInputRequest = z.infer<typeof updateCustomerInputRequest>;

export type Customer = Omit<CreateCustomerInputRequest, 'addresses'> & {
addresses: Address[];
//setting addresses as optional to allow partial updates
addresses?: Address[];
};

0 comments on commit a123c34

Please sign in to comment.