Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions resources/js/api/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseBlobJSONContent } from "@/utils/request";
import { blobHasMimeType, parseBlobJSONContent } from "@/utils/request";
import { handleErrorAlert } from "./errors";
import { Axios, AxiosError, isCancel } from "axios";
import { CURRENT_PAGE_URL } from "@/api/headers";
Expand Down Expand Up @@ -50,9 +50,9 @@ export function installInterceptors(api: Axios) {
const response = error.response;

if(response.data instanceof Blob) {
if(response.data.type === 'application/json') {
if(blobHasMimeType(response.data, 'application/json')) {
response.data = await parseBlobJSONContent(response.data);
} else if(response.data.type === 'text/html') {
} else if(blobHasMimeType(response.data, 'text/html')) {
response.data = await response.data.text();
}
}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/commands/CommandManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandData, CommandFormData, CommandResponseData, FormData } from "@/types";
import { api } from "@/api/api";
import { RootAlertDialog, showAlert, showConfirm } from "@/utils/dialogs";
import { parseBlobJSONContent } from "@/utils/request";
import { blobHasMimeType, parseBlobJSONContent } from "@/utils/request";
import { AxiosResponse } from "axios";
import { reactive } from "vue";
import { __ } from "@/utils/i18n";
Expand Down Expand Up @@ -140,7 +140,7 @@ export class CommandManager {
}

async handleCommandApiResponse(response: AxiosResponse<Blob>): Promise<void> {
if(response.data.type !== 'application/json') {
if(!blobHasMimeType(response.data, 'application/json')) {
const link = document.createElement("a");
link.download = (response.headers['content-disposition'] as string)?.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/)?.[1] ?? 'download';
link.href = URL.createObjectURL(response.data);
Expand Down
7 changes: 7 additions & 0 deletions resources/js/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export function parseBlobJSONContent(blob) {
});
}

/**
* Compare only the MIME type, as Blob.type may include parameters (e.g. "application/json; charset=utf-8")
*/
export function blobHasMimeType(blob: Blob, mimeType: string) {
return blob.type.split(';')[0].trim().toLowerCase() === mimeType;
}

export function getCsrfToken() {
return session('_token');
}
Loading