-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add search.query event api for search results (#1241)
- Loading branch information
Showing
5 changed files
with
85 additions
and
3 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
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { VtexCommerce } from './commerce' | ||
import { IntelligentSearch } from './search' | ||
import { SP } from './sp' | ||
import type { Context, Options } from '..' | ||
|
||
export type Clients = ReturnType<typeof getClients> | ||
|
||
export const getClients = (options: Options, ctx: Context) => { | ||
const search = IntelligentSearch(options, ctx) | ||
const commerce = VtexCommerce(options, ctx) | ||
const sp = SP(options, ctx) | ||
|
||
return { | ||
search, | ||
commerce, | ||
sp, | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Client for SP, Intelligent search's analytics event API | ||
* More info at: https://www.notion.so/vtexhandbook/Event-API-Documentation-48eee26730cf4d7f80f8fd7262231f84 | ||
*/ | ||
import { fetchAPI } from '../fetch' | ||
import type { Options, Context } from '../../index' | ||
|
||
const THIRTY_MINUTES_S = 30 * 60 | ||
const ONE_YEAR_S = 365 * 24 * 3600 | ||
|
||
const randomUUID = () => (Math.random() * 1e6).toFixed(0) | ||
|
||
const timelapsed = (past: number) => (Date.now() - past) / 1e3 | ||
|
||
const createId = (expiresSecond: number) => { | ||
let payload = randomUUID() | ||
let createdAt = Date.now() | ||
|
||
return () => { | ||
if (timelapsed(createdAt) > expiresSecond) { | ||
payload = randomUUID() | ||
createdAt = Date.now() | ||
} | ||
|
||
return payload | ||
} | ||
} | ||
|
||
const user = { | ||
anonymous: createId(ONE_YEAR_S), | ||
session: createId(THIRTY_MINUTES_S), | ||
} | ||
|
||
export type SearchEvent = { | ||
type: 'search.query' | ||
text: string // 'zapatilha' | ||
misspelled: boolean | ||
match: number | ||
operator: 'and' | 'or' | ||
session?: string | ||
anonymous?: string | ||
} | ||
|
||
export const SP = ({ account }: Options, _: Context) => { | ||
const base = `https://sp.vtex.com/event-api/v1/${account}/event` | ||
|
||
const sendEvent = (options: SearchEvent) => { | ||
return fetchAPI(base, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
...options, | ||
agent: '@faststore/api', | ||
anonymous: user.anonymous(), | ||
session: user.session(), | ||
// session: 'zZlNhqz1vFJP6iPG5Oqtt', | ||
// anonymous: 'Om1TNluGvgmSgU5OOTvkkd', | ||
}), | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}) | ||
} | ||
|
||
return { | ||
sendEvent, | ||
} | ||
} |
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
6895d28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
faststore – ./
faststore-faststore-docs.vercel.app
faststore-git-master-faststore-docs.vercel.app
faststore-docs.vercel.app