File tree Expand file tree Collapse file tree 5 files changed +51
-24
lines changed Expand file tree Collapse file tree 5 files changed +51
-24
lines changed Original file line number Diff line number Diff line change @@ -7,10 +7,18 @@ type Props = {
7
7
8
8
const BASEURL = process . env . NEXT_PUBLIC_BASE_URL ?? "http://localhost:3000"
9
9
10
+ async function getCollection ( handle : string ) {
11
+ const res = await fetch ( `${ BASEURL } /collections?handle=${ handle } ` )
12
+
13
+ if ( ! res . ok ) {
14
+ throw new Error ( `Failed to fetch collection: ${ handle } ` )
15
+ }
16
+
17
+ return res . json ( )
18
+ }
19
+
10
20
export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
11
- const { collection } = await fetch (
12
- `${ BASEURL } /collections?handle=${ params . handle } `
13
- ) . then ( ( res ) => res . json ( ) )
21
+ const { collection } = await getCollection ( params . handle )
14
22
15
23
return {
16
24
title : `${ collection . title } | Acme Store` ,
@@ -19,9 +27,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
19
27
}
20
28
21
29
export default async function CollectionPage ( { params } : Props ) {
22
- const { collection } = await fetch (
23
- `${ BASEURL } /collections?handle=${ params . handle } `
24
- ) . then ( ( res ) => res . json ( ) )
30
+ const { collection } = await getCollection ( params . handle )
25
31
26
32
return < CollectionTemplate collection = { collection } />
27
33
}
Original file line number Diff line number Diff line change @@ -6,15 +6,23 @@ type Props = {
6
6
params : { id : string }
7
7
}
8
8
9
+ async function getOrder ( id : string ) {
10
+ const res = await medusaRequest ( "GET" , `/orders/${ id } ` )
11
+
12
+ if ( ! res . ok ) {
13
+ throw new Error ( `Failed to fetch order: ${ id } ` )
14
+ }
15
+
16
+ return res . body
17
+ }
18
+
9
19
export const metadata : Metadata = {
10
20
title : "Order Confirmed" ,
11
21
description : "You purchase was successful" ,
12
22
}
13
23
14
24
export default async function CollectionPage ( { params } : Props ) {
15
- const { order } = await medusaRequest ( "GET" , `/orders/${ params . id } ` ) . then (
16
- ( res ) => res . body
17
- )
25
+ const { order } = await getOrder ( params . id )
18
26
19
27
return < OrderCompletedTemplate order = { order } />
20
28
}
Original file line number Diff line number Diff line change @@ -6,10 +6,18 @@ type Props = {
6
6
params : { id : string }
7
7
}
8
8
9
+ async function getOrder ( id : string ) {
10
+ const res = await medusaRequest ( "GET" , `/orders/${ id } ` )
11
+
12
+ if ( ! res . ok ) {
13
+ throw new Error ( `Failed to fetch order: ${ id } ` )
14
+ }
15
+
16
+ return res . body
17
+ }
18
+
9
19
export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
10
- const { order } = await medusaRequest ( "GET" , `/orders/${ params . id } ` ) . then (
11
- ( res ) => res . body
12
- )
20
+ const { order } = await getOrder ( params . id )
13
21
14
22
return {
15
23
title : `Order #${ order . display_id } ` ,
@@ -18,9 +26,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
18
26
}
19
27
20
28
export default async function CollectionPage ( { params } : Props ) {
21
- const { order } = await medusaRequest ( "GET" , `/orders/${ params . id } ` ) . then (
22
- ( res ) => res . body
23
- )
29
+ const { order } = await getOrder ( params . id )
24
30
25
31
return < OrderCompletedTemplate order = { order } />
26
32
}
Original file line number Diff line number Diff line change @@ -6,12 +6,22 @@ type Props = {
6
6
params : { handle : string }
7
7
}
8
8
9
- export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
10
- const { products } = await medusaRequest ( "GET" , "/products" , {
9
+ async function getProducts ( handle : string ) {
10
+ const res = await medusaRequest ( "GET" , "/products" , {
11
11
query : {
12
- handle : params . handle ,
12
+ handle,
13
13
} ,
14
- } ) . then ( ( res ) => res . body )
14
+ } )
15
+
16
+ if ( ! res . ok ) {
17
+ throw new Error ( `Failed to fetch product: ${ handle } ` )
18
+ }
19
+
20
+ return res . body
21
+ }
22
+
23
+ export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
24
+ const { products } = await getProducts ( params . handle )
15
25
16
26
const product = products [ 0 ]
17
27
@@ -27,11 +37,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
27
37
}
28
38
29
39
export default async function CollectionPage ( { params } : Props ) {
30
- const { products } = await medusaRequest ( "GET" , "/products" , {
31
- query : {
32
- handle : params . handle ,
33
- } ,
34
- } ) . then ( ( res ) => res . body )
40
+ const { products } = await getProducts ( params . handle )
35
41
36
42
return < ProductTemplate product = { products [ 0 ] } />
37
43
}
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ export default async function medusaRequest(
40
40
41
41
return {
42
42
status : result . status ,
43
+ ok : result . ok ,
43
44
body,
44
45
}
45
46
} catch ( error : any ) {
You can’t perform that action at this time.
0 commit comments