import {z} from 'zod' export type GetApiApplicationsConsentsArgs = {limit?: number | undefined, offset?: number | undefined, identifier?: string | undefined, orderBy?: ('id' | 'createdAt') | undefined}; export type GetApiApplicationsConsentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const applicationConsent = z.object({id: z.number().int().optional(), installedBy: z.object({id: z.number().int().optional(), username: z.string().optional(), fullName: z.string().optional(), avatarUrl: z.string().optional()}).nullable().optional(), identifier: z.string().optional(), name: z.string().nullable().optional(), status: z.enum(["granted", "denied"]).optional(), scopes: z.array(z.string()).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const applicationConsentResource = z.object({data: applicationConsent}); export const pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const applicationConsentCollectionResource = z.object({data: z.array(applicationConsentResource), pagination: pagination.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 }; /** * List Application Consent Decisions * * GET /applications/consents */ export const getApiApplicationsConsents = async (args: GetApiApplicationsConsentsArgs, options?: GetApiApplicationsConsentsOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.identifier != null) query.append("identifier", String(args.identifier)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) const url = `${baseUrl.replace(/\/+$/, '')}/applications/consents${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(applicationConsentCollectionResource, await res.json()) };