import {z} from 'zod' export type GetApiRestapiV10DictionaryCountryArgs = {loginAllowed?: boolean | undefined, signupAllowed?: boolean | undefined, numberSelling?: boolean | undefined, page?: number | undefined, perPage?: number | undefined, freeSoftphoneLine?: boolean | undefined}; export type GetApiRestapiV10DictionaryCountryOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const countryInfoDictionaryModel = z.object({id: z.string().optional(), uri: z.string().optional(), name: z.string().optional(), isoCode: z.string().optional(), callingCode: z.string().optional(), emergencyCalling: z.boolean().optional(), numberSelling: z.boolean().optional(), loginAllowed: z.boolean().optional(), signupAllowed: z.boolean().optional(), freeSoftphoneLine: z.boolean().optional(), localDialing: z.boolean().optional()}); export const pageNavigationUri = z.object({uri: z.string().optional()}); export const pageNavigationModel = z.object({firstPage: pageNavigationUri.optional(), nextPage: pageNavigationUri.optional(), previousPage: pageNavigationUri.optional(), lastPage: pageNavigationUri.optional()}); export const enumeratedPagingModel = z.object({perPage: z.number().int().gte(1).lte(1000), page: z.number().int().gte(1).lte(1000).optional(), pageStart: z.number().int().gte(0).optional(), pageEnd: z.number().int().gte(0).optional(), totalPages: z.number().int().gte(0).optional(), totalElements: z.number().int().gte(0).optional()}); export const countryListDictionaryModel = z.object({uri: z.string().optional(), records: z.array(countryInfoDictionaryModel), navigation: pageNavigationModel, paging: enumeratedPagingModel}); 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 Countries * * GET /restapi/v1.0/dictionary/country */ export const getApiRestapiV10DictionaryCountry = async (args: GetApiRestapiV10DictionaryCountryArgs, options?: GetApiRestapiV10DictionaryCountryOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.loginAllowed != null) query.append("loginAllowed", String(args.loginAllowed)) if (args.signupAllowed != null) query.append("signupAllowed", String(args.signupAllowed)) if (args.numberSelling != null) query.append("numberSelling", String(args.numberSelling)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.freeSoftphoneLine != null) query.append("freeSoftphoneLine", String(args.freeSoftphoneLine)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/dictionary/country${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(countryListDictionaryModel, await res.json()) };