Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
f6c4b4c9
authored
Apr 07, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: openapi crd
parent
22cc9c85
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
131 additions
and
17 deletions
+131
-17
src/api/openapi.ts
+3
-3
src/pages/api/openapi/getKeys.ts
+11
-8
src/pages/api/openapi/postKey.ts
+1
-1
src/pages/openapi/index.tsx
+114
-5
src/types/openapi.d.ts
+2
-0
No files found.
src/api/openapi.ts
View file @
f6c4b4c9
...
...
@@ -3,14 +3,14 @@ import { UserOpenApiKey } from '@/types/openapi';
/**
* crete a api key
*/
export
const
createA
ApiKey
=
()
=>
POST
(
'/openapi/postKey'
);
export
const
createA
OpenApiKey
=
()
=>
POST
<
string
>
(
'/openapi/postKey'
);
/**
* get api keys
*/
export
const
getApiKeys
=
()
=>
GET
<
UserOpenApiKey
[]
>
(
'/openapi/getKeys'
);
export
const
get
Open
ApiKeys
=
()
=>
GET
<
UserOpenApiKey
[]
>
(
'/openapi/getKeys'
);
/**
* delete api by id
*/
export
const
delApiById
=
(
id
:
string
)
=>
DELETE
(
`/openapi/delKet?id=
${
id
}
`
);
export
const
del
Open
ApiById
=
(
id
:
string
)
=>
DELETE
(
`/openapi/delKet?id=
${
id
}
`
);
src/pages/api/openapi/getKeys.ts
View file @
f6c4b4c9
...
...
@@ -17,16 +17,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await
connectToDatabase
();
const
findResponse
=
await
OpenApi
.
find
({
userId
});
const
findResponse
=
await
OpenApi
.
find
({
userId
})
.
sort
({
_id
:
-
1
})
;
// jus save four data
const
apiKeys
=
findResponse
.
map
<
UserOpenApiKey
>
((
item
)
=>
{
const
key
=
item
.
apiKey
;
return
{
id
:
item
.
_id
,
apiKey
:
`
${
key
.
substring
(
0
,
2
)}
******
${
key
.
substring
(
key
.
length
-
2
)}
`
};
});
const
apiKeys
=
findResponse
.
map
<
UserOpenApiKey
>
(
({
_id
,
apiKey
,
createTime
,
lastUsedTime
})
=>
{
return
{
id
:
_id
,
apiKey
:
`
${
apiKey
.
substring
(
0
,
2
)}
******
${
apiKey
.
substring
(
apiKey
.
length
-
2
)}
`
,
createTime
,
lastUsedTime
};
}
);
jsonRes
(
res
,
{
data
:
apiKeys
...
...
src/pages/api/openapi/postKey.ts
View file @
f6c4b4c9
...
...
@@ -4,7 +4,7 @@ import { jsonRes } from '@/service/response';
import
{
connectToDatabase
,
OpenApi
}
from
'@/service/mongo'
;
import
{
authToken
}
from
'@/service/utils/tools'
;
import
{
customAlphabet
}
from
'nanoid'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890
-'
,
20
);
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890
'
);
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
...
...
src/pages/openapi/index.tsx
View file @
f6c4b4c9
import
React
from
'react'
;
import
{
Card
,
Box
,
Flex
,
Button
,
Input
}
from
'@chakra-ui/react'
;
import
React
,
{
useState
}
from
'react'
;
import
{
Card
,
Box
,
Button
,
Table
,
Thead
,
Tbody
,
Tr
,
Th
,
Td
,
TableContainer
,
IconButton
,
Modal
,
ModalOverlay
,
ModalContent
,
ModalHeader
,
ModalCloseButton
,
ModalBody
}
from
'@chakra-ui/react'
;
import
{
getOpenApiKeys
,
createAOpenApiKey
,
delOpenApiById
}
from
'@/api/openapi'
;
import
{
useQuery
,
useMutation
}
from
'@tanstack/react-query'
;
import
{
useLoading
}
from
'@/hooks/useLoading'
;
import
dayjs
from
'dayjs'
;
import
{
DeleteIcon
}
from
'@chakra-ui/icons'
;
import
{
useCopyData
}
from
'@/utils/tools'
;
const
OpenApi
=
()
=>
{
const
{
Loading
}
=
useLoading
();
const
{
data
:
apiKeys
=
[],
isLoading
:
isGetting
,
refetch
}
=
useQuery
([
getOpenApiKeys
],
getOpenApiKeys
);
const
[
apiKey
,
setApiKey
]
=
useState
(
''
);
const
{
copyData
}
=
useCopyData
();
const
{
mutate
:
onclickCreateApiKey
,
isLoading
:
isCreating
}
=
useMutation
({
mutationFn
:
()
=>
createAOpenApiKey
(),
onSuccess
(
res
)
{
setApiKey
(
res
);
refetch
();
}
});
const
{
mutate
:
onclickRemove
,
isLoading
:
isDeleting
}
=
useMutation
({
mutationFn
:
async
(
id
:
string
)
=>
delOpenApiById
(
id
),
onSuccess
()
{
refetch
();
}
});
return
(
<
Card
px=
{
6
}
py=
{
4
}
>
ss
</
Card
>
<>
<
Card
px=
{
6
}
py=
{
4
}
position=
{
'relative'
}
>
<
Box
fontSize=
{
'xl'
}
fontWeight=
{
'bold'
}
>
Open Api Key
</
Box
>
<
Box
fontSize=
{
'sm'
}
mt=
{
2
}
>
请注意保管你的 key,不要泄露!
</
Box
>
<
TableContainer
mt=
{
2
}
position=
{
'relative'
}
>
<
Table
>
<
Thead
>
<
Tr
>
<
Th
>
Api Key
</
Th
>
<
Th
>
创建时间
</
Th
>
<
Th
>
最后一次使用时间
</
Th
>
<
Th
/>
</
Tr
>
</
Thead
>
<
Tbody
fontSize=
{
'sm'
}
>
{
apiKeys
.
map
(({
id
,
apiKey
,
createTime
,
lastUsedTime
})
=>
(
<
Tr
key=
{
id
}
>
<
Td
>
{
apiKey
}
</
Td
>
<
Td
>
{
dayjs
(
createTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
}
</
Td
>
<
Td
>
{
dayjs
(
lastUsedTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
}
</
Td
>
<
Td
>
<
IconButton
icon=
{
<
DeleteIcon
/>
}
size=
{
'xs'
}
aria
-
label=
{
'delete'
}
variant=
{
'outline'
}
colorScheme=
{
'gray'
}
onClick=
{
()
=>
onclickRemove
(
id
)
}
/>
</
Td
>
</
Tr
>
))
}
</
Tbody
>
</
Table
>
<
Button
mt=
{
5
}
isLoading=
{
isCreating
}
isDisabled=
{
apiKeys
.
length
>=
5
}
title=
{
apiKeys
.
length
>=
5
?
'最多五组 Api Key'
:
''
}
onClick=
{
()
=>
onclickCreateApiKey
()
}
>
添加新的 Api Key
</
Button
>
</
TableContainer
>
<
Loading
loading=
{
isGetting
||
isDeleting
}
fixed=
{
false
}
/>
</
Card
>
<
Modal
isOpen=
{
!!
apiKey
}
onClose=
{
()
=>
setApiKey
(
''
)
}
>
<
ModalOverlay
/>
<
ModalContent
>
<
ModalHeader
>
Api Key
</
ModalHeader
>
<
ModalCloseButton
/>
<
ModalBody
mb=
{
5
}
>
请保管好你的Api Key
<
Box
userSelect=
{
'all'
}
onClick=
{
()
=>
copyData
(
apiKey
)
}
>
{
apiKey
}
</
Box
>
</
ModalBody
>
</
ModalContent
>
</
Modal
>
</>
);
};
...
...
src/types/openapi.d.ts
View file @
f6c4b4c9
export
interface
UserOpenApiKey
{
id
:
string
;
apiKey
:
string
;
createTime
:
Date
;
lastUsedTime
:
Date
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment