mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 05:56:59 +00:00
feat(poc): remove old lib things
This commit is contained in:
parent
d583346041
commit
9855dfe88e
@ -6,3 +6,4 @@ SHOPWARE_STORE_DOMAIN=""
|
|||||||
SHOPWARE_API_TYPE="store-api"
|
SHOPWARE_API_TYPE="store-api"
|
||||||
SHOPWARE_ACCESS_TOKEN=""
|
SHOPWARE_ACCESS_TOKEN=""
|
||||||
SHOPWARE_USE_SEO_URLS="false"
|
SHOPWARE_USE_SEO_URLS="false"
|
||||||
|
SHOPWARE_REVALIDATION_SECRET=""
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { revalidate } from 'lib/shopify';
|
import { revalidate } from 'lib/shopware';
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
@ -27,4 +27,3 @@ export const TAGS = {
|
|||||||
|
|
||||||
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
|
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
|
||||||
export const DEFAULT_OPTION = 'Default Title';
|
export const DEFAULT_OPTION = 'Default Title';
|
||||||
export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json';
|
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
import { headers } from 'next/headers';
|
||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { revalidateTag } from 'next/cache';
|
||||||
|
import { TAGS } from 'lib/constants';
|
||||||
import {
|
import {
|
||||||
requestCategory,
|
requestCategory,
|
||||||
requestCategoryList,
|
requestCategoryList,
|
||||||
@ -291,3 +295,40 @@ export async function getProductRecommendations(productId: string): Promise<Prod
|
|||||||
|
|
||||||
return products ? transformProducts(products) : [];
|
return products ? transformProducts(products) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is called from `app/api/revalidate.ts` so providers can control revalidation logic.
|
||||||
|
export async function revalidate(req: NextRequest): Promise<NextResponse> {
|
||||||
|
return NextResponse.json({
|
||||||
|
status: 200,
|
||||||
|
message: 'This is currently not working and was never tested.',
|
||||||
|
now: Date.now()
|
||||||
|
});
|
||||||
|
// We always need to respond with a 200 status code,
|
||||||
|
// otherwise it will continue to retry the request.
|
||||||
|
const collectionWebhooks = ['collections/create', 'collections/delete', 'collections/update'];
|
||||||
|
const productWebhooks = ['products/create', 'products/delete', 'products/update'];
|
||||||
|
const topic = headers().get('x-shopware-topic') || 'unknown';
|
||||||
|
const secret = req.nextUrl.searchParams.get('secret');
|
||||||
|
const isCollectionUpdate = collectionWebhooks.includes(topic);
|
||||||
|
const isProductUpdate = productWebhooks.includes(topic);
|
||||||
|
|
||||||
|
if (!secret || secret !== process.env.SHOPWARE_REVALIDATION_SECRET) {
|
||||||
|
console.error('Invalid revalidation secret.');
|
||||||
|
return NextResponse.json({ status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isCollectionUpdate && !isProductUpdate) {
|
||||||
|
// We don't need to revalidate anything for any other topics.
|
||||||
|
return NextResponse.json({ status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCollectionUpdate) {
|
||||||
|
revalidateTag(TAGS.collections);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isProductUpdate) {
|
||||||
|
revalidateTag(TAGS.products);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ status: 200, revalidated: true, now: Date.now() });
|
||||||
|
}
|
||||||
|
@ -1,26 +1,3 @@
|
|||||||
export interface ShopifyErrorLike {
|
|
||||||
status: number;
|
|
||||||
message: Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isObject = (object: unknown): object is Record<string, unknown> => {
|
export const isObject = (object: unknown): object is Record<string, unknown> => {
|
||||||
return typeof object === 'object' && object !== null && !Array.isArray(object);
|
return typeof object === 'object' && object !== null && !Array.isArray(object);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isShopifyError = (error: unknown): error is ShopifyErrorLike => {
|
|
||||||
if (!isObject(error)) return false;
|
|
||||||
|
|
||||||
if (error instanceof Error) return true;
|
|
||||||
|
|
||||||
return findError(error);
|
|
||||||
};
|
|
||||||
|
|
||||||
function findError<T extends object>(error: T): boolean {
|
|
||||||
if (Object.prototype.toString.call(error) === '[object Error]') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const prototype = Object.getPrototypeOf(error) as T | null;
|
|
||||||
|
|
||||||
return prototype === null ? false : findError(prototype);
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user