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

feat: primitives scaffolding #679

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35,987 changes: 23,381 additions & 12,606 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
"publish": "npm run build:packages && npx changeset publish"
},
"workspaces": [
"packages/*",
"packages/core",
"packages/next",
"packages/primitives",
"projects/*",
"wp/*"
],
"devDependencies": {
"@10up/eslint-config": "^3.1.0",
"@changesets/cli": "^2.22.0",
"@10up/eslint-config": "^4.0.0",
"@changesets/cli": "^2.27.1",
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
Expand All @@ -46,7 +48,8 @@
"jest": "^29.0.3",
"jest-environment-jsdom": "^29.0.3",
"lint-staged": "^11.1.2",
"turbo": "^1.9.3"
"turbo": "^1.9.3",
"prettier": "3.2.5"
},
"nextBundleAnalysis": {
"buildOutputDirectory": "./projects/wp-nextjs/.next",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"jest": "^29.3.1",
"msw": "^0.35.0",
"ts-jest": "^29.0.3",
"typescript": "^5.0.4",
"typescript": "^5.4.2",
"whatwg-fetch": "^3.6.2"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/data/api/fetch-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const apiGet = async (
const queryArgs = burstCache
? {
cacheTime: new Date().getTime(),
}
}
: {};

const config = getHeadlessConfig();
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/data/utils/postHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ export function removeFieldsFromPostRelatedData(
'wp:featuredmedia': post._embedded?.['wp:featuredmedia']
? post._embedded?.['wp:featuredmedia']?.map((attachments) =>
removeFields(fieldsToRemove, attachments as AttachmentEntity[]),
)
)
: [],
author: post._embedded.author
? (removeFields(fieldsToRemove, post._embedded.author) as AuthorEntity[])
: [],
'wp:term': post._embedded?.['wp:term']
? post._embedded['wp:term']?.map(
(terms) => removeFields(fieldsToRemove, terms) as TermEntity[],
)
)
: [],
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/react/components/BlocksRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function BlocksRenderer({ html, ksesAllowList, sanitizeFn, children }: Bl

return options.replace(childNode);
},
})
})
: null,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/react/components/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FC } from 'react';
import { PropsWithChildren } from 'react';

interface RawLinkProps {
href: string;
}

