11import { isValidSignature , SIGNATURE_HEADER_NAME } from "@sanity/webhook" ;
2- import algoliasearch from "algoliasearch" ;
2+ import { algoliasearch } from "algoliasearch" ;
33
44const secret = process . env . PRIVATE_ALGOLIA_WEBOOK_SECRET ;
55const algoliaAdminApiKey = process . env . PRIVATE_ALGOLIA_ADMIN_API_KEY ;
66const algoliaAppId = process . env . NEXT_PUBLIC_ALGOLIA_APP_ID ;
77const algoliaIndex = process . env . NEXT_PUBLIC_ALGOLIA_INDEX ;
88
9- if ( ! algoliaAdminApiKey ) throw "Missing PRIVATE_ALGOLIA_ADMIN_API_KEY" ;
10- if ( ! algoliaAppId ) throw "Missing NEXT_PUBLIC_ALGOLIA_APP_ID" ;
11- if ( ! algoliaIndex ) throw "Missing NEXT_PUBLIC_ALGOLIA_INDEX" ;
9+ if ( ! algoliaAdminApiKey ) {
10+ throw "Missing PRIVATE_ALGOLIA_ADMIN_API_KEY" ;
11+ }
12+ if ( ! algoliaAppId ) {
13+ throw "Missing NEXT_PUBLIC_ALGOLIA_APP_ID" ;
14+ }
15+ if ( ! algoliaIndex ) {
16+ throw "Missing NEXT_PUBLIC_ALGOLIA_INDEX" ;
17+ }
1218
13- const algolia = algoliasearch ( algoliaAppId , algoliaAdminApiKey ) ;
14- const index = algolia . initIndex ( algoliaIndex ) ;
19+ const client = algoliasearch ( algoliaAppId , algoliaAdminApiKey ) ;
1520
1621function toAlgoliaObject ( sanityDoc : any ) {
1722 const doc = { ...sanityDoc } ;
23+ // biome-ignore lint/performance/noDelete: <explanation>
1824 delete doc . content ;
1925
2026 return (
@@ -41,19 +47,21 @@ function toAlgoliaObject(sanityDoc: any) {
4147}
4248
4349export async function POST ( request : Request ) {
44- if ( ! secret )
50+ if ( ! secret ) {
4551 return Response . json (
4652 { success : false , error : "Missing Secret PRIVATE_ALGOLIA_WEBOOK_SECRET" } ,
4753 { status : 400 } ,
4854 ) ;
55+ }
4956
5057 const signature = request . headers . get ( SIGNATURE_HEADER_NAME ) ;
5158
52- if ( ! signature )
59+ if ( ! signature ) {
5360 return Response . json (
5461 { success : false , error : "Missing Signature Header" } ,
5562 { status : 401 } ,
5663 ) ;
64+ }
5765
5866 const body = await request . text ( ) ;
5967 if ( ! ( await isValidSignature ( body , signature , secret ) ) ) {
@@ -81,19 +89,31 @@ export async function POST(request: Request) {
8189 const deleted = sanityDoc . deleted ;
8290 const updated = sanityDoc . updated ;
8391
92+ // biome-ignore lint/performance/noDelete: <explanation>
8493 delete sanityDoc . created ;
94+ // biome-ignore lint/performance/noDelete: <explanation>
8595 delete sanityDoc . deleted ;
96+ // biome-ignore lint/performance/noDelete: <explanation>
8697 delete sanityDoc . updated ;
8798
8899 const algoliaDoc = toAlgoliaObject ( sanityDoc ) ;
89100
90101 try {
91102 if ( created ) {
92- await index . saveObjects ( algoliaDoc ) . wait ( ) ;
103+ await client . saveObjects ( {
104+ indexName : algoliaIndex as string ,
105+ objects : [ algoliaDoc ] ,
106+ } ) ;
93107 } else if ( updated ) {
94- await index . saveObjects ( algoliaDoc ) . wait ( ) ;
108+ await client . saveObjects ( {
109+ indexName : algoliaIndex as string ,
110+ objects : [ algoliaDoc ] ,
111+ } ) ;
95112 } else {
96- await index . deleteObject ( sanityDoc . objectID ) . wait ( ) ;
113+ await client . deleteObject ( {
114+ indexName : algoliaIndex as string ,
115+ objectID : sanityDoc . objectID ,
116+ } ) ;
97117 }
98118 } catch ( e ) {
99119 const error = JSON . stringify ( e ) ;
0 commit comments