feat(poc): remove old lib things

This commit is contained in:
Björn Meyer 2023-08-04 15:03:18 +02:00
parent d583346041
commit 9855dfe88e
5 changed files with 43 additions and 25 deletions

View File

@ -6,3 +6,4 @@ SHOPWARE_STORE_DOMAIN=""
SHOPWARE_API_TYPE="store-api"
SHOPWARE_ACCESS_TOKEN=""
SHOPWARE_USE_SEO_URLS="false"
SHOPWARE_REVALIDATION_SECRET=""

View File

@ -1,4 +1,4 @@
import { revalidate } from 'lib/shopify';
import { revalidate } from 'lib/shopware';
import { NextRequest, NextResponse } from 'next/server';
export const runtime = 'edge';

View File

@ -27,4 +27,3 @@ export const TAGS = {
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
export const DEFAULT_OPTION = 'Default Title';
export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json';

View File

@ -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 {
requestCategory,
requestCategoryList,
@ -291,3 +295,40 @@ export async function getProductRecommendations(productId: string): Promise<Prod
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() });
}

View File

@ -1,26 +1,3 @@
export interface ShopifyErrorLike {
status: number;
message: Error;
}
export const isObject = (object: unknown): object is Record<string, unknown> => {
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);
}