@okam/directus-next 0.4.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,8 +4,11 @@ const logger = require("../logger.js");
4
4
  async function fetchPageSettingsTranslation(path) {
5
5
  const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
6
6
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
7
- if (!graphqlEndpoint || !graphqlApiKey) {
8
- throw new Error("Missing GraphQL configuration");
7
+ if (!graphqlEndpoint) {
8
+ throw new Error("Missing GraphQL configuration `graphqlEndpoint`");
9
+ }
10
+ if (!graphqlApiKey) {
11
+ throw new Error("Missing GraphQL configuration `graphqlApiKey`");
9
12
  }
10
13
  const query = `
11
14
  query Languages_code($filter: page_settings_translations_filter) {
@@ -49,10 +52,15 @@ async function fetchPageSettingsTranslation(path) {
49
52
  return null;
50
53
  }
51
54
  }
55
+ function removeLocaleFromPathname(pathname, config) {
56
+ const currentLocale = Object.values(config.localeMap ?? {}).find((locale) => pathname.startsWith(`/${locale}/`));
57
+ return { locale: currentLocale, pathname: currentLocale ? pathname.replace(`/${currentLocale}/`, "/") : pathname };
58
+ }
52
59
  async function directusRouteRouter(request, config, NextResponse) {
53
60
  var _a, _b;
54
- const { pathname } = request.nextUrl;
55
- logger.log("Processing request for pathname:", pathname);
61
+ const { pathname: localizedPathname } = request.nextUrl;
62
+ const { locale, pathname } = removeLocaleFromPathname(localizedPathname, config);
63
+ logger.log("Processing request for pathname:", { locale, pathname });
56
64
  const translations = await fetchPageSettingsTranslation(pathname);
57
65
  if (!translations || translations.length === 0) {
58
66
  logger.log("No translation found for path:", pathname);
@@ -2,8 +2,11 @@ import { log } from "../logger.mjs";
2
2
  async function fetchPageSettingsTranslation(path) {
3
3
  const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
4
4
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
5
- if (!graphqlEndpoint || !graphqlApiKey) {
6
- throw new Error("Missing GraphQL configuration");
5
+ if (!graphqlEndpoint) {
6
+ throw new Error("Missing GraphQL configuration `graphqlEndpoint`");
7
+ }
8
+ if (!graphqlApiKey) {
9
+ throw new Error("Missing GraphQL configuration `graphqlApiKey`");
7
10
  }
8
11
  const query = `
9
12
  query Languages_code($filter: page_settings_translations_filter) {
@@ -47,10 +50,15 @@ async function fetchPageSettingsTranslation(path) {
47
50
  return null;
48
51
  }
49
52
  }
53
+ function removeLocaleFromPathname(pathname, config) {
54
+ const currentLocale = Object.values(config.localeMap ?? {}).find((locale) => pathname.startsWith(`/${locale}/`));
55
+ return { locale: currentLocale, pathname: currentLocale ? pathname.replace(`/${currentLocale}/`, "/") : pathname };
56
+ }
50
57
  async function directusRouteRouter(request, config, NextResponse) {
51
58
  var _a, _b;
52
- const { pathname } = request.nextUrl;
53
- log("Processing request for pathname:", pathname);
59
+ const { pathname: localizedPathname } = request.nextUrl;
60
+ const { locale, pathname } = removeLocaleFromPathname(localizedPathname, config);
61
+ log("Processing request for pathname:", { locale, pathname });
54
62
  const translations = await fetchPageSettingsTranslation(pathname);
55
63
  if (!translations || translations.length === 0) {
56
64
  log("No translation found for path:", pathname);
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("server-only");
4
+ const createServerContext = require("server-only-context");
5
+ function pageSettingsContext(defaultValue) {
6
+ const [pageSettings, setPageSettings] = createServerContext(defaultValue);
7
+ return [pageSettings, setPageSettings];
8
+ }
9
+ function pageSettingsVariablesContext(variables) {
10
+ const [pageSettingsVariables, setPageSettingsVariables] = createServerContext(variables);
11
+ return [pageSettingsVariables, setPageSettingsVariables];
12
+ }
13
+ exports.pageSettingsContext = pageSettingsContext;
14
+ exports.pageSettingsVariablesContext = pageSettingsVariablesContext;
@@ -0,0 +1,14 @@
1
+ import "server-only";
2
+ import createServerContext from "server-only-context";
3
+ function pageSettingsContext(defaultValue) {
4
+ const [pageSettings, setPageSettings] = createServerContext(defaultValue);
5
+ return [pageSettings, setPageSettings];
6
+ }
7
+ function pageSettingsVariablesContext(variables) {
8
+ const [pageSettingsVariables, setPageSettingsVariables] = createServerContext(variables);
9
+ return [pageSettingsVariables, setPageSettingsVariables];
10
+ }
11
+ export {
12
+ pageSettingsContext,
13
+ pageSettingsVariablesContext
14
+ };
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("@tanstack/react-query");
4
+ const query = require("../../../directus-query/src/lib/query.js");
5
+ require("../../../directus-query/src/lib/request.js");
6
+ require("../../../directus-query/src/logger.js");
7
+ require("graphql-request");
8
+ require("../../../directus-query/src/lib/config.js");
9
+ const radashi = require("radashi");
10
+ const logger = require("../logger.js");
11
+ const context = require("./context.js");
12
+ const [getPageSettings, setPageSettings] = context.pageSettingsContext();
13
+ const [getVariables, setVariables] = context.pageSettingsVariablesContext();
14
+ function isDirectusRouteConfig(config) {
15
+ return !!config && "localeMap" in config;
16
+ }
17
+ function getDirectusVariables(variables, config) {
18
+ const localeMap = isDirectusRouteConfig(config) ? config.localeMap : config;
19
+ if (!localeMap) {
20
+ return variables;
21
+ }
22
+ const locale = radashi.get(variables, "locale");
23
+ const directusLocale = radashi.invert(localeMap)[locale] ?? locale;
24
+ return { ...variables, locale: directusLocale };
25
+ }
26
+ async function usePageSettings(props, itemKey) {
27
+ var _a, _b, _c, _d, _e;
28
+ const { variables, config } = props ?? {};
29
+ const directusVariables = getDirectusVariables(variables, config);
30
+ const defaultReturn = getPageSettings() ?? {};
31
+ if (!props || radashi.isEqual(getVariables(), directusVariables)) {
32
+ logger.log("Using cached page settings", { path: (_c = (_b = (_a = defaultReturn.page_settings) == null ? void 0 : _a.translations) == null ? void 0 : _b[0]) == null ? void 0 : _c.path });
33
+ return defaultReturn;
34
+ }
35
+ const { document } = props;
36
+ const key = itemKey ?? radashi.get(document, "definitions[0].selectionSet.selections[0].name.value");
37
+ logger.log("Querying new page settings", directusVariables);
38
+ const result = await query.queryGql(document, directusVariables);
39
+ const items = result == null ? void 0 : result[key];
40
+ const currentItem = Array.isArray(items) ? items == null ? void 0 : items[0] : items;
41
+ const currentPageSettings = currentItem == null ? void 0 : currentItem.page_settings;
42
+ const currentPath = (_e = (_d = currentPageSettings == null ? void 0 : currentPageSettings.translations) == null ? void 0 : _d[0]) == null ? void 0 : _e.path;
43
+ if (!currentItem) {
44
+ logger.log("No item found. Falling back to cached page settings", { path: currentPath }, "warn");
45
+ return defaultReturn;
46
+ }
47
+ if (!currentPageSettings) {
48
+ logger.log("No page settings found. Falling back to cached page settings", { path: currentPath }, "warn");
49
+ return defaultReturn;
50
+ }
51
+ logger.log("Caching new page settings", { path: currentPath });
52
+ setPageSettings(currentItem);
53
+ setVariables(variables);
54
+ return currentItem;
55
+ }
56
+ exports.usePageSettings = usePageSettings;
@@ -0,0 +1,56 @@
1
+ import "@tanstack/react-query";
2
+ import { queryGql } from "../../../directus-query/src/lib/query.mjs";
3
+ import "../../../directus-query/src/lib/request.mjs";
4
+ import "../../../directus-query/src/logger.mjs";
5
+ import "graphql-request";
6
+ import "../../../directus-query/src/lib/config.mjs";
7
+ import { isEqual, get, invert } from "radashi";
8
+ import { log } from "../logger.mjs";
9
+ import { pageSettingsContext, pageSettingsVariablesContext } from "./context.mjs";
10
+ const [getPageSettings, setPageSettings] = pageSettingsContext();
11
+ const [getVariables, setVariables] = pageSettingsVariablesContext();
12
+ function isDirectusRouteConfig(config) {
13
+ return !!config && "localeMap" in config;
14
+ }
15
+ function getDirectusVariables(variables, config) {
16
+ const localeMap = isDirectusRouteConfig(config) ? config.localeMap : config;
17
+ if (!localeMap) {
18
+ return variables;
19
+ }
20
+ const locale = get(variables, "locale");
21
+ const directusLocale = invert(localeMap)[locale] ?? locale;
22
+ return { ...variables, locale: directusLocale };
23
+ }
24
+ async function usePageSettings(props, itemKey) {
25
+ var _a, _b, _c, _d, _e;
26
+ const { variables, config } = props ?? {};
27
+ const directusVariables = getDirectusVariables(variables, config);
28
+ const defaultReturn = getPageSettings() ?? {};
29
+ if (!props || isEqual(getVariables(), directusVariables)) {
30
+ log("Using cached page settings", { path: (_c = (_b = (_a = defaultReturn.page_settings) == null ? void 0 : _a.translations) == null ? void 0 : _b[0]) == null ? void 0 : _c.path });
31
+ return defaultReturn;
32
+ }
33
+ const { document } = props;
34
+ const key = itemKey ?? get(document, "definitions[0].selectionSet.selections[0].name.value");
35
+ log("Querying new page settings", directusVariables);
36
+ const result = await queryGql(document, directusVariables);
37
+ const items = result == null ? void 0 : result[key];
38
+ const currentItem = Array.isArray(items) ? items == null ? void 0 : items[0] : items;
39
+ const currentPageSettings = currentItem == null ? void 0 : currentItem.page_settings;
40
+ const currentPath = (_e = (_d = currentPageSettings == null ? void 0 : currentPageSettings.translations) == null ? void 0 : _d[0]) == null ? void 0 : _e.path;
41
+ if (!currentItem) {
42
+ log("No item found. Falling back to cached page settings", { path: currentPath }, "warn");
43
+ return defaultReturn;
44
+ }
45
+ if (!currentPageSettings) {
46
+ log("No page settings found. Falling back to cached page settings", { path: currentPath }, "warn");
47
+ return defaultReturn;
48
+ }
49
+ log("Caching new page settings", { path: currentPath });
50
+ setPageSettings(currentItem);
51
+ setVariables(variables);
52
+ return currentItem;
53
+ }
54
+ export {
55
+ usePageSettings
56
+ };
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const directusRouteRouter = require("./lib/directusRouteRouter.js");
4
+ const context = require("./pageSettings/context.js");
5
+ const usePageSettings = require("./pageSettings/usePageSettings.js");
4
6
  exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
7
+ exports.pageSettingsContext = context.pageSettingsContext;
8
+ exports.pageSettingsVariablesContext = context.pageSettingsVariablesContext;
9
+ exports.usePageSettings = usePageSettings.usePageSettings;
@@ -1,4 +1,9 @@
1
1
  import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
2
+ import { pageSettingsContext, pageSettingsVariablesContext } from "./pageSettings/context.mjs";
3
+ import { usePageSettings } from "./pageSettings/usePageSettings.mjs";
2
4
  export {
3
- directusRouteRouter
5
+ directusRouteRouter,
6
+ pageSettingsContext,
7
+ pageSettingsVariablesContext,
8
+ usePageSettings
4
9
  };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
3
+ process.env.NEXT_PUBLIC_API_TOKEN;
@@ -0,0 +1,2 @@
1
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
2
+ process.env.NEXT_PUBLIC_API_TOKEN;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("radashi");
4
+ const request = require("./request.js");
5
+ function queryGql(document, queryKey, client = request.graphqlRequestClient) {
6
+ return client.request(document, {
7
+ ...queryKey
8
+ });
9
+ }
10
+ exports.queryGql = queryGql;
@@ -0,0 +1,10 @@
1
+ import "radashi";
2
+ import { graphqlRequestClient } from "./request.mjs";
3
+ function queryGql(document, queryKey, client = graphqlRequestClient) {
4
+ return client.request(document, {
5
+ ...queryKey
6
+ });
7
+ }
8
+ export {
9
+ queryGql
10
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const reactQuery = require("@tanstack/react-query");
4
+ const graphqlRequest = require("graphql-request");
5
+ const logger = require("../logger.js");
6
+ const GRAPHQL_ENDPOINT_PUBLIC = process.env.NEXT_PUBLIC_GRAPHQL_URL;
7
+ const GRAPHQL_ENDPOINT_SERVER = process.env.NEXT_SERVER_GRAPHQL_URL;
8
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
9
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
10
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
11
+ const graphqlRequestClient = new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT_SERVER || GRAPHQL_ENDPOINT_PUBLIC, {
12
+ credentials: "include",
13
+ mode: "cors",
14
+ fetch,
15
+ headers: {
16
+ Authorization: `Bearer ${AUTH_TOKEN}`
17
+ }
18
+ });
19
+ new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
20
+ credentials: "include",
21
+ mode: "cors",
22
+ fetch,
23
+ headers: {
24
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
25
+ }
26
+ });
27
+ new reactQuery.QueryClient({
28
+ queryCache: new reactQuery.QueryCache({
29
+ onError: (error) => {
30
+ logger.logger.log("QueryClient Error", "error", { error });
31
+ }
32
+ }),
33
+ defaultOptions: {
34
+ queries: {
35
+ staleTime: 5 * 1e3
36
+ }
37
+ }
38
+ });
39
+ exports.default = graphqlRequestClient;
40
+ exports.graphqlRequestClient = graphqlRequestClient;
@@ -0,0 +1,40 @@
1
+ import { QueryClient, QueryCache } from "@tanstack/react-query";
2
+ import { GraphQLClient } from "graphql-request";
3
+ import { logger } from "../logger.mjs";
4
+ const GRAPHQL_ENDPOINT_PUBLIC = process.env.NEXT_PUBLIC_GRAPHQL_URL;
5
+ const GRAPHQL_ENDPOINT_SERVER = process.env.NEXT_SERVER_GRAPHQL_URL;
6
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
7
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
8
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
9
+ const graphqlRequestClient = new GraphQLClient(GRAPHQL_ENDPOINT_SERVER || GRAPHQL_ENDPOINT_PUBLIC, {
10
+ credentials: "include",
11
+ mode: "cors",
12
+ fetch,
13
+ headers: {
14
+ Authorization: `Bearer ${AUTH_TOKEN}`
15
+ }
16
+ });
17
+ new GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
18
+ credentials: "include",
19
+ mode: "cors",
20
+ fetch,
21
+ headers: {
22
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
23
+ }
24
+ });
25
+ new QueryClient({
26
+ queryCache: new QueryCache({
27
+ onError: (error) => {
28
+ logger.log("QueryClient Error", "error", { error });
29
+ }
30
+ }),
31
+ defaultOptions: {
32
+ queries: {
33
+ staleTime: 5 * 1e3
34
+ }
35
+ }
36
+ });
37
+ export {
38
+ graphqlRequestClient as default,
39
+ graphqlRequestClient
40
+ };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("../../../stack/logger/src/lib/logger.js");
4
+ const factoryLogger = require("../../../stack/logger/src/lib/factoryLogger.js");
5
+ const logger = factoryLogger("[DirectusQuery]");
6
+ exports.logger = logger;
@@ -0,0 +1,6 @@
1
+ import "../../../stack/logger/src/lib/logger.mjs";
2
+ import createLogger from "../../../stack/logger/src/lib/factoryLogger.mjs";
3
+ const logger = createLogger("[DirectusQuery]");
4
+ export {
5
+ logger
6
+ };
@@ -0,0 +1,24 @@
1
+ export type TFiles = {
2
+ id?: string | null;
3
+ title?: string | null;
4
+ description?: string | null;
5
+ tags?: any | null;
6
+ location?: string | null;
7
+ storage?: string | null;
8
+ focal_point_divider?: string | null;
9
+ focal_point_x?: number | null;
10
+ focal_point_y?: number | null;
11
+ filename_disk?: string | null;
12
+ filename_download?: string | null;
13
+ type?: string | null;
14
+ width?: number | null;
15
+ height?: number | null;
16
+ duration?: number | null;
17
+ embed?: string | null;
18
+ caption?: string | null;
19
+ thumbhash?: string | null;
20
+ /**
21
+ * @deprecated Use `filename_download` instead
22
+ */
23
+ filenameDownload?: string | null;
24
+ };
package/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export * from './draft';
2
2
  export { logger as DirectusNextLogger } from './logger';
