Skip to content

Instantly share code, notes, and snippets.

@chunleng
chunleng / App.tsx
Created August 9, 2021 14:55
Medium: OpenAPI and CodeGen - React
import React from "react";
import { Bar, FirstApi } from "./gen/api";
function App() {
const [bar, setBar] = React.useState<Bar>({})
React.useEffect(() => {
new FirstApi(undefined, "http://localhost:8000").getFoo().then((res) => {
setBar(res.data);
});
@chunleng
chunleng / api.ts
Created August 9, 2021 13:41
Medium: OpenAPI and CodeGen - Codegen Part 2
/**
* FirstApi - object-oriented interface
* @export
* @class FirstApi
* @extends {BaseAPI}
*/
export class FirstApi extends BaseAPI {
/**
*
* @summary Get Foo
@chunleng
chunleng / api.ts
Last active August 9, 2021 13:41
Medium: OpenAPI and CodeGen - Codegen Part 1
/**
*
* @export
* @interface Bar
*/
export interface Bar {
/**
*
* @type {Array<string>}
* @memberof Bar
@chunleng
chunleng / main.py
Last active August 9, 2021 14:55
Medium: OpenAPI and CodeGen - FastAPI
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=['*'])
class Bar(BaseModel):
items: list[str] = []