hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
676 stars 52 forks source link

I want to improve the delivery of XLSX files to support a wider range of tasks #749

Open ez-kraivit opened 4 days ago

ez-kraivit commented 4 days ago

Description

I need to add a condition. Let me explain first

export const getResponseBody = async (response: Response): Promise<unknown> => {
    if (response.status !== 204) {
        try {
            const contentType = response.headers.get('Content-Type');
            if (contentType) {
                const binaryTypes = ['application/octet-stream', 'application/pdf', 'application/zip', 'audio/', 'image/', 'video/'];
                if (contentType.includes('application/json') || contentType.includes('+json')) {
                    return await response.json();
                } else if (binaryTypes.some(type => contentType.includes(type))) {
                    return await response.blob();
                } else if (contentType.includes('multipart/form-data')) {
                    return await response.formData();
                } else if (contentType.includes('text/')) {
                    return await response.text();
                }
            }
        } catch (error) {
            console.error(error);
        }
    }
    return undefined;
};

I found that const contentType = response.headers.get('Content-Type'); in my code sends "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet". I want to modify it to have the condition:

if (contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
    return await response.arrayBuffer();
}

Because I need arrayBuffer(), and I've tested and found that json() or text() are insufficient conditions to support response handling adequately. Please grant me permission to make these changes in the file path core/request.ts."

If you need further adjustments or have more details to add, feel free to let me know!

export const getResponseBody = async (response: Response): Promise<unknown> => {
    if (response.status !== 204) {
        try {
            const contentType = response.headers.get('Content-Type');
            if (contentType) {
                const binaryTypes = ['application/octet-stream', 'application/pdf', 'application/zip', 'audio/', 'image/', 'video/'];
                if (contentType.includes('application/json') || contentType.includes('+json')) {
                    return await response.json();
                } else if (binaryTypes.some(type => contentType.includes(type))) {
                    return await response.blob();
                } else if (contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
                    return await response.arrayBuffer();
                } else if (contentType.includes('multipart/form-data')) {
                    return await response.formData();
                } else if (contentType.includes('text/')) {
                    return await response.text();
                }
            }
        } catch (error) {
            console.error(error);
        }
    }
    return undefined;
};
mrlubos commented 4 days ago

Hey @ez-kraivit, are you able to use the new Fetch API client?