Skip to content
/ restfy Public

A Flask extension for creating RESTful APIs with TypeScript type generation.

License

Notifications You must be signed in to change notification settings

Fy-/restfy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

restfy

A Flask extension for creating RESTful APIs with TypeScript type generation. This library is still in development and should be considered a playground at this stage. Do not use it in production environments.

Installation

Install the package using pip:

pip install git+https://github.com/Fy-/restfy.git

Usage

Import and use the RestAPI class in your Flask application:

from flask import Flask
from fy_rest import RestAPI
api = RestApi(base_url="http://localhost:5000", load_user: None, refresh_user: None)

app = Flask(__name__)
api.init_app(app)

Creating API Routes

from dataclasses import dataclass
from fy_rest import APIResponse
from flask import jsonify

@dataclass
class User:
    id: int
    name: str
    
@dataclass
class UserResponse(APIResponse):
    data: User

@app.route('/users', methods=['GET'], response_type=UserResponse)
def get_users(ctx):
    users = [User(id=1, name='Alice'), User(id=2, name='Bob')]
    return jsonify(UserResponse(data=users, success=True, time=ctx.get_time()))
    
    
#: Or with blueprints
rest_api = RestAPI()
#... rest_api.init_app(app)
persona_bp = Blueprint('persona', __name__)
rest_api.register_blueprint(persona_bp) #: RestAPI instance
persona_bp.route('/xxx', methods=['GET'], response_type=UserResponse) #: etc...

CLI Commands

Fyrest provides three CLI commands to help you generate TypeScript types and fetch functions for your API:

flask rest-all: Prints both the TypeScript types and fetch functions for your API.

This command will output the TypeScript types and fetch functions as requested. Or you can just access https://localhost:5000/ts

$ flask rest-all
import { v4 as uuidv4 } from "uuid";

export interface User {
  id: number;
  name: string;
}

export interface UserResponse {
  success: boolean;
  data: User;
  message: string;
  time: number;
}

export async function usersFetch(params: { [key: string]: any }): Promise<UserResponse> {
    const queryParams = Object.entries(params).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');
    const url = `http://localhost:5000/users` + (queryParams ? `?${queryParams}` : '');
    const response = await fetch(url, {
        method: 'GET',
        headers: new Headers({"Content-Type": "application/json", "X-Request-Id": uuidv4(), "X-Fyrest-Session": session})
    });
    return (await response.json()) as UserResponse;
}

About

A Flask extension for creating RESTful APIs with TypeScript type generation.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages