Skip to content

Commit

Permalink
chore: Refactored vectors to use uploadUrl and include returnFile for…
Browse files Browse the repository at this point in the history
… queries
  • Loading branch information
stevedylandev committed Dec 4, 2024
1 parent a5b6447 commit 106bac1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/core/files/deleteFileVectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const deleteFileVectors = async (

let endpoint: string = "https://uploads.pinata.cloud/v3";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
if (config.uploadUrl) {
endpoint = config.uploadUrl;
}

try {
Expand Down
4 changes: 2 additions & 2 deletions src/core/files/vectorizeFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const vectorizeFile = async (

let endpoint: string = "https://uploads.pinata.cloud/v3";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
if (config.uploadUrl) {
endpoint = config.uploadUrl;
}

try {
Expand Down
20 changes: 17 additions & 3 deletions src/core/files/vectorizeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
GetCIDResponse,
PinataConfig,
VectorizeQuery,
VectorizeQueryResponse,
Expand All @@ -10,10 +11,12 @@ import {
ValidationError,
} from "../../utils/custom-errors";

import { getCid } from "../gateway/getCid";

export const vectorizeQuery = async (
config: PinataConfig | undefined,
options: VectorizeQuery,
): Promise<VectorizeQueryResponse> => {
): Promise<VectorizeQueryResponse | GetCIDResponse> => {
if (!config) {
throw new ValidationError("Pinata configuration is missing");
}
Expand All @@ -34,8 +37,8 @@ export const vectorizeQuery = async (

let endpoint: string = "https://uploads.pinata.cloud/v3";

if (config.endpointUrl) {
endpoint = config.endpointUrl;
if (config.uploadUrl) {
endpoint = config.uploadUrl;
}

const body = JSON.stringify({
Expand Down Expand Up @@ -67,8 +70,19 @@ export const vectorizeQuery = async (
errorData,
);
}

const res = await request.json();
const resData: VectorizeQueryResponse = res.data;

if (options.returnFile) {
if (resData.matches.length === 0) {
throw new PinataError(`No files returned in query to fetch`);
}
const cid = resData.matches[0].cid;
const fileRes: GetCIDResponse = await getCid(config, cid, undefined);
return fileRes;
}

return resData;
} catch (error) {
if (error instanceof PinataError) {
Expand Down
4 changes: 3 additions & 1 deletion src/core/pinataSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ class Files {
return vectorizeFile(this.config, fileId);
}

queryVectors(options: VectorizeQuery): Promise<VectorizeQueryResponse> {
queryVectors(
options: VectorizeQuery,
): Promise<VectorizeQueryResponse | GetCIDResponse> {
return vectorizeQuery(this.config, options);
}

Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ export type VectorizeFileResponse = {
export type VectorizeQuery = {
groupId: string;
query: string;
returnFile?: boolean;
};

export type VectorQueryMatch = {
Expand Down

0 comments on commit 106bac1

Please sign in to comment.