2915 rest api documentation (#3020)
* Init rest-api page * Add ugly form to fetch open api schema * Clean code * Make the form design more acceptable * Update doc * Use local storage * Update design * Add isLoading * Fix typo * Fix long lines * Code review returns * Remove staging and local url from servers
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import { createGraphiQLFetcher } from "@graphiql/toolkit";
|
||||
import { GraphiQL } from "graphiql";
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom";
|
||||
import Layout from "@theme/Layout";
|
||||
import BrowserOnly from "@docusaurus/BrowserOnly";
|
||||
|
||||
@ -16,9 +15,7 @@ const GraphiQLComponent = () => {
|
||||
window.localStorage.setItem("graphiql:theme", "light");
|
||||
}
|
||||
|
||||
const fetcher = createGraphiQLFetcher({
|
||||
url: "https://api.twenty.com/graphql",
|
||||
});
|
||||
const fetcher = createGraphiQLFetcher({url: "https://api.twenty.com/graphql"});
|
||||
return (
|
||||
<div className="fullHeightPlayground">
|
||||
<GraphiQL fetcher={fetcher} />;
|
||||
|
||||
67
packages/twenty-docs/src/pages/rest-api.css
Normal file
67
packages/twenty-docs/src/pages/rest-api.css
Normal file
@ -0,0 +1,67 @@
|
||||
.container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 90vh;
|
||||
}
|
||||
|
||||
.form {
|
||||
text-align: center;
|
||||
padding: 50px;
|
||||
}
|
||||
|
||||
.link {
|
||||
color: #16233f;
|
||||
text-decoration: none;
|
||||
position: relative;
|
||||
font-weight: bold;
|
||||
transition: color 0.3s ease;
|
||||
}
|
||||
|
||||
.input {
|
||||
padding: 4px;
|
||||
margin: 20px 0 5px 0;
|
||||
max-width: 460px;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
background-color: #f3f3f3;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
|
||||
}
|
||||
|
||||
.invalid {
|
||||
border: 1px solid red;
|
||||
}
|
||||
|
||||
.token-invalid {
|
||||
color: red;
|
||||
font-size: 12px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.not-visible {
|
||||
visibility: hidden;
|
||||
}
|
||||
|
||||
.loader {
|
||||
color: #16233f;
|
||||
font-size: 2rem;
|
||||
animation: animate 2s infinite;
|
||||
}
|
||||
|
||||
@keyframes animate {
|
||||
0% {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
100% {
|
||||
transform: rotate(720deg);
|
||||
}
|
||||
}
|
||||
|
||||
.loader-container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 50px;
|
||||
}
|
||||
114
packages/twenty-docs/src/pages/rest-api.tsx
Normal file
114
packages/twenty-docs/src/pages/rest-api.tsx
Normal file
@ -0,0 +1,114 @@
|
||||
import Layout from "@theme/Layout";
|
||||
import BrowserOnly from "@docusaurus/BrowserOnly";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { API } from '@stoplight/elements';
|
||||
import '@stoplight/elements/styles.min.css';
|
||||
import './rest-api.css'
|
||||
import { parseJson } from "nx/src/utils/json";
|
||||
import { TbLoader2 } from "react-icons/tb";
|
||||
|
||||
type TokenFormProps = {
|
||||
onSubmit: (token: string) => void,
|
||||
isTokenValid: boolean,
|
||||
isLoading: boolean,
|
||||
token: string,
|
||||
}
|
||||
|
||||
const TokenForm = ({onSubmit, isTokenValid, token, isLoading}: TokenFormProps)=> {
|
||||
const updateToken = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
localStorage.setItem('TryIt_securitySchemeValues', JSON.stringify({bearerAuth: event.target.value}))
|
||||
onSubmit(event.target.value)
|
||||
}
|
||||
|
||||
return !isTokenValid && (
|
||||
<div>
|
||||
<div className='container'>
|
||||
<form className="form">
|
||||
<label>
|
||||
To load your REST API schema, <a className='link' href='https://app.twenty.com/settings/developers/api-keys'>generate an API key</a> and paste it here:
|
||||
</label>
|
||||
<p>
|
||||
<input
|
||||
className={(token && !isLoading) ? "input invalid" : "input"}
|
||||
type='text'
|
||||
disabled={isLoading}
|
||||
placeholder='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMD...'
|
||||
defaultValue={token}
|
||||
onChange={updateToken}
|
||||
/>
|
||||
<p className={`token-invalid ${(!token || isLoading )&& 'not-visible'}`}>Token invalid</p>
|
||||
<div className='loader-container'>
|
||||
<TbLoader2 className={`loader ${!isLoading && 'not-visible'}`} />
|
||||
</div>
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const RestApiComponent = () => {
|
||||
const [openApiJson, setOpenApiJson] = useState({})
|
||||
const [isTokenValid, setIsTokenValid] = useState(false)
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const storedToken = parseJson(localStorage.getItem('TryIt_securitySchemeValues'))?.bearerAuth ?? ''
|
||||
|
||||
const validateToken = (openApiJson) => setIsTokenValid(!!openApiJson.tags)
|
||||
|
||||
const getJson = async (token: string ) => {
|
||||
setIsLoading(true)
|
||||
return await fetch(
|
||||
"https://api.twenty.com/open-api",
|
||||
{headers: {Authorization: `Bearer ${token}`}}
|
||||
)
|
||||
.then((res)=> res.json())
|
||||
.then((result)=> {
|
||||
validateToken(result)
|
||||
setIsLoading(false)
|
||||
return result
|
||||
})
|
||||
.catch(() => setIsLoading(false))
|
||||
}
|
||||
|
||||
const submitToken = async (token) => {
|
||||
if (isLoading) return
|
||||
const json = await getJson(token)
|
||||
setOpenApiJson(json)
|
||||
}
|
||||
|
||||
useEffect(()=> {
|
||||
(async ()=> {
|
||||
await submitToken(storedToken)
|
||||
})()
|
||||
},[])
|
||||
|
||||
return isTokenValid !== undefined && (
|
||||
<>
|
||||
<TokenForm
|
||||
onSubmit={submitToken}
|
||||
isTokenValid={isTokenValid}
|
||||
isLoading={isLoading}
|
||||
token={storedToken}
|
||||
/>
|
||||
{
|
||||
isTokenValid && (
|
||||
<API
|
||||
apiDescriptionDocument={JSON.stringify(openApiJson)}
|
||||
router="hash"
|
||||
/>
|
||||
)
|
||||
}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const restApi = () => (
|
||||
<Layout
|
||||
title="REST API Playground"
|
||||
description="REST API Playground for Twenty"
|
||||
>
|
||||
<BrowserOnly>{() => <RestApiComponent />}</BrowserOnly>
|
||||
</Layout>
|
||||
);
|
||||
|
||||
export default restApi;
|
||||
Reference in New Issue
Block a user