0.0.82 โ€ข Published 11 months ago

@woen4/higher v0.0.82

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

Higher is a framework it was built thinking in some requirements

  • The minimum of code lines to create an endpoint
  • Files with a unique responsibilty, representing a unique endpoint
  • Easy to test
  • Performance
  • Good developer experience
  • And that it works well in a serverless environment

ย 

has support to:

  • ๐Ÿ”„ Middlewares (per folder level)
  • โœ… Body and query params validation (using Zod)
  • โคต Dependency injection (Test is very easy)

ย 

and was built-in using:

  • Fastify
  • Zod
  • Typescript
  • Tsup (Dev server and build process is very fast)

ย 

How to install:

  npx create-higher-app

ย 

Initial guide:

Higher follow a well defined structure, let's start with a route to retrieve users

get.ts :

  export handle = () => {
    return ["Kaio", "Woen"]
  }

index.ts :

  import { bootstrap } from "@woen4/higher";

  const server = bootstrap();

  server.listen(3000)

On run dev script, this will run your server exposing a route /users of method GET

so you need create a folder providers in src directory and exports a object from there

prisma.ts :

  import { PrismaClient } from '@prisma/client'

  export const prisma = new PrismaClient()

index.ts :

  export type Providers = typeof import("./index");

  export * from './prisma'

And then, use this in your route handler

get.ts :

  import { Providers } from '../../providers'

  export handle = (ctx: Providers) => {
    return ctx.prisma.users.findMany()
  }

But... how to acess request in handler? Simple modules/users/post.ts

  import { Providers } from '../../providers'

  export handle = (ctx: Providers, { body }: HigherRequest) => {
    return ctx.prisma.users.create({ data: body })
  }

But... how to validate the body data? Also is simple

  import { Providers } from '../../providers'
  import { z } from "zod";

  export const schema = z.object({
    name: z.string(),
    email: z.string(),
  });

  export handle = (ctx: Providers, { body }: HigherRequest<void, typeof schema>) => {
    return ctx.prisma.users.create({ data: body })
  }

The generic type <void, typeof schema> will offer the intellisense in your editor when using body object

Ok, but I need of a middleware to authenticate my routes, how do it?. Also is very simple

Create a file named middleware.ts inside the modules folder and it will be apply to all routes, otherwise if you put your middleware file inside the users folder, it will only apply to users routes

middleware.ts

  import { HigherRequest, HigherResponse } from "@woen4/higher";
  import { Providers } from '../providers'

  export type WithAuth = {
    user: {
      id: string;
    };
  };

  export const handle = (ctx: Providers, request: HigherRequest<WithAuth>) => {
    /* 
    
    Some lines of code to implementate authorization

    */

    request.user = {
      id: '123',
    }
  };

One more question, how to create routes with parameters? Simple and plain

userId/get.ts

  import { Providers } from '../../providers'
  import { WithAuth } from '../../../middleware.ts'

  export handle = (ctx: Providers, { user }: HigherRequest<WithAuth>) => {
    
    return ctx.prisma.users.findFirst({ where: { id: user.id } })
  }

ย 

Full documention:

is coming...

0.0.82

11 months ago

0.0.81

1 year ago

0.0.80

1 year ago

0.0.79

1 year ago

0.0.78

1 year ago

0.0.77

1 year ago

0.0.76

1 year ago

0.0.75

1 year ago

0.0.74

1 year ago

0.0.73

1 year ago

0.0.72

1 year ago

0.0.71

1 year ago

0.0.70

1 year ago

0.0.69

1 year ago

0.0.68

1 year ago

0.0.67

1 year ago

0.0.66

1 year ago

0.0.65

1 year ago

0.0.64

1 year ago

0.0.63

1 year ago

0.0.62

1 year ago

0.0.61

1 year ago

0.0.60

1 year ago

0.0.59

1 year ago

0.0.58

1 year ago

0.0.57

1 year ago

0.0.56

1 year ago

0.0.55

1 year ago

0.0.54

1 year ago

0.0.53

1 year ago

0.0.52

1 year ago

0.0.51

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago