From 722ee68b4ccdf5e646f36ae794fa569026d2827d Mon Sep 17 00:00:00 2001 From: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Date: Thu, 14 Mar 2024 19:29:58 +0530 Subject: [PATCH] feat: Paginated Surveys Management API (#2198) --- .../app/docs/api/management/surveys/page.mdx | 29 ++++++++++++------- .../app/api/v1/management/surveys/route.ts | 7 ++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/apps/formbricks-com/app/docs/api/management/surveys/page.mdx b/apps/formbricks-com/app/docs/api/management/surveys/page.mdx index 00afe9edf9..00108619e7 100644 --- a/apps/formbricks-com/app/docs/api/management/surveys/page.mdx +++ b/apps/formbricks-com/app/docs/api/management/surveys/page.mdx @@ -1,13 +1,14 @@ import { Fence } from "@/components/shared/Fence"; -import {generateManagementApiMetadata} from "@/lib/utils" +import { generateManagementApiMetadata } from "@/lib/utils"; -export const metadata = generateManagementApiMetadata("Surveys",["Fetch","Create","Update","Delete"]) +export const metadata = generateManagementApiMetadata("Surveys", ["Fetch", "Create", "Update", "Delete"]); #### Management API # Surveys API This set of API can be used to + - [List All Surveys](#list-all-surveys) - [Get Survey](#get-survey-by-id) - [Create Survey](#create-survey) @@ -22,8 +23,7 @@ This set of API can be used to - - Retrieve all the surveys you have for the environment. + Retrieve all the surveys you have for the environment with pagination. ### Mandatory Headers @@ -33,14 +33,26 @@ This set of API can be used to + ### Query Parameters + + + The number of surveys to skip before returning the results. + + + + The number of surveys to return. + + + ```bash {{ title: 'cURL' }} + curl --location \ - 'https://app.formbricks.com/api/v1/management/surveys' \ + 'https://app.formbricks.com/api/v1/management/surveys?offset=20&limit=10' \ --header \ 'x-api-key: ' ``` @@ -403,7 +415,6 @@ This set of API can be used to ``` - @@ -453,7 +464,7 @@ This set of API can be used to } } ``` - + ```json {{ title: '401 Not Authenticated' }} { "code": "not_authenticated", @@ -497,7 +508,6 @@ This set of API can be used to ``` - @@ -568,7 +578,7 @@ This set of API can be used to } } ``` - + ```json {{ title: '401 Not Authenticated' }} { "code": "not_authenticated", @@ -585,7 +595,6 @@ This set of API can be used to --- - ## Delete Survey by ID {{ tag: 'DELETE', label: '/api/v1/management/surveys/' }} diff --git a/apps/web/app/api/v1/management/surveys/route.ts b/apps/web/app/api/v1/management/surveys/route.ts index e7041e6dd6..f7dc67187b 100644 --- a/apps/web/app/api/v1/management/surveys/route.ts +++ b/apps/web/app/api/v1/management/surveys/route.ts @@ -10,7 +10,12 @@ export async function GET(request: Request) { try { const authentication = await authenticateRequest(request); if (!authentication) return responses.notAuthenticatedResponse(); - const surveys = await getSurveys(authentication.environmentId!); + + const searchParams = new URL(request.url).searchParams; + const limit = searchParams.has("limit") ? Number(searchParams.get("limit")) : undefined; + const offset = searchParams.has("offset") ? Number(searchParams.get("offset")) : undefined; + + const surveys = await getSurveys(authentication.environmentId!, limit, offset); return responses.successResponse(surveys); } catch (error) { if (error instanceof DatabaseError) {