From 07b66aa742123062b9b3d756fa9201cb22f9c1c3 Mon Sep 17 00:00:00 2001 From: ElysaSrc <101974839+ElysaSrc@users.noreply.github.com> Date: Sat, 14 Sep 2024 13:18:54 +0200 Subject: [PATCH] backend, frontend: api typings --- backend/src/api/root.rs | 56 +++++++++++++++++++--------------- backend/src/doc.rs | 6 +++- frontend/lib.d.ts | 4 +-- frontend/lib/admin-client.ts | 21 +++++++------ frontend/lib/viewer-state.ts | 15 ++------- frontend/openapi.json | 59 +++++++++++++++++++++--------------- 6 files changed, 88 insertions(+), 73 deletions(-) diff --git a/backend/src/api/root.rs b/backend/src/api/root.rs index eb2a9b7..8032629 100644 --- a/backend/src/api/root.rs +++ b/backend/src/api/root.rs @@ -65,22 +65,7 @@ pub async fn status(State(app_state): State) -> AppJson, - - /// List of known categories - categories: Vec, - - /// List of allowed categories - allowed_categories: Vec, - - /// List of allowed tags - allowed_tags: Vec, - +pub struct BootstrapPermissions { /// Permission to list entities can_list_entities: bool, @@ -104,6 +89,27 @@ pub struct BootstrapResponse { /// Permission to add a comment to an entity can_add_comment: bool, +} + +#[derive(Serialize, ToSchema)] +pub struct BootstrapResponse { + /// Signed token for subsequent requests + signed_token: String, + + /// List of known families + families: Vec, + + /// List of known categories + categories: Vec, + + /// List of allowed categories + allowed_categories: Vec, + + /// List of allowed tags + allowed_tags: Vec, + + /// Permissions + permissions: BootstrapPermissions, /// List of tags tags: Vec, @@ -224,14 +230,16 @@ async fn bootstrap( categories, allowed_categories, allowed_tags, - can_list_entities: perms.can_list_entities, - can_list_without_query: perms.can_list_without_query, - can_list_with_filters: perms.can_list_with_filters, - can_list_with_enum_constraints: perms.can_list_with_enum_constraints, - can_access_entity: perms.can_access_entity, - can_add_entity: perms.can_add_entity, - can_access_comments: perms.can_access_comments, - can_add_comment: perms.can_add_comment, + permissions: BootstrapPermissions { + can_list_entities: perms.can_list_entities, + can_list_without_query: perms.can_list_without_query, + can_list_with_filters: perms.can_list_with_filters, + can_list_with_enum_constraints: perms.can_list_with_enum_constraints, + can_access_entity: perms.can_access_entity, + can_access_comments: perms.can_access_comments, + can_add_entity: perms.can_add_entity, + can_add_comment: perms.can_add_comment, + }, tags, }; diff --git a/backend/src/doc.rs b/backend/src/doc.rs index e1e633d..5c07db4 100644 --- a/backend/src/doc.rs +++ b/backend/src/doc.rs @@ -9,7 +9,10 @@ use crate::{ self, FetchEntityRequest, FetchedEntity, NewCommentRequest, PublicNewEntityRequest, PublicNewEntityResponse, SearchRequest as MapSearchRequest, ViewRequest, }, - root::{self, BootstrapResponse, SafeHavenVersionResponse, SafeMode, StatusResponse}, + root::{ + self, BootstrapPermissions, BootstrapResponse, SafeHavenVersionResponse, SafeMode, + StatusResponse, + }, ErrorResponse, }, helpers::postgis_polygons::MultiPolygon, @@ -131,6 +134,7 @@ use utoipa::OpenApi; StatusResponse, SafeMode, BootstrapResponse, + BootstrapPermissions, SafeHavenVersionResponse, // options SafeHavenOptions, diff --git a/frontend/lib.d.ts b/frontend/lib.d.ts index e8f9eae..a5834f0 100644 --- a/frontend/lib.d.ts +++ b/frontend/lib.d.ts @@ -60,9 +60,7 @@ export type Permissions = api.components['schemas']['Permissions'] export type NewOrUpdateAccessToken = api.components['schemas']['NewOrUpdateAccessToken'] export type AccessToken = api.components['schemas']['AccessToken'] export type AccessTokenStats = api.components['schemas']['AccessTokenStats'] -export type PublicPermissions = api.components['schemas']['Permissions'] - -export type LimitedPublicPermissions = Omit +export type BootstrapPermissions = api.components['schemas']['BootstrapPermissions'] export type User = api.components['schemas']['User'] export type NewOrUpdatedUser = api.components['schemas']['NewOrUpdatedUser'] diff --git a/frontend/lib/admin-client.ts b/frontend/lib/admin-client.ts index dacbca7..b4b3afb 100644 --- a/frontend/lib/admin-client.ts +++ b/frontend/lib/admin-client.ts @@ -22,6 +22,9 @@ import type { AccessTokenStats, AdminEntityWithRelations, SafeHavenVersion, + Family, + AdminPaginatedCachedEntities, + AccessToken, } from '~/lib' // client as a closure @@ -160,13 +163,13 @@ export default function useClient() { return data }, - async listFamilies() { + async listFamilies(): Promise { const { data, error } = await this.rawClient.GET('/api/admin/families') if (error) throw error return data }, - async getFamily(id: string) { + async getFamily(id: string): Promise { const { data, error } = await this.rawClient.GET('/api/admin/families/{id}', { params: { path: { id } }, }) @@ -174,13 +177,13 @@ export default function useClient() { return data }, - async createFamily(family: NewOrUpdateFamily) { + async createFamily(family: NewOrUpdateFamily): Promise { const { data, error } = await this.rawClient.POST('/api/admin/families', { body: family }) if (error) throw error return data }, - async updateFamily(id: string, family: NewOrUpdateFamily) { + async updateFamily(id: string, family: NewOrUpdateFamily): Promise { const { data, error } = await this.rawClient.PUT('/api/admin/families/{id}', { body: family, params: { path: { id } }, @@ -208,7 +211,7 @@ export default function useClient() { async searchEntities( pagination: { page: number, page_size: number }, search_request: AdminSearchRequestBody, - ) { + ): Promise { const { data, error } = await this.rawClient.POST('/api/admin/entities/search', { params: { query: pagination }, body: search_request, @@ -381,19 +384,19 @@ export default function useClient() { return data }, - async listAccessTokens() { + async listAccessTokens(): Promise { const { data, error } = await this.rawClient.GET('/api/admin/access_tokens') if (error) throw error return data }, - async createAccessToken(token: NewOrUpdateAccessToken) { + async createAccessToken(token: NewOrUpdateAccessToken): Promise { const { data, error } = await this.rawClient.POST('/api/admin/access_tokens', { body: token }) if (error) throw error return data }, - async getAccessToken(id: string) { + async getAccessToken(id: string): Promise { const { data, error } = await this.rawClient.GET('/api/admin/access_tokens/{id}', { params: { path: { id } }, }) @@ -409,7 +412,7 @@ export default function useClient() { return data }, - async updateAccessToken(id: string, token: NewOrUpdateAccessToken) { + async updateAccessToken(id: string, token: NewOrUpdateAccessToken): Promise { const { data, error } = await this.rawClient.PUT('/api/admin/access_tokens/{id}', { body: token, params: { path: { id } }, diff --git a/frontend/lib/viewer-state.ts b/frontend/lib/viewer-state.ts index 5c5ab17..5c1d185 100644 --- a/frontend/lib/viewer-state.ts +++ b/frontend/lib/viewer-state.ts @@ -13,7 +13,7 @@ import type { InitConfig, EnumFilter, ViewerPaginatedCachedEntities, - LimitedPublicPermissions, + BootstrapPermissions, } from '~/lib' type ViewData = { @@ -34,7 +34,7 @@ export class AppState { } public initConfig: InitConfig | null = null - public permissions: LimitedPublicPermissions | null = null + public permissions: BootstrapPermissions | null = null private familiesData: Family[] | null = null private categoriesData: AllowedCategory[] | null = null @@ -237,16 +237,7 @@ export class AppState { const data = await this.client.bootstrap(token) - this.permissions = { - can_list_entities: data.can_list_entities, - can_access_entity: data.can_access_entity, - can_add_entity: data.can_add_entity, - can_access_comments: data.can_access_comments, - can_add_comment: data.can_add_comment, - can_list_without_query: data.can_list_without_query, - can_list_with_filters: data.can_list_with_filters, - can_list_with_enum_constraints: data.can_list_with_enum_constraints, - } + this.permissions = data.permissions this.familiesData = data.families .sort((a, b) => a.sort_order - b.sort_order) diff --git a/frontend/openapi.json b/frontend/openapi.json index ac2fff6..e6fb371 100644 --- a/frontend/openapi.json +++ b/frontend/openapi.json @@ -3279,14 +3279,9 @@ } } }, - "BootstrapResponse": { + "BootstrapPermissions": { "type": "object", "required": [ - "signed_token", - "families", - "categories", - "allowed_categories", - "allowed_tags", "can_list_entities", "can_list_without_query", "can_list_with_filters", @@ -3294,26 +3289,9 @@ "can_access_entity", "can_access_comments", "can_add_entity", - "can_add_comment", - "tags" + "can_add_comment" ], "properties": { - "allowed_categories": { - "type": "array", - "items": { - "type": "string", - "format": "uuid" - }, - "description": "List of allowed categories" - }, - "allowed_tags": { - "type": "array", - "items": { - "type": "string", - "format": "uuid" - }, - "description": "List of allowed tags" - }, "can_access_comments": { "type": "boolean", "description": "Permission to view an entity's comments" @@ -3345,6 +3323,36 @@ "can_list_without_query": { "type": "boolean", "description": "Permission to list entities with an empty or short query (can be used to list all entities)" + } + } + }, + "BootstrapResponse": { + "type": "object", + "required": [ + "signed_token", + "families", + "categories", + "allowed_categories", + "allowed_tags", + "permissions", + "tags" + ], + "properties": { + "allowed_categories": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "description": "List of allowed categories" + }, + "allowed_tags": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "description": "List of allowed tags" }, "categories": { "type": "array", @@ -3360,6 +3368,9 @@ }, "description": "List of known families" }, + "permissions": { + "$ref": "#/components/schemas/BootstrapPermissions" + }, "signed_token": { "type": "string", "description": "Signed token for subsequent requests"