Created
August 28, 2023 08:18
-
-
Save onmax/c1b4adcdda8402d9d33e942219acbc1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import data from "./YOUR_JSON.json" assert { type: "json" }; | |
const apiEndpoint = 'https://{SUPABASE_REF}.supabase.co/rest/v1/rpc/upsert_location_with_gmaps_api'; | |
const apiKey = 'API_KEY'; | |
const authorizationBearer = 'YOU AUTH'; | |
const makeRequest = async (data: any[]) => { | |
const headers = new Headers(); | |
headers.append('Content-Type', 'application/json'); | |
headers.append('apikey', apiKey); | |
headers.append('Authorization', `Bearer ${authorizationBearer}`); | |
const requestOptions: RequestInit = { | |
method: 'POST', | |
headers: headers, | |
body: JSON.stringify({ p_locations: data }) | |
}; | |
try { | |
const response = await fetch(apiEndpoint, requestOptions); | |
const result = await response.json(); | |
return { success: true, data: result }; | |
} catch (error) { | |
return { success: false, data: data }; | |
} | |
}; | |
const main = async () => { | |
const chunkSize = 3; | |
const allAdded: any[] = []; | |
const allErrors: any[] = []; | |
const allMultiples: any[] = []; | |
const locations = data.p_locations; | |
for (let i = 0; i < locations.length; i += chunkSize) { | |
const chunk = locations.slice(i, i + chunkSize); | |
const result = await makeRequest(chunk); | |
if (result.success) { | |
allAdded.push(...result.data.added); | |
allErrors.push(...result.data.errors); | |
allMultiples.push(...result.data.multiples); | |
// print error messages | |
result.data.errors.forEach((error: any) => { | |
console.log(error.error); | |
}); | |
console.log(result.data.added.length); | |
} | |
} | |
await Deno.writeTextFile('./success.json', JSON.stringify(allAdded, null, 2)); | |
await Deno.writeTextFile('./errors.json', JSON.stringify(allErrors, null, 2)); | |
await Deno.writeTextFile('./multiple.json', JSON.stringify(allMultiples, null, 2)); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment