-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from PinataCloud/feat/vectors
feat/vectors
- Loading branch information
Showing
9 changed files
with
388 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { PinataConfig, VectorizeFileResponse } from "../types"; | ||
import { | ||
PinataError, | ||
NetworkError, | ||
AuthenticationError, | ||
ValidationError, | ||
} from "../../utils/custom-errors"; | ||
|
||
export const deleteFileVectors = async ( | ||
config: PinataConfig | undefined, | ||
fileId: string, | ||
): Promise<VectorizeFileResponse> => { | ||
if (!config) { | ||
throw new ValidationError("Pinata configuration is missing"); | ||
} | ||
|
||
let headers: Record<string, string>; | ||
|
||
if (config.customHeaders && Object.keys(config.customHeaders).length > 0) { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
...config.customHeaders, | ||
}; | ||
} else { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
Source: "sdk/vectorizeFile", | ||
}; | ||
} | ||
|
||
let endpoint: string = "https://uploads.pinata.cloud/v3"; | ||
|
||
if (config.endpointUrl) { | ||
endpoint = config.endpointUrl; | ||
} | ||
|
||
try { | ||
const request = await fetch(`${endpoint}/vectorize/files/${fileId}`, { | ||
method: "DELETE", | ||
headers: headers, | ||
}); | ||
|
||
if (!request.ok) { | ||
const errorData = await request.text(); | ||
if (request.status === 401 || request.status === 403) { | ||
throw new AuthenticationError( | ||
`Authentication failed: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
throw new NetworkError( | ||
`HTTP error: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
|
||
const res: VectorizeFileResponse = await request.json(); | ||
return res; | ||
} catch (error) { | ||
if (error instanceof PinataError) { | ||
throw error; | ||
} | ||
if (error instanceof Error) { | ||
throw new PinataError( | ||
`Error processing vectorize file: ${error.message}`, | ||
); | ||
} | ||
throw new PinataError("An unknown error occurred while vectorizing file"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { PinataConfig, VectorizeFileResponse } from "../types"; | ||
import { | ||
PinataError, | ||
NetworkError, | ||
AuthenticationError, | ||
ValidationError, | ||
} from "../../utils/custom-errors"; | ||
|
||
export const vectorizeFile = async ( | ||
config: PinataConfig | undefined, | ||
fileId: string, | ||
): Promise<VectorizeFileResponse> => { | ||
if (!config) { | ||
throw new ValidationError("Pinata configuration is missing"); | ||
} | ||
|
||
let headers: Record<string, string>; | ||
|
||
if (config.customHeaders && Object.keys(config.customHeaders).length > 0) { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
...config.customHeaders, | ||
}; | ||
} else { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
Source: "sdk/vectorizeFile", | ||
}; | ||
} | ||
|
||
let endpoint: string = "https://uploads.pinata.cloud/v3"; | ||
|
||
if (config.endpointUrl) { | ||
endpoint = config.endpointUrl; | ||
} | ||
|
||
try { | ||
const request = await fetch(`${endpoint}/vectorize/files/${fileId}`, { | ||
method: "POST", | ||
headers: headers, | ||
}); | ||
|
||
if (!request.ok) { | ||
const errorData = await request.text(); | ||
if (request.status === 401 || request.status === 403) { | ||
throw new AuthenticationError( | ||
`Authentication failed: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
throw new NetworkError( | ||
`HTTP error: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
|
||
const res: VectorizeFileResponse = await request.json(); | ||
return res; | ||
} catch (error) { | ||
if (error instanceof PinataError) { | ||
throw error; | ||
} | ||
if (error instanceof Error) { | ||
throw new PinataError( | ||
`Error processing vectorize file: ${error.message}`, | ||
); | ||
} | ||
throw new PinataError("An unknown error occurred while vectorizing file"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { | ||
PinataConfig, | ||
VectorizeQuery, | ||
VectorizeQueryResponse, | ||
} from "../types"; | ||
import { | ||
PinataError, | ||
NetworkError, | ||
AuthenticationError, | ||
ValidationError, | ||
} from "../../utils/custom-errors"; | ||
|
||
export const vectorizeQuery = async ( | ||
config: PinataConfig | undefined, | ||
options: VectorizeQuery, | ||
): Promise<VectorizeQueryResponse> => { | ||
if (!config) { | ||
throw new ValidationError("Pinata configuration is missing"); | ||
} | ||
|
||
let headers: Record<string, string>; | ||
|
||
if (config.customHeaders && Object.keys(config.customHeaders).length > 0) { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
...config.customHeaders, | ||
}; | ||
} else { | ||
headers = { | ||
Authorization: `Bearer ${config.pinataJwt}`, | ||
Source: "sdk/vectorQuery", | ||
}; | ||
} | ||
|
||
let endpoint: string = "https://uploads.pinata.cloud/v3"; | ||
|
||
if (config.endpointUrl) { | ||
endpoint = config.endpointUrl; | ||
} | ||
|
||
const body = JSON.stringify({ | ||
text: options.query, | ||
}); | ||
|
||
try { | ||
const request = await fetch( | ||
`${endpoint}/vectorize/groups/${options.groupId}/query`, | ||
{ | ||
method: "POST", | ||
headers: headers, | ||
body: body, | ||
}, | ||
); | ||
|
||
if (!request.ok) { | ||
const errorData = await request.text(); | ||
if (request.status === 401 || request.status === 403) { | ||
throw new AuthenticationError( | ||
`Authentication failed: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
throw new NetworkError( | ||
`HTTP error: ${errorData}`, | ||
request.status, | ||
errorData, | ||
); | ||
} | ||
const res = await request.json(); | ||
const resData: VectorizeQueryResponse = res.data; | ||
return resData; | ||
} catch (error) { | ||
if (error instanceof PinataError) { | ||
throw error; | ||
} | ||
if (error instanceof Error) { | ||
throw new PinataError( | ||
`Error processing vectorize file: ${error.message}`, | ||
); | ||
} | ||
throw new PinataError("An unknown error occurred while vectorizing file"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.