3
3
  export { directusRouteRouter } from './lib/directusRouteRouter';
4
4
  export { getJsonErrorResponse } from './response';
5
+ export type { TPageSettings, TPageSettingsTranslation, TPageSettingsItemQuery, TPageSettingsItemDocument, TUsePageSettingsProps, } from './pageSettings/interface';
6
+ export type { TFiles } from './files/interface';
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@okam/directus-next",
3
3
  "main": "./index.js",
4
- "version": "0.4.0",
4
+ "version": "1.1.0",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
8
- "import": "./index.mjs",
9
- "require": "./index.js"
8
+ "import": {
9
+ "types": "./index.d.ts",
10
+ "default": "./index.mjs"
11
+ },
12
+ "require": {
13
+ "types": "./index.d.ts",
14
+ "default": "./index.mjs"
15
+ }
10
16
  },
11
17
  "./server": {
12
- "import": "./server.mjs",
13
- "require": "./server.js"
18
+ "import": {
19
+ "types": "./server.d.ts",
20
+ "default": "./server.mjs"
21
+ },
22
+ "require": {
23
+ "types": "./server.d.ts",
24
+ "default": "./server.mjs"
25
+ }
14
26
  }
15
27
  },
16
28
  "repository": {
@@ -22,6 +34,11 @@
22
34
  "dependencies": {
23
35
  "@okam/logger": "1.1.0",
24
36
  "next": "^14.1.1",
25
- "radashi": "^12.3.0"
37
+ "radashi": "^12.3.0",
38
+ "@okam/directus-query": "1.4.1",
39
+ "server-only": "0.0.1",
40
+ "graphql-request": "7.1.2",
41
+ "server-only-context": "0.1.0",
42
+ "@graphql-typed-document-node/core": "3.2.0"
26
43
  }
27
44
  }
