mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
42 lines
947 B
TypeScript
42 lines
947 B
TypeScript
// import ky from 'ky'
|
|
import fetch from 'isomorphic-unfetch'
|
|
import pMemoize from 'p-memoize'
|
|
import ExpiryMap from 'expiry-map'
|
|
|
|
import { api } from './config'
|
|
import * as types from './types'
|
|
|
|
export const searchNotion = pMemoize(searchNotionImpl, {
|
|
cacheKey: (args) => args[0]?.query,
|
|
cache: new ExpiryMap(10000)
|
|
})
|
|
|
|
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
|
|
return Promise.reject(error)
|
|
})
|
|
.then((res) => res.json())
|
|
|
|
// return ky
|
|
// .post(api.searchNotion, {
|
|
// json: params
|
|
// })
|
|
// .json()
|
|
}
|