Skip to content

Commit

Permalink
backend, frontend: api typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ElysaSrc committed Sep 14, 2024
1 parent b84bc24 commit 07b66aa
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 73 deletions.
56 changes: 32 additions & 24 deletions backend/src/api/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,7 @@ pub async fn status(State(app_state): State<AppState>) -> AppJson<StatusResponse
}

#[derive(Serialize, ToSchema)]
pub struct BootstrapResponse {
/// Signed token for subsequent requests
signed_token: String,

/// List of known families
families: Vec<Family>,

/// List of known categories
categories: Vec<Category>,

/// List of allowed categories
allowed_categories: Vec<Uuid>,

/// List of allowed tags
allowed_tags: Vec<Uuid>,

pub struct BootstrapPermissions {
/// Permission to list entities
can_list_entities: bool,

Expand All @@ -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<Family>,

/// List of known categories
categories: Vec<Category>,

/// List of allowed categories
allowed_categories: Vec<Uuid>,

/// List of allowed tags
allowed_tags: Vec<Uuid>,

/// Permissions
permissions: BootstrapPermissions,

/// List of tags
tags: Vec<Tag>,
Expand Down Expand Up @@ -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,
};

Expand Down
6 changes: 5 additions & 1 deletion backend/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -131,6 +134,7 @@ use utoipa::OpenApi;
StatusResponse,
SafeMode,
BootstrapResponse,
BootstrapPermissions,
SafeHavenVersionResponse,
// options
SafeHavenOptions,
Expand Down
4 changes: 1 addition & 3 deletions frontend/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<api.components['schemas']['Permissions'], 'categories_policy', 'families_policy', 'geographic_restrictions', 'tags_policy'>
export type BootstrapPermissions = api.components['schemas']['BootstrapPermissions']

export type User = api.components['schemas']['User']
export type NewOrUpdatedUser = api.components['schemas']['NewOrUpdatedUser']
Expand Down
21 changes: 12 additions & 9 deletions frontend/lib/admin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import type {
AccessTokenStats,
AdminEntityWithRelations,
SafeHavenVersion,
Family,
AdminPaginatedCachedEntities,
AccessToken,
} from '~/lib'

// client as a closure
Expand Down Expand Up @@ -160,27 +163,27 @@ export default function useClient() {
return data
},

async listFamilies() {
async listFamilies(): Promise<Family[]> {
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<Family> {
const { data, error } = await this.rawClient.GET('/api/admin/families/{id}', {
params: { path: { id } },
})
if (error) throw error
return data
},

async createFamily(family: NewOrUpdateFamily) {
async createFamily(family: NewOrUpdateFamily): Promise<Family> {
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<Family> {
const { data, error } = await this.rawClient.PUT('/api/admin/families/{id}', {
body: family,
params: { path: { id } },
Expand Down Expand Up @@ -208,7 +211,7 @@ export default function useClient() {
async searchEntities(
pagination: { page: number, page_size: number },
search_request: AdminSearchRequestBody,
) {
): Promise<AdminPaginatedCachedEntities> {
const { data, error } = await this.rawClient.POST('/api/admin/entities/search', {
params: { query: pagination },
body: search_request,
Expand Down Expand Up @@ -381,19 +384,19 @@ export default function useClient() {
return data
},

async listAccessTokens() {
async listAccessTokens(): Promise<AccessToken[]> {
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<AccessToken> {
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<AccessToken> {
const { data, error } = await this.rawClient.GET('/api/admin/access_tokens/{id}', {
params: { path: { id } },
})
Expand All @@ -409,7 +412,7 @@ export default function useClient() {
return data
},

async updateAccessToken(id: string, token: NewOrUpdateAccessToken) {
async updateAccessToken(id: string, token: NewOrUpdateAccessToken): Promise<AccessToken> {
const { data, error } = await this.rawClient.PUT('/api/admin/access_tokens/{id}', {
body: token,
params: { path: { id } },
Expand Down
15 changes: 3 additions & 12 deletions frontend/lib/viewer-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
InitConfig,
EnumFilter,
ViewerPaginatedCachedEntities,
LimitedPublicPermissions,
BootstrapPermissions,
} from '~/lib'

type ViewData = {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 35 additions & 24 deletions frontend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3279,41 +3279,19 @@
}
}
},
"BootstrapResponse": {
"BootstrapPermissions": {
"type": "object",
"required": [
"signed_token",
"families",
"categories",
"allowed_categories",
"allowed_tags",
"can_list_entities",
"can_list_without_query",
"can_list_with_filters",
"can_list_with_enum_constraints",
"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"
Expand Down Expand Up @@ -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",
Expand All @@ -3360,6 +3368,9 @@
},
"description": "List of known families"
},
"permissions": {
"$ref": "#/components/schemas/BootstrapPermissions"
},
"signed_token": {
"type": "string",
"description": "Signed token for subsequent requests"
Expand Down

0 comments on commit 07b66aa

Please sign in to comment.