Files
nextjs-notion-starter-kit/lib/search-notion.ts

40 lines
906 B
TypeScript

import ExpiryMap from 'expiry-map'
import pMemoize from 'p-memoize'
import type * as types from './types'
import { api } from './config'
export const searchNotion = pMemoize(searchNotionImpl, {
cacheKey: (args) => args[0]?.query,
cache: new ExpiryMap(10_000)
})
async function searchNotionImpl(
params: types.SearchParams
): Promise<types.SearchResults> {
return fetch(api.searchNotion, {
method: 'POST',
body: JSON.stringify(params),
headers: {
'content-type': 'application/json'
}
})
.then((res) => {
if (res.ok) {
return res
}
// convert non-2xx HTTP responses into errors
const error: any = new Error(res.statusText)
error.response = res
throw error
})
.then((res) => res.json() as Promise<types.SearchResults>)
// return ky
// .post(api.searchNotion, {
// json: params
// })
// .json()
}