Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Router - Revalidate Handler + Test Refactor to use sourceUrl #798

Merged
6 changes: 6 additions & 0 deletions .changeset/moody-emus-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/core": minor
"@headstartwp/next": minor
---

Introducing `revalidateRouteHandler` for handling revalidate requests in Route Handlers (App Router)
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/core/src/data/fetchFn/__tests__/fetchAppSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { expectTypeOf } from 'expect-type';
import { AppEntity, EndpointParams } from '../..';
import { fetchAppSettings } from '../fetchAppSettings';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchAppSettings', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});

it('allows overriding types', async () => {
interface MyAppEntity extends AppEntity {
myCustomSetting: string;
Expand Down
21 changes: 10 additions & 11 deletions packages/core/src/data/fetchFn/__tests__/fetchPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { PostParams } from '../..';
import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '../../../../test/server-handlers';
import { PostEntity } from '../../types';
import { fetchPost } from '../fetchPost';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchPost', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});

it('fetches data properly', async () => {
const { data } = await fetchPost({
params: { slug: 'modi-qui-dignissimos-sed-assumenda-sint-iusto' },
Expand Down Expand Up @@ -86,11 +94,7 @@ describe('fetchPost', () => {

it('reads param from the url and sets isMainQuery flag', async () => {
const result = await fetchPost({
path: '/modi-qui-dignissimos-sed-assumenda-sint-iusto/',
params: {
fullPath:
'https://js1.10up.com/2020/05/07/modi-qui-dignissimos-sed-assumenda-sint-iusto/',
},
path: '/2020/05/07/modi-qui-dignissimos-sed-assumenda-sint-iusto/',
});

expect(result.data.post.slug).toBe('modi-qui-dignissimos-sed-assumenda-sint-iusto');
Expand Down Expand Up @@ -125,12 +129,7 @@ describe('fetchPost', () => {

it('matches post.link with fullPath when set', async () => {
const { data } = await fetchPost({
path: '/modi-qui-dignissimos-sed-assumenda-sint-iusto',
params: {
// force post path mapping against this path
fullPath:
'https://js1.10up.com/2020/05/07/modi-qui-dignissimos-sed-assumenda-sint-iusto',
},
path: '/2020/05/07/modi-qui-dignissimos-sed-assumenda-sint-iusto',
});

expect(data.post.slug).toBe('modi-qui-dignissimos-sed-assumenda-sint-iusto');
Expand Down
24 changes: 13 additions & 11 deletions packages/core/src/data/fetchFn/__tests__/fetchPosts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expectTypeOf } from 'expect-type';

import { PostEntity, PostsArchiveParams, fetchPosts } from '../..';
import { setHeadlessConfig } from '../../../utils';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchPosts', () => {
beforeEach(() => {
setHeadlessConfig({
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});
Expand Down Expand Up @@ -128,16 +129,17 @@ describe('fetchPosts', () => {
it('throws params.matchArchivepath is true and path does not match', async () => {
await expect(
fetchPosts({
path: 'https://js1.10up.com/category/asdasd/uncategorized',
path: '/category/asdasd/uncategorized',
params: { category: 'uncategorized', per_page: 1, matchArchivePath: true },
}),
).rejects.toThrow(
`Posts were found but did not match current path: "https://js1.10up.com/category/asdasd/uncategorized"`,
`Posts were found but did not match current path: "/category/asdasd/uncategorized"`,
);
});

it('throws matchArchivepath config option is true and path does not match', async () => {
setHeadlessConfig({
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
customTaxonomies: (defaultTaxonomies) => {
return defaultTaxonomies.map((taxonomy) => ({
Expand All @@ -149,16 +151,17 @@ describe('fetchPosts', () => {

await expect(
fetchPosts({
path: 'https://js1.10up.com/category/asdasd/uncategorized',
path: '/category/asdasd/uncategorized',
params: { category: 'uncategorized', per_page: 2 },
}),
).rejects.toThrow(
`Posts were found but did not match current path: "https://js1.10up.com/category/asdasd/uncategorized"`,
`Posts were found but did not match current path: "/category/asdasd/uncategorized"`,
);
});

it('does not throws when matchArchivepath config option is true and path matches', async () => {
setHeadlessConfig({
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
customTaxonomies: (defaultTaxonomies) => {
return defaultTaxonomies.map((taxonomy) => ({
Expand All @@ -169,8 +172,7 @@ describe('fetchPosts', () => {
});

const { data } = await fetchPosts({
// Need this bc source url removal is not working in the tests
path: 'https://js1.10up.com/category/uncategorized',
path: '/category/uncategorized',
params: {
category: 'uncategorized',
per_page: 1,
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/data/fetchFn/__tests__/fetchPostsOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { PostEntity } from '../../types';
import { fetchPostOrPosts } from '../fetchPostOrPosts';

describe('fetchPostsOrPosts', () => {
setHeadstartWPConfig({
useWordPressPlugin: true,
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});

it('fetches data properly (archive)', async () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/data/fetchFn/__tests__/fetchSearch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { expectTypeOf } from 'expect-type';

import { PostSearchEntity, SearchParams, TermSearchEntity, fetchSearch } from '../..';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchSearch', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});

it('returns empty results instead of throwing if not found', async () => {
const { data } = await fetchSearch({ params: { search: 'not-found' } });

Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/data/fetchFn/__tests__/fetchTerms.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { expectTypeOf } from 'expect-type';
import { TaxonomyArchiveParams, TermEntity, fetchTerms } from '../..';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchTerms types', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
});

it('allows overriding types', async () => {
interface Genre extends TermEntity {
editor: string;
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/data/strategies/VerifyTokenFetchStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppEntity } from '../types';
import { Entity } from '../types';
import { AbstractFetchStrategy, EndpointParams, FetchOptions } from './AbstractFetchStrategy';
import { endpoints } from '../utils';

Expand All @@ -9,16 +9,31 @@ export interface VerifyTokenParams extends EndpointParams {
authToken?: string;
}

/**
* The TokenEntity represents a token issued by the headless wp plugin
*/
export interface TokenEntity extends Entity {
/**
* The path that the token was issued for
*/
path: string;

/**
* The post_id that the token was issued for
*/
post_id: number;
}

/**
* The Verify Token strategy is used to verify tokens issued by the
* headless wp plugin
*
* @category Data Fetching
*/
export class VerifyTokenFetchStrategy extends AbstractFetchStrategy<
AppEntity,
EndpointParams,
AppEntity
TokenEntity,
VerifyTokenParams,
TokenEntity
> {
getDefaultEndpoint(): string {
return endpoints.tokenVerify;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { setHeadstartWPConfig } from '../../../utils';
import { AuthorArchiveFetchStrategy } from '../AuthorArchiveFetchStrategy';

const config = {
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
};

describe('SearchFetchStrategy', () => {
let fetchStrategy: AuthorArchiveFetchStrategy;

beforeEach(() => {
fetchStrategy = new AuthorArchiveFetchStrategy();
fetchStrategy = new AuthorArchiveFetchStrategy(config.sourceUrl);
setHeadstartWPConfig(config);
});

it('parses the url properly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { NotFoundError, setHeadstartWPConfig } from '../../../utils';
import { PostOrPostsFetchStrategy, PostOrPostsParams } from '../PostOrPostsFetchStrategy';
import { PostEntity } from '../../types';

const config = {
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
};

describe('PostOrPostsFetchStrategy', () => {
let fetchStrategy: PostOrPostsFetchStrategy;

beforeEach(() => {
fetchStrategy = new PostOrPostsFetchStrategy('');
setHeadstartWPConfig(config);
fetchStrategy = new PostOrPostsFetchStrategy(config.sourceUrl);
});

it('returns `@postOrPosts` as the default endpoint', () => {
Expand Down Expand Up @@ -98,11 +104,6 @@ describe('PostOrPostsFetchStrategy', () => {
});

it('fetches the proper resource', async () => {
setHeadstartWPConfig({
sourceUrl: '',
useWordPressPlugin: true,
});

let params = fetchStrategy.getParamsFromURL('/');
let response = await fetchStrategy.fetcher('', merge(params, { priority: 'archive' }));
expect(response.result.isArchive).toBeTruthy();
Expand Down Expand Up @@ -152,10 +153,6 @@ describe('PostOrPostsFetchStrategy', () => {
'distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
);

// this is to force post path nmapping to work
p.single.fullPath =
'https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/';

params = merge(
fetchStrategy.getParamsFromURL(
'/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/',
Expand All @@ -172,7 +169,6 @@ describe('PostOrPostsFetchStrategy', () => {

// with this set to true it should error out if the path does not match
p.single.matchCurrentPath = true;
delete p.single.fullPath;

params = merge(
fetchStrategy.getParamsFromURL(
Expand All @@ -186,14 +182,9 @@ describe('PostOrPostsFetchStrategy', () => {
'Neither single or archive returned data: The request to /wp-json/wp/v2/posts?_embed=true&categories=distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam returned no data, Post #54 - "https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/" was found but did not match current path: "/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam"',
);

// now make it work by faking full path
// this simulates the post url returned by wp matching the front-end url
p.single.fullPath =
'https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/';

params = merge(
fetchStrategy.getParamsFromURL(
'/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
'/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
p,
),
p,
Expand Down Expand Up @@ -345,17 +336,12 @@ describe('PostOrPostsFetchStrategy', () => {
});

it('normalizes data for caching', async () => {
setHeadstartWPConfig({
sourceUrl: '',
useWordPressPlugin: true,
});

let params = merge(fetchStrategy.getParamsFromURL('/'), { priority: 'single' });
let response = await fetchStrategy.fetcher('', params);

let normalizedResponse = fetchStrategy.normalizeForCache(response, params);
expect(normalizedResponse.additionalCacheObjects?.[0].key).toStrictEqual({
args: { _embed: true, sourceUrl: '' },
args: { _embed: true, sourceUrl: config.sourceUrl },
url: '/wp-json/wp/v2/posts',
});

Expand All @@ -375,7 +361,7 @@ describe('PostOrPostsFetchStrategy', () => {
normalizedResponse = fetchStrategy.normalizeForCache(response, params);

expect(normalizedResponse.additionalCacheObjects?.[0].key).toStrictEqual({
args: { _embed: true, ...params.single, sourceUrl: '' },
args: { _embed: true, ...params.single, sourceUrl: config.sourceUrl },
url: '/wp-json/wp/v2/posts',
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { setHeadstartWPConfig } from '../../../utils';
import { SearchFetchStrategy } from '../SearchFetchStrategy';

const config = {
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
};

describe('SearchFetchStrategy', () => {
let fetchStrategy: SearchFetchStrategy;

beforeEach(() => {
fetchStrategy = new SearchFetchStrategy();
fetchStrategy = new SearchFetchStrategy(config.sourceUrl);
setHeadstartWPConfig(config);
});

it('parses the url properly', async () => {
Expand Down
Loading
Loading