@unchainedshop/cockpit-api 2.1.0 → 2.1.2

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.
@@ -13,9 +13,8 @@ export interface CockpitAPIOptions {
13
13
  /**
14
14
  * Default language that maps to Cockpit's "default" locale.
15
15
  * When a request uses this language, it will be sent as "default" to Cockpit.
16
- * @default "de"
17
16
  */
18
- defaultLanguage?: string;
17
+ defaultLanguage?: string | null;
19
18
  /** Cache configuration */
20
19
  cache?: {
21
20
  /** Max entries (falls back to COCKPIT_CACHE_MAX env var, default: 100) */
@@ -36,7 +35,7 @@ export interface CockpitConfig {
36
35
  readonly tenant?: string;
37
36
  readonly apiKey?: string;
38
37
  readonly useAdminAccess: boolean;
39
- readonly defaultLanguage: string;
38
+ readonly defaultLanguage: string | null;
40
39
  readonly cachePrefix: string;
41
40
  }
42
41
  /**
@@ -12,20 +12,23 @@ export function createConfig(options = {}) {
12
12
  if (endpointStr === undefined || endpointStr === "") {
13
13
  throw new Error("Cockpit: endpoint is required (provide via options or COCKPIT_GRAPHQL_ENDPOINT env var)");
14
14
  }
15
+ // Normalize empty string tenant to undefined
16
+ const tenant = options.tenant === undefined || options.tenant === ""
17
+ ? undefined
18
+ : options.tenant;
15
19
  // Validate tenant format to prevent path traversal
16
- if (options.tenant !== undefined &&
17
- !VALID_TENANT_PATTERN.test(options.tenant)) {
20
+ if (tenant !== undefined && !VALID_TENANT_PATTERN.test(tenant)) {
18
21
  throw new Error("Cockpit: Invalid tenant format (only alphanumeric, hyphens, and underscores allowed)");
19
22
  }
20
23
  const endpoint = new URL(endpointStr);
21
- const apiKey = resolveApiKey(options.tenant, options);
24
+ const apiKey = resolveApiKey(tenant, options);
22
25
  // Build config object with all properties before freezing
23
26
  const config = Object.freeze({
24
27
  endpoint,
25
28
  useAdminAccess: options.useAdminAccess ?? false,
26
- defaultLanguage: options.defaultLanguage ?? "de",
27
- cachePrefix: `${endpointStr}:${options.tenant ?? "default"}:`,
28
- ...(options.tenant !== undefined && { tenant: options.tenant }),
29
+ defaultLanguage: options.defaultLanguage ?? null,
30
+ cachePrefix: `${endpointStr}:${tenant ?? "default"}:`,
31
+ ...(tenant !== undefined && { tenant }),
29
32
  ...(apiKey !== undefined && { apiKey }),
30
33
  });
31
34
  return config;
@@ -5,7 +5,7 @@
5
5
  * Creates a locale normalizer for the given default language.
6
6
  * Maps the default language to Cockpit's "default" locale.
7
7
  *
8
- * @param defaultLanguage - The language that should map to "default"
8
+ * @param defaultLanguage - The language that should map to "default", or null to disable mapping
9
9
  * @returns A function that normalizes locale strings
10
10
  */
11
- export declare function createLocaleNormalizer(defaultLanguage: string): (locale?: string) => string;
11
+ export declare function createLocaleNormalizer(defaultLanguage: string | null): (locale?: string) => string;
@@ -5,12 +5,14 @@
5
5
  * Creates a locale normalizer for the given default language.
6
6
  * Maps the default language to Cockpit's "default" locale.
7
7
  *
8
- * @param defaultLanguage - The language that should map to "default"
8
+ * @param defaultLanguage - The language that should map to "default", or null to disable mapping
9
9
  * @returns A function that normalizes locale strings
10
10
  */
11
11
  export function createLocaleNormalizer(defaultLanguage) {
12
12
  return (locale) => {
13
- if (locale === undefined || locale === defaultLanguage)
13
+ if (locale === undefined)
14
+ return "default";
15
+ if (defaultLanguage !== null && locale === defaultLanguage)
14
16
  return "default";
15
17
  return locale;
16
18
  };
@@ -25,8 +25,8 @@ export interface MakeCockpitSchemaOptions {
25
25
  transforms?: unknown[];
26
26
  /** Custom tenant extractor function */
27
27
  extractTenant?: (context: CockpitExecutorContext | undefined) => string | undefined;
28
- /** CockpitAPI options to pass through (endpoint, apiKey, useAdminAccess) */
29
- cockpitOptions?: Pick<CockpitAPIOptions, "endpoint" | "apiKey" | "useAdminAccess">;
28
+ /** CockpitAPI options to pass through (endpoint, apiKey, useAdminAccess, preloadRoutes) */
29
+ cockpitOptions?: Pick<CockpitAPIOptions, "endpoint" | "apiKey" | "useAdminAccess" | "preloadRoutes">;
30
30
  /** Maximum number of clients to keep in the pool (default: 100) */
31
31
  maxClients?: number;
32
32
  }
@@ -54,7 +54,11 @@ export function createRemoteExecutor(options = {}) {
54
54
  if (pending)
55
55
  return pending;
56
56
  // Create new client and cache it
57
- const clientOpts = { ...cockpitOptions };
57
+ // Default preloadRoutes to true for page link resolution in GraphQL responses
58
+ const clientOpts = {
59
+ preloadRoutes: true,
60
+ ...cockpitOptions,
61
+ };
58
62
  if (tenant !== undefined)
59
63
  clientOpts.tenant = tenant;
60
64
  const clientPromise = CockpitAPI(clientOpts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/cockpit-api",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "A package to interact with the Cockpit CMS API, including functionalities to handle GraphQL requests and various CMS content manipulations.",
5
5
  "main": "dist/index.js",
6
6
  "homepage": "https://unchained.shop",