From 81ab77cf7cd6b4c6564f2415022eaf140edbc8bc Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Thu, 22 Feb 2024 12:18:23 +0100 Subject: [PATCH] feat: schema for paginated applications (#6309) --- .../spec/applications-query-parameters.ts | 57 +++++++++++++++++++ src/lib/openapi/spec/applications-schema.ts | 6 ++ src/lib/routes/admin-api/metrics.ts | 5 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/lib/openapi/spec/applications-query-parameters.ts diff --git a/src/lib/openapi/spec/applications-query-parameters.ts b/src/lib/openapi/spec/applications-query-parameters.ts new file mode 100644 index 000000000000..e8e08e503953 --- /dev/null +++ b/src/lib/openapi/spec/applications-query-parameters.ts @@ -0,0 +1,57 @@ +import { FromQueryParams } from '../util/from-query-params'; + +export const applicationsQueryParameters = [ + { + name: 'query', + schema: { + type: 'string', + example: 'first_app', + }, + description: 'The search query for the application name', + in: 'query', + }, + { + name: 'offset', + schema: { + type: 'string', + example: '50', + }, + description: + 'The number of applications to skip when returning a page. By default it is set to 0.', + in: 'query', + }, + { + name: 'limit', + schema: { + type: 'string', + example: '50', + }, + description: + 'The number of applications to return in a page. By default it is set to 1000.', + in: 'query', + }, + { + name: 'sortBy', + schema: { + type: 'string', + example: 'type', + }, + description: + 'The field to sort the results by. By default it is set to "appName".', + in: 'query', + }, + { + name: 'sortOrder', + schema: { + type: 'string', + example: 'desc', + }, + description: + 'The sort order for the sortBy. By default it is det to "asc".', + in: 'query', + }, +] as const; + +export type ApplicationsQueryParameters = Partial< + FromQueryParams +>; diff --git a/src/lib/openapi/spec/applications-schema.ts b/src/lib/openapi/spec/applications-schema.ts index 5841fc537729..84434e74b72f 100644 --- a/src/lib/openapi/spec/applications-schema.ts +++ b/src/lib/openapi/spec/applications-schema.ts @@ -7,8 +7,14 @@ export const applicationsSchema = { additionalProperties: false, description: 'An object containing a list of applications that have connected to Unleash via an SDK.', + required: ['total', 'applications'], type: 'object', properties: { + total: { + type: 'integer', + example: 50, + description: 'The total number of project applications.', + }, applications: { description: 'The list of applications that have connected to this Unleash instance.', diff --git a/src/lib/routes/admin-api/metrics.ts b/src/lib/routes/admin-api/metrics.ts index 79033445cc91..db1a7337704f 100644 --- a/src/lib/routes/admin-api/metrics.ts +++ b/src/lib/routes/admin-api/metrics.ts @@ -23,6 +23,7 @@ import { applicationOverviewSchema, } from '../../openapi/spec/application-overview-schema'; import { OpenApiService } from '../../services'; +import { applicationsQueryParameters } from '../../openapi/spec/applications-query-parameters'; class MetricsController extends Controller { private logger: Logger; @@ -104,6 +105,7 @@ class MetricsController extends Controller { summary: 'Get all applications', description: 'Returns all applications registered with Unleash. Applications can be created via metrics reporting or manual creation', + parameters: [...applicationsQueryParameters], operationId: 'getApplications', responses: { 200: createResponseSchema('applicationsSchema'), @@ -193,7 +195,8 @@ class MetricsController extends Controller { query, extractUserIdFromUser(user), ); - res.json({ applications }); + // todo: change to total with pagination later + res.json({ applications, total: applications.length }); } async getApplication(