4
0
forked from crowetic/commerce

Apply prettier over all files

This commit is contained in:
Luis Alvarez
2020-10-01 20:40:40 -05:00
parent 2314ad760a
commit 0ef6449aff
72 changed files with 648 additions and 652 deletions

View File

@@ -5,7 +5,7 @@ export const responsiveImageFragment = /* GraphQL */ `
urlLarge: url(width: $imgLargeWidth, height: $imgLargeHeight)
urlXL: url(width: $imgXLWidth, height: $imgXLHeight)
}
`;
`
export const productInfoFragment = /* GraphQL */ `
fragment productInfo on Product {
@@ -63,4 +63,4 @@ export const productInfoFragment = /* GraphQL */ `
}
${responsiveImageFragment}
`;
`

View File

@@ -1,17 +1,17 @@
import { CommerceAPIConfig } from 'lib/commerce/api';
import { GetAllProductsQueryVariables } from '../schema';
import fetchAPI from './utils/fetch-api';
import { CommerceAPIConfig } from 'lib/commerce/api'
import { GetAllProductsQueryVariables } from '../schema'
import fetchAPI from './utils/fetch-api'
export interface Images {
small?: ImageOptions;
medium?: ImageOptions;
large?: ImageOptions;
xl?: ImageOptions;
small?: ImageOptions
medium?: ImageOptions
large?: ImageOptions
xl?: ImageOptions
}
export interface ImageOptions {
width: number;
height?: number;
width: number
height?: number
}
export type ProductImageVariables = Pick<
@@ -24,39 +24,39 @@ export type ProductImageVariables = Pick<
| 'imgLargeHeight'
| 'imgXLWidth'
| 'imgXLHeight'
>;
>
export interface BigcommerceConfigOptions extends CommerceAPIConfig {
images?: Images;
images?: Images
}
export interface BigcommerceConfig extends BigcommerceConfigOptions {
readonly imageVariables?: ProductImageVariables;
readonly imageVariables?: ProductImageVariables
}
const API_URL = process.env.BIGCOMMERCE_STOREFRONT_API_URL;
const API_TOKEN = process.env.BIGCOMMERCE_STOREFRONT_API_TOKEN;
const API_URL = process.env.BIGCOMMERCE_STOREFRONT_API_URL
const API_TOKEN = process.env.BIGCOMMERCE_STOREFRONT_API_TOKEN
if (!API_URL) {
throw new Error(
`The environment variable BIGCOMMERCE_STOREFRONT_API_URL is missing and it's required to access your store`
);
)
}
if (!API_TOKEN) {
throw new Error(
`The environment variable BIGCOMMERCE_STOREFRONT_API_TOKEN is missing and it's required to access your store`
);
)
}
export class Config {
private config: BigcommerceConfig;
private config: BigcommerceConfig
constructor(config: BigcommerceConfigOptions) {
this.config = {
...config,
get imageVariables() {
const { images } = this;
const { images } = this
return images
? {
imgSmallWidth: images.small?.width,
@@ -68,17 +68,17 @@ export class Config {
imgXLWidth: images.xl?.height,
imgXLHeight: images.xl?.height,
}
: undefined;
: undefined
},
};
}
}
getConfig() {
return this.config;
return this.config
}
setConfig(newConfig: Partial<BigcommerceConfig>) {
Object.assign(this.config, newConfig);
Object.assign(this.config, newConfig)
}
}
@@ -86,12 +86,12 @@ const config = new Config({
commerceUrl: API_URL,
apiToken: API_TOKEN,
fetch: fetchAPI,
});
})
export function getConfig() {
return config.getConfig();
return config.getConfig()
}
export function setConfig(newConfig: Partial<BigcommerceConfig>) {
return config.setConfig(newConfig);
return config.setConfig(newConfig)
}

View File

