11import type { ServerToolConfig } from "@/toolkits/types" ;
22import type { getListing } from "./base" ;
3- import type { Etsy } from "etsy-ts" ;
43import { api } from "@/trpc/server" ;
54
65
76export const getListingServerConfig = (
8- etsy : Etsy
7+
98 ) : ServerToolConfig <
109 typeof getListing . inputSchema . shape ,
1110 typeof getListing . outputSchema . shape
@@ -16,16 +15,39 @@ export const getListingServerConfig = (
1615 const account = await api . accounts . getAccountByProvider ( "etsy" ) ;
1716 const userID = account ?. providerAccountId ;
1817 const etsyUserId = Number ( userID ) ;
18+ const apiKey = process . env . AUTH_ETSY_ID ;
19+ if ( ! apiKey ) throw new Error ( "Missing AUTH_ETSY_ID" ) ;
20+ const accessToken = account ?. access_token ;
21+ if ( ! accessToken ) throw new Error ( "Missing Etsy access token" ) ;
22+
23+
24+
25+ const shopResponse = await fetch (
26+ `https://openapi.etsy.com/v3/application/users/${ etsyUserId } /shops` ,
27+ {
28+ headers : {
29+ "x-api-key" : apiKey ,
30+ Authorization : `Bearer ${ accessToken } ` ,
31+ }
32+ } ,
33+ ) ;
34+
35+ const shop = ( await shopResponse . json ( ) ) ;
36+
37+ const listingResponse = await fetch (
38+ `https://openapi.etsy.com/v3/application/shops/${ shop . shop_id } /listings` ,
39+ {
40+ headers : {
41+ "x-api-key" : apiKey ,
42+ Authorization : `Bearer ${ accessToken } ` ,
43+ }
44+ } ,
45+ ) ;
46+ const listings = ( await listingResponse . json ( ) ) ;
1947
2048
21- const shop = await etsy . Shop . getShopByOwnerUserId ( etsyUserId , { etsyUserId} ) ;
22- const shopId = shop . data . shop_id ;
23- if ( typeof shopId !== "number" ) {
24- throw new Error ( "shop_id is undefined" ) ;
25- }
26- const listings = await etsy . ShopListing . getListingsByShop ( { shopId : shopId } , { etsyUserId } ) ;
2749 return {
28- listings : listings . data ,
50+ listings : listings ,
2951 } ;
3052 } catch ( error ) {
3153 console . error ( "Etsy API error:" , error ) ;
0 commit comments