-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Weaverse/dev
v2.3.1
- Loading branch information
Showing
39 changed files
with
1,312 additions
and
474 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
@weaverse:registry=https://registry.npmjs.com | ||
@shopify:registry=https://registry.npmjs.com | ||
progress=false |
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
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,66 @@ | ||
import {type RouteLoaderArgs} from '@weaverse/hydrogen'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
type JudgemeProductData = { | ||
product: { | ||
id: string; | ||
handle: string; | ||
}; | ||
}; | ||
|
||
type JudgemeReviewsData = { | ||
reviews: { | ||
rating: number; | ||
}[]; | ||
}; | ||
|
||
async function getInternalIdByHandle( | ||
api_token: string, | ||
shop_domain: string, | ||
handle: string, | ||
) { | ||
let api = | ||
`https://judge.me/api/v1/products/-1?` + | ||
new URLSearchParams({ | ||
api_token, | ||
shop_domain, | ||
handle: handle, | ||
}); | ||
let data = (await fetch(api).then((res) => res.json())) as JudgemeProductData; | ||
return data?.product?.id; | ||
} | ||
|
||
export async function loader(args: RouteLoaderArgs) { | ||
let {params, context} = args; | ||
let env = context.env; | ||
let handle = params.productHandle; | ||
let api_token = env.JUDGEME_PUBLIC_TOKEN; | ||
let shop_domain = env.PUBLIC_STORE_DOMAIN; | ||
invariant(handle, 'Missing product handle'); | ||
if (!api_token) | ||
return { | ||
error: 'Missing JUDGEME_PUBLIC_TOKEN', | ||
}; | ||
let internalId = await getInternalIdByHandle(api_token, shop_domain, handle); | ||
if (internalId) { | ||
let data = (await fetch( | ||
`https://judge.me/api/v1/reviews?` + | ||
new URLSearchParams({ | ||
api_token, | ||
shop_domain, | ||
product_id: internalId, | ||
}), | ||
).then((res) => res.json())) as JudgemeReviewsData; | ||
let reviews = data.reviews; | ||
let rating = | ||
reviews.reduce((acc, review) => acc + review.rating, 0) / | ||
(reviews.length || 1); | ||
return { | ||
rating, | ||
reviewNumber: reviews.length, | ||
}; | ||
} | ||
return { | ||
rating: 0, | ||
}; | ||
} |
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
Oops, something went wrong.