@@ -1,6 +1,6 @@
import type { GetAllProductPathsQuery } from 'lib/bigcommerce/schema';
import type { RecursivePartial, RecursiveRequired } from '../utils/types';
import { BigcommerceConfig, getConfig } from '..';
import type { GetAllProductPathsQuery } from 'lib/bigcommerce/schema'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, getConfig } from '..'
export const getAllProductPathsQuery = /* GraphQL */ `
query getAllProductPaths {
@@ -14,41 +14,41 @@ export const getAllProductPathsQuery = /* GraphQL */ `
}
}
}
`;
`
export interface GetAllProductPathsResult<T> {
products: T extends GetAllProductPathsQuery
? NonNullable<T['site']['products']['edges']>
: unknown;
: unknown
}
async function getAllProductPaths(opts?: {
query?: string;
config?: BigcommerceConfig;
}): Promise<GetAllProductPathsResult<GetAllProductPathsQuery>>;
query?: string
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult<GetAllProductPathsQuery>>
async function getAllProductPaths<T, V = any>(opts: {
query: string;
config?: BigcommerceConfig;
}): Promise<GetAllProductPathsResult<T>>;
query: string
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult<T>>
async function getAllProductPaths({
query = getAllProductPathsQuery,
config = getConfig(),
}: {
query?: string;
config?: BigcommerceConfig;
query?: string
config?: BigcommerceConfig
} = {}): Promise<GetAllProductPathsResult<GetAllProductPathsQuery>> {
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `query`
const data = await config.fetch<RecursivePartial<GetAllProductPathsQuery>>(
query
);
const products = data.site?.products?.edges;
)
const products = data.site?.products?.edges
return {
products: (products as RecursiveRequired<typeof products>) ?? [],
};
}
}
export default getAllProductPaths;
export default getAllProductPaths

View File

