import {z} from 'zod' export type PlaylistLibraryPlaylistIdentifier = {type: 'playlist', playlist_id: number}; export type PlaylistLibraryExplorePlaylistIdentifier = {type: 'explore_playlist', playlist_id: string}; export type PlaylistLibraryFolder = {id: string, type: 'folder', name: string, contents: Array}; export type UserPlaylistLibrary = {contents: Array}; export type CreateUserRequestBody = {user_id?: string | undefined, handle: string, wallet: string, name?: string | undefined, bio?: string | undefined, location?: string | undefined, website?: string | undefined, donation?: string | undefined, twitter_handle?: string | undefined, instagram_handle?: string | undefined, tiktok_handle?: string | undefined, profile_picture?: string | undefined, profile_picture_sizes?: string | undefined, cover_photo?: string | undefined, cover_photo_sizes?: string | undefined, profile_type?: ('label' | null) | undefined, allow_ai_attribution?: boolean | undefined, spl_usdc_payout_wallet?: string | undefined, coin_flair_mint?: string | undefined, playlist_library?: UserPlaylistLibrary | undefined, events?: ({referrer?: string | undefined, is_mobile_user?: boolean | undefined}) | undefined}; export type CreateApiUsersArgs = {body: CreateUserRequestBody}; export type CreateApiUsersOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createUserResponse = z.object({transaction_hash: z.string().optional(), block_hash: z.string().optional(), block_number: z.number().int().optional(), user_id: z.string().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 }; /** * Creates a new user * * POST /users * * Also accepts auth: BasicAuth, OAuth2 */ export const createApiUsers = async (args: CreateApiUsersArgs, options: CreateApiUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/users${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createUserResponse, await res.json()) };