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

@creator.co/wapi-open-api-generator

v1.0.3

Published

A tool to generate OpenAPI specifications from Wapi Routes in TypeScript files.

Downloads

83

Readme

wapi-open-api-generator

A tool to generate OpenAPI specifications from Wapi Routes in TypeScript files.

Installation

You don't need to install the package globally. You can use npx to run the command directly.

Usage

To generate OpenAPI specifications, run:

npx @creator.co/wapi-open-api-generator

This command will:

  1. Recursively find all router.ts files in the src directory.
  2. Extract Wapi Routes from these files.
  3. Generate OpenAPI components and save them to ./docs/api.yaml.

Requirements

Ensure your project meets the following requirements:

  1. TypeScript files Wapi exported Router, named router.ts.
  2. A base.json file in the docs folder.

Configuration

The base.json file should contain your OpenAPI base configuration. Here is an example:

{
    "contact": {
        "email": "[email protected]",
        "name": "Creator.co",
        "url": "http://creator.co"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Local server"
        },
        {
            "url": "https://analytics.dev.creator.co",
            "description": "Dev server"
        }
    ],
    "security": {
        "UserAuth": {
            "bearerFormat": "JWT",
            "description": "User custom JSON web token.",
            "scheme": "bearer",
            "type": "http"
        },
        "APIKey": {
            "description": "API Key",
            "type": "apiKey",
            "in": "header",
            "name": "apiKey"
        }
    }
}

Examples

Route

import { Route, HttpMethod, Response } from '@creator.co/wapi'

import {
  AgenciesListInputType,
  AgenciesListInputSchema,
  AgenciesResponseType,
  AgenciesResponseSchema,
} from './types.js'
import Identity from '../../core/Identity.js'

interface PostRouteType extends Route<AgenciesListInputType, AgenciesResponseType> {}
export default class Post implements PostRouteType {
  public path: string = '/agencies'
  public method: HttpMethod = HttpMethod.POST
  public inputSchema = AgenciesListInputSchema
  public openApi = {
    summary: 'List Agencies',
    description: 'Paginated Agencies Listing',
    outputSchema: AgenciesResponseSchema,
    tags: ['Agencies'],
    security: [{ UserAuth: [] }],
  }
  public handler: PostRouteType['handler'] = async transaction => {
    return await new Identity.Core(transaction, Identity.Globals.AccessLevel.ADMIN).authenticate(
      async core => {
        const b = transaction.request.getBody()
        const resp = await core.agencyService!.agency.list(b.nextToken || undefined)
        if (resp instanceof Response) return resp
        return Response.SuccessResponse(resp)
      }
    )
  }
}

Input Body Example

// Input Body Example
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import { z } from 'zod'

import { AgencyEntity } from '../../core/components/database/entities/Agency.js'

extendZodWithOpenApi(z)

/* Post */
export const AgenciesListInputSchema = z
  .object({
    nextToken: z.string().nullish().openapi({
      description: 'Optional next token',
    }),
  })
  .openapi({
    description: 'Agencies list input body',
    name: 'AgenciesListInput',
  })

export type AgenciesListInputType = z.infer<typeof AgenciesListInputSchema>

Output Body Example


// Output Body Example
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import { z } from 'zod'

import { AgencyEntity } from '../../core/components/database/entities/Agency.js'

extendZodWithOpenApi(z)

/* Post */
export const AgenciesResponseSchema = z
  .object({
    agencies: z.array(AgencyEntity).openapi({
      description: 'List of agencies',
    }),
    nextToken: z.string().optional().openapi({
      description: 'Next token for pagination',
    }),
  })
  .openapi({
    description: 'Agencies list response body',
    name: 'AgenciesResponse',
  })

export type AgenciesResponseType = z.infer<typeof AgenciesResponseSchema>

Path Parameter Example


// Path Parameter Example
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import { z } from 'zod'

import { AgencyEntity } from '../../core/components/database/entities/Agency.js'

extendZodWithOpenApi(z)

/* Path */
export const AgencyPathSchema = z
  .object({
    agencyId: z.string().openapi({
      param: {
        name: 'agencyId',
        in: 'path',
      },
      example: 'Agency Id',
    }),
  })
  .openapi({
    description: 'Agency authorized route path parameters',
  })

export type AgencyPathType = z.infer<typeof AgencyPathSchema>