import {z} from 'zod' export type MockApiType = 'openapi' | 'grpc' | 'graphql' | null; export type CreateMockApiRequest = {mockApi?: ({name: string, description?: string | undefined, apiTemplateId?: string | undefined, type?: MockApiType | undefined, hostname?: string | undefined}) | undefined}; export type CreateApiV1MockApisArgs = {body: CreateMockApiRequest}; export type CreateApiV1MockApisOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mockApiType = z.enum(["openapi", "grpc", "graphql"]).nullable(); export const mockApiData = z.object({id: z.string(), name: z.string().max(256), description: z.string().max(256).optional(), type: mockApiType.optional(), state: z.string(), adminSecurityEnabled: z.boolean(), createdDate: z.string(), aclObject: z.string().optional(), mockHost: z.string().nullable().optional(), links: z.object({self: z.string(), requests: z.string(), mappings: z.string(), scenarios: z.string(), securitySettings: z.string().optional(), recordings: z.object({start: z.string(), stop: z.string(), status: z.string(), snapshot: z.string()}), imports: z.string(), exports: z.object({wiremock: z.string()}).optional(), organisation: z.string(), invitations: z.string(), acl: z.string(), versionHistoryCommits: z.string()}), domainNames: z.array(z.object({domainName: z.string(), urls: z.array(z.object({url: z.string()}))})), baseUrl: z.string(), domains: z.array(z.string())}); export const mockApi = z.object({mockApi: mockApiData}); 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 mock API * * POST /v1/mock-apis */ export const createApiV1MockApis = async (args: CreateApiV1MockApisArgs, options?: CreateApiV1MockApisOptions) => { const {baseUrl = "https://wmc.wiremockapi.cloud", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (tokenAuth !== undefined) headers.set("Authorization", tokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/mock-apis${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(mockApi, await res.json()) };