import {z} from 'zod' export type CreateApiSdkV1CohortsMembershipArgs = {isstudio?: (boolean | 'true' | 'false') | undefined, 'project-id'?: number | undefined, body: {cohortName: string, userIds: Array}}; export type CreateApiSdkV1CohortsMembershipOptions = {baseUrl?: string | undefined, sdkApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSdkV1CohortsMembershipResponse = z.object({cohortExists: z.boolean(), users: z.array(z.object({userId: z.string(), isMember: z.boolean()}))}); 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 }; /** * Check cohort membership * * POST /sdk/v1/cohorts/membership */ export const createApiSdkV1CohortsMembership = async (args: CreateApiSdkV1CohortsMembershipArgs, options?: CreateApiSdkV1CohortsMembershipOptions) => { const {baseUrl = "https://api.gamebeast.gg", fetch: fetchFn = fetch, sdkApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.isstudio != null) headers.set("isstudio", String(args.isstudio)) if (args['project-id'] != null) headers.set("project-id", String(args['project-id'])) if (sdkApiKey !== undefined) headers.set("authorization", sdkApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/sdk/v1/cohorts/membership${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(createApiSdkV1CohortsMembershipResponse, await res.json()) };