Netherworld's Pretend Minibar

    koa-change-case
    TypeScript icon, indicating that this package has built-in type declarations

    1.2.4 • Public • Published

    Koa change case

    Expansible Koa middleware for converting objects in context

    Use

    import Koa from "koa";
    import { snakeCase, camelCase, requestBody, responseBody } from "koa-change-case";
    import bodyParser from "koa-bodyparser";
    
    const app = new Koa();
    
    app.use(bodyParser())
    app.use(camelCase(requestBody))
    
    // Set your middlewares
    
    app.use(snakeCase(responseBody))

    Expansion

    Convert custom type

    import { ConvertProcessor, Converter, snakeCase } from "koa-change-case";
    
    class MapConvertProcessor
      implements ConvertProcessor<Map<unknown, unknown>, Map<unknown, unknown>> {
      constructor(private readonly changeCase: (name: string) => string) {}
    
      convert(
        map: Map<unknown, unknown>,
        converter: Converter
      ): Map<unknown, unknown> {
        const newMap = new Map<unknown, unknown>();
        map.forEach((value, key) => {
          const convertedKey = typeof key !== "string" ? key : this.changeCase(key);
          newMap.set(convertedKey, converter.convert(value));
        });
    
        return newMap;
      }
    
      isConvertible(value: unknown): boolean {
        return value instanceof Map;
      }
    }
    
    snakeCase.register(new MapConvertProcessor(snakeCase.changeCase))

    Convert other convention

    import { toMiddlewareFactory, createDefaultConverterManager } from "koa-change-case";
    
    const toOtherCase: (key: string) => string // TODO(implement) 
    
    const otherCase = toMiddlewareFactory(
      createDefaultConverterManager(toOtherCase)
    );

    Keywords

    none

    Install

    npm i koa-change-case

    DownloadsWeekly Downloads

    3

    Version

    1.2.4

    License

    MIT

    Unpacked Size

    16.8 kB

    Total Files

    14

    Last publish

    Collaborators

    • araxsiyual