Files
supabase/apps/studio/data/api-keys/api-key-create-mutation.ts
Jonathan Summers-Muir 4649bf911e feat: new api keys [hidden] (#33252)
* feat: add basic api keys ui

* init JWT secrets. rough

* Update JWTSecretKeysTable.tsx

* added some info hover cards.

• found this this is probably the wrong direction
• will create a new page for next iteration.

* init new version

* add illustrations

* Update JWTSecretKeysTablev2.tsx

* chore: delete API key now works

* some style changes

* added better tables

* Update JWTSecretKeysTablev2.tsx

* add public JWT dialog

* moar

* adding sub layout in

* starts adding in a ButtonGroup

* about to make into separate components

* added quick copy to project loading screen

* build state

* basic loading

* confirm dialog and loading states

* switched for better loading experience

* moved styles of Input to InputVariants

* issue with ref type

* loading,error and rest states

* new loading states

* alt l;ayout

* add group

* updated error states for permissions

* copy button behaviour for secret keys

* delete dialog

* Update QuickKeyCopy.tsx

* fix type errors

* Update JWTSecretKeysTablev2.tsx

* update menu to hide pages

* Update SettingsMenu.utils.tsx

* Update resource-query.ts

* remove old file

* moved JWT secrets to use valtio

* Update api-keys-query.ts

* fix typecheck

* rename files

* remove JWT stuff

* revert file

* remove more JWT stuff

* Update package.json

* Update pnpm-lock.yaml

* Update ProjectLayout.tsx

* Update PublishableAPIKeys.tsx

* Update api-keys-query.ts

* refactor api-keys-query

* Update SettingsMenu.utils.tsx

* Some clean up

* more clean up and refactor

* Update APIKeyRow.tsx

* Update LayoutHeader.tsx

* resolve comments

* Update CreateSecretAPIKeyModal.tsx

* Update APIKeyRow.tsx

* Add perms check for delete API keys

* Remove console log

* Delete ConnectDialog.tsx

* use project ref

---------

Co-authored-by: Stojan Dimitrovski <sdimitrovski@gmail.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-02-05 15:21:10 +01:00

86 lines
2.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { apiKeysKeys } from './keys'
export type APIKeyCreateVariables = {
projectRef?: string
description: string
} & (
| {
type: 'publishable'
}
| {
type: 'secret'
// secret_jwt_template?: { // @mildtomato (Jonny) removed this field to reduce scope
// role: string
// } | null
}
)
export async function createAPIKey(payload: APIKeyCreateVariables) {
if (!payload.projectRef) throw new Error('projectRef is required')
const { data, error } = await post('/v1/projects/{ref}/api-keys', {
params: {
path: { ref: payload.projectRef },
query: {
reveal: false,
},
},
body:
payload.type === 'secret'
? {
type: payload.type,
description: payload.description || null,
// secret_jwt_template: payload?.secret_jwt_template || null,
secret_jwt_template: {
role: 'service_role', // @mildtomato (Jonny) this should be default in API for type secret
},
}
: {
type: payload.type,
description: payload.description || null,
},
})
if (error) handleError(error)
return data
}
type APIKeyCreateData = Awaited<ReturnType<typeof createAPIKey>>
export const useAPIKeyCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<APIKeyCreateData, ResponseError, APIKeyCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<APIKeyCreateData, ResponseError, APIKeyCreateVariables>(
(vars) => createAPIKey(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(apiKeysKeys.list(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to mutate: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}