-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
334 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createSwaggerSpec } from 'next-swagger-doc' | ||
import ReactSwagger from './react-swagger' | ||
import 'swagger-ui-react/swagger-ui.css' | ||
import {getContractSwagger} from '@/utils/swagger' | ||
|
||
const getApiDocs = async (contract_id: string) => { | ||
return createSwaggerSpec({definition: await getContractSwagger(contract_id)}) | ||
} | ||
|
||
export default async function IndexPage({ | ||
params, | ||
}: { | ||
params: Promise<{ contract_id: string }> | ||
}) | ||
{ | ||
const contract_id = (await params).contract_id.replace("%40", "@"); | ||
|
||
try { | ||
const spec = await getApiDocs(contract_id) | ||
return ( | ||
<section className='container'> | ||
<ReactSwagger spec={spec} /> | ||
</section> | ||
) | ||
} | ||
catch (error) { | ||
return ( | ||
<div style={{ textAlign: 'center', padding: '50px' }}> | ||
<h1>{'An error occurred'}</h1> | ||
<p>{`The contract '${contract_id}' either does not exist or has an invalid ABI.`}</p> | ||
<p>{`${error}`}</p> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use client' | ||
|
||
import SwaggerUI from 'swagger-ui-react' | ||
import 'swagger-ui-react/swagger-ui.css' | ||
|
||
type Props = { | ||
spec: Record<string, any> | ||
} | ||
|
||
function ReactSwagger({ spec }: Props) { | ||
return <SwaggerUI spec={spec} docExpansion="none" /> | ||
} | ||
|
||
export default ReactSwagger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AppError, handleError } from '@/utils/errors' | ||
import { getContractSwagger } from '@/utils/swagger' | ||
|
||
/** | ||
* @swagger | ||
* /v1/contract/{contract_id}/swagger: | ||
* get: | ||
* tags: [Contracts] | ||
* description: Returns a Swagger file for the Contract's methods | ||
* summary: Returns a Swagger file for the Contract, detailing its methods. | ||
* parameters: | ||
* - name: contract_id | ||
* schema: | ||
* type: string | ||
* in: path | ||
* description: Koinos address of the contract, name of the contract (for system contracts) or KAP name | ||
* required: true | ||
* example: 15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL | ||
* responses: | ||
* 200: | ||
* description: Swagger | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* example: | ||
* openapi: 3.0.0 | ||
* info: | ||
* title: "'15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL' REST API" | ||
* version: 1.0.0 | ||
* tags: | ||
* - name: Contracts | ||
* description: Includes endpoints for interacting with the '15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL' contract on Koinos | ||
* paths: {} | ||
*/ | ||
|
||
export async function GET(request: Request, { params }: { params: { contract_id: string } }) { | ||
try { | ||
return Response.json(await getContractSwagger(params.contract_id)) | ||
} catch (error) { | ||
return handleError(error as Error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.