This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add serializing capabilities
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 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,34 @@ | ||
import algoliaClient from 'algoliasearch'; | ||
import algoliaHelper from 'algoliasearch-helper'; | ||
|
||
export const serialize = function(helper) { | ||
if (!(helper instanceof algoliaHelper.AlgoliaSearchHelper)) { | ||
throw new TypeError('Serialize expects an algolia helper instance.'); | ||
} | ||
|
||
const client = helper.getClient(); | ||
|
||
const serialized = { | ||
searchParameters: Object.assign({}, helper.state), | ||
appId: client.applicationID, | ||
apiKey: client.apiKey, | ||
response: helper.lastResults._rawResults, | ||
}; | ||
|
||
return serialized; | ||
}; | ||
|
||
export const unserialize = function(data) { | ||
const client = algoliaClient(data.appId, data.apiKey); | ||
const helper = algoliaHelper( | ||
client, | ||
data.searchParameters.index, | ||
data.searchParameters | ||
); | ||
helper.lastResults = new algoliaHelper.SearchResults( | ||
helper.state, | ||
data.response | ||
); | ||
|
||
return helper; | ||
}; |
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