1+ import {
2+ VectorizeAPIConfig ,
3+ ConnectorConfig ,
4+ UserAction
5+ } from "../../baseOAuth/types" ;
6+ import {
7+ createSourceConnector ,
8+ manageUser ,
9+ getOneTimeConnectorToken as baseGetOneTimeConnectorToken
10+ } from "../../baseOAuth/core/apiFunctions" ;
11+ import { NotionConnectorType } from "../types" ;
12+
13+ /**
14+ * Create a Vectorize Notion OAuth Connector Source
15+ *
16+ * @param config - An object containing your organization ID and authorization token
17+ * @param connectorName - Name for the connector
18+ * @param platformUrl - URL of the Vectorize API (primarily used for testing)
19+ *
20+ * @returns A Promise that resolves with the connector ID
21+ */
22+ export async function createVectorizeNotionConnector (
23+ config : VectorizeAPIConfig ,
24+ connectorName : string ,
25+ platformUrl : string = "https://api.vectorize.io/v1" ,
26+ ) : Promise < any > {
27+ try {
28+
29+ const connector : ConnectorConfig = {
30+ name : connectorName ,
31+ type : NotionConnectorType . VECTORIZE
32+ } ;
33+
34+ try {
35+ const result = await createSourceConnector ( config , connector , platformUrl ) ;
36+ return result ;
37+ } catch ( error : unknown ) {
38+ // Format the error data properly to avoid [object Object]
39+ const innerError = error instanceof Error ? error : new Error ( typeof error === 'string' ? error : 'Unknown error' ) ;
40+
41+ // Extract and format error details
42+ let errorDetails = "" ;
43+ try {
44+ // Try to parse the error message if it contains JSON-like structure
45+ const errorMatch = innerError . message . match ( / E r r o r : \s * ( .* ) / ) ;
46+ if ( errorMatch && errorMatch [ 1 ] ) {
47+ try {
48+ // Attempt to parse if it looks like JSON
49+ if ( errorMatch [ 1 ] . includes ( '{' ) && errorMatch [ 1 ] . includes ( '}' ) ) {
50+ const jsonStr = errorMatch [ 1 ] . replace ( / \[ o b j e c t O b j e c t \] / g, '{}' ) ;
51+ const errorObj = JSON . parse ( jsonStr ) ;
52+ errorDetails = JSON . stringify ( errorObj , null , 2 ) ;
53+ } else {
54+ errorDetails = errorMatch [ 1 ] ;
55+ }
56+ } catch {
57+ errorDetails = errorMatch [ 1 ] ;
58+ }
59+ }
60+ } catch {
61+ // If parsing fails, use the original message
62+ errorDetails = innerError . message ;
63+ }
64+
65+ console . error ( `[Vectorize] Error in createSourceConnector: ${ innerError . message } ` ) ;
66+ console . error ( `[Vectorize] Error details (formatted):` , errorDetails ) ;
67+
68+ // Additional debug info about the error object
69+ if ( error instanceof Object ) {
70+ try {
71+ const errorKeys = Object . keys ( error ) ;
72+ console . error ( `[Vectorize] Error object keys:` , errorKeys ) ;
73+
74+ // Log all properties of the error object
75+ errorKeys . forEach ( key => {
76+ const value = ( error as any ) [ key ] ;
77+ const displayValue = typeof value === 'object' ? JSON . stringify ( value , null , 2 ) : value ;
78+ console . error ( `[Vectorize] Error.${ key } :` , displayValue ) ;
79+ } ) ;
80+ } catch ( err ) {
81+ console . error ( `[Vectorize] Could not enumerate error properties:` , err ) ;
82+ }
83+ }
84+
85+ console . error ( `[Vectorize] Stack trace:` , innerError . stack ) ;
86+ throw innerError ;
87+ }
88+ } catch ( error : unknown ) {
89+ const outerError = error instanceof Error
90+ ? error
91+ : new Error ( typeof error === 'string' ? error : 'Unknown error' ) ;
92+
93+ console . error ( `[Vectorize] Unexpected error in createVectorizeNotionConnector: ${ outerError . message } ` ) ;
94+ console . error ( `[Vectorize] Stack trace:` , outerError . stack ) ;
95+
96+ // Create a more informative error with properly formatted details
97+ let enhancedMessage = `Failed to create Vectorize Notion connector: ${ outerError . message } ` ;
98+
99+ // If we have a 500 error with [object Object], try to improve the message
100+ if ( outerError . message . includes ( '[object Object]' ) ) {
101+ enhancedMessage = `Failed to create Vectorize Notion connector (API returned 500 Internal Server Error). Check the server logs for more details.` ;
102+ }
103+
104+ throw new Error ( enhancedMessage ) ;
105+ }
106+ }
107+
108+ /**
109+ * Create a White Label Notion OAuth Connector Source
110+ *
111+ * @param config - An object containing your organization ID and authorization token
112+ * @param connectorName - Name for the connector
113+ * @param clientId - Notion API client ID for the white label connector
114+ * @param clientSecret - Notion API client secret for the white label connector
115+ * @param platformUrl - URL of the Vectorize API (primarily used for testing)
116+ *
117+ * @returns A Promise that resolves with the connector ID
118+ */
119+ export async function createWhiteLabelNotionConnector (
120+ config : VectorizeAPIConfig ,
121+ connectorName : string ,
122+ clientId : string ,
123+ clientSecret : string ,
124+ platformUrl : string = "https://api.vectorize.io/v1" ,
125+ ) : Promise < string > {
126+ if ( ! clientId || ! clientSecret ) {
127+ throw new Error ( "Client ID and Client Secret are required for white label connectors" ) ;
128+ }
129+
130+ const connector : ConnectorConfig = {
131+ name : connectorName ,
132+ type : NotionConnectorType . WHITE_LABEL ,
133+ config : {
134+ "client-id" : clientId ,
135+ "client-secret" : clientSecret
136+ }
137+ } ;
138+
139+ return createSourceConnector ( config , connector , platformUrl ) ;
140+ }
141+
142+ /**
143+ * Manages a Notion user for a connector, allowing you to add, edit, or remove users.
144+ *
145+ * @param config VectorizeAPIConfig containing authorization and organizationId
146+ * @param connectorId ID of the connector
147+ * @param selectedPages Record of selected pages with their metadata
148+ * @param accessToken Notion OAuth access token
149+ * @param userId User ID to manage
150+ * @param action Action to perform ("add", "edit", or "remove")
151+ * @param platformUrl Optional URL of the Vectorize API (primarily used for testing)
152+ * @returns Promise that resolves with the API response
153+ */
154+ export async function manageNotionUser (
155+ config : VectorizeAPIConfig ,
156+ connectorId : string ,
157+ selectedPages : Record < string , { title : string ; pageId : string ; parentType ?: string } > | null ,
158+ accessToken : string ,
159+ userId : string ,
160+ action : UserAction ,
161+ platformUrl : string = "https://api.vectorize.io/v1" ,
162+ ) : Promise < Response > {
163+ // Validate required parameters for add/edit actions
164+ if ( action === "add" || action === "edit" ) {
165+ if ( ! selectedPages || Object . keys ( selectedPages ) . length === 0 ) {
166+ throw new Error ( `Selected pages are required for ${ action } action` ) ;
167+ }
168+
169+ if ( ! accessToken ) {
170+ throw new Error ( `Access token is required for ${ action } action` ) ;
171+ }
172+ }
173+
174+ // Create the Notion specific payload
175+ const payload : Record < string , any > = { } ;
176+
177+ // Only include selectedPages and accessToken for add/edit, not for remove
178+ if ( action !== "remove" ) {
179+ payload . selectedPages = selectedPages ;
180+ payload . accessToken = accessToken ;
181+ }
182+
183+ return manageUser ( config , connectorId , userId , action , payload , platformUrl ) ;
184+ }
185+
186+ /**
187+ * Gets a one-time authentication token for connector operations
188+ * This is a direct re-export of the base function for consistency
189+ */
190+ export const getOneTimeConnectorToken = baseGetOneTimeConnectorToken ;
0 commit comments