npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apistoryai-generate-collection

v1.1.10

Published

A CLI tool that generates a collection.json from a schema.py file, defining API endpoints based on the provided database schema.

Downloads

124

Readme

APIStoryAI Collection Generator

APIStoryAI Collection Generator is a CLI tool designed to generate a collection.json file from any schema file written in any language. (e.g., SQLAlchemy models). It utilizes OpenAI's GPT-4 to dynamically create API endpoints based on the database schema provided. This is useful for developers who want to quickly scaffold an API collection to use in their projects, saving time and effort.

Features

  • Automatically generates collection.json from a database schema written in any programming language; for the example here we used schema.py written in python.
  • Integrates with OpenAI to ensure accurate and detailed API specification generation.
  • Supports common API patterns like GET, POST, PUT, and DELETE.
  • Outputs a JSON file with API endpoints, descriptions, and request formats.

Installation

To install and set up the package, follow these steps:

  1. Initialize npm if you havent already via npm:
npm init --y
  1. Install via npm:
npm i apistoryai-generate-collection

Usage

Saving OpenAI API Key in System Path

Before using the package, you need to save your OpenAI API key in your system's environment variables. This allows the package to access the OpenAI API.

For macOS/Linux:

  1. Open your terminal.

  2. Run the following command to add your API key to your shell configuration file (e.g., .bashrc or .zshrc):

echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.bashrc

If you're using Zsh (the default shell in newer versions of macOS), use .zshrc:

echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.zshrc
  1. Reload your shell configuration by running:
source ~/.bashrc

Or for Zsh:

source ~/.zshrc

For Windows:

  • Open the Start Menu and search for "Environment Variables."
  • Click on "Edit the system environment variables."
  • In the "System Properties" window, click the "Environment Variables" button. Under "User variables," click "New."
  • Set the "Variable name" as OPENAI_API_KEY and the "Variable value" as your OpenAI API key.
  • Click "OK" to save the changes.
  • After this, your API key will be available for the CLI tool to use.

Command-line usage:

Once your API key is set up, run the following command to generate the collection.json file from your schema:

1. Command-line usage:

Run the command, replacing <path-to-schema.py> with the actual path to your schema file:

npx apistoryai-generate-collection <path-to-schema.py>

Example:

npx apistoryai-generate-collection /Users/username/Documents/AIProject/Schemas/schema.py

2.Generate API Collection:

The script will generate a collection.json file in the current directory. This file will define API endpoints that match the structure of your schema, including paths, methods, descriptions, and database schema.

Example Schema (python)

The schema here schema.py should defines the database models using an ORM like SQLAlchemy, for example:

class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String, unique=True)
msisdn = Column(String, unique=True)

class Subscription(Base):
__tablename__ = "subscriptions"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
service = Column(String)
date_subscribed = Column(Date)

Based on the schema, the generated Collection looks like this:

{
  "endpoints": [
    {
      "path": "/users",
      "method": "GET",
      "description": "Retrieve a list of users",
      "schema": {
        "name": "users",
        "columns": [
          { "name": "name", "type": "string" },
          { "name": "email", "type": "string" },
          { "name": "msisdn", "type": "string" }
        ]
      }
    },
    {
      "path": "/subscriptions_by_users/{user_id}",
      "method": "GET",
      "description": "Get subscriptions for a specific user",
      "parameters": [
        { "name": "user_id", "in": "path", "type": "integer", "required": true }
      ],
      "schema": {
        "name": "subscriptions",
        "columns": [
          { "name": "service", "type": "string" },
          { "name": "date_subscribed", "type": "date" },
          { "name": "user_id", "type": "integer" }
        ]
      }
    }
  ]
}

Requirements

This package uses the following dependencies:

  • fs-extra: To read and write files.
  • openai: For generating the API collection using OpenAI GPT.
  • python-shell: In case of additional Python script execution.

License

This project is licensed under the ISC License.

Author

[sterlingking].