@djangocfg/ext-knowbase 1.0.9 → 1.0.10

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.
Files changed (28) hide show
  1. package/dist/config.cjs +1 -1
  2. package/dist/config.js +1 -1
  3. package/dist/hooks.cjs +65 -13
  4. package/dist/hooks.d.cts +6 -5
  5. package/dist/hooks.d.ts +6 -5
  6. package/dist/hooks.js +66 -15
  7. package/dist/index.cjs +65 -13
  8. package/dist/index.d.cts +81 -25
  9. package/dist/index.d.ts +81 -25
  10. package/dist/index.js +66 -15
  11. package/package.json +7 -7
  12. package/src/api/generated/ext_knowbase/CLAUDE.md +2 -9
  13. package/src/api/generated/ext_knowbase/_utils/fetchers/ext_knowbase__knowbase.ts +2 -1
  14. package/src/api/generated/ext_knowbase/_utils/hooks/ext_knowbase__knowbase.ts +2 -1
  15. package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveSearchResult.schema.ts +3 -3
  16. package/src/api/generated/ext_knowbase/_utils/schemas/CfgKnowbaseSystemArchivesCreateRequest.schema.ts +20 -0
  17. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchive.schema.ts +1 -1
  18. package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveDetail.schema.ts +1 -1
  19. package/src/api/generated/ext_knowbase/_utils/schemas/index.ts +1 -0
  20. package/src/api/generated/ext_knowbase/api-instance.ts +61 -13
  21. package/src/api/generated/ext_knowbase/client.ts +23 -2
  22. package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/client.ts +9 -2
  23. package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/models.ts +13 -0
  24. package/src/api/generated/ext_knowbase/http.ts +8 -2
  25. package/src/api/generated/ext_knowbase/index.ts +3 -1
  26. package/src/api/index.ts +6 -1
  27. package/src/contexts/knowbase/DocumentsContext.tsx +4 -2
  28. package/src/contexts/knowbase/types.ts +1 -0
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Zod schema for CfgKnowbaseSystemArchivesCreateRequest
3
+ *
4
+ * This schema provides runtime validation and type inference.
5
+ * */
6
+ import { z } from 'zod'
7
+
8
+ export const CfgKnowbaseSystemArchivesCreateRequestSchema = z.object({
9
+ file: z.union([z.instanceof(File), z.instanceof(Blob)]).optional(),
10
+ title: z.string().optional(),
11
+ description: z.string().optional(),
12
+ category_ids: z.array(z.string()).optional(),
13
+ is_public: z.boolean().optional(),
14
+ process_immediately: z.boolean().optional(),
15
+ })
16
+
17
+ /**
18
+ * Infer TypeScript type from Zod schema
19
+ */
20
+ export type CfgKnowbaseSystemArchivesCreateRequest = z.infer<typeof CfgKnowbaseSystemArchivesCreateRequestSchema>
@@ -17,7 +17,7 @@ export const DocumentArchiveSchema = z.object({
17
17
  description: z.string().optional(),
18
18
  categories: z.array(DocumentCategorySchema),
19
19
  is_public: z.boolean().optional(),
20
- archive_file: z.url(),
20
+ archive_file: z.union([z.url(), z.literal('')]),
21
21
  original_filename: z.string(),
22
22
  file_size: z.int(),
23
23
  archive_type: z.nativeEnum(Enums.DocumentArchiveArchiveType),
@@ -18,7 +18,7 @@ export const DocumentArchiveDetailSchema = z.object({
18
18
  description: z.string().optional(),
19
19
  categories: z.array(DocumentCategorySchema),
20
20
  is_public: z.boolean().optional(),
21
- archive_file: z.url(),
21
+ archive_file: z.union([z.url(), z.literal('')]),
22
22
  original_filename: z.string(),
23
23
  file_size: z.int(),
24
24
  archive_type: z.nativeEnum(Enums.DocumentArchiveDetailArchiveType),
@@ -27,6 +27,7 @@ export * from './ArchiveProcessingResult.schema'
27
27
  export * from './ArchiveSearchRequestRequest.schema'
28
28
  export * from './ArchiveSearchResult.schema'
29
29
  export * from './ArchiveStatistics.schema'
30
+ export * from './CfgKnowbaseSystemArchivesCreateRequest.schema'
30
31
  export * from './ChatHistory.schema'
31
32
  export * from './ChatMessage.schema'
32
33
  export * from './ChatQueryRequest.schema'
@@ -1,29 +1,37 @@
1
1
  // Auto-generated by DjangoCFG - see CLAUDE.md
2
2
  /**
3
- * Global API Instance - Singleton configuration
3
+ * Global API Instance - Singleton configuration with auto-configuration support
4
4
  *
5
- * This module provides a global API instance that can be configured once
6
- * and used throughout your application.
5
+ * This module provides a global API instance that auto-configures from
6
+ * environment variables or can be configured manually.
7
7
  *
8
- * Usage:
8
+ * AUTO-CONFIGURATION (recommended):
9
+ * Set one of these environment variables and the API will auto-configure:
10
+ * - NEXT_PUBLIC_API_URL (Next.js)
11
+ * - VITE_API_URL (Vite)
12
+ * - REACT_APP_API_URL (Create React App)
13
+ * - API_URL (generic)
14
+ *
15
+ * Then just use fetchers and hooks directly:
16
+ * ```typescript
17
+ * import { getUsers } from './_utils/fetchers'
18
+ * const users = await getUsers({ page: 1 })
19
+ * ```
20
+ *
21
+ * MANUAL CONFIGURATION:
9
22
  * ```typescript
10
- * // Configure once (e.g., in your app entry point)
11
23
  * import { configureAPI } from './api-instance'
12
24
  *
13
25
  * configureAPI({
14
26
  * baseUrl: 'https://api.example.com',
15
27
  * token: 'your-jwt-token'
16
28
  * })
17
- *
18
- * // Then use fetchers and hooks anywhere without configuration
19
- * import { getUsers } from './fetchers'
20
- * const users = await getUsers({ page: 1 })
21
29
  * ```
22
30
  *
23
31
  * For SSR or multiple instances:
24
32
  * ```typescript
25
33
  * import { API } from './index'
26
- * import { getUsers } from './fetchers'
34
+ * import { getUsers } from './_utils/fetchers'
27
35
  *
28
36
  * const api = new API('https://api.example.com')
29
37
  * const users = await getUsers({ page: 1 }, api)
@@ -33,27 +41,67 @@
33
41
  import { API, type APIOptions } from './index'
34
42
 
35
43
  let globalAPI: API | null = null
44
+ let autoConfigAttempted = false
45
+
46
+ /**
47
+ * Auto-configure from environment variable if available (Next.js pattern)
48
+ * This allows hooks and fetchers to work without explicit configureAPI() call
49
+ *
50
+ * Supported environment variables:
51
+ * - NEXT_PUBLIC_API_URL (Next.js)
52
+ * - VITE_API_URL (Vite)
53
+ * - REACT_APP_API_URL (Create React App)
54
+ * - API_URL (generic)
55
+ */
56
+ function tryAutoConfigureFromEnv(): void {
57
+ // Only attempt once
58
+ if (autoConfigAttempted) return
59
+ autoConfigAttempted = true
60
+
61
+ // Skip if already configured
62
+ if (globalAPI) return
63
+
64
+ // Skip if process is not available (pure browser without bundler)
65
+ if (typeof process === 'undefined' || !process.env) return
66
+
67
+ // Try different environment variable patterns
68
+ const baseUrl =
69
+ process.env.NEXT_PUBLIC_API_URL ||
70
+ process.env.VITE_API_URL ||
71
+ process.env.REACT_APP_API_URL ||
72
+ process.env.API_URL
73
+
74
+ if (baseUrl) {
75
+ globalAPI = new API(baseUrl)
76
+ }
77
+ }
36
78
 
37
79
  /**
38
80
  * Get the global API instance
39
- * @throws Error if API is not configured
81
+ * Auto-configures from environment variables on first call if not manually configured.
82
+ * @throws Error if API is not configured and no env variable is set
40
83
  */
41
84
  export function getAPIInstance(): API {
85
+ // Try auto-configuration on first access (lazy initialization)
86
+ tryAutoConfigureFromEnv()
87
+
42
88
  if (!globalAPI) {
43
89
  throw new Error(
44
90
  'API not configured. Call configureAPI() with your base URL before using fetchers or hooks.\n\n' +
45
91
  'Example:\n' +
46
92
  ' import { configureAPI } from "./api-instance"\n' +
47
- ' configureAPI({ baseUrl: "https://api.example.com" })'
93
+ ' configureAPI({ baseUrl: "https://api.example.com" })\n\n' +
94
+ 'Or set environment variable: NEXT_PUBLIC_API_URL, VITE_API_URL, or REACT_APP_API_URL'
48
95
  )
49
96
  }
50
97
  return globalAPI
51
98
  }
52
99
 
53
100
  /**
54
- * Check if API is configured
101
+ * Check if API is configured (or can be auto-configured)
55
102
  */
56
103
  export function isAPIConfigured(): boolean {
104
+ tryAutoConfigureFromEnv()
57
105
  return globalAPI !== null
58
106
  }
59
107
 
@@ -25,6 +25,7 @@ export class APIClient {
25
25
  private httpClient: HttpClientAdapter;
26
26
  private logger: APILogger | null = null;
27
27
  private retryConfig: RetryConfig | null = null;
28
+ private tokenGetter: (() => string | null) | null = null;
28
29
 
29
30
  // Sub-clients
30
31
  public ext_knowbase_knowbase: ExtKnowbaseKnowbase;
@@ -35,10 +36,12 @@ export class APIClient {
35
36
  httpClient?: HttpClientAdapter;
36
37
  loggerConfig?: Partial<LoggerConfig>;
37
38
  retryConfig?: RetryConfig;
39
+ tokenGetter?: () => string | null;
38
40
  }
39
41
  ) {
40
42
  this.baseUrl = baseUrl.replace(/\/$/, '');
41
43
  this.httpClient = options?.httpClient || new FetchAdapter();
44
+ this.tokenGetter = options?.tokenGetter || null;
42
45
 
43
46
  // Initialize logger if config provided
44
47
  if (options?.loggerConfig !== undefined) {
@@ -69,6 +72,21 @@ export class APIClient {
69
72
  return null;
70
73
  }
71
74
 
75
+ /**
76
+ * Get the base URL for building streaming/download URLs.
77
+ */
78
+ getBaseUrl(): string {
79
+ return this.baseUrl;
80
+ }
81
+
82
+ /**
83
+ * Get JWT token for URL authentication (used in streaming endpoints).
84
+ * Returns null if no token getter is configured or no token is available.
85
+ */
86
+ getToken(): string | null {
87
+ return this.tokenGetter ? this.tokenGetter() : null;
88
+ }
89
+
72
90
  /**
73
91
  * Make HTTP request with Django CSRF and session handling.
74
92
  * Automatically retries on network errors and 5xx server errors.
@@ -80,6 +98,7 @@ export class APIClient {
80
98
  params?: Record<string, any>;
81
99
  body?: any;
82
100
  formData?: FormData;
101
+ binaryBody?: Blob | ArrayBuffer;
83
102
  headers?: Record<string, string>;
84
103
  }
85
104
  ): Promise<T> {
@@ -116,6 +135,7 @@ export class APIClient {
116
135
  params?: Record<string, any>;
117
136
  body?: any;
118
137
  formData?: FormData;
138
+ binaryBody?: Blob | ArrayBuffer;
119
139
  headers?: Record<string, string>;
120
140
  }
121
141
  ): Promise<T> {
@@ -129,8 +149,8 @@ export class APIClient {
129
149
  ...(options?.headers || {})
130
150
  };
131
151
 
132
- // Don't set Content-Type for FormData (browser will set it with boundary)
133
- if (!options?.formData && !headers['Content-Type']) {
152
+ // Don't set Content-Type for FormData/binaryBody (browser will set it with boundary)
153
+ if (!options?.formData && !options?.binaryBody && !headers['Content-Type']) {
134
154
  headers['Content-Type'] = 'application/json';
135
155
  }
136
156
 
@@ -157,6 +177,7 @@ export class APIClient {
157
177
  params: options?.params,
158
178
  body: options?.body,
159
179
  formData: options?.formData,
180
+ binaryBody: options?.binaryBody,
160
181
  });
161
182
 
162
183
  const duration = Date.now() - startTime;
@@ -356,8 +356,15 @@ export class ExtKnowbaseKnowbase {
356
356
  *
357
357
  * Upload archive file and process it synchronously
358
358
  */
359
- async systemArchivesCreate(data: FormData): Promise<Models.ArchiveProcessingResult> {
360
- const response = await this.client.request('POST', "/cfg/knowbase/system/archives/", { formData: data });
359
+ async systemArchivesCreate(data: Models.CfgKnowbaseSystemArchivesCreateRequest): Promise<Models.ArchiveProcessingResult> {
360
+ const formData = new FormData();
361
+ formData.append('file', data.file);
362
+ if (data.title !== undefined) formData.append('title', String(data.title));
363
+ if (data.description !== undefined) formData.append('description', String(data.description));
364
+ if (data.category_ids !== undefined) formData.append('category_ids', String(data.category_ids));
365
+ if (data.is_public !== undefined) formData.append('is_public', String(data.is_public));
366
+ if (data.process_immediately !== undefined) formData.append('process_immediately', String(data.process_immediately));
367
+ const response = await this.client.request('POST', "/cfg/knowbase/system/archives/", { formData });
361
368
  return response;
362
369
  }
363
370
 
@@ -434,6 +434,19 @@ export interface PaginatedDocumentArchiveListList {
434
434
  results: Array<DocumentArchiveList>;
435
435
  }
436
436
 
437
+ /**
438
+ *
439
+ * Request model (no read-only fields).
440
+ */
441
+ export interface CfgKnowbaseSystemArchivesCreateRequest {
442
+ file?: File | Blob;
443
+ title?: string;
444
+ description?: string;
445
+ category_ids?: Array<string>;
446
+ is_public?: boolean;
447
+ process_immediately?: boolean;
448
+ }
449
+
437
450
  /**
438
451
  * Archive processing result serializer.
439
452
  *
@@ -14,6 +14,8 @@ export interface HttpRequest {
14
14
  params?: Record<string, any>;
15
15
  /** FormData for file uploads (multipart/form-data) */
16
16
  formData?: FormData;
17
+ /** Binary data for octet-stream uploads */
18
+ binaryBody?: Blob | ArrayBuffer;
17
19
  }
18
20
 
19
21
  export interface HttpResponse<T = any> {
@@ -37,7 +39,7 @@ export interface HttpClientAdapter {
37
39
  */
38
40
  export class FetchAdapter implements HttpClientAdapter {
39
41
  async request<T = any>(request: HttpRequest): Promise<HttpResponse<T>> {
40
- const { method, url, headers, body, params, formData } = request;
42
+ const { method, url, headers, body, params, formData, binaryBody } = request;
41
43
 
42
44
  // Build URL with query params
43
45
  let finalUrl = url;
@@ -58,12 +60,16 @@ export class FetchAdapter implements HttpClientAdapter {
58
60
  const finalHeaders: Record<string, string> = { ...headers };
59
61
 
60
62
  // Determine body and content-type
61
- let requestBody: string | FormData | undefined;
63
+ let requestBody: string | FormData | Blob | ArrayBuffer | undefined;
62
64
 
63
65
  if (formData) {
64
66
  // For multipart/form-data, let browser set Content-Type with boundary
65
67
  requestBody = formData;
66
68
  // Don't set Content-Type - browser will set it with boundary
69
+ } else if (binaryBody) {
70
+ // Binary upload (application/octet-stream)
71
+ finalHeaders['Content-Type'] = 'application/octet-stream';
72
+ requestBody = binaryBody;
67
73
  } else if (body) {
68
74
  // JSON request
69
75
  finalHeaders['Content-Type'] = 'application/json';
@@ -133,10 +133,11 @@ export class API {
133
133
 
134
134
  this._loadTokensFromStorage();
135
135
 
136
- // Initialize APIClient
136
+ // Initialize APIClient with token getter for URL authentication
137
137
  this._client = new APIClient(this.baseUrl, {
138
138
  retryConfig: this.options?.retryConfig,
139
139
  loggerConfig: this.options?.loggerConfig,
140
+ tokenGetter: () => this.getToken(),
140
141
  });
141
142
 
142
143
  // Always inject auth header wrapper (reads token dynamically from storage)
@@ -155,6 +156,7 @@ export class API {
155
156
  this._client = new APIClient(this.baseUrl, {
156
157
  retryConfig: this.options?.retryConfig,
157
158
  loggerConfig: this.options?.loggerConfig,
159
+ tokenGetter: () => this.getToken(),
158
160
  });
159
161
 
160
162
  // Always inject auth header wrapper (reads token dynamically from storage)
package/src/api/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { createExtensionAPI } from '@djangocfg/ext-base/api';
1
+ import { createExtensionAPI, initializeExtensionAPI } from '@djangocfg/ext-base/api';
2
2
 
3
3
  /**
4
4
  * Knowbase Extension API
@@ -6,7 +6,12 @@ import { createExtensionAPI } from '@djangocfg/ext-base/api';
6
6
  * Pre-configured API instance with shared authentication
7
7
  */
8
8
  import { API } from './generated/ext_knowbase';
9
+ import { configureAPI } from './generated/ext_knowbase/api-instance';
9
10
 
11
+ // Initialize global API singleton for hooks and fetchers
12
+ initializeExtensionAPI(configureAPI);
13
+
14
+ // Create instance for direct usage
10
15
  export const apiKnowbase = createExtensionAPI(API);
11
16
 
12
17
  // Export types, fetchers, and hooks
@@ -31,6 +31,7 @@ import type {
31
31
  PaginatedDocumentArchiveListList,
32
32
  DocumentArchive,
33
33
  DocumentArchiveDetail,
34
+ ArchiveCreateRequest,
34
35
  PatchedDocumentArchiveRequest,
35
36
  ArchiveProcessingResult,
36
37
  ArchiveStatistics,
@@ -66,7 +67,7 @@ export interface KnowbaseDocumentsContextValue {
66
67
  // Archive operations
67
68
  getArchive: (id: string) => Promise<DocumentArchiveDetail | undefined>;
68
69
  getArchiveStatistics: () => Promise<ArchiveStatistics | undefined>;
69
- createArchive: (data: FormData) => Promise<ArchiveProcessingResult>;
70
+ createArchive: (data: ArchiveCreateRequest) => Promise<ArchiveProcessingResult>;
70
71
  updateArchive: (
71
72
  id: string,
72
73
  title: string,
@@ -196,7 +197,7 @@ export function KnowbaseDocumentsProvider({ children }: { children: ReactNode })
196
197
  };
197
198
 
198
199
  // Create archive
199
- const createArchive = async (data: FormData): Promise<ArchiveProcessingResult> => {
200
+ const createArchive = async (data: ArchiveCreateRequest): Promise<ArchiveProcessingResult> => {
200
201
  const result = await createArchiveMutation(data, apiKnowbase);
201
202
  await refreshArchives();
202
203
  return result as unknown as ArchiveProcessingResult;
@@ -293,6 +294,7 @@ export type {
293
294
  PaginatedDocumentArchiveListList,
294
295
  DocumentArchive,
295
296
  DocumentArchiveDetail,
297
+ ArchiveCreateRequest,
296
298
  PatchedDocumentArchiveRequest,
297
299
  ArchiveProcessingResult,
298
300
  ArchiveStatistics,
@@ -54,6 +54,7 @@ export type DocumentStats = NonNullable<Awaited<ReturnType<typeof useKnowbaseAdm
54
54
  export type PaginatedDocumentArchiveListList = NonNullable<Awaited<ReturnType<typeof useKnowbaseSystemArchivesList>>['data']>;
55
55
  export type DocumentArchive = NonNullable<Awaited<ReturnType<typeof useKnowbaseSystemArchivesRetrieve>>['data']>;
56
56
  export type DocumentArchiveDetail = NonNullable<Awaited<ReturnType<typeof useKnowbaseSystemArchivesRetrieve>>['data']>;
57
+ export type ArchiveCreateRequest = Parameters<ReturnType<typeof useCreateKnowbaseSystemArchivesCreate>>[0];
57
58
  export type PatchedDocumentArchiveRequest = Parameters<ReturnType<typeof usePartialUpdateKnowbaseSystemArchivesPartialUpdate>>[1];
58
59
  export type ArchiveProcessingResult = Awaited<ReturnType<ReturnType<typeof useCreateKnowbaseSystemArchivesCreate>>>;
59
60
  export type ArchiveStatistics = NonNullable<Awaited<ReturnType<typeof useKnowbaseSystemArchivesStatisticsRetrieve>>['data']>;