import {z} from 'zod' export type GetApiApiV1WatchlistsPublicUserIdArgs = {'x-request-id': string, 'x-api-key': string, 'x-user-key': string, userId: number, itemsPerPageForSingle?: number | undefined}; export type GetApiApiV1WatchlistsPublicUserIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const watchlistItemDto = z.object({itemId: z.number().int(), itemType: z.string(), itemRank: z.number().int().optional(), itemAddedReason: z.string().optional(), itemAddedDate: z.string().optional(), market: z.object({id: z.string().optional(), symbolName: z.string().optional(), displayName: z.string().optional(), assetTypeId: z.number().int().optional(), assetTypeSubCategoryId: z.number().int().nullable().optional(), exchangeId: z.number().int().optional(), hasExpirationDate: z.boolean().optional(), avatar: z.object({small: z.string().optional(), medium: z.string().optional(), large: z.string().optional(), svg: z.object({url: z.string().optional(), backgroundColor: z.string().optional(), textColor: z.string().optional()}).nullable().optional()}).optional()}).optional()}); export const watchlistResponse = z.object({watchlistId: z.string().optional(), name: z.string().optional(), Gcid: z.number().int().optional(), watchlistType: z.enum(["Static", "Dynamic", "RecentlyInvested", "Default"]).optional(), totalItems: z.number().int().optional(), isDefault: z.boolean().optional(), isUserSelectedDefault: z.boolean().optional(), watchlistRank: z.number().int().optional(), dynamicUrl: z.string().nullable().optional(), items: z.array(watchlistItemDto).optional(), relatedAssets: z.array(z.number().int()).nullable().optional()}); export const watchlistsResponse = z.object({status: z.number().int().optional(), watchlists: z.array(watchlistResponse).optional(), exception: z.object({invalidItems: z.array(z.string()).optional(), reason: z.string().optional(), message: z.string().optional()}).optional(), meta: z.object({pageNumber: z.number().int().optional(), itemsPerPage: z.number().int().optional(), maxItemsInWatchlistLimit: z.number().int().optional(), maxWatchlistsLimit: z.number().int().optional()}).optional(), isSucceeded: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get user's public watchlists * * GET /api/v1/watchlists/public/{userId} * * Also accepts auth: oauth2, oauth2, oauth2, oauth2, oauth2 */ export const getApiApiV1WatchlistsPublicUserId = async (args: GetApiApiV1WatchlistsPublicUserIdArgs, options?: GetApiApiV1WatchlistsPublicUserIdOptions) => { const {baseUrl = "https://public-api.etoro.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-request-id'] != null) headers.set("x-request-id", String(args['x-request-id'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['x-user-key'] != null) headers.set("x-user-key", String(args['x-user-key'])) if (args.itemsPerPageForSingle != null) query.append("itemsPerPageForSingle", String(args.itemsPerPageForSingle)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/watchlists/public/${encodeURIComponent(String(args.userId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(watchlistsResponse, await res.json()) };