openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

feat(services): add ability to provide ApiRequestOptions in operation methods

Open kerwanp opened this issue 1 year ago • 3 comments

Related issues

  • #342

Why ?

I have to work with badly written Openapi specifications that does not contain all the available properties available. So when generating the client, the unreferenced properties are not available in the generated types.

As their is a destructuration done in the operation functions it is not possible to pass non typed parameters.

// preview property is not referenced in openapi specifications
getMentors({ filters: [], sort: [], preview: false });
                                      /\ Custom search params not referenced in specifications
// Generated file
	public getMentors(data: $OpenApiTs['/mentors']['get']['req'] = {}): CancelablePromise<$OpenApiTs['/mentors']['get']['res'][200]> {
		const {
                    sort,
                    filters,
                    locale   // <- preview is not extracted from data
                } = data;

		return this.httpRequest.request({
			method: 'GET',
			url: '/mentors',
			query: {
				sort, filters, locale <- data is not passed 
			},
			errors: {
				400: `Bad Request`,
				401: `Unauthorized`,
				403: `Forbidden`,
				404: `Not Found`,
				500: `Internal Server Error`,
			},
		});

What has been done?

The goal of this PR is to provide the ability to override the ApiRequestOptions by adding a new Partial<ApiRequestOptions> in services methods arguments.

    public static postServiceWithEmptyTag(
        data: $OpenApiTs['/api/v{api-version}/no-tag']['post']['req'],
        options: Partial<ApiRequestOptions> = {}
    ): CancelablePromise<$OpenApiTs['/api/v{api-version}/no-tag']['post']['res'][200]> {}

As we do not want to override but merge, I've added a mergeDeep function. (currently located in core/request.ts but could be in a core/utils.ts).

Then we merge the objects together before passing them to the request function.

    public static postServiceWithEmptyTag(
        data: $OpenApiTs['/api/v{api-version}/no-tag']['post']['req'],
        options: Partial<ApiRequestOptions> = {}
    ): CancelablePromise<$OpenApiTs['/api/v{api-version}/no-tag']['post']['res'][200]> {
        const { requestBody } = data;
        return __request(
            OpenAPI,
            mergeDeep(
                {
                    method: 'POST',
                    url: '/api/v{api-version}/no-tag',
                    body: requestBody,
                    mediaType: 'application/json',
                },
                options
            )
        );
    }
}

kerwanp avatar Apr 13 '24 22:04 kerwanp

⚠️ No Changeset found

Latest commit: 7b9b1719d46d702c99e4cfe78629cff97284e930

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Apr 13 '24 22:04 changeset-bot[bot]

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 13, 2024 10:36pm

vercel[bot] avatar Apr 13 '24 22:04 vercel[bot]

I can clean my commit history if needed 😐

kerwanp avatar Apr 13 '24 22:04 kerwanp