-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added trustpilot service to retrieve Trustpilot data
- Loading branch information
1 parent
f69fe13
commit 293fa1d
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { atom, useAtomValue } from 'jotai' | ||
import { useHydrateAtoms } from 'jotai/utils' | ||
import { type TrustpilotData } from './trustpilot.types' | ||
|
||
type JSONResponse = { | ||
score: { | ||
trustScore: number | ||
} | ||
numberOfReviews: { | ||
total: number | ||
} | ||
} | ||
|
||
const trustpilotAtom = atom<TrustpilotData | null>(null) | ||
|
||
export const useTrustpilotData = () => { | ||
return useAtomValue(trustpilotAtom) | ||
} | ||
|
||
export const useHydrateTrustpilotData = (data: TrustpilotData | null) => { | ||
useHydrateAtoms([[trustpilotAtom, data] as const]) | ||
} | ||
|
||
export const fetchTrustpilotData = async () => { | ||
try { | ||
const hedvigBusinessUnitId = process.env.TRUSTPILOT_HEDVIG_BUSINESS_UNIT_ID | ||
if (!hedvigBusinessUnitId) { | ||
throw new Error('TRUSTPILOT_HEDVIG_BUSINESS_UNIT_ID is not configured') | ||
} | ||
|
||
const trustpilotApiKey = process.env.TRUSTPILOT_API_KEY | ||
if (!trustpilotApiKey) { | ||
throw new Error('`TRUSTPILOT_API_KEY` is not configured') | ||
} | ||
|
||
const response = await fetch( | ||
`https://api.trustpilot.com/v1/business-units/${hedvigBusinessUnitId}`, | ||
{ | ||
headers: { | ||
apiKey: trustpilotApiKey, | ||
}, | ||
}, | ||
) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to retrieve Trustpilot information: ${response.statusText}`) | ||
} | ||
|
||
const { score, numberOfReviews }: JSONResponse = await response.json() | ||
|
||
return { | ||
score: score.trustScore, | ||
totalReviews: numberOfReviews.total, | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
return null | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type TrustpilotData = { | ||
score: number | ||
totalReviews: number | ||
} |