forked from formbricks/formbricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add CSV export feature to responses page
- Loading branch information
1 parent
e894ab9
commit 042b580
Showing
7 changed files
with
299 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { AsyncParser } from "@json2csv/node"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const data = await request.json(); | ||
let csv: string = ""; | ||
|
||
const { json, fields, fileName } = data; | ||
|
||
const parser = new AsyncParser({ | ||
fields, | ||
}); | ||
|
||
try { | ||
csv = await parser.parse(json).promise(); | ||
} catch (err) { | ||
console.log({ err }); | ||
throw new Error("Failed to convert to CSV"); | ||
} | ||
|
||
const headers = new Headers(); | ||
headers.set("Content-Type", "text/csv;charset=utf-8;"); | ||
headers.set("Content-Disposition", `attachment; filename=${fileName ?? "converted"}.csv`); | ||
|
||
return NextResponse.json( | ||
{ | ||
csvResponse: csv, | ||
}, | ||
{ | ||
headers, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const convertToCSV = async (data: { json: any; fields?: string[]; fileName?: string }) => { | ||
const response = await fetch("/api/csv-conversion", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (!response.ok) throw new Error("Failed to convert to CSV"); | ||
|
||
return response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.