import {z} from 'zod' export type PWAStoreEnum = 'App Store' | 'Google Play'; export type ScreenshotRefDTO = {assetId?: (string | null) | undefined, key?: (string | null) | undefined}; export type PWADTO = {pwaName?: (string | null) | undefined, store?: (PWAStoreEnum | null) | undefined, systemName?: (string | null) | undefined, pwaLanguage?: (string | null) | undefined, forceLanguage?: (string | null) | undefined, tagsList?: (Array | null) | undefined, developer?: (string | null) | undefined, verified?: (boolean | null) | undefined, ad?: (boolean | null) | undefined, purchase?: (boolean | null) | undefined, editorsChoice?: (boolean | null) | undefined, rating: number, ratingAbove5: number, ratingAbove4: number, ratingAbove3: number, ratingAbove2: number, ratingAbove1: number, reviewsCount?: (number | null) | undefined, downloadCount?: (number | null) | undefined, ageRating?: (number | null) | undefined, description?: (string | null) | undefined, version?: (string | null) | undefined, releasedAt?: (string | null) | undefined, androidVersion?: (number | null) | undefined, interactiveElements?: (string | null) | undefined, whatsNew?: (string | null) | undefined, categoryList?: (Array | null) | undefined, safetyDescription?: (string | null) | undefined, dataTransfer?: (string | null) | undefined, dataCollection?: (string | null) | undefined, dataEncryption?: (string | null) | undefined, dataDeletion?: (string | null) | undefined, appSize?: (string | null) | undefined, comments?: (Array<{[key: string]: unknown}> | null) | undefined, userComment?: (string | null) | undefined, updatedAppAt?: (string | null) | undefined, logoAssetId?: (string | null) | undefined, backgroundAssetId?: (string | null) | undefined, removeLogo?: boolean | undefined, removeBackground?: boolean | undefined, screenshots?: (Array | null) | undefined}; export type CreateApiApiV1PwaArgs = {body: PWADTO}; export type CreateApiApiV1PwaOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pWAResponseDTO = z.object({id: z.string()}); 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 }; /** * Create a new PWA * * POST /api/v1/pwa */ export const createApiApiV1Pwa = async (args: CreateApiApiV1PwaArgs, options: CreateApiApiV1PwaOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/pwa${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(pWAResponseDTO, await res.json()) };