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

@map-colonies/openapi-helpers

v1.2.0

Published

A package that provides utilities for working with openapi files

Downloads

26

Readme

openapi-helpers

This package contains scripts, types and functions to help you work with openapi.

Installation

Run the following commands:

npm install --save-dev @map-colonies/openapi-helpers supertest prettier openapi-typescript @types/express

types-generator

The package contains a script that wraps the openapi-typescript package and generates types for the openapi schema. The script also formats the generated types using prettier.

The command structure is as follows:

npx @map-colonies/openapi-helpers <input-file> <output-file> --format --add-typed-request-handler

For example:

npx @map-colonies/openapi-helpers ./openapi3.yaml ./src/openapi.d.ts --format --add-typed-request-handler

options

  • --format - format the generated types using prettier.
  • --add-typed-request-handler - add the TypedRequestHandler type to the generated types.

TypedRequestHandler

The package contains a wrapper for the express types package that provides autocomplete for all the request Handlers to the API based on the openapi. The TypedRequestHandler is initialized with the the typed generated by openapi-typescript, and is configured based on operation name or method and path.

Usage

import { TypedRequestHandlers } from '@map-colonies/openapi-helpers/typedRequestHandler';
import type { paths, operations } from './src/openapi.d.ts';

// Initialize the TypedRequestHandlers with the paths and operations types
// This can be done in a separate file and exported, in the same file or even in the same line
type MyHandlers = TypedRequestHandlers<paths, operations>;

export class Controller {
  // Define the handler for the operation based method and path
  public getResource: MyHandlers['GET /resource'] = (req, res) => {
    res.status(httpStatus.OK).json({id: 1, description: 'description', name: 'name'});
  };


  // Define the handler for the operation based on the operation name
  public getResource: MyHandlers['getResource'] = (req, res) => {
    res.status(httpStatus.OK).json({id: 1, description: 'description', name: 'name'});
  };
}

RequestSender

The package contains a wrapper for the supertest package that provides autocomplete for all the requests to the API based on the openapi. The requestSender is initialized with the server's base url and the openapi schema and the types exported by openapi-typescript.

import { RequestSender } from '@map-colonies/openapi-helpers/requestSender';
import type { paths, operations } from './src/openapi.d.ts';

const requestSender = await createRequestSender<paths, operations>('path/to/openapi3.yaml', expressApp);

The requestSender object contains all the paths and operations defined in the openapi schema. For example, to send a request to the getUsers operation with the /users path and with the GET method, you can use the following code:

const response = await requestSender.getUsers();

// or

const response = await requestSender.sendRequest({ 
  method: 'get', 
  path: '/simple-request'
});

The package supports all the operations defined in the openapi schema, either by operation name, or by using the sendRequest function with the method, path and parameters.

[!IMPORTANT] For the package function properly, you need to make sure that the following values are configured in your tsconfig.json or jsconfig.json files under compilerOptions:

  • module: "NodeNext"
  • moduleResolution: "NodeNext"