@@ -1,15 +1,10 @@
import type {
GetAllProductsQuery,
GetAllProductsQueryVariables,
} from 'lib/bigcommerce/schema';
import type { RecursivePartial, RecursiveRequired } from '../utils/types';
import { productInfoFragment } from '../fragments/product';
import {
BigcommerceConfig,
getConfig,
Images,
ProductImageVariables,
} from '..';
} from 'lib/bigcommerce/schema'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { productInfoFragment } from '../fragments/product'
import { BigcommerceConfig, getConfig, Images, ProductImageVariables } from '..'
export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts(
@@ -40,53 +35,53 @@ export const getAllProductsQuery = /* GraphQL */ `
}
${productInfoFragment}
`;
`
export interface GetAllProductsResult<T> {
products: T extends GetAllProductsQuery
? NonNullable<T['site']['products']['edges']>
: unknown;
: unknown
}
export type ProductVariables = Images &
Omit<GetAllProductsQueryVariables, keyof ProductImageVariables>;
Omit<GetAllProductsQueryVariables, keyof ProductImageVariables>
async function getAllProducts(opts?: {
query?: string;
variables?: ProductVariables;
config?: BigcommerceConfig;
}): Promise<GetAllProductsResult<GetAllProductsQuery>>;
query?: string
variables?: ProductVariables
config?: BigcommerceConfig
}): Promise<GetAllProductsResult<GetAllProductsQuery>>
async function getAllProducts<T, V = any>(opts: {
query: string;
variables?: V;
config?: BigcommerceConfig;
}): Promise<GetAllProductsResult<T>>;
query: string
variables?: V
config?: BigcommerceConfig
}): Promise<GetAllProductsResult<T>>
async function getAllProducts({
query = getAllProductsQuery,
variables: vars,
config = getConfig(),
}: {
query?: string;
variables?: ProductVariables;
config?: BigcommerceConfig;
query?: string
variables?: ProductVariables
config?: BigcommerceConfig
} = {}): Promise<GetAllProductsResult<GetAllProductsQuery>> {
const variables: GetAllProductsQueryVariables = {
...config.imageVariables,
...vars,
};
}
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `query`
const data = await config.fetch<RecursivePartial<GetAllProductsQuery>>(
query,
{ variables }
);
const products = data.site?.products?.edges;
)
const products = data.site?.products?.edges
return {
products: (products as RecursiveRequired<typeof products>) ?? [],
};
}
}
export default getAllProducts;
export default getAllProducts

View File

@@ -1,10 +1,10 @@
import type {
GetProductQuery,
GetProductQueryVariables,
} from 'lib/bigcommerce/schema';
import type { RecursivePartial, RecursiveRequired } from '../utils/types';
import { productInfoFragment } from '../fragments/product';
import { BigcommerceConfig, getConfig, Images } from '..';
} from 'lib/bigcommerce/schema'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { productInfoFragment } from '../fragments/product'
import { BigcommerceConfig, getConfig, Images } from '..'
export const getProductQuery = /* GraphQL */ `
query getProduct(
@@ -31,55 +31,55 @@ export const getProductQuery = /* GraphQL */ `
}
${productInfoFragment}
`;
`
export interface GetProductResult<T> {
product?: T extends GetProductQuery
? Extract<T['site']['route']['node'], { __typename: 'Product' }>
: unknown;
: unknown
}
export type ProductVariables = Images &
({ path: string; slug?: never } | { path?: never; slug: string });
({ path: string; slug?: never } | { path?: never; slug: string })
async function getProduct(opts: {
query?: string;
variables: ProductVariables;
config?: BigcommerceConfig;
}): Promise<GetProductResult<GetProductQuery>>;
query?: string
variables: ProductVariables
config?: BigcommerceConfig
}): Promise<GetProductResult<GetProductQuery>>
async function getProduct<T, V = any>(opts: {
query: string;
variables: V;
config?: BigcommerceConfig;
}): Promise<GetProductResult<T>>;
query: string
variables: V
config?: BigcommerceConfig
}): Promise<GetProductResult<T>>
async function getProduct({
query = getProductQuery,
variables: { slug, ...vars },
config = getConfig(),
}: {
query?: string;
variables: ProductVariables;
config?: BigcommerceConfig;
query?: string
variables: ProductVariables
config?: BigcommerceConfig
}): Promise<GetProductResult<GetProductQuery>> {
const variables: GetProductQueryVariables = {
...config.imageVariables,
...vars,
path: slug ? `/${slug}/` : vars.path!,
};
}
const data = await config.fetch<RecursivePartial<GetProductQuery>>(query, {
variables,
});
const product = data.site?.route?.node;
})
const product = data.site?.route?.node
if (product?.__typename === 'Product') {
return {
product: product as RecursiveRequired<typeof product>,
};
}
}
return {};
return {}
}
export default getProduct;
export default getProduct

View File

@@ -1,11 +1,11 @@
import { CommerceAPIFetchOptions } from 'lib/commerce/api';
import { getConfig } from '..';
import { CommerceAPIFetchOptions } from 'lib/commerce/api'
import { getConfig } from '..'
export default async function fetchAPI<Q, V = any>(
query: string,
{ variables, preview }: CommerceAPIFetchOptions<V> = {}
): Promise<Q> {
const config = getConfig();
const config = getConfig()
const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), {
method: 'POST',
headers: {
@@ -16,12 +16,12 @@ export default async function fetchAPI<Q, V = any>(
query,
variables,
}),
});
})
const json = await res.json();
const json = await res.json()
if (json.errors) {
console.error(json.errors);
throw new Error('Failed to fetch API');
console.error(json.errors)
throw new Error('Failed to fetch API')
}
return json.data;
return json.data
}

View File

@@ -1,7 +1,7 @@
export type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
[P in keyof T]?: RecursivePartial<T[P]>
}
export type RecursiveRequired<T> = {
[P in keyof T]-?: RecursiveRequired<T[P]>;
};
[P in keyof T]-?: RecursiveRequired<T[P]>
}