1.1.8 โ€ข Published 29 days ago

next-rocket-kit v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

next-rocket-kit ๐Ÿš€

Note ๐Ÿงช: I will start the migration for next.js versions 15 and higher.

and in the future we will migrate this package to the @rocket-kit family, where there is also @rocket-kit/edge for limit functions in supabase and we will have new functions

@rocket-kit/next new functions:

  • migrate from tsdx to dts

  • swagger automatic documentation

  • creator of server actions
  • it will be divided into @rocket-kit/next/server and @rocket-kit/next/client in the client we will add hooks like

  • useUrls that types and generates only a json that is packaged in a hook

  • add @form-instant by auto generate forms

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.8

29 days ago

1.1.7

10 months ago

1.1.6

10 months ago

1.1.5

10 months ago

1.1.1

10 months ago

1.0.0

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.5.3

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.5.0

1 year ago

0.7.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.7.11

1 year ago

0.7.10

1 year ago

0.7.9

1 year ago

0.7.12

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.4.23

1 year ago

0.8.1

1 year ago

0.6.3

1 year ago

0.8.0

1 year ago

0.6.2

1 year ago

0.6.5

1 year ago

0.8.2

1 year ago

0.6.4

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.4.20

1 year ago

0.4.21

1 year ago

0.4.9

1 year ago

0.4.8

1 year ago

0.4.22

1 year ago

0.4.10

1 year ago

0.4.17

1 year ago

0.4.18

1 year ago

0.4.15

1 year ago

0.4.16

1 year ago

0.4.13

1 year ago

0.4.14

1 year ago

0.4.11

1 year ago

0.4.12

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.7

1 year ago

0.4.6

1 year ago

0.4.1

1 year ago

0.3.2

1 year ago

0.2.3

1 year ago

0.3.1

1 year ago

0.2.2

1 year ago

0.4.3

1 year ago

0.3.4

1 year ago

0.4.2

1 year ago

0.3.3

1 year ago

0.2.4

1 year ago

0.1.0

1 year ago