ferdikoomen / openapi-typescript-codegen

NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
MIT License
2.79k stars 513 forks source link

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

Open ez-kraivit opened 3 days ago

ez-kraivit commented 3 days ago

I need to add a condition. Let me explain first

export const getResponseBody = async (response: Response): Promise<any> => {
    if (response.status !== 204) {
        try {
            const contentType = response.headers.get('Content-Type');
            if (contentType) {
                const jsonTypes = ['application/json', 'application/problem+json'];
                const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
                if (isJSON) {
                    return await response.json();
                } else if (contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
                    return await response.arrayBuffer();
                } else {
                    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<any> => {
    if (response.status !== 204) {
        try {
            const contentType = response.headers.get('Content-Type');
            if (contentType) {
                if (contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
                    return await response.arrayBuffer()
                }
                const jsonTypes = ['application/json', 'application/problem+json']
                const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
                if (isJSON) {
                    return await response.json();
                } else {
                    return await response.text();
                }
            }
        } catch (error) {
            console.error(error);
        }
    }
    return undefined;
};