mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Add generateApiKey mutation in Web
This commit is contained in:
parent
d27f79c250
commit
af4a2ed3be
1 changed files with 47 additions and 0 deletions
|
|
@ -0,0 +1,47 @@
|
|||
import { gql } from 'graphql-request'
|
||||
import { gqlFetcher } from '../networkHelpers'
|
||||
import { ApiKey } from '../queries/useGetApiKeysQuery'
|
||||
|
||||
export interface GenerateApiKeyInput {
|
||||
name: string
|
||||
scopes?: string[]
|
||||
expiresAt: Date
|
||||
}
|
||||
|
||||
interface GenerateApiKeyResult {
|
||||
generateApiKey: GenerateApiKey
|
||||
errorCodes?: unknown[]
|
||||
}
|
||||
|
||||
type GenerateApiKey = {
|
||||
apiKey: ApiKey
|
||||
}
|
||||
|
||||
export async function generateApiKeyMutation(
|
||||
input: GenerateApiKeyInput
|
||||
): Promise<string | undefined> {
|
||||
const mutation = gql`
|
||||
mutation GenerateApiKey($input: GenerateApiKeyInput!) {
|
||||
generateApiKey(input: $input) {
|
||||
... on GenerateApiKeySuccess {
|
||||
apiKey {
|
||||
key
|
||||
}
|
||||
}
|
||||
... on GenerateApiKeyError {
|
||||
errorCodes
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
try {
|
||||
const data = (await gqlFetcher(mutation, {
|
||||
input,
|
||||
})) as GenerateApiKeyResult
|
||||
return data.errorCodes ? undefined : data.generateApiKey.apiKey.key
|
||||
} catch (error) {
|
||||
console.log('generateApiKeyMutation error', error)
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue