Skip to content

Commit

Permalink
refactor(next-drupal)!: move non-DrupalClient code to src/deprecated
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
The JsonApiWithAuthOptions type has been renamed to JsonApiWithAuthOption. The
JsonApiWithLocaleOptions type is now considered deprecated for DrupalClient
usage and can be replaced with the JsonApiOptions type.
  • Loading branch information
JohnAlbin committed Feb 21, 2024
1 parent f22e86a commit f04c617
Show file tree
Hide file tree
Showing 28 changed files with 558 additions and 572 deletions.
19 changes: 10 additions & 9 deletions examples/example-search-api/pages/advanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DrupalNode,
getSearchIndexFromContext,
deserialize,
JsonApiSearchApiResponse,
DrupalSearchApiJsonApiResponse,
DrupalSearchApiFacet,
} from "next-drupal"
import { GetStaticPropsResult } from "next"
Expand Down Expand Up @@ -209,14 +209,15 @@ export default function AdvancedPage({
export async function getStaticProps(
context
): Promise<GetStaticPropsResult<AdvancedPageProps>> {
const results = await getSearchIndexFromContext<JsonApiSearchApiResponse>(
"property",
context,
{
deserialize: false,
params,
}
)
const results =
await getSearchIndexFromContext<DrupalSearchApiJsonApiResponse>(
"property",
context,
{
deserialize: false,
params,
}
)

return {
props: {
Expand Down
16 changes: 10 additions & 6 deletions packages/next-drupal/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ module.exports = {
coverageProvider: "v8",
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
coveragePathIgnorePatterns: ["./src/get-*"],
coveragePathIgnorePatterns: [
"./src/deprecated/*",
"./src/deprecated.ts",
"./src/navigation.ts",
"./src/types/*",
],
coverageReporters: ["lcov", "text", "text-summary"],
coverageThreshold: {
global: {
// @TODO Make these thresholds strict once #608 is completed.
statements: 50, // 55.1,
branches: 80, // 84.16,
functions: 70, // 72.41,
lines: 50, // 55.1,
statements: 82.07,
branches: 86.9,
functions: 80.76,
lines: 82.07,
},
},
}
46 changes: 19 additions & 27 deletions packages/next-drupal/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import type {
FetchOptions,
JsonApiCreateFileResourceBody,
JsonApiCreateResourceBody,
JsonApiOptions,
JsonApiParams,
JsonApiResource,
JsonApiResourceWithPath,
JsonApiResponse,
JsonApiUpdateResourceBody,
JsonApiWithAuthOptions,
JsonApiWithAuthOption,
JsonApiWithCacheOptions,
JsonApiWithLocaleOptions,
Locale,
PathAlias,
PathPrefix,
Expand Down Expand Up @@ -312,7 +312,7 @@ export class DrupalClient {
async createResource<T extends JsonApiResource>(
type: string,
body: JsonApiCreateResourceBody,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -350,7 +350,7 @@ export class DrupalClient {
async createFileResource<T = DrupalFile>(
type: string,
body: JsonApiCreateFileResourceBody,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -396,7 +396,7 @@ export class DrupalClient {
type: string,
uuid: string,
body: JsonApiUpdateResourceBody,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -435,7 +435,7 @@ export class DrupalClient {
async deleteResource(
type: string,
uuid: string,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<boolean> {
options = {
withAuth: true,
Expand Down Expand Up @@ -467,9 +467,7 @@ export class DrupalClient {
async getResource<T extends JsonApiResource>(
type: string,
uuid: string,
options?: JsonApiWithLocaleOptions &
JsonApiWithAuthOptions &
JsonApiWithCacheOptions
options?: JsonApiOptions & JsonApiWithCacheOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -523,8 +521,7 @@ export class DrupalClient {
options?: {
pathPrefix?: PathPrefix
isVersionable?: boolean
} & JsonApiWithLocaleOptions &
JsonApiWithAuthOptions
} & JsonApiOptions
): Promise<T> {
const type = typeof input === "string" ? input : input.jsonapi.resourceName

Expand Down Expand Up @@ -605,8 +602,7 @@ export class DrupalClient {
path: string,
options?: {
isVersionable?: boolean
} & JsonApiWithLocaleOptions &
JsonApiWithAuthOptions
} & JsonApiOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -716,8 +712,7 @@ export class DrupalClient {
type: string,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions &
JsonApiWithAuthOptions
} & JsonApiOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand Down Expand Up @@ -754,8 +749,7 @@ export class DrupalClient {
context: GetStaticPropsContext,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions &
JsonApiWithAuthOptions
} & JsonApiOptions
): Promise<T> {
options = {
deserialize: true,
Expand All @@ -778,7 +772,7 @@ export class DrupalClient {
options?: {
params?: JsonApiParams
pathPrefix?: PathPrefix
} & JsonApiWithAuthOptions
} & JsonApiWithAuthOption
): Promise<GetStaticPathsResult<{ slug: string[] }>["paths"]> {
options = {
withAuth: this.withAuth,
Expand Down Expand Up @@ -893,7 +887,7 @@ export class DrupalClient {

async translatePath(
path: string,
options?: JsonApiWithAuthOptions
options?: JsonApiWithAuthOption
): Promise<DrupalTranslatedPath | null> {
options = {
withAuth: this.withAuth,
Expand Down Expand Up @@ -926,7 +920,7 @@ export class DrupalClient {
context: GetStaticPropsContext,
options?: {
pathPrefix?: PathPrefix
} & JsonApiWithAuthOptions
} & JsonApiWithAuthOption
): Promise<DrupalTranslatedPath | null> {
options = {
pathPrefix: "/",
Expand Down Expand Up @@ -1160,9 +1154,7 @@ export class DrupalClient {

async getMenu<T = DrupalMenuLinkContent>(
name: string,
options?: JsonApiWithLocaleOptions &
JsonApiWithAuthOptions &
JsonApiWithCacheOptions
options?: JsonApiOptions & JsonApiWithCacheOptions
): Promise<{
items: T[]
tree: T[]
Expand Down Expand Up @@ -1246,7 +1238,7 @@ export class DrupalClient {

async getView<T = JsonApiResource>(
name: string,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<DrupalView<T>> {
options = {
withAuth: this.withAuth,
Expand Down Expand Up @@ -1291,7 +1283,7 @@ export class DrupalClient {

async getSearchIndex<T = JsonApiResource[]>(
name: string,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand Down Expand Up @@ -1327,7 +1319,7 @@ export class DrupalClient {
async getSearchIndexFromContext<T = JsonApiResource[]>(
name: string,
context: GetStaticPropsContext,
options?: JsonApiWithLocaleOptions & JsonApiWithAuthOptions
options?: JsonApiOptions
): Promise<T> {
return await this.getSearchIndex<T>(name, {
...options,
Expand Down Expand Up @@ -1493,7 +1485,7 @@ export class DrupalClient {

private getAuthFromContextAndOptions(
context: GetStaticPropsContext,
options: JsonApiWithAuthOptions
options: JsonApiWithAuthOption
) {
// If not in preview or withAuth is provided, use that.
if (!context.preview) {
Expand Down
19 changes: 19 additions & 0 deletions packages/next-drupal/src/deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export * from "./deprecated/get-access-token"
export * from "./deprecated/get-menu"
export * from "./deprecated/get-paths"
export * from "./deprecated/get-resource-collection"
export * from "./deprecated/preview"
export * from "./deprecated/get-resource-type"
export * from "./deprecated/get-resource"
export * from "./deprecated/get-search-index"
export * from "./deprecated/get-view"
export * from "./deprecated/translate-path"
export {
deserialize,
buildUrl,
getJsonApiIndex,
getJsonApiPathForResourceType,
syncDrupalPreviewRoutes,
} from "./deprecated/utils"

export type * from "./types/deprecated"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cache } from "./get-cache"
import type { AccessToken } from "./types"
import type { AccessToken } from "../types"

const CACHE_KEY = "NEXT_DRUPAL_ACCESS_TOKEN"

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { buildHeaders, buildUrl, deserialize } from "./utils"
import type {
AccessToken,
DrupalMenuLinkContent,
JsonApiWithLocaleOptions,
} from "./types"
import type { AccessToken, DrupalMenuLinkContent } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

export async function getMenu<T extends DrupalMenuLinkContent>(
name: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getResourceCollection } from "./get-resource-collection"
import type { GetStaticPathsContext, GetStaticPathsResult } from "next"
import type { AccessToken, JsonApiParams, Locale } from "./types"
import type { AccessToken, JsonApiParams, Locale } from "../types"

export async function getPathsFromContext(
types: string | string[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import {
getJsonApiPathForResourceType,
} from "./utils"
import type { GetStaticPropsContext } from "next"
import type {
AccessToken,
JsonApiParams,
JsonApiResource,
JsonApiWithLocaleOptions,
} from "./types"
import type { AccessToken, JsonApiParams, JsonApiResource } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

export async function getResourceCollection<T = JsonApiResource[]>(
type: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { translatePathFromContext } from "./translate-path"
import type { GetStaticPropsContext } from "next"
import type { AccessToken } from "./types"
import type { AccessToken } from "../types"

export async function getResourceTypeFromContext(
context: GetStaticPropsContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import {
getPathFromContext,
} from "./utils"
import type { GetStaticPropsContext } from "next"
import type {
AccessToken,
JsonApiParams,
JsonApiResource,
JsonApiWithLocaleOptions,
} from "./types"
import type { AccessToken, JsonApiParams, JsonApiResource } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

export async function getResourceFromContext<T extends JsonApiResource>(
type: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { buildHeaders, buildUrl, deserialize } from "./utils"
import type { GetStaticPropsContext } from "next"
import type {
AccessToken,
JsonApiResource,
JsonApiWithLocaleOptions,
} from "./types"
import type { AccessToken, JsonApiResource } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

export async function getSearchIndex<T = JsonApiResource[]>(
name: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildHeaders, buildUrl, deserialize } from "./utils"
import type { AccessToken, JsonApiWithLocaleOptions } from "./types"
import type { AccessToken } from "../types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

export async function getView<T>(
name: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getResourceByPath } from "./get-resource"
import type { NextApiRequest, NextApiResponse } from "next"
import type { JsonApiWithLocaleOptions } from "./types"
import type { JsonApiWithLocaleOptions } from "../types/deprecated"

interface PreviewOptions {
errorMessages?: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildHeaders, buildUrl, getPathFromContext } from "./utils"
import type { GetStaticPropsContext } from "next"
import type { AccessToken, DrupalTranslatedPath } from "./types"
import type { AccessToken, DrupalTranslatedPath } from "../types"

export async function translatePath(
path: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRouter } from "next/router"
import { useEffect, useState } from "react"
import { getMenu } from "../get-menu"
import { getMenu } from "./get-menu"
import type { DrupalMenuLinkContent } from "../types"

export function useMenu<T extends DrupalMenuLinkContent>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Jsona } from "jsona"
import { stringify } from "qs"
import { getAccessToken } from "./get-access-token"
import type { GetStaticPropsContext } from "next"
import type { AccessToken, Locale } from "./types"
import type { AccessToken, Locale } from "../types"

const JSONAPI_PREFIX = process.env.DRUPAL_JSONAPI_PREFIX || "/jsonapi"

Expand Down
Loading

0 comments on commit f04c617

Please sign in to comment.