Skip to content

Commit

Permalink
fix: update error message for incompatible Server Component APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Apr 27, 2023
1 parent a73eb4b commit d558c91
Showing 1 changed file with 112 additions and 26 deletions.
138 changes: 112 additions & 26 deletions src/react-server/unsupported.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,184 @@
// Exports in this file are unsupported in the react-server environment.
// All environments should export the same functions to maintian API compatability.

function buildErrorMessage(fnName: string) {
return `${fnName} is not supported in Server Components. Remove all uses of ${fnName} in Server Components.`;
function buildIncompatibleInServerComponentsErrorMessage(fnName: string) {
return `${fnName} is not supported in Server Components. Remove ${fnName} in Server Components or add the "use client" directive to the component using ${fnName}.`;
}

function buildHookErrorMessage(fnName: string) {
return `${fnName} is not supported in Server Components. Replace all uses of ${fnName} in Server Components with direct use of \`@prismicio/client\`.`;
function buildIncompatibleQueryHookInServerComponentsErrorMessage(
fnName: string,
) {
return `${fnName} is not supported in Server Components. Replace ${fnName} in Server Components with direct use of \`@prismicio/client\` (recommended) or add the "use client" directive to the component using the hook.`;
}

export function PrismicProvider(): JSX.Element {
throw new Error(buildErrorMessage("<PrismicProvider>"));
throw new Error(
buildIncompatibleInServerComponentsErrorMessage("<PrismicProvider>"),
);
}

export function usePrismicContext() {
throw new Error(buildErrorMessage("usePrismicContext()"));
throw new Error(
buildIncompatibleInServerComponentsErrorMessage("usePrismicContext()"),
);
}

export function usePrismicClient() {
throw new Error(buildHookErrorMessage("usePrismicClient()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicClient()",
),
);
}

export function usePrismicPreviewResolver() {
throw new Error(buildHookErrorMessage("usePrismicPreviewResolver()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicPreviewResolver()",
),
);
}

export function useAllPrismicDocumentsDangerously() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsDangerously()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsDangerously()",
),
);
}

export function useAllPrismicDocumentsByEveryTag() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsByEveryTag()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsByEveryTag()",
),
);
}

export function useAllPrismicDocumentsByIDs() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsByIDs()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsByIDs()",
),
);
}

export function useAllPrismicDocumentsBySomeTags() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsBySomeTags()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsBySomeTags()",
),
);
}

export function useAllPrismicDocumentsByTag() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsByTag()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsByTag()",
),
);
}

export function useAllPrismicDocumentsByType() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsByType()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsByType()",
),
);
}

export function useAllPrismicDocumentsByUIDs() {
throw new Error(buildHookErrorMessage("useAllPrismicDocumentsByUIDs()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useAllPrismicDocumentsByUIDs()",
),
);
}

export function useFirstPrismicDocument() {
throw new Error(buildHookErrorMessage("useFirstPrismicDocument()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useFirstPrismicDocument()",
),
);
}

export function usePrismicDocumentByID() {
throw new Error(buildHookErrorMessage("usePrismicDocumentByID()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentByID()",
),
);
}

export function usePrismicDocumentByUID() {
throw new Error(buildHookErrorMessage("usePrismicDocumentByUID()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentByUID()",
),
);
}

export function usePrismicDocuments() {
throw new Error(buildHookErrorMessage("usePrismicDocuments()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocuments()",
),
);
}

export function usePrismicDocumentsByEveryTag() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsByEveryTag()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsByEveryTag()",
),
);
}

export function usePrismicDocumentsByIDs() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsByIDs()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsByIDs()",
),
);
}

export function usePrismicDocumentsBySomeTags() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsBySomeTags()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsBySomeTags()",
),
);
}

export function usePrismicDocumentsByTag() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsByTag()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsByTag()",
),
);
}

export function usePrismicDocumentsByType() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsByType()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsByType()",
),
);
}

export function usePrismicDocumentsByUIDs() {
throw new Error(buildHookErrorMessage("usePrismicDocumentsByUIDs()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"usePrismicDocumentsByUIDs()",
),
);
}

export function useSinglePrismicDocument() {
throw new Error(buildHookErrorMessage("useSinglePrismicDocument()"));
throw new Error(
buildIncompatibleQueryHookInServerComponentsErrorMessage(
"useSinglePrismicDocument()",
),
);
}

0 comments on commit d558c91

Please sign in to comment.