@@ -0,0 +1,4 @@
1
+ import type { Variables } from 'graphql-request';
2
+ import type { TPageSettingsQueryItem, TUsePageSettingsReturn } from './interface';
3
+ export declare function pageSettingsContext<Item extends TPageSettingsQueryItem>(defaultValue?: TUsePageSettingsReturn<Item>): readonly [() => TUsePageSettingsReturn<Item> | undefined, (v: TUsePageSettingsReturn<Item> | undefined) => void];
4
+ export declare function pageSettingsVariablesContext(variables?: Variables): readonly [() => object | undefined, (v: object | undefined) => void];
@@ -0,0 +1,3 @@
1
+ export * from './context';
2
+ export type { TPageSettings, TPageSettingsTranslation, TPageSettingsItemDocument, TPageSettingsItemQuery, } from './interface';
3
+ export * from './usePageSettings';
@@ -0,0 +1,69 @@
1
+ import { type TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import type { Variables } from 'graphql-request';
3
+ import type { TFiles } from '../files/interface';
4
+ import type { DirectusRouteConfig } from '../types/directusRouteConfig';
5
+ export type Fragmentize<FragmentData, FragmentName extends string = string> = {
6
+ ' $fragmentRefs'?: {
7
+ [FragmentKey in FragmentName]?: FragmentData | null | undefined;
8
+ } | null | undefined;
9
+ } | null | undefined;
10
+ export type MaybeArray<T> = T | (T | null | undefined)[] | null | undefined;
11
+ /**
12
+ * Directus page settings translations collection.
13
+ */
14
+ export type TPageSettingsTranslation = {
15
+ slug?: string | null;
16
+ title?: string | null;
17
+ path?: string | null;
18
+ languages_code?: {
19
+ code: string;
20
+ } | null;
21
+ page_settings_id?: TPageSettings | null;
22
+ canonical_url?: string | null;
23
+ meta_description?: string | null;
24
+ no_follow?: boolean | null;
25
+ no_index?: boolean | null;
26
+ og_image?: TFiles | null;
27
+ };
28
+ /**
29
+ * Directus page settings collection.
30
+ */
31
+ export type TPageSettings = {
32
+ id: string;
33
+ belongs_to_collection?: string | null;
34
+ belongs_to_key?: string | null;
35
+ translations?: Array<TPageSettingsTranslation | null> | null;
36
+ route?: {
37
+ translations?: Array<{
38
+ route?: string | null;
39
+ } | null> | null;
40
+ } | null;
41
+ };
42
+ export type TPageSettingsQueryItem = {
43
+ page_settings?: TPageSettings | Fragmentize<TPageSettings, 'PageSettingsFragment'> | null | undefined;
44
+ } | null | undefined;
45
+ export type TPageSettingsItemQuery<Item extends TPageSettingsQueryItem, ItemKey extends string> = {
46
+ __typename?: 'Query';
47
+ } & {
48
+ [Key in ItemKey]?: MaybeArray<Item> | MaybeArray<Fragmentize<Item>>;
49
+ };
50
+ export type TPageSettingsItemDocument<Item extends TPageSettingsQueryItem, ItemKey extends string, QueryVariables extends Variables> = TypedDocumentNode<TPageSettingsItemQuery<Item, ItemKey>, QueryVariables>;
51
+ export type TUsePageSettingsConfig = DirectusRouteConfig | Record<string, string>;
52
+ /**
53
+ * If not using a fragment in the item key, all type parameters must be passed. Otherwise, only the `page_settings` field will be in the type definition.
54
+ * If using a fragment in the item key, the return type will contain the fragment.
55
+ */
56
+ export interface TUsePageSettingsProps<Item extends TPageSettingsQueryItem, ItemKey extends string, QueryVariables extends Variables> {
57
+ document: TPageSettingsItemDocument<Item, ItemKey, QueryVariables>;
58
+ /**
59
+ * `variables.locale` is a special value that will get mapped according to the config.
60
+ */
61
+ variables?: QueryVariables;
62
+ /**
63
+ * Either a directus route config or directly a locale map. Not passing a config while passing a document will result in direct usage of the `locale` variable.
64
+ */
65
+ config?: TUsePageSettingsConfig;
66
+ }
67
+ export type TUsePageSettingsReturn<Item extends TPageSettingsQueryItem> = Omit<Item, 'page_settings'> & {
68
+ page_settings?: Exclude<NonNullable<Item>['page_settings'], Fragmentize<TPageSettings, 'PageSettingsFragment'>> | null | undefined;
69
+ };
@@ -0,0 +1,30 @@
1
+ import type { Variables } from 'graphql-request';
2
+ import type { TUsePageSettingsProps, TPageSettingsQueryItem, TUsePageSettingsReturn } from './interface';
3
+ /**
4
+ * @param props Optional props. Passing new props will trigger a new query if the variables have changed compared to the value of the variables in the cache. Omitting props will return the cached value.
5
+ * @param itemKey Required if the query has more than a single root field
6
+ * @example
7
+ * #### `itemKey` not required
8
+ * ```graphql
9
+ * query PageById($id: ID!) {
10
+ * pages_by_id(id: $id) {
11
+ * ...Page
12
+ * }
13
+ * }
14
+ * ```
15
+ *
16
+ * #### `itemKey` required
17
+ * Otherwise the first root field will be used as the item key automatically (not necessarily the correct one)
18
+ * ```graphql
19
+ * query PageAndPostById($id: ID!) {
20
+ * pages_by_id(id: $id) {
21
+ * ...Page
22
+ * }
23
+ * posts_by_id(id: $id) {
24
+ * ...Post
25
+ * }
26
+ * }
27
+ * ```
28
+ * @returns The new queried page settings item or the cached value if the variables have not changed. If the query contains a fragment, the contents of the fragment will be returned.
29
+ */
30
+ export declare function usePageSettings<Item extends TPageSettingsQueryItem, ItemKey extends string = string, QueryVariables extends Variables = Variables>(props?: TUsePageSettingsProps<Item, ItemKey, QueryVariables>, itemKey?: Exclude<ItemKey, '__typename'>): Promise<TUsePageSettingsReturn<Item>>;
package/server.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { directusRouteRouter } from './lib/directusRouteRouter';
2
2
  export type { DirectusRouteConfig } from './types/directusRouteConfig';
3
+ export * from './pageSettings';