Playwright client generation
Author: DavidTelenkoCreated Jul 31, 2025Updated Sep 20, 2026
Labelsenhancementhelp wanted
Playwright has it's own request client which has mostly similar (different at places) structure of options to axios config.
Our naive implementation when trying to make it work with orval was to use 'axios-functions' client as a base client and override the fetcher with custom one to redirect axios signature to playwright, essentially by tricking orval generation into thinking that we're using axios:
In config we provide our magic mutator.
import path from 'path';
import serviceConfig from './service.config';
import { OptionsExport } from 'orval';
export default {
input: {
target: serviceConfig.schema,
},
output: {
baseUrl: serviceConfig.baseUrl,
clean: true,
client: 'axios-functions',
fileExtension: '.gen.ts',
mode: 'tags-split',
prettier: true,
schemas: 'models',
target: 'endpoints',
workspace: path.resolve(__dirname, 'client', 'orval'),
override: {
mutator: {
name: 'playwrightFetch',
path: path.resolve(__dirname, 'utils', 'playwrightFetch.ts'),
},
},
},
} satisfies OptionsExport;And mutator itself naively passes config down to playwright's context.fetch, which is type unsafe but works for now. In prospect we could map all types from axios to playwright.
// utils/playwrightFetch.ts
import { request, type APIRequestContext } from '@playwright/test';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const playwrightFetch = async <_T>(
config: AxiosRequestConfig, // we don't care about this error for now
context?: APIRequestContext
) => {
if (!context) {
context = await request.newContext();
}
return await context.fetch(config.url!, config);
};Generated code turns out to be quite client-agnostic looking like this:
export const foo = (
params: FooParams,
options?: SecondParameter<typeof playwrightFetch>
) => {
return playwrightFetch<FooResponse>(
{ url: `https://baseUrl/foo`, method: 'GET', params },
options
);
};The questions are:
- Is this approach an adequate one?
- Can something like Playwright generator be implemented as part of orval packages?
- Is there any notion of custom generators for orval? (Sorry for rtfm question, but as of right now nothing about this in docs)
Source: orval-labs/orval