import {z} from 'zod' export type AppIsAllowedRequest = {node?: (string | null) | undefined, compose_hash: string, device_id?: (string | null) | undefined, chain_id?: (number | null) | undefined}; export type CreateApiApiV1AppsAppIdIsAllowedArgs = {app_id: string, body: AppIsAllowedRequest}; export type CreateApiApiV1AppsAppIdIsAllowedOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const appIsAllowedResponse = z.object({app_contract_address: z.string(), compose_hash: z.string(), device_id: z.string(), compose_hash_allowed: z.boolean(), allow_any_device: z.boolean(), device_id_allowed: z.boolean().nullable().optional(), is_allowed: z.boolean(), error: z.string().nullable().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 }; /** * Check if app deployment is allowed on-chain * * POST /api/v1/apps/{app_id}/is-allowed */ export const createApiApiV1AppsAppIdIsAllowed = async (args: CreateApiApiV1AppsAppIdIsAllowedArgs, options?: CreateApiApiV1AppsAppIdIsAllowedOptions) => { const {baseUrl = "https://cloud-api.phala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/apps/${encodeURIComponent(String(args.app_id))}/is-allowed${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(appIsAllowedResponse, await res.json()) };