@lableb/javascript-sdk
Version:
Lableb cloud search client for javascript
68 lines (53 loc) • 2.61 kB
text/typescript
import { customIdentity, customPickBy } from '../../utils';
import { FeedbackDocument } from '../feedback/feedback.document.type';
import { GlobalRequestOptions } from '../main/main.request.type';
import { buildBatchSearchFeedbackRequestSchema } from './search-feedback.schema';
import { BatchSearchFeedbackRequestParams, BatchSearchFeedbackRequestResult, SingleSearchFeedbackRequestParams } from './search-feedback.request.type';
export async function buildSingleSearchFeedbackRequest(this: GlobalRequestOptions, params: SingleSearchFeedbackRequestParams): Promise<BatchSearchFeedbackRequestResult> {
const { documentFeedback, ...rest } = params;
const options: any = {
...rest,
}
if (documentFeedback) {
options.documentsFeedbacks = [documentFeedback];
}
return buildBatchSearchFeedbackRequest.bind(this)(options);
}
export async function buildBatchSearchFeedbackRequest(this: GlobalRequestOptions, params: BatchSearchFeedbackRequestParams): Promise<BatchSearchFeedbackRequestResult> {
const validatedParams = await buildBatchSearchFeedbackRequestSchema
.validate(
customPickBy({
...params,
indexName: params.indexName || this?.indexName || process.env.GLOBAL_DEFAULT_INDEX_NAME,
platformName: params.platformName || this?.platformName,
searchHandler: params.searchHandler || this?.searchHandler || process.env.GLOBAL_DEFAULT_SEARCH_HANDLER,
APIKey: params.APIKey || this?.APIKey,
documentsFeedbacks: params.documentsFeedbacks?.map(feedback => customPickBy({
...feedback,
userCountry: feedback.userCountry || this?.userCountry,
userId: feedback.userId || this?.userId,
userIp: feedback.userIp || this?.userIp,
sessionId:
feedback.sessionId ||
this?.sessionId ||
(this.sessionIdGenerator ? this.sessionIdGenerator() : undefined),
}, customIdentity)),
}, customIdentity)
);
const {
indexName,
platformName,
searchHandler,
APIKey,
documentsFeedbacks,
} = validatedParams;
return {
method: 'POST',
url: `${process.env.API_BASE_URL}/projects/${platformName}/indices/${indexName}/search/${searchHandler}/feedback/hits`,
params: {
apikey: APIKey,
},
headers: {},
body: documentsFeedbacks as FeedbackDocument[],
}
}