export const RawLink: FC<RawLinkProps> = ({ children, href, ...props }) => {
export const RawLink = ({ children, href, ...props }: PropsWithChildren<RawLinkProps>) => {
return (
<a href={href} {...props}>
{children}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/react/provider/DataFetchingProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react';
import { PropsWithChildren } from 'react';
import { SWRConfig, SWRConfiguration } from 'swr';

export { unstable_serialize as serializeKey } from 'swr';
Expand All @@ -14,10 +14,10 @@ export type DataFetchingProviderProps = {
data: SWRConfiguration['fallback'];
};

export const DataFetchingProvider: FC<DataFetchingProviderProps> = ({
export const DataFetchingProvider = ({
swrConfig,
data,
children,
}) => {
}: PropsWithChildren<DataFetchingProviderProps>) => {
return <SWRConfig value={{ fallback: data, ...swrConfig }}>{children}</SWRConfig>;
};
4 changes: 2 additions & 2 deletions packages/core/src/react/provider/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, createContext, useMemo } from 'react';
import { PropsWithChildren, createContext, useMemo } from 'react';
import { getHeadlessConfig } from '../../utils/config';
import { SettingsContextProps } from './types';

Expand All @@ -8,7 +8,7 @@ interface ProviderProps {
settings: SettingsContextProps;
}

export const SettingsProvider: FC<ProviderProps> = ({ settings, children }) => {
export const SettingsProvider = ({ settings, children }: PropsWithChildren<ProviderProps>) => {
const settingsValue = useMemo(
() => ({
...getHeadlessConfig(),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/react/provider/ThemeSettingsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, createContext } from 'react';
import { PropsWithChildren, createContext } from 'react';
import { ThemeJSON } from './types';

export const ThemeSettingsContext = createContext<ThemeJSON>({});
Expand All @@ -7,6 +7,6 @@ interface ProviderProps {
data: ThemeJSON;
}

export const ThemeSettingsProvider: FC<ProviderProps> = ({ data, children }) => {
export const ThemeSettingsProvider = ({ data, children }: PropsWithChildren<ProviderProps>) => {
return <ThemeSettingsContext.Provider value={data}>{children}</ThemeSettingsContext.Provider>;
};
6 changes: 3 additions & 3 deletions packages/core/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ function setPath(object: Record<string, any>, path: string[], value: any) {
object[key] =
i === lastIndex
? // If at end of path, assign the intended value.
value
value
: // Otherwise, advance to the next object in the path, creating
// it if it does not yet exist.
object[key] || (isNextKeyArrayIndex ? [] : {});
// it if it does not yet exist.
object[key] || (isNextKeyArrayIndex ? [] : {});

if (Array.isArray(object[key]) && !isNextKeyArrayIndex) {
// If we current key is non-numeric, but the next value is an
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"devDependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.5",
"@testing-library/react-hooks": "^8.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^29.0.3",
"@types/react": "^17.0.45",
"@types/react": "^18.2.65",
"esbuild": "^0.14.39",
"jest": "^29.0.3",
"plop": "^3.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
"expect-type": "^0.15.0",
"jest": "^29.0.3",
"next-router-mock": "^0.9.1",
"node-mocks-http": "^1.11.0",
"node-mocks-http": "^1.14.1",
"ts-jest": "^29.0.1",
"typescript": "^5.0.4",
"typescript": "^5.4.2",
"whatwg-fetch": "^3.6.2"
},
"peerDependencies": {
Expand Down
30 changes: 17 additions & 13 deletions packages/next/src/handlers/__tests__/previewHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createMocks } from 'node-mocks-http';
import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '@headstartwp/core/test';
import { removeSourceUrl, setHeadstartWPConfig } from '@headstartwp/core';
import { NextApiRequest, NextApiResponse } from 'next';
import { previewHandler } from '../previewHandler';

describe('previewHandler', () => {
it('does not accepts POST requests', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'POST',
query: {},
});
Expand All @@ -17,7 +18,7 @@ describe('previewHandler', () => {
});

it('rejects requests missing params', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: 1 },
});
Expand All @@ -29,7 +30,7 @@ describe('previewHandler', () => {
});

it('fails if a valid auth token is not provided', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: 'test', post_type: 'post' },
});
Expand All @@ -41,7 +42,7 @@ describe('previewHandler', () => {
});

it('works if a valid auth token is provided', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -57,7 +58,7 @@ describe('previewHandler', () => {
});

it('sets preview cookie path', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('previewHandler', () => {
],
});

const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -123,7 +124,10 @@ describe('previewHandler', () => {
'/book/modi-qui-dignissimos-sed-assumenda-sint-iusto-preview=true',
);

const { req: reqWithLocale, res: resWithLocale } = createMocks({
const { req: reqWithLocale, res: resWithLocale } = createMocks<
NextApiRequest,
NextApiResponse
>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -155,7 +159,7 @@ describe('previewHandler', () => {
});

it('sets preview cookie path with locale', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -187,7 +191,7 @@ describe('previewHandler', () => {
});

it('set preview cookie path to all paths if onRedirect is passed without getRedirectPath', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -213,7 +217,7 @@ describe('previewHandler', () => {
});

it('set preview cookie path redirectPath if getRedirectPath is passed', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -239,7 +243,7 @@ describe('previewHandler', () => {
});

it('correctly takes into account `options`', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down Expand Up @@ -271,7 +275,7 @@ describe('previewHandler', () => {
});

it('fails if post type is not defined', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'recipe' },
});
Expand All @@ -290,7 +294,7 @@ describe('previewHandler', () => {
preview: { usePostLinkForRedirect: true },
});

const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/handlers/__tests__/revalidateHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createMocks } from 'node-mocks-http';
import { NextApiRequest, NextApiResponse } from 'next';
import { revalidateHandler } from '../revalidateHandler';

describe('revalidateHandler', () => {
it('does not accepts POST requests', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'POST',
query: {},
});
Expand All @@ -15,7 +16,7 @@ describe('revalidateHandler', () => {
});

it('rejects requests missing params', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: 1 },
});
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/handlers/previewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export async function previewHandler(
typeof options.getRedirectPath === 'function'
? withPreviewSuffix(
options.getRedirectPath(getDefaultRedirectPath(), result, postTypeDef),
)
)
: withPreviewSuffix(getDefaultRedirectPath());

// we should set the path cookie if onRedirect is undefined (i.e we're just using default behasvior)
Expand Down
12 changes: 12 additions & 0 deletions packages/primitives/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
parser: '@typescript-eslint/parser',
extends: ['@10up/eslint-config/react', '@10up/eslint-config/jest'],
plugins: ['@typescript-eslint'],
rules: {
'import/no-unresolved': 'off',
'import/extensions': 'off',
},
settings: {
'import/resolver': 'typescript',
},
};
8 changes: 8 additions & 0 deletions packages/primitives/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"*.[tj]s": [
"eslint"
],
"*.[tj]sx": [
"eslint"
]
}
19 changes: 19 additions & 0 deletions packages/primitives/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
17 changes: 17 additions & 0 deletions packages/primitives/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @headstartwp/blocks-primitives
[![Support Level](https://img.shields.io/badge/support-active-green.svg)](#support-level) [![Next Package MIT License](https://img.shields.io/badge/next%20package-MIT-green)](https://github.com/10up/headstartwp/blob/develop/packages/next/LICENSE.md)


## Installation

```
npm install --save @headstartwp/blocks-primitives
```

## Support Level

**Active:** 10up is actively working on this, and we expect to continue work for the foreseeable future including keeping tested up to the most recent version of WordPress. Bug reports, feature requests, questions, and pull requests are welcome.

## Like what you see?

<a href="http://10up.com/contact/"><img src="https://10up.com/uploads/2016/10/10up-Github-Banner.png" width="850" alt="10up"></a>
Loading
Loading