Skip to content

typescript-fetch-client2 - Type 'application/json' is not assignable to type 'application/xml' #33

@taras

Description

@taras

Related #32

Using the same the petstore swagger.json, the generated TypeScript includes the following code:

		loginUser(__params: UserApi.LoginUserRequest, options?: RequestInit): (fetch?: FetchAPI, basePath?: string) => Promise<UserApi.LoginUserResponse> {
			const localVarFetchArgs = UserApiFetchParamCreator(configuration).loginUser(__params, options);
			return async (fetch: FetchAPI = defaultFetch, basePath: string = BASE_PATH) => {
				const response = await fetch(basePath + localVarFetchArgs.url, localVarFetchArgs.options)
				const contentType = response.headers.get('Content-Type');
				const mimeType = contentType ? contentType.replace(/;.*/, '') : undefined;
				
				if (response.status === 200) {
					if (mimeType === 'application/json') {
						return {
							status: 200,
							contentType: 'application/json',
							body: await response.json() as string,
							headers: {
								xExpiresAfter: response.headers.get('X-Expires-After'),
								xRateLimit: response.headers.get('X-Rate-Limit'),
							},
						}
					}
					if (mimeType === 'application/xml') {
						return {
							status: 200,
							contentType: 'application/xml',
							body: await response.text(),
							headers: {
								xExpiresAfter: response.headers.get('X-Expires-After'),
								xRateLimit: response.headers.get('X-Rate-Limit'),
							},
						}
					}
					throw response;
				}
				if (response.status === 400) {
					return {
						status: 400,
						/* No content */
					}
				}
				throw response;
			};
		},

It doesn't compile because of the following error,

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions