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

Fix: Type Errors #76

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions packages/core/src/api/get-all-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export async function getAllItems(postTypes: string[]): Promise<Items> {
// check if postTypes are valid
postTypes.forEach((postType) => {
const isPostTypeValid = Object.keys(actualPostTypes).some(
(key) => actualPostTypes[key].rest_base === postType
(key) => actualPostTypes[key]?.rest_base === postType
);

if (!isPostTypeValid) {
const existingPostTypes = Object.keys(actualPostTypes)
.map((key) => actualPostTypes[key].rest_base)
.map((key) => actualPostTypes[key]?.rest_base)
.join(", ");

throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/get-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Items = {
}[];

export async function getItems({ restBase = "pages" }): Promise<Items> {
let allData = [];
let allData: Items = [];
let page = 1;
let morePagesAvailable = true;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/api/get-menu-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function getMenuItems({

// get menu items by menu id
const req = await fetch(
`${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/menu-items?menus=${menu.id}&acf_format=standard`,
`${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/menu-items?menus=${menu?.id}&acf_format=standard`,
args
);

Expand All @@ -103,7 +103,7 @@ export async function getMenuItems({
data = (await req.json()) as MenuResponse;
} catch (err: any) {
throw new Error(
`Error fetching menu items for menu id ${menu.id}: ${err.message}`
`Error fetching menu items for menu id ${menu?.id}: ${err.message}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/get-options-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ code: ${data.code}

${msg}`);
}
} catch (err) {
} catch (err: any) {
throw new Error(
`Error getting options page with slug '${slug}': ${err.message}`
);
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/api/get-page-data/get-node-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ export async function getNodeInfo(uri: string): Promise<{

for (const key in postTypes) {
// get rest base for post type
if (uri.startsWith(postTypes[key].slug)) {
rest_base = postTypes[key].rest_base;
if (uri.startsWith(String(postTypes[key]?.slug))) {
rest_base = String(postTypes[key]?.rest_base);
}
// check if uri matches a post type archive uri
if (postTypes[key].has_archive === uri) {
if (postTypes[key]?.has_archive === uri) {
archive = postTypes[key];
}
}

for (const key in taxonomies) {
if (uri.startsWith(taxonomies[key].slug)) {
if (uri.startsWith(String(taxonomies[key]?.slug))) {
taxonomy = taxonomies[key];
rest_base = taxonomy.rest_base;
rest_base = String(taxonomy?.rest_base);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/get-taxonomies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function getTaxonomies(): Promise<{
try {
const data = await req.json();
return data;
} catch (err) {
} catch (err: any) {
throw new Error(`getTaxonomies: Error fetching taxonomies: ${err.message}`);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/api/taxonomy/get-taxonomy-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function getTaxonomyPage({
}

const currentPageQueryString = new URLSearchParams(params).toString();
const endpoint = `${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/${postType.rest_base}?${currentPageQueryString}`;
const endpoint = `${process.env.NEXT_PUBLIC_WP_URL}/wp-json/wp/v2/${postType?.rest_base}?${currentPageQueryString}`;

const termItemsRequest = await fetch(endpoint);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { getPostTypes } from "../../api/get-post-types";
import { getSiteSettings } from "../../api/get-site-settings";

export async function getArchiveStaticParams({ postTypes }) {
interface GetArchivePaginationInfo {
postTypes: string[]
}

interface GetStaticParamsFromPaginationInfo {
archiveSlug: string;
paginationInfo: {
totalPages: number | undefined;
totalItems: number | undefined;
} | null
}

export async function getArchiveStaticParams({ postTypes }: GetArchivePaginationInfo) {
const wpPostTypes = await getPostTypes();
const settings = await getSiteSettings();
const staticParams: { paths: string[] }[] = [];
Expand All @@ -18,7 +30,7 @@ export async function getArchiveStaticParams({ postTypes }) {

const paginationInfo = await getArchivePaginationInfo({
rest_base: "posts",
per_page: settings.posts_per_page,
per_page: Number(settings.posts_per_page),
});

const postArchiveStaticParams = getStaticParamsFromPaginationInfo({
Expand All @@ -34,17 +46,17 @@ export async function getArchiveStaticParams({ postTypes }) {

for (const postType of postTypes) {
const itemKey = Object.keys(wpPostTypes).find(
(key) => wpPostTypes[key].rest_base === postType
(key) => wpPostTypes[key]?.rest_base === postType
);
const matchingPostType = wpPostTypes[itemKey];
const matchingPostType = wpPostTypes[String(itemKey)];

if (
matchingPostType.has_archive &&
matchingPostType?.has_archive &&
typeof matchingPostType.has_archive === "string"
) {
const paginationInfo = await getArchivePaginationInfo({
rest_base: matchingPostType.rest_base,
per_page: settings.posts_per_page,
per_page: Number(settings.posts_per_page),
});

const postTypeArchiveStaticParams = getStaticParamsFromPaginationInfo({
Expand Down Expand Up @@ -135,10 +147,10 @@ async function getArchivePaginationInfo({
}
}

function getStaticParamsFromPaginationInfo({ archiveSlug, paginationInfo }) {
function getStaticParamsFromPaginationInfo({ archiveSlug, paginationInfo }: GetStaticParamsFromPaginationInfo) {
const staticParams = [];

for (let i = 1; i <= paginationInfo.totalPages; i++) {
for (let i = 1; i <= Number(paginationInfo?.totalPages); i++) {
if (i === 1) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export async function getTaxonomyStaticParams({

for (const postType of postTypes) {
const itemKey = Object.keys(wpPostTypes).find(
(key) => wpPostTypes[key].rest_base === postType
(key) => wpPostTypes[key]?.rest_base === postType
);
if (!itemKey) {
continue;
}

const matchingPostType = wpPostTypes[itemKey];
const hasArchive =
matchingPostType.has_archive &&
matchingPostType?.has_archive &&
typeof matchingPostType.has_archive === "string";
const isPosts = matchingPostType.rest_base === "posts";

Expand Down Expand Up @@ -139,13 +139,13 @@ function getStaticParamsFromPaginationInfo({
}: {
paths: string[];
paginationInfo: {
totalPages: number;
totalItems: number;
totalPages?: number;
totalItems?: number;
};
}) {
const staticParams = [];

for (let i = 1; i <= paginationInfo.totalPages; i++) {
for (let i = 1; i <= Number(paginationInfo.totalPages); i++) {
if (i === 1) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/route-handlers/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function revalidate(request: Request) {
message: `Paths revalidated: ${correctPaths.join(", ")}`,
})
);
} catch (err) {
} catch (err: any) {
return new Response(JSON.stringify({ message: err.message }));
}
}
Loading