diff --git a/src/index.ts b/src/index.ts index a3298078..1afc87ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { Leads, Inventory, Agents, + Cart, } from './modules'; import { setContext } from '@apollo/client/link/context'; @@ -76,6 +77,7 @@ export default class KanvasCore { public leads: Leads; public inventory: Inventory; public agents: Agents; + public cart: Cart; constructor(protected options: Options) { this.client = new ApolloClient({ @@ -92,6 +94,7 @@ export default class KanvasCore { this.leads = new Leads(this.client); this.inventory = new Inventory(this.client); this.agents = new Agents(this.client); + this.cart = new Cart(this.client); } protected generateURL() { diff --git a/src/modules/commerce/cart/index.ts b/src/modules/commerce/cart/index.ts new file mode 100644 index 00000000..61c32b4e --- /dev/null +++ b/src/modules/commerce/cart/index.ts @@ -0,0 +1,16 @@ +import { ClientType } from '../../../index'; +import { ADD_TO_CART_MUTATION } from '../../../mutations'; +import { CartItemData, CartItemInput } from '../../../types/commerce'; + +export class Cart { + constructor(protected client: ClientType) {} + + public async addToCart(input: CartItemInput): Promise { + const response = await this.client.mutate({ + mutation: ADD_TO_CART_MUTATION, + variables: { input }, + }); + + return response.data + } +} diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts new file mode 100644 index 00000000..6d1430f1 --- /dev/null +++ b/src/modules/commerce/index.ts @@ -0,0 +1 @@ +export * from "./cart" \ No newline at end of file diff --git a/src/modules/index.ts b/src/modules/index.ts index d4580cfd..31269b9f 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -6,3 +6,4 @@ export * from './locations'; export * from './leads'; export * from './inventory'; export * from './agents'; +export * from "./commerce" \ No newline at end of file diff --git a/src/mutations/commerce.mutation.ts b/src/mutations/commerce.mutation.ts new file mode 100644 index 00000000..ae25e239 --- /dev/null +++ b/src/mutations/commerce.mutation.ts @@ -0,0 +1,12 @@ +import { gql } from '@apollo/client/core'; + +export const ADD_TO_CART_MUTATION = gql` + mutation($input: [CartItemInput!]!) { + addToCart(items: $input) { + id + name + price + quantity + } + } +`; diff --git a/src/mutations/index.ts b/src/mutations/index.ts index 7c391a20..2270bca9 100644 --- a/src/mutations/index.ts +++ b/src/mutations/index.ts @@ -4,5 +4,4 @@ export * from './users.mutation'; export * from './custom-fields.mutation'; export * from './leads.mutation'; export * from './inventory.mutation'; - - +export * from './commerce.mutation'; diff --git a/src/types/commerce.ts b/src/types/commerce.ts new file mode 100644 index 00000000..0417e759 --- /dev/null +++ b/src/types/commerce.ts @@ -0,0 +1,17 @@ +export interface CartItemInput { + input: { + quantity: number; + variant_id: number; + }[]; +} + +export interface CartItemData { + data: { + addToCart: { + id: number; + name: string; + price: number; + quantity: number; + }[]; + }; +}