base.ts128 lines · main
| 1 | import type { PermissionAction } from '@supabase/shared-types/out/constants' |
| 2 | import type jsonLogic from 'json-logic-js' |
| 3 | |
| 4 | import type { OrganizationBase } from '@/data/organizations/organizations-query' |
| 5 | import type { PlanId } from '@/data/subscriptions/types' |
| 6 | import type { ManagedBy } from '@/lib/constants/infrastructure' |
| 7 | |
| 8 | export interface Organization extends OrganizationBase { |
| 9 | managed_by: ManagedBy |
| 10 | partner_id?: string |
| 11 | plan: { id: PlanId; name: string } |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * @deprecated Please use type from projects-query OR project-details-query.ts instead |
| 16 | */ |
| 17 | export interface ProjectBase { |
| 18 | id: number |
| 19 | ref: string |
| 20 | name: string |
| 21 | status: string |
| 22 | organization_id: number |
| 23 | cloud_provider: string |
| 24 | region: string |
| 25 | inserted_at: string |
| 26 | subscription_id: string |
| 27 | preview_branch_refs: string[] |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @deprecated Please use type from project-details-query.ts instead |
| 32 | */ |
| 33 | export interface Project extends ProjectBase { |
| 34 | // available after projects.fetchDetail |
| 35 | connectionString?: string | null |
| 36 | dbVersion?: string |
| 37 | restUrl?: string |
| 38 | lastDatabaseResizeAt?: string | null |
| 39 | maxDatabasePreprovisionGb?: string | null |
| 40 | parent_project_ref?: string |
| 41 | is_branch_enabled?: boolean |
| 42 | |
| 43 | /** |
| 44 | * postgrestStatus is available on client side only. |
| 45 | * We use this status to check if a project instance is HEALTHY or not |
| 46 | * If not we will show ConnectingState and run a polling until it's back online |
| 47 | */ |
| 48 | postgrestStatus?: 'ONLINE' | 'OFFLINE' |
| 49 | /** |
| 50 | * Only available on client side only, for components that require the parentRef |
| 51 | * irregardless of being on any branch, such as ProjectDropdown and Vercel integration |
| 52 | * */ |
| 53 | parentRef?: string |
| 54 | volumeSizeGb?: number |
| 55 | } |
| 56 | |
| 57 | export interface User { |
| 58 | id: number |
| 59 | mobile: string | null |
| 60 | primary_email: string |
| 61 | username: string |
| 62 | first_name: string |
| 63 | last_name: string |
| 64 | gotrue_id: string |
| 65 | is_alpha_user: boolean |
| 66 | free_project_limit: number |
| 67 | } |
| 68 | |
| 69 | export interface Role { |
| 70 | id: number |
| 71 | name: string |
| 72 | } |
| 73 | |
| 74 | export interface Permission { |
| 75 | actions: PermissionAction[] |
| 76 | condition: jsonLogic.RulesLogic |
| 77 | organization_slug: string |
| 78 | resources: string[] |
| 79 | restrictive?: boolean |
| 80 | project_refs: string[] |
| 81 | } |
| 82 | |
| 83 | export interface ResponseFailure { |
| 84 | error: ResponseError |
| 85 | } |
| 86 | |
| 87 | export type SupaResponse<T> = T | ResponseFailure |
| 88 | |
| 89 | // [Joshen] Trialing returning metadata for the error object. It's meant to be generic |
| 90 | // but typed properly here, and we can create more types if needed with the | operator |
| 91 | type CostMetadata = { |
| 92 | cost: number |
| 93 | sql: string |
| 94 | } |
| 95 | |
| 96 | export type ErrorMetadata = CostMetadata |
| 97 | |
| 98 | export class ResponseError extends Error { |
| 99 | code?: number |
| 100 | requestId?: string |
| 101 | retryAfter?: number |
| 102 | requestPathname?: string |
| 103 | metadata?: CostMetadata |
| 104 | errorType?: string |
| 105 | formattedError?: string |
| 106 | |
| 107 | constructor( |
| 108 | message: string | undefined, |
| 109 | code?: number, |
| 110 | requestId?: string, |
| 111 | retryAfter?: number, |
| 112 | requestPathname?: string, |
| 113 | metadata?: CostMetadata, |
| 114 | formattedError?: string |
| 115 | ) { |
| 116 | super(message || 'API error happened while trying to communicate with the server.') |
| 117 | this.code = code |
| 118 | this.requestId = requestId |
| 119 | this.retryAfter = retryAfter |
| 120 | this.requestPathname = requestPathname |
| 121 | this.metadata = metadata |
| 122 | this.formattedError = formattedError |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | export interface Dictionary<T> { |
| 127 | [Key: string]: T |
| 128 | } |