From e4c1050880a0a8d8fd8e31fcdc74bc73c8678059 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Mon, 29 Mar 2021 17:05:20 -0600 Subject: [PATCH] update get categories hook --- framework/swell/api/index.ts | 6 ++++- framework/swell/api/utils/fetch-swell-api.ts | 9 +++++++ framework/swell/utils/get-categories.ts | 25 +++++++------------- 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 framework/swell/api/utils/fetch-swell-api.ts diff --git a/framework/swell/api/index.ts b/framework/swell/api/index.ts index fc48116a6..9c41b18ae 100644 --- a/framework/swell/api/index.ts +++ b/framework/swell/api/index.ts @@ -21,8 +21,11 @@ if (!API_TOKEN) { } import fetchGraphqlApi from './utils/fetch-graphql-api' +import fetchSwellApi from './utils/fetch-swell-api' -export interface SwellConfig extends CommerceAPIConfig {} +export interface SwellConfig extends CommerceAPIConfig { + fetchSwell: any +} export class Config { private config: SwellConfig @@ -49,6 +52,7 @@ const config = new Config({ apiToken: API_TOKEN!, cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE, cartCookieMaxAge: SHOPIFY_COOKIE_EXPIRE, + fetchSwell: fetchSwellApi, fetch: fetchGraphqlApi, customerCookie: SHOPIFY_CUSTOMER_TOKEN_COOKIE, }) diff --git a/framework/swell/api/utils/fetch-swell-api.ts b/framework/swell/api/utils/fetch-swell-api.ts new file mode 100644 index 000000000..c4c9472cf --- /dev/null +++ b/framework/swell/api/utils/fetch-swell-api.ts @@ -0,0 +1,9 @@ +import { swellConfig } from '../../index' + +const fetchSwellApi = async (query: string, method: string) => { + const { swell } = swellConfig + const res = await swell[query][method]() + + return res +} +export default fetchSwellApi diff --git a/framework/swell/utils/get-categories.ts b/framework/swell/utils/get-categories.ts index 0f8c3c9d8..5b9ba501e 100644 --- a/framework/swell/utils/get-categories.ts +++ b/framework/swell/utils/get-categories.ts @@ -1,28 +1,19 @@ import { SwellConfig } from '../api' -import { CollectionEdge } from '../schema' -import getSiteCollectionsQuery from './queries/get-all-collections-query' export type Category = { - entityId: string + id: string name: string - path: string + slug: string } const getCategories = async (config: SwellConfig): Promise => { - const { data } = await config.fetch(getSiteCollectionsQuery, { - variables: { - first: 250, - }, - }) - + const data = await config.fetchSwell('categories', 'get') return ( - data.collections?.edges?.map( - ({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({ - entityId, - name, - path: `/${handle}`, - }) - ) ?? [] + data.results.map(({ id: entityId, name, slug }: Category) => ({ + entityId, + name, + path: `/${slug}`, + })) ?? [] ) }