Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/shop api functions #236

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]> (https://crystallize.com)",
"contributors": [
"Sébastien Morel <[email protected]>",
Expand Down
136 changes: 136 additions & 0 deletions components/js-api-client/src/core/editCart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove the try catch here, or we can log and throw the exception again, but we should not catch like that. it would hide the error for consumers

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> => {
try {
const mutation = {
place: {
__args: {
id: cartId,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.place;
} catch (exception: any) {
console.error(`Failed to place cart ${cartId}`, exception.message);
}
};

export const addSkuItem = async (
cartId: string,
sku: string,
quantity: number,
{ apiClient }: Deps,
extraQuery?: any,
) => {
try {
const mutation = {
addSkuItem: {
__args: {
id: cartId,
input: {
sku,
quantity,
},
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.addSkuItem;
} catch (exception: any) {
console.error(`Failed to add sku item for cart ${cartId}`, exception.message);
}
};

export const removeCartItem = async (
cartId: string,
sku: string,
quantity: number,
{ apiClient }: Deps,
extraQuery?: any,
) => {
try {
const mutation = {
removeCartItem: {
__args: {
id: cartId,
sku,
quantity,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.removeCartItem;
} catch (exception: any) {
console.error(`Failed to remove from cart ${cartId}`, exception.message);
}
};

export const setCartMeta = async (
cartId: string,
meta: Array<{
key: string;
value: string;
}>,
merge: Boolean,
{ apiClient }: Deps,
extraQuery?: any,
): Promise<void> => {
try {
const mutation = {
setMeta: {
__args: {
id: cartId,
merge,
meta,
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.setMeta;
} catch (exception: any) {
console.error(`Failed to set meta for ${cartId}`, exception.message);
}
};

export const setCartCustomer = async (
cartId: string,
customer: Customer,
isGuest: boolean,
{ apiClient }: Deps,
extraQuery?: any,
): Promise<void> => {
try {
const mutation = {
setCustomer: {
__args: {
id: cartId,
input: {
isGuest,
...customer,
},
},
id: true,
...extraQuery,
},
};
const response = await apiClient.shopCartApi(jsonToGraphQLQuery({ mutation }));
return response.setCustomer;
} catch (exception: any) {
console.error('Failed to update customer', exception.message);
}
};
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[];
};
Loading