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

@getflip/error-code-generator

v0.7.0

Published

A library to extract error codes from an open api spec and generate error codes types and objects for TypeScript and Dart.

Downloads

819

Readme

Error Code Generator Library for Open API Specs

Overview

Error Code Generator is a TypeScript library aimed at simplifying the process of managing and generating error codes in a systematic manner across different programming languages in your projects. This allows you to create, manage, and implement error codes efficiently, ensuring coherent error management throughout your applications. Define the error codes in your OpenAPI specification, and the library will generate the associated code in the languages of your choice.

Installation

Ensure to import the main class and the relevant factory for a seamless integration:

import {
  ErrorCodeGenerator,
  CodeGeneratorHandler,
} from "@getflip/error-code-generator";

Key Components

ErrorCodeGenerator

Overview

ErrorCodeGenerator is the pivotal class responsible for orchestrating the error code generation process. This class coordinates with various handlers, ensuring a systematic workflow from extracting error codes to generating the associated TypeScript code and writing them to files.

Usage

  1. Initialization:

    Instantiate the ErrorCodeGenerator with an array of code generators:

    const generator = new ErrorCodeGenerator(codeGenerators);

    Where codeGenerators contains instances of code generators, such as TypeScript or Dart generators. The codeGenerators array can be created through the CodeGeneratorFactory. There are two languages supported at the moment: TypeScript and Dart.

    const codeGenerators = [
      CodeGeneratorFactory.createGenerator("TypeScript"),
      CodeGeneratorFactory.createGenerator("Dart"),
    ];
    
    // or
    const generator = new ErrorCodeGenerator([
      CodeGeneratorFactory.createGenerator("TypeScript"),
      CodeGeneratorFactory.createGenerator("Dart"),
    ]);
  2. Configuration: The Error Code Generator requires a few configurations to be set before running the generation process. These configurations are:

    • Set the source path:

      generator.setSourcePath("[your-openapi-spec-source-path]");
    • Set the output directory:

      generator.setOutputDirectory("[your-output-directory]");
    • Optionally, add more code generators, in case you need additional languages:

      generator.addCodeGenerators(additionalCodeGenerators);
  3. Generate Error Codes:

    Kickstart the error code generation process by invoking:

    generator.generate();

    Ensure all mandatory configurations are set before invoking the generate method. Otherwise, an error will be thrown indicating the absence of necessary configurations.

Handlers

  • ErrorCodeExtractorHandler: Extracts error codes from a source file.
  • CodeGeneratorHandler: Generates code, based on the extracted error codes.
  • FileWriterHandler: Writes the generated code to files in a specified directory.

Adding Code Generators

The Error Code Generator library supports TypeScript and Dart code generation out of the box through the TypeScriptCodeGenerator and DartCodeGenerator classes. However, the library is designed to be extensible, allowing you to add support for additional languages. To add support for a new language, you need to create a new class that implements the CodeGenerator interface. The interface contains a single method, generate, which takes in an array of ErrorCode objects and returns a string containing the generated code.

  • CodeGeneratorFactory: A factory to instantiate and manage various code generators, facilitating easy extension to support additional languages in the future.

  • CodeGenerator: An interface that defines the contract for code generators. The interface contains a single method, generate, which takes in an array of ErrorCode objects and returns a string containing the generated code.

    export interface CodeGenerator {
      language: string;
      fileExtension: string;
      generateCode(): GeneratedCode;
      setEndpointErrorCollection(
        errorCollection: EndpointErrorCollection,
      ): CodeGenerator;
    }

Example Usage

The following example demonstrates how to use the Error Code Generator library. As it is build with the help of the Builder pattern, you can chain the methods to set the configurations and generate the error codes.

const generator = new ErrorCodeGenerator([
  CodeGeneratorFactory.createGenerator("TypeScript"),
  CodeGeneratorFactory.createGenerator("Dart"),
])
  .setSourcePath("[your-openapi-spec-source-path]")
  .setOutputDirectory("[your-output-directory]")
  .generate();

Ensure to replace placeholders such as [your-source-path] and [your-output-directory] with actual paths relevant to your project.

Contributing and Extending

Extend and customize ErrorCodeGenerator and associated handlers to accommodate any project-specific requirements. Any contributions, feature requests, and issue tracking are welcome! To get started with contributing, please refer to our contribution guide.

Development

To develop locally, run yarn, then yarn dev generate -s {source-yaml} -o {output-folder}.

License

This library is licensed under [Your License Name]. For more details, see the LICENSE file in the repository.


Feel free to modify documentation according to any specific features or nuances of the library, providing accurate links, paths, and guidelines for developers and users.