|
1
|
import { |
|
2
|
InstantSearchAdapterOptions, |
|
3
|
PUBLISHABLE_KEY_HEADER, |
|
4
|
SearchQuery, |
|
5
|
SearchRequester, |
|
6
|
SearchResult, |
|
7
|
} from "./types" |
|
8
|
|
|
9
|
function isAbsoluteUrl(value?: string): boolean { |
|
10
|
return !!value && /^https?:\/\//i.test(value) |
|
11
|
} |
|
12
|
|
|
13
|
export function resolveSearchUrl( |
|
14
|
baseUrl: string | undefined, |
|
15
|
path: string |
|
16
|
): string { |
|
17
|
if (isAbsoluteUrl(path)) { |
|
18
|
return path |
|
19
|
} |
|
20
|
if (!baseUrl) { |
|
21
|
throw new Error( |
|
22
|
"@medusajs/instantsearch-adapter: `baseUrl` is required when `path` is relative" |
|
23
|
) |
|
24
|
} |
|
25
|
const origin = baseUrl.replace(/\/$/, "") |
|
26
|
const suffix = path.startsWith("/") ? path : `/${path}` |
|
27
|
return `${origin}${suffix}` |
|
28
|
} |
|
29
|
|
|
30
|
function isSearchResult(value: unknown): value is SearchResult { |
|
31
|
return ( |
|
32
|
!!value && |
|
33
|
typeof value === "object" && |
|
34
|
"hits" in value && |
|
35
|
"metadata" in value |
|
36
|
) |
|
37
|
} |
|
38
|
|
|
39
|
function extractResults(payload: unknown): SearchResult[] { |
|
40
|
if (Array.isArray(payload)) { |
|
41
|
return payload as SearchResult[] |
|
42
|
} |
|
43
|
|
|
44
|
if (payload && typeof payload === "object") { |
|
45
|
if ("results" in payload && Array.isArray(payload.results)) { |
|
46
|
return payload.results as SearchResult[] |
|
47
|
} |
|
48
|
if (isSearchResult(payload)) { |
|
49
|
return [payload] |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
throw new Error( |
|
54
|
"@medusajs/instantsearch-adapter: unexpected search response shape. Expected `{ results: SearchResult[] }`, an array of results, or a single SearchResult." |
|
55
|
) |
|
56
|
} |
|
57
|
|
|
58
|
export function unwrapSearchResults( |
|
59
|
payload: unknown, |
|
60
|
expected: number |
|
61
|
): SearchResult[] { |
|
62
|
const results = extractResults(payload) |
|
63
|
if (results.length !== expected) { |
|
64
|
throw new Error( |
|
65
|
`@medusajs/instantsearch-adapter: expected ${expected} search result(s), received ${results.length}` |
|
66
|
) |
|
67
|
} |
|
68
|
return results |
|
69
|
} |
|
70
|
|
|
71
|
async function postJson( |
|
72
|
url: string, |
|
73
|
body: unknown, |
|
74
|
options: InstantSearchAdapterOptions |
|
75
|
): Promise<unknown> { |
|
76
|
const headers: Record<string, string> = { |
|
77
|
"content-type": "application/json", |
|
78
|
accept: "application/json", |
|
79
|
...options.headers, |
|
80
|
} |
|
81
|
|
|
82
|
if (options.publishableApiKey) { |
|
83
|
headers[PUBLISHABLE_KEY_HEADER] = options.publishableApiKey |
|
84
|
} |
|
85
|
|
|
86
|
const response = await fetch(url, { |
|
87
|
method: "POST", |
|
88
|
headers, |
|
89
|
body: JSON.stringify(body), |
|
90
|
}) |
|
91
|
|
|
92
|
const payload = await response.json().catch(() => ({})) |
|
93
|
|
|
94
|
if (!response.ok) { |
|
95
|
const message = |
|
96
|
payload && |
|
97
|
typeof payload === "object" && |
|
98
|
"message" in payload && |
|
99
|
typeof payload.message === "string" |
|
100
|
? payload.message |
|
101
|
: response.statusText || |
|
102
|
`Search request failed with status ${response.status}` |
|
103
|
throw new Error(message) |
|
104
|
} |
|
105
|
|
|
106
|
return payload |
|
107
|
} |
|
108
|
|
|
109
|
export function assertAdapterTransport( |
|
110
|
options: InstantSearchAdapterOptions |
|
111
|
): void { |
|
112
|
if (options.requester) { |
|
113
|
return |
|
114
|
} |
|
115
|
|
|
116
|
if (!options.path) { |
|
117
|
throw new Error("@medusajs/instantsearch-adapter: provide `path`") |
|
118
|
} |
|
119
|
|
|
120
|
if (!options.sdk && !options.baseUrl && !isAbsoluteUrl(options.path)) { |
|
121
|
throw new Error( |
|
122
|
"@medusajs/instantsearch-adapter: provide `sdk`, `requester`, or `baseUrl`" |
|
123
|
) |
|
124
|
} |
|
125
|
} |
|
126
|
|
|
127
|
export function createSearchRequester( |
|
128
|
options: InstantSearchAdapterOptions |
|
129
|
): SearchRequester { |
|
130
|
if (options.requester) { |
|
131
|
return options.requester |
|
132
|
} |
|
133
|
|
|
134
|
const path = options.path |
|
135
|
if (!path) { |
|
136
|
throw new Error("@medusajs/instantsearch-adapter: provide `path`") |
|
137
|
} |
|
138
|
|
|
139
|
const batch = options.batch !== false |
|
140
|
|
|
141
|
if (options.sdk) { |
|
142
|
const sdk = options.sdk |
|
143
|
return async (queries: SearchQuery[]) => { |
|
144
|
if (!batch) { |
|
145
|
const payloads = await Promise.all( |
|
146
|
queries.map((query) => |
|
147
|
sdk.client.fetch(path, { |
|
148
|
method: "POST", |
|
149
|
body: query, |
|
150
|
headers: options.headers, |
|
151
|
}) |
|
152
|
) |
|
153
|
) |
|
154
|
return payloads.map((payload) => unwrapSearchResults(payload, 1)[0]) |
|
155
|
} |
|
156
|
|
|
157
|
const payload = await sdk.client.fetch(path, { |
|
158
|
method: "POST", |
|
159
|
body: { queries }, |
|
160
|
headers: options.headers, |
|
161
|
}) |
|
162
|
return unwrapSearchResults(payload, queries.length) |
|
163
|
} |
|
164
|
} |
|
165
|
|
|
166
|
const url = resolveSearchUrl(options.baseUrl, path) |
|
167
|
|
|
168
|
return async (queries: SearchQuery[]) => { |
|
169
|
if (!batch) { |
|
170
|
const payloads = await Promise.all( |
|
171
|
queries.map((query) => postJson(url, query, options)) |
|
172
|
) |
|
173
|
return payloads.map((payload) => unwrapSearchResults(payload, 1)[0]) |
|
174
|
} |
|
175
|
|
|
176
|
const payload = await postJson(url, { queries }, options) |
|
177
|
return unwrapSearchResults(payload, queries.length) |
|
178
|
} |
|
179
|
} |