Codacy API client for Typescript
npm install @codacy/api-typescriptbash
sudo apt-get install libunwind8
`The AutoRest version we are using only works up to Node.js v14.
Usage
$3
To run the entire process:
`bash
npm ci
`To fetch the current version of the API:
`bash
npm run fetch-api
`To generate the code:
`bash
npm run generate
`$3
Current API mock-server doesn't support a basePath, so you need to fetch the API, comment it, and then generate the client. Now you can mock an API server for development or testing purposes.
`bash
.\mock-api.sh
`$3
Locally, run yarn link from this project root folder, and then run yarn link @codacy/api-typescript on the project you want to use it. The best practice for using this library would be:#### 1. Creating a context
`typescript
import React from 'react'
import { Client } from '@codacy/api-typescript'const AppContext = React.createContext(null)
export default AppContext
`#### 2. Wrapping your app in that context provider
`typescript
...
import ApiContext from './ApiContext'
import { Client } from '@codacy/api-typescript'
...
const App: React.FC = () => {
return (
value={
new Client({
baseUri: process.env.REACT_APP_CODACY_API_URI,
})
}>
{/ Your app's code here /}
...
`#### 3. Using the client's context to fetch data
`typescript
import React, { useState, useEffect, useContext } from 'react'
import ApiContext from '../ApiContext'
import { User, BaseApiError, UnauthorizedApiError } from '@codacy/api-typescript/lib/models'export interface UserInformationProps {
id: number
}
export const UserInformation: React.FC = ({ id }) => {
const [isLoading, setIsLoading] = useState(true)
const [user, setUser] = useState()
const [error, setError] = useState()
const client = useContext(ApiContext)
useEffect(() => {
async function fetchUser() {
try {
const response = await client!.getUser(id)
setUser(response!.data)
} catch (err) {
if ( err instanceof UnauthorizedApiError ) {
// catch specific errors
} else {
setError(err as BaseApiError)
}
} finally {
setIsLoading(false)
}
}
fetchUser()
}, [client, id])
return (
<>
{isLoading && Loading ...}
{!!user && (
{user.username} ({user.mainEmail})
)}
{!isLoading && !!error && (
{error.message} ({error.code})
)}
>
)
}
`Developement
$3
- Autorest (npm install -g autorest`)