1.1.7 โ€ข Published 3 months ago

next-rocket-kit v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

next-rocket-kit ๐Ÿš€

This package is intended to make it easy to build back-end applications in the framework, next.js with, using the app directory.

Index

Configuration object ๐Ÿ”ฉ๐Ÿ”ง

This object is used to define types and versions of the tools.

  • Default object. In the event that the user does not place a configuration object, this will be the object that will be used by default in the kit.

    import  { ConfigObject }  from  "next-rocket-kit";
    
    const configObjet: ConfigObject = {
      resolver: "zod",
      oas: "3.1"
    };
      ```

Tools we offer ๐Ÿ”ง๐Ÿ”จ

Note ๐Ÿงช: It is important to note that we will all be adding new tools to the kit in the future.

Route Module ๐Ÿ”ƒ

This tool helps to quickly create an endpoint using the next.js API folder.

  • We create the rocket to be able to access the tools in the kit.

    "path file" ~ ./libs/rocketKit/tools

  import { createRocket } from "next-rocket-kit";

  export const { onRoute, http, OpenApi } = createRocket();
  • We define the router which should be used on the server side only, for this next.js gives us the comment function use serve.

    // "path file" ~ ./libs/rocketKit/Route
    
    "use serve"
    
    import { onRoute } from "./tools";
    
    export const { Route } = onRoute();
  • We define barrel file.

    // "path file" ~ ./libs/rocketKit/index
    
    export * from './tools';
    export * from './Route';
  • We define a basic endPoint.

    // "path file" ~ ./src/app/api/Route.ts
    
    import { Route } from "@/libs/rocketKit";
    
    // End Point GET basic
    export const GET = Route({
      Handler(req, reply, context) {
        return reply.json({ message: "Hello World!" }, { status: 201 });
      },
    });

Configuration object for Route

  • Handler: is the function that is executed when calling the end point. With the rocket Route it is much easier for us to create endpoints, such as a GET method endpoint. The handler function receives three parameters to handle and control the request video cycle, these parameters are as follows.

    • req: Everything that arrives from the client and gives access to all the native methods of NextRequest.

    Rocket functions in req.

    • req.getBody(): return body.

    • req.getQuery(): return queries.

    • req.getContext(): return context include path params.

    • req.getHeaders(): return headers.

    • reply: used to reply to the client and gives access to all the native methods of NextResponse.

    • context (The native context of nextjs)

  • Schema (Schema valid): The schemas attribute allows you to validate the type and format of the data that enters and leaves the Route, to handle these validations Route is compatible with two possible third party libraries, "zod" and "yup". By default, createRocketKit() uses "zod" as the validation library.

    // "path file" ~ ./src/app/api/route.ts
    import { Route } from "@/libs/Route";
    
    // End Point GET basic
    export const POST = Route({
      schemas: {
        body: Schema,
        query: Schema,
        context: Schema,
        headers: Schema,
        response: Schema,
      },
      Handler(req, reply, context) {
        return reply.json({ message: "Hello World!" }, { status: 201 });
      },
    });

The Schemas attribute uses a life cycle to execute the validations, the order of that life cycle is as follows.

graph LR
A(headers) --> B(context) --> C(query) --> D(body) --> E(Handler) --> F(response)

Prisma client creator ๐Ÿ”ƒ

This tool helps to quickly create an PrismaClient.

"path file" ~ ./libs/rocketKit/tools

  import { createRocket } from "next-rocket-kit";

  export const { onPrisma } = createRocket();
  import { PrismaClient } from "@prisma/client";
  import { onPrisma } from "./rocket";

  export const { prisma } = onPrisma(PrismaClient);

Global where in the select.

  import { PrismaClient } from "@prisma/client";
  import { onPrisma } from "./rocket";

  export const { prisma } = onPrisma(PrismaClient, {
    where: { delete_at: null }
  });

Http Module ๐Ÿ“

The Http tool will help you manage http status, to better manage and organize your request responses.

Note ๐Ÿ“ฆ: rocket-kit uses the http-status-codes package

const Http = {
  ReasonPhrases,
  StatusCodes,
  getReasonPhrase,
  getStatusCode,
};
// "path file" ~ ./libs/rocketKit
import  { createRocket }  from  "next-rocket-kit";

export  const  { Http } =  createRocket();
// "path file" ~ ./src/app/api/route.ts
import { Http } from '@/libs/rocketKit';
import { Route } from '@/libs/Route';

// End Point GET basic
export const GET = Route({
  Handler(req,  reply,  context)  {
    return  reply.json({
      message:  Http.ReasonPhrases.OK, // "OK"
    },
    {
      status:  Http.StatusCodes.OK, // 200
    });
  },
});

OpenAPI Module ๐Ÿ“

OpenAPI allows you to create a json in openapi "3.0" or "3.1" format, compatible with tools like swagger, postman and anyone that receives the openapi format.

Note ๐Ÿ“ฆ: rocket-kit uses the openapi3-ts package.

To define the openapi version you must use the oas attribute in the kit configuration object.

// "path file" ~ ./libs/rocketKit
import  { createRocket }  from  "next-rocket-kit";

export  const  { Http, OpenApi } =  createRocket();

OpenApi Example

// "path file" ~ ./libs/rocketKit
import  { createRocket } from  "next-rocket-kit";

export  const  { Http, OpenApi } =  createRocket();
import { OpenApi } from "./libs/rocketkt";

// declare info and openapi version.
const openApi = OpenApi({
  openapi: "3.0.3", // or 3.1.0
  info: {
    title: "example",
    description: "string",
    termsOfService: "string",
    contact: {
      name: "Author",
    },
    license: {
      name: "MIT",
    },
    version: "1.0.0",
  },
});

openApi.addSchema("User", {
  type: "object",
  properties: {
    id: {
      type: "string",
    },
    name: {
      type: "string",
    },
  },
});

openApi.addPath("/items", {
  description: "return item list",
  post: {
  description: "get items",
  summary: "get items",
  requestBody: {
    description: "body",
    content: {
      "application/json": {
        schema: { $ref: "#/components/schemas/User" },
      },
    },
  },
  responses: {
      200: {
        description: "ok",
        content: {
          "application/json": {
          schema: {
              type: "object",
              properties: {
                id: {
                  type: "string",
                },
              },
            },
          },
        },
      },
    },
  },
});

// return json string
openApi.getSpecAsJson()
// or return yml stirng
openApi.getSpecAsYaml()

Recommendations for use with third-party packages to OpeanApi

How use OpenApi with "zod".

  • @anatine/zod-openapi: With this package we can reuse the "zod" validation schemas that you should already be using in the Route schema field to validate the body or some other field of the request.
import { OpenApi } from "@/libs/rocketKit"
import { generateSchema, extendZodWithOpenApi } from '@anatine/zod-openapi';
import { z } from 'zod';

// extend zod
extendZodWithOpenApi(z);


// declare info and openapi version.
const openApi = OpenApi({
    openapi: "3.0.3", // or 3.1.0
    info: {
        title: "example",
        description: "string",
        termsOfService: "string",
        version: "1.0.0",
        contact: {
          name: "Author",
        },
        license: {
          name: "MIT",
        },
    },
});

const ItemZodSchema = z
  .object({
    id: z.string().uuid().nonempty().openapi({
      title: "Item ID",
      description: "A UUID generated by the server",
    }),
    name: z.string().min(2),
  })
  .openapi({
    title: "Item",
    description: "A item schema",
  });

const ItemOpenAPiSchema = generateSchema(ItemZodSchema);

openApi.addSchema("Item", ItemOpenAPiSchema);

Note ๐Ÿงช: In the case of Yup we have not found a package that meets the standards we are looking for, we remain attentive to options proposed by the community.

1.1.7

3 months ago

1.1.6

3 months ago

1.1.5

3 months ago

1.1.1

3 months ago

1.0.0

6 months ago

0.7.2

7 months ago

0.7.1

7 months ago

0.5.3

8 months ago

0.7.4

7 months ago

0.7.3

7 months ago

0.5.0

8 months ago

0.7.0

7 months ago

0.5.2

8 months ago

0.5.1

8 months ago

0.7.11

7 months ago

0.7.10

7 months ago

0.7.9

7 months ago

0.7.12

7 months ago

0.7.6

7 months ago

0.7.5

7 months ago

0.7.8

7 months ago

0.7.7

7 months ago

0.4.23

8 months ago

0.8.1

7 months ago

0.6.3

7 months ago

0.8.0

7 months ago

0.6.2

7 months ago

0.6.5

7 months ago

0.8.2

6 months ago

0.6.4

7 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.4.20

8 months ago

0.4.21

8 months ago

0.4.9

8 months ago

0.4.8

8 months ago

0.4.22

8 months ago

0.4.10

8 months ago

0.4.17

8 months ago

0.4.18

8 months ago

0.4.15

8 months ago

0.4.16

8 months ago

0.4.13

8 months ago

0.4.14

8 months ago

0.4.11

8 months ago

0.4.12

8 months ago

0.3.0

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.4.5

8 months ago

0.4.4

8 months ago

0.4.7

8 months ago

0.4.6

8 months ago

0.4.1

8 months ago

0.3.2

8 months ago

0.2.3

8 months ago

0.3.1

8 months ago

0.2.2

8 months ago

0.4.3

8 months ago

0.3.4

8 months ago

0.4.2

8 months ago

0.3.3

8 months ago

0.2.4

8 months ago

0.1.0

9 months ago