Skip to content

Commit

Permalink
fix: make garden REST API call compatible with local dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
th0rgall committed Mar 14, 2024
1 parent cb894a1 commit 89de6c8
Showing 1 changed file with 55 additions and 46 deletions.
101 changes: 55 additions & 46 deletions src/lib/api/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@ export const getAllListedGardens = async () => {

let appCheckTokenResponse;
try {
appCheckTokenResponse = await getToken(appCheck(), /* forceRefresh= */ false);
// Use AppCheck if it is initialized (not on localhost development, for example)
if (typeof import.meta.env.VITE_FIREBASE_APP_CHECK_PUBLIC_KEY !== 'undefined') {
appCheckTokenResponse = await getToken(appCheck(), /* forceRefresh= */ false);
}
} catch (err) {
// Handle any errors if the token was not retrieved.
console.error('Error fetching app check token:', err);
return;
}

Expand All @@ -135,55 +139,60 @@ export const getAllListedGardens = async () => {
do {
iteration++;

const url = `${
// Change the REST API base URL depending on the environment
import.meta.env.VITE_FIREBASE_PROJECT_ID === 'demo-test'
? 'http://127.0.0.1:8080/v1/projects/'
: 'https://firestore.googleapis.com/v1/projects/'
}${import.meta.env.VITE_FIREBASE_PROJECT_ID}/databases/(default)/documents:runQuery`;
// Query the chunk of gardens using the REST api
const gardensChunkResponse = (await fetch(
`https://firestore.googleapis.com/v1/projects/${
import.meta.env.VITE_FIREBASE_PROJECT_ID
}/databases/(default)/documents:runQuery`,
{
headers: {
'X-Firebase-AppCheck': appCheckTokenResponse.token
},
method: 'POST',
body: JSON.stringify({
structuredQuery: {
from: [
{
collectionId: 'campsites',
allDescendants: false
const gardensChunkResponse = (await fetch(url, {
...(appCheckTokenResponse
? {
headers: {
'X-Firebase-AppCheck': appCheckTokenResponse.token
}
}
: {}),
method: 'POST',
body: JSON.stringify({
structuredQuery: {
from: [
{
collectionId: 'campsites',
allDescendants: false
}
],
where: {
fieldFilter: {
field: {
fieldPath: 'listed'
},
op: 'EQUAL',
value: {
booleanValue: true
}
],
where: {
fieldFilter: {
field: {
fieldPath: 'listed'
},
op: 'EQUAL',
value: {
booleanValue: true
}
},
limit: CHUNK_SIZE,
// https://stackoverflow.com/a/71812269/4973029
orderBy: [
{
direction: 'ASCENDING',
field: { fieldPath: '__name__' }
}
],
...(startAfterDocRef
? {
startAt: {
before: false,
values: [{ referenceValue: startAfterDocRef }]
}
}
},
limit: CHUNK_SIZE,
// https://stackoverflow.com/a/71812269/4973029
orderBy: [
{
direction: 'ASCENDING',
field: { fieldPath: '__name__' }
}
],
...(startAfterDocRef
? {
startAt: {
before: false,
values: [{ referenceValue: startAfterDocRef }]
}
}
: {})
}
})
}
).then((r) => r.json())) as RESTGardenDoc[];
: {})
}
})
}).then((r) => r.json())) as RESTGardenDoc[];

// Query the chunk of gardens
if (gardensChunkResponse.length === CHUNK_SIZE) {
Expand Down

0 comments on commit 89de6c8

Please sign in to comment.