Skip to content

Commit

Permalink
Merge pull request #11 from afosto/Feature/session-id
Browse files Browse the repository at this point in the history
Feature/session-id
  • Loading branch information
Rapid0o authored Mar 29, 2022
2 parents 06f99a8 + f9d4a03 commit 60b952b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@afosto/instant-search-client",
"version": "1.0.3",
"version": "1.0.4",
"private": false,
"description": "The Afosto InstantSearch client",
"main": "./dist/afosto-instant-search.min.js",
Expand Down
1 change: 1 addition & 0 deletions src/adapters/SearchRequestAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const SearchRequestAdapter = () => {
indices: [request.indexName],
q: query,
threshold: options.threshold || DEFAULT_OPTIONS.threshold,
session_id: request.session_id,
__queryID: request.__queryID,
...pagination,
}];
Expand Down
4 changes: 3 additions & 1 deletion src/afostoInstantSearch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { v4 as uuid } from 'uuid';
import { SearchResponseAdapter, SearchRequestAdapter } from './adapters';
import { DEFAULT_OPTIONS } from './constants';
import getSessionID from './utils/getSessionID';

/**
* Afosto instant search client
Expand All @@ -18,6 +19,7 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {
const url = clientOptions.baseUrl?.replace('{key}', searchEngineKey);
const searchRequestAdapter = SearchRequestAdapter();
const searchResponseAdapter = SearchResponseAdapter();
const sessionID = getSessionID();

const getSettings = async () => {
try {
Expand Down Expand Up @@ -56,7 +58,7 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {

const search = async requests => {
try {
const searchRequests = requests.map(request => ({ ...request, __queryID: uuid() }));
const searchRequests = requests.map(request => ({ ...request, session_id: sessionID, __queryID: uuid() }));
const searchContexts = searchRequestAdapter.transform(searchRequests, clientOptions);
const [searchRequestContext] = searchContexts;

Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export const OPERATOR_CONVERSION = {
'<=': 'lte',
'=': 'in',
};

export const SESSION_KEY = 'afostoInstantSearch_sessionID';
21 changes: 21 additions & 0 deletions src/utils/getSessionID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { v4 as uuid } from 'uuid';
import { SESSION_KEY } from '../constants';

const getSessionID = () => {
const hasSessionStorage = !!window.sessionStorage;

if (!hasSessionStorage) {
return uuid();
}

let sessionID = sessionStorage.getItem(SESSION_KEY);

if (!sessionID) {
sessionID = uuid();
sessionStorage.setItem(SESSION_KEY, sessionID);
}

return sessionID;
};

export default getSessionID;

0 comments on commit 60b952b

Please sign